Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Fill object TypeMeta/GroupVersionKind in case it is missed in history snapshot #33

Merged
merged 1 commit into from
Aug 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions pkg/apis/frameworkcontroller/v1/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,12 @@ type Config struct {
// such as persistence, metrics conversion, visualization, alerting, acting,
// analysis, etc.
// Notes:
// 1. The snapshot is logged to stderr.
// 2. Check GetFrameworkSnapshotLogTail and GetPodSnapshotLogTail to see how
// to extract the snapshot from stderr.
// 1. The snapshot is logged to stderr and can be extracted by the regular
// expression ": ObjectSnapshot: (.+)".
// 2. To determine the type of the snapshot, using object.apiVersion and
// object.kind.
// 3. The same snapshot may be logged more than once in some rare cases, so
// external systems may need to deduplicate them by object.ResourceVersion.
// external systems may need to deduplicate them by object.resourceVersion.
// 4. The snapshot triggered by deletion may be missed to log during the
// FrameworkController downtime.
LogObjectSnapshot LogObjectSnapshot `yaml:"logObjectSnapshot"`
Expand Down
3 changes: 3 additions & 0 deletions pkg/apis/frameworkcontroller/v1/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const (
FrameworkCRDName = FrameworkPlural + "." + GroupName
FrameworkKind = "Framework"
ConfigMapKind = "ConfigMap"
PodKind = "Pod"
ObjectUIDFieldPath = "metadata.uid"

ConfigFilePath = "./frameworkcontroller.yaml"
Expand Down Expand Up @@ -99,6 +100,8 @@ const (

var FrameworkGroupVersionKind = SchemeGroupVersion.WithKind(FrameworkKind)
var ConfigMapGroupVersionKind = core.SchemeGroupVersion.WithKind(ConfigMapKind)
var PodGroupVersionKind = core.SchemeGroupVersion.WithKind(PodKind)

var ObjectUIDEnvVarSource = &core.EnvVarSource{
FieldRef: &core.ObjectFieldSelector{FieldPath: ObjectUIDFieldPath},
}
Expand Down
20 changes: 16 additions & 4 deletions pkg/apis/frameworkcontroller/v1/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,24 @@ func SplitTaskAttemptInstanceUID(taskAttemptInstanceUID *types.UID) (
return int32(i), common.PtrUIDStr(parts[1])
}

func GetFrameworkSnapshotLogTail(podPtr interface{}) string {
return "FrameworkSnapshot: " + common.ToJson(podPtr)
func getObjectSnapshotLogTail(obj interface{}) string {
return ": ObjectSnapshot: " + common.ToJson(obj)
}

func GetPodSnapshotLogTail(frameworkPtr interface{}) string {
return "PodSnapshot: " + common.ToJson(frameworkPtr)
func GetFrameworkSnapshotLogTail(f *Framework) string {
if f.GroupVersionKind().Empty() {
f = f.DeepCopy()
f.SetGroupVersionKind(FrameworkGroupVersionKind)
}
return getObjectSnapshotLogTail(f)
}

func GetPodSnapshotLogTail(pod *core.Pod) string {
if pod.GroupVersionKind().Empty() {
pod = pod.DeepCopy()
pod.SetGroupVersionKind(PodGroupVersionKind)
}
return getObjectSnapshotLogTail(pod)
}

///////////////////////////////////////////////////////////////////////////////////////
Expand Down
22 changes: 12 additions & 10 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,11 @@ func NewFrameworkController() *FrameworkController {
DeleteFunc: func(obj interface{}) {
c.enqueueFrameworkObj(obj, "Framework Deleted", func() string {
if *c.cConfig.LogObjectSnapshot.Framework.OnFrameworkDeletion {
return ": " + ci.GetFrameworkSnapshotLogTail(obj)
} else {
return ""
if f := internal.ToFramework(obj); f != nil {
return ci.GetFrameworkSnapshotLogTail(f)
}
}
return ""
})
},
})
Expand All @@ -288,10 +289,11 @@ func NewFrameworkController() *FrameworkController {
DeleteFunc: func(obj interface{}) {
c.enqueueFrameworkPodObj(obj, "Framework Pod Deleted", func() string {
if *c.cConfig.LogObjectSnapshot.Pod.OnPodDeletion {
return ": " + ci.GetPodSnapshotLogTail(obj)
} else {
return ""
if pod := internal.ToPod(obj); pod != nil {
return ci.GetPodSnapshotLogTail(pod)
}
}
return ""
})
},
})
Expand Down Expand Up @@ -872,8 +874,8 @@ func (c *FrameworkController) syncFrameworkState(f *ci.Framework) (err error) {
// The completed FrameworkAttempt has been persisted, so it is safe to also
// expose it as one history snapshot.
if *c.cConfig.LogObjectSnapshot.Framework.OnFrameworkRetry {
klog.Infof(logPfx+
"Framework will be retried: %v", ci.GetFrameworkSnapshotLogTail(f))
klog.Infof(logPfx + "Framework will be retried" +
ci.GetFrameworkSnapshotLogTail(f))
}

f.Status.RetryPolicyStatus.TotalRetriedCount++
Expand Down Expand Up @@ -1356,8 +1358,8 @@ func (c *FrameworkController) syncTaskState(
// The completed TaskAttempt has been persisted, so it is safe to also
// expose it as one history snapshot.
if *c.cConfig.LogObjectSnapshot.Framework.OnTaskRetry {
klog.Infof(logPfx+
"Task will be retried: %v", ci.GetFrameworkSnapshotLogTail(f))
klog.Infof(logPfx + "Task will be retried" +
ci.GetFrameworkSnapshotLogTail(f))
}

taskStatus.RetryPolicyStatus.TotalRetriedCount++
Expand Down
26 changes: 26 additions & 0 deletions pkg/internal/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ package internal

import (
"fmt"
ci "github.com/microsoft/frameworkcontroller/pkg/apis/frameworkcontroller/v1"
frameworkClient "github.com/microsoft/frameworkcontroller/pkg/client/clientset/versioned"
"github.com/microsoft/frameworkcontroller/pkg/common"
core "k8s.io/api/core/v1"
Expand Down Expand Up @@ -145,6 +146,31 @@ func SplitKey(key string) (namespace, name string, err error) {
return cache.SplitMetaNamespaceKey(key)
}

// obj could be *core.Framework or cache.DeletedFinalStateUnknown.
func ToFramework(obj interface{}) *ci.Framework {
f, ok := obj.(*ci.Framework)

if !ok {
deletedFinalStateUnknown, ok := obj.(cache.DeletedFinalStateUnknown)
if !ok {
klog.Errorf(
"Failed to convert obj to Framework or DeletedFinalStateUnknown: %#v",
obj)
return nil
}

f, ok = deletedFinalStateUnknown.Obj.(*ci.Framework)
if !ok {
klog.Errorf(
"Failed to convert DeletedFinalStateUnknown.Obj to Framework: %#v",
deletedFinalStateUnknown)
return nil
}
}

return f
}

// obj could be *core.ConfigMap or cache.DeletedFinalStateUnknown.
func ToConfigMap(obj interface{}) *core.ConfigMap {
cm, ok := obj.(*core.ConfigMap)
Expand Down