@@ -19,12 +19,14 @@ import (
1919 "strings"
2020
2121 "github.com/go-logr/logr"
22+ appsv1 "k8s.io/api/apps/v1"
2223 corev1 "k8s.io/api/core/v1"
2324 apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
2425 apierrors "k8s.io/apimachinery/pkg/api/errors"
2526 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2627 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2728 "k8s.io/apimachinery/pkg/labels"
29+ "k8s.io/apimachinery/pkg/runtime"
2830 "k8s.io/apimachinery/pkg/types"
2931 "k8s.io/client-go/kubernetes"
3032 "k8s.io/client-go/rest"
@@ -45,6 +47,7 @@ import (
4547
4648const (
4749 projectsveltos = "projectsveltos"
50+ deploymentKind = "Deployment"
4851)
4952
5053func getResourceSummaryNamespace () string {
@@ -300,6 +303,14 @@ func deployDriftDetectionManagerResources(ctx context.Context, restConfig *rest.
300303 currentLabels [k ] = lbls [k ]
301304 }
302305 policy .SetLabels (currentLabels )
306+
307+ if policy .GetKind () == deploymentKind {
308+ policy , err = addTemplateSpecLabels (policy , lbls )
309+ if err != nil {
310+ logger .V (logs .LogInfo ).Info (fmt .Sprintf ("failed to set deployment spec.template.labels: %v" , err ))
311+ return err
312+ }
313+ }
303314 }
304315
305316 var referencedUnstructured []* unstructured.Unstructured
@@ -655,7 +666,7 @@ func getDriftDetectionManagerPatches(ctx context.Context, c client.Client,
655666 patch := libsveltosv1beta1.Patch {
656667 Patch : configMap .Data [k ],
657668 Target : & libsveltosv1beta1.PatchSelector {
658- Kind : "Deployment" ,
669+ Kind : deploymentKind ,
659670 Group : "apps" ,
660671 },
661672 }
@@ -664,3 +675,27 @@ func getDriftDetectionManagerPatches(ctx context.Context, c client.Client,
664675
665676 return patches , nil
666677}
678+
679+ func addTemplateSpecLabels (u * unstructured.Unstructured , lbls map [string ]string ) (* unstructured.Unstructured , error ) {
680+ var deployment appsv1.Deployment
681+ err := runtime .DefaultUnstructuredConverter .FromUnstructured (u .UnstructuredContent (), & deployment )
682+ if err != nil {
683+ return nil , err
684+ }
685+
686+ if deployment .Spec .Template .Labels == nil {
687+ deployment .Spec .Template .Labels = map [string ]string {}
688+ }
689+ for k := range lbls {
690+ deployment .Spec .Template .Labels [k ] = lbls [k ]
691+ }
692+
693+ content , err := runtime .DefaultUnstructuredConverter .ToUnstructured (& deployment )
694+ if err != nil {
695+ return nil , err
696+ }
697+
698+ var uDeployment unstructured.Unstructured
699+ uDeployment .SetUnstructuredContent (content )
700+ return & uDeployment , nil
701+ }
0 commit comments