github.com/argoproj/argo-cd/v3@v3.2.1/controller/sync_namespace.go (about) 1 package controller 2 3 import ( 4 gitopscommon "github.com/argoproj/gitops-engine/pkg/sync/common" 5 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 6 7 "github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1" 8 ) 9 10 // syncNamespace determine if Argo CD should create and/or manage the namespace 11 // where the application will be deployed. 12 func syncNamespace(syncPolicy *v1alpha1.SyncPolicy) func(m *unstructured.Unstructured, l *unstructured.Unstructured) (bool, error) { 13 // This function must return true for the managed namespace to be synced. 14 return func(managedNs, liveNs *unstructured.Unstructured) (bool, error) { 15 if managedNs == nil { 16 return false, nil 17 } 18 19 isNewNamespace := liveNs == nil 20 isManagedNamespace := syncPolicy != nil && syncPolicy.ManagedNamespaceMetadata != nil 21 22 // should only sync the namespace if it doesn't exist in k8s or if 23 // syncPolicy is defined to manage the metadata 24 if !isManagedNamespace && !isNewNamespace { 25 return false, nil 26 } 27 28 if isManagedNamespace { 29 managedNamespaceMetadata := syncPolicy.ManagedNamespaceMetadata 30 managedNs.SetLabels(managedNamespaceMetadata.Labels) 31 // managedNamespaceMetadata relies on SSA in order to avoid overriding 32 // existing labels and annotations in namespaces 33 managedNs.SetAnnotations(appendSSAAnnotation(managedNamespaceMetadata.Annotations)) 34 } 35 36 // TODO: https://github.com/argoproj/argo-cd/issues/11196 37 // err := resourceTracking.SetAppInstance(managedNs, appLabelKey, appName, "", trackingMethod) 38 // if err != nil { 39 // return false, fmt.Errorf("failed to set app instance tracking on the namespace %s: %s", managedNs.GetName(), err) 40 // } 41 42 return true, nil 43 } 44 } 45 46 // appendSSAAnnotation will set the managed namespace to be synced 47 // with server-side apply 48 func appendSSAAnnotation(in map[string]string) map[string]string { 49 r := map[string]string{} 50 for k, v := range in { 51 r[k] = v 52 } 53 r[gitopscommon.AnnotationSyncOptions] = gitopscommon.SyncOptionServerSideApply 54 return r 55 }