99 "encoding/json"
1010 "fmt"
1111
12+ v1alpha1 "github.com/DataDog/datadog-operator/api/datadoghq/v1alpha1"
13+ v2alpha1 "github.com/DataDog/datadog-operator/api/datadoghq/v2alpha1"
1214 "k8s.io/apimachinery/pkg/types"
1315 "sigs.k8s.io/controller-runtime/pkg/client"
1416 logf "sigs.k8s.io/controller-runtime/pkg/log"
@@ -21,27 +23,32 @@ func getObjKind(obj client.Object) string {
2123 // First try to get the kind from GVK
2224 objKind := obj .GetObjectKind ().GroupVersionKind ().Kind
2325
24- // There is a known bug where the object frequently has empty GVK info. This is a workaround to get object Kind if that happens.
26+ // There is a known bug where the object frequently has empty GVK info.
2527 // Ref: https://github.com/kubernetes-sigs/controller-runtime/issues/1735
28+ // If GVK is empty, prefer inspecting the concrete type to avoid annotation-induced misclassification
2629 if objKind == "" {
27- // Try to get it from the last-applied-configuration annotation
28- if annotations := obj .GetAnnotations (); annotations != nil {
29- if lastConfig , exists := annotations ["kubectl.kubernetes.io/last-applied-configuration" ]; exists {
30- var config map [string ]any
31- if err := json .Unmarshal ([]byte (lastConfig ), & config ); err == nil {
32- if kind , ok := config ["kind" ].(string ); ok {
33- objKind = kind
30+ switch obj .(type ) {
31+ case * v2alpha1.DatadogAgent :
32+ objKind = datadogAgentKind
33+ case * v1alpha1.DatadogAgentInternal :
34+ objKind = datadogAgentInternalKind
35+ case * v1alpha1.DatadogMonitor :
36+ objKind = datadogMonitorKind
37+ default :
38+ // As a fallback, get it from the last-applied-configuration annotation.
39+ if annotations := obj .GetAnnotations (); annotations != nil {
40+ if lastConfig , exists := annotations ["kubectl.kubernetes.io/last-applied-configuration" ]; exists {
41+ var config map [string ]any
42+ if err := json .Unmarshal ([]byte (lastConfig ), & config ); err == nil {
43+ if kind , ok := config ["kind" ].(string ); ok {
44+ objKind = kind
45+ }
3446 }
3547 }
3648 }
3749 }
3850 }
3951
40- // Last fallback after GVK is empty and last-applied-configuration annotation is not present
41- if objKind == "" {
42- objKind = "Unknown"
43- }
44-
4552 return objKind
4653}
4754
0 commit comments