From 7bd15219c3362cffff10369d102bd789c4ec88aa Mon Sep 17 00:00:00 2001 From: Eden Federman Date: Sun, 21 Jul 2024 09:45:36 +0300 Subject: [PATCH] Fix env vars uninstall (#1372) --- instrumentor/main.go | 27 ++++++++++--------- odiglet/pkg/kube/instrumentation_ebpf/pods.go | 4 +++ .../pkg/kube/runtime_details/inspection.go | 6 +++-- .../runtime_details/instconfig_controller.go | 4 +++ odiglet/pkg/kube/utils/utils.go | 9 +++++-- 5 files changed, 34 insertions(+), 16 deletions(-) diff --git a/instrumentor/main.go b/instrumentor/main.go index 2053fecf8..5bde75905 100644 --- a/instrumentor/main.go +++ b/instrumentor/main.go @@ -116,10 +116,11 @@ func main() { newDep := &appsv1.Deployment{ TypeMeta: deployment.TypeMeta, ObjectMeta: metav1.ObjectMeta{ - Name: deployment.Name, - Namespace: deployment.Namespace, - Labels: deployment.Labels, - UID: deployment.UID, + Name: deployment.Name, + Namespace: deployment.Namespace, + Labels: deployment.Labels, + Annotations: deployment.Annotations, + UID: deployment.UID, }, Status: deployment.Status, } @@ -134,10 +135,11 @@ func main() { newSs := &appsv1.StatefulSet{ TypeMeta: ss.TypeMeta, ObjectMeta: metav1.ObjectMeta{ - Name: ss.Name, - Namespace: ss.Namespace, - Labels: ss.Labels, - UID: ss.UID, + Name: ss.Name, + Namespace: ss.Namespace, + Labels: ss.Labels, + Annotations: ss.Annotations, + UID: ss.UID, }, Status: ss.Status, } @@ -152,10 +154,11 @@ func main() { newDs := &appsv1.DaemonSet{ TypeMeta: ds.TypeMeta, ObjectMeta: metav1.ObjectMeta{ - Name: ds.Name, - Namespace: ds.Namespace, - Labels: ds.Labels, - UID: ds.UID, + Name: ds.Name, + Namespace: ds.Namespace, + Labels: ds.Labels, + Annotations: ds.Annotations, + UID: ds.UID, }, Status: ds.Status, } diff --git a/odiglet/pkg/kube/instrumentation_ebpf/pods.go b/odiglet/pkg/kube/instrumentation_ebpf/pods.go index 0f39b999b..35978065c 100644 --- a/odiglet/pkg/kube/instrumentation_ebpf/pods.go +++ b/odiglet/pkg/kube/instrumentation_ebpf/pods.go @@ -112,6 +112,10 @@ func (p *PodsReconciler) getPodWorkloadObject(ctx context.Context, pod *corev1.P for _, owner := range pod.OwnerReferences { name, kind, err := kubeutils.GetWorkloadNameFromOwnerReference(owner) if err != nil { + if kubeutils.IsErrorKindNotSupported(err) { + return nil, nil + } + return nil, err } diff --git a/odiglet/pkg/kube/runtime_details/inspection.go b/odiglet/pkg/kube/runtime_details/inspection.go index fd67b1eae..0a0c43ed0 100644 --- a/odiglet/pkg/kube/runtime_details/inspection.go +++ b/odiglet/pkg/kube/runtime_details/inspection.go @@ -109,8 +109,10 @@ func runtimeInspection(pods []corev1.Pod, ignoredContainers []string) ([]odigosv if inspectProc == nil { log.Logger.V(0).Info("unable to detect language for any process", "pod", pod.Name, "container", container.Name, "namespace", pod.Namespace) lang = common.UnknownProgrammingLanguage - } else if len(processes) > 1 { - log.Logger.V(0).Info("multiple processes found in pod container, only taking the first one with detected language into account", "pod", pod.Name, "container", container.Name, "namespace", pod.Namespace) + } else { + if len(processes) > 1 { + log.Logger.V(0).Info("multiple processes found in pod container, only taking the first one with detected language into account", "pod", pod.Name, "container", container.Name, "namespace", pod.Namespace) + } // Convert map to slice for k8s format envs = make([]odigosv1.EnvVar, 0, len(inspectProc.Envs)) for envName, envValue := range inspectProc.Envs { diff --git a/odiglet/pkg/kube/runtime_details/instconfig_controller.go b/odiglet/pkg/kube/runtime_details/instconfig_controller.go index baee684f2..9e6b4b644 100644 --- a/odiglet/pkg/kube/runtime_details/instconfig_controller.go +++ b/odiglet/pkg/kube/runtime_details/instconfig_controller.go @@ -46,6 +46,10 @@ func (i *InstrumentationConfigReconciler) Reconcile(ctx context.Context, request } workload, labels, err := getWorkloadAndLabelsfromOwner(ctx, i.Client, instConfig.Namespace, instConfig.OwnerReferences[0]) + if err != nil { + logger.Error(err, "Failed to get workload and labels from owner") + return reconcile.Result{}, err + } err = inspectRuntimesOfRunningPods(ctx, &logger, labels, i.Client, i.Scheme, workload) if err != nil { return reconcile.Result{}, ignoreNoPodsFoundError(err) diff --git a/odiglet/pkg/kube/utils/utils.go b/odiglet/pkg/kube/utils/utils.go index c3d2ca58a..2928fcde1 100644 --- a/odiglet/pkg/kube/utils/utils.go +++ b/odiglet/pkg/kube/utils/utils.go @@ -3,7 +3,6 @@ package utils import ( "context" "errors" - "fmt" "strings" odigosclientset "github.com/odigos-io/odigos/api/generated/odigos/clientset/versioned" @@ -17,6 +16,12 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" ) +var errKindNotSupported = errors.New("kind not supported") + +func IsErrorKindNotSupported(err error) bool { + return err == errKindNotSupported +} + func IsPodInCurrentNode(pod *corev1.Pod) bool { return pod.Spec.NodeName == env.Current.NodeName } @@ -81,5 +86,5 @@ func GetWorkloadNameFromOwnerReference(ownerReference metav1.OwnerReference) (st } else if kind == "DaemonSet" || kind == "Deployment" || kind == "StatefulSet" { return name, kind, nil } - return "", "", fmt.Errorf("kind %s not supported", kind) + return "", "", errKindNotSupported }