From f23b538f115140b7fa159e31a1e0bc055c720110 Mon Sep 17 00:00:00 2001 From: Hussein Galal Date: Mon, 10 Feb 2025 10:24:56 +0200 Subject: [PATCH] Fix metadata information for the virtual pods (#228) * Fix metadata information for the virtual pods Signed-off-by: galal-hussein * Fixes Signed-off-by: galal-hussein * Fixes Signed-off-by: galal-hussein --------- Signed-off-by: galal-hussein --- k3k-kubelet/provider/provider.go | 49 ++++++++++++++++++++++++++++++-- k3k-kubelet/translate/host.go | 4 +++ 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/k3k-kubelet/provider/provider.go b/k3k-kubelet/provider/provider.go index a02c827d..0ad0c751 100644 --- a/k3k-kubelet/provider/provider.go +++ b/k3k-kubelet/provider/provider.go @@ -446,6 +446,15 @@ func (p *Provider) transformVolumes(ctx context.Context, podNamespace string, vo } } else if volume.PersistentVolumeClaim != nil { volume.PersistentVolumeClaim.ClaimName = p.Translator.TranslateName(podNamespace, volume.PersistentVolumeClaim.ClaimName) + } else if volume.DownwardAPI != nil { + for _, downwardAPI := range volume.DownwardAPI.Items { + if downwardAPI.FieldRef.FieldPath == translate.MetadataNameField { + downwardAPI.FieldRef.FieldPath = fmt.Sprintf("metadata.annotations['%s']", translate.ResourceNameAnnotation) + } + if downwardAPI.FieldRef.FieldPath == translate.MetadataNamespaceField { + downwardAPI.FieldRef.FieldPath = fmt.Sprintf("metadata.annotations['%s']", translate.ResourceNamespaceAnnotation) + } + } } } return nil @@ -802,9 +811,45 @@ func getSecretsAndConfigmaps(pod *corev1.Pod) ([]string, []string) { return secrets, configMaps } -// fetchFieldPathAnnotations will retrieve all annotations created by the pod mutator webhook -// to assign env fieldpaths to pods +// configureFieldPathEnv will retrieve all annotations created by the pod mutator webhook +// to assign env fieldpaths to pods, it will also make sure to change the metadata.name and metadata.namespace to the +// assigned annotations func (p *Provider) configureFieldPathEnv(pod, tPod *v1.Pod) error { + // override metadata.name and metadata.namespace with pod annotations + for i, container := range pod.Spec.InitContainers { + for j, envVar := range container.Env { + if envVar.ValueFrom == nil || envVar.ValueFrom.FieldRef == nil { + continue + } + + fieldPath := envVar.ValueFrom.FieldRef.FieldPath + + if fieldPath == translate.MetadataNameField { + envVar.ValueFrom.FieldRef.FieldPath = fmt.Sprintf("metadata.annotations['%s']", translate.ResourceNameAnnotation) + pod.Spec.InitContainers[i].Env[j] = envVar + } + if fieldPath == translate.MetadataNamespaceField { + envVar.ValueFrom.FieldRef.FieldPath = fmt.Sprintf("metadata.annotations['%s']", translate.MetadataNamespaceField) + pod.Spec.InitContainers[i].Env[j] = envVar + } + } + } + for i, container := range pod.Spec.Containers { + for j, envVar := range container.Env { + if envVar.ValueFrom == nil || envVar.ValueFrom.FieldRef == nil { + continue + } + fieldPath := envVar.ValueFrom.FieldRef.FieldPath + if fieldPath == translate.MetadataNameField { + envVar.ValueFrom.FieldRef.FieldPath = fmt.Sprintf("metadata.annotations['%s']", translate.ResourceNameAnnotation) + pod.Spec.Containers[i].Env[j] = envVar + } + if fieldPath == translate.MetadataNamespaceField { + envVar.ValueFrom.FieldRef.FieldPath = fmt.Sprintf("metadata.annotations['%s']", translate.ResourceNameAnnotation) + pod.Spec.Containers[i].Env[j] = envVar + } + } + } for name, value := range pod.Annotations { if strings.Contains(name, webhook.FieldpathField) { containerIndex, envName, err := webhook.ParseFieldPathAnnotationKey(name) diff --git a/k3k-kubelet/translate/host.go b/k3k-kubelet/translate/host.go index d2a4918f..3ec372f4 100644 --- a/k3k-kubelet/translate/host.go +++ b/k3k-kubelet/translate/host.go @@ -18,6 +18,10 @@ const ( // ResourceNamespaceAnnotation is the key for the annotation that contains the original namespace of this // resource in the virtual cluster ResourceNamespaceAnnotation = "k3k.io/namespace" + // MetadataNameField is the downwardapi field for object's name + MetadataNameField = "metadata.name" + // MetadataNamespaceField is the downward field for the object's namespace + MetadataNamespaceField = "metadata.namespace" ) type ToHostTranslator struct {