forked from argoproj/argo-cd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: enable metadata to be set on namespaces (argoproj#10672)
* namespace labels Signed-off-by: pashavictorovich <[email protected]> * create namespace should support annotations Signed-off-by: pashavictorovich <[email protected]> * handle also modification hook Signed-off-by: pashavictorovich <[email protected]> * regenerate entity on modify hook Signed-off-by: pashavictorovich <[email protected]> * manifests Signed-off-by: pashavictorovich <[email protected]> * feat: enable metadata to be set on namespaces This builds upon the work that @pasha-codefresh did in argoproj#10288. The main differences between this PR and the previous one is that we use SSA to diff between different versions of the namespace, as well as having a slightly different API in gitops-engine for setting the namespace modifier. We now also set the ownership of the namespace in ArgoCD. Closes argoproj#4628 Closes argoproj#6215 Closes argoproj#7799 Signed-off-by: Blake Pettersson <[email protected]> * fix: don't always track namespaces For now, only allow namespaces managed with `managedNamespaceMetadata` to have tracking set by Argo. Ideally we'd like new namespaces to also be tracked by Argo, but there's currently an issue with a failing integration test. Also wrap error message if setting the app instance errors on the namespace. Signed-off-by: Blake Pettersson <[email protected]> * fix: always return true with `hasManagedMetadata` If `hasManagedMetadata` is set, `true` should always be returned. Signed-off-by: Blake Pettersson <[email protected]> * docs: add clarifying docs on resource tracking Signed-off-by: Blake Pettersson <[email protected]> * style: pr tweaks Signed-off-by: Blake Pettersson <[email protected]> * fix: re-add label unsetting Signed-off-by: Blake Pettersson <[email protected]> * Update gitops-engine to current master Signed-off-by: Leonardo Luz Almeida <[email protected]> Signed-off-by: pashavictorovich <[email protected]> Signed-off-by: Blake Pettersson <[email protected]> Signed-off-by: Leonardo Luz Almeida <[email protected]> Co-authored-by: pashavictorovich <[email protected]> Co-authored-by: Leonardo Luz Almeida <[email protected]>
- Loading branch information
1 parent
ebf2682
commit 7773021
Showing
25 changed files
with
3,198 additions
and
735 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package controller | ||
|
||
import ( | ||
"fmt" | ||
cdcommon "github.com/argoproj/argo-cd/v2/common" | ||
"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" | ||
"github.com/argoproj/argo-cd/v2/util/argo" | ||
gitopscommon "github.com/argoproj/gitops-engine/pkg/sync/common" | ||
"github.com/argoproj/gitops-engine/pkg/utils/kube" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
) | ||
|
||
func syncNamespace(resourceTracking argo.ResourceTracking, appLabelKey string, trackingMethod v1alpha1.TrackingMethod, appName string, syncPolicy *v1alpha1.SyncPolicy) func(un *unstructured.Unstructured) (bool, error) { | ||
return func(liveNs *unstructured.Unstructured) (bool, error) { | ||
if liveNs != nil && kube.GetAppInstanceLabel(liveNs, cdcommon.LabelKeyAppInstance) != "" { | ||
kube.UnsetLabel(liveNs, cdcommon.LabelKeyAppInstance) | ||
return true, nil | ||
} | ||
|
||
isNewNamespace := liveNs != nil && liveNs.GetUID() == "" && liveNs.GetResourceVersion() == "" | ||
|
||
if liveNs != nil && syncPolicy != nil { | ||
// managedNamespaceMetadata relies on SSA, and since the diffs are computed by the k8s control plane we | ||
// always need to call the k8s api server, so we'll always need to return true if managedNamespaceMetadata is set. | ||
hasManagedMetadata := syncPolicy.ManagedNamespaceMetadata != nil | ||
if hasManagedMetadata { | ||
managedNamespaceMetadata := syncPolicy.ManagedNamespaceMetadata | ||
liveNs.SetLabels(managedNamespaceMetadata.Labels) | ||
liveNs.SetAnnotations(appendSSAAnnotation(managedNamespaceMetadata.Annotations)) | ||
|
||
err := resourceTracking.SetAppInstance(liveNs, appLabelKey, appName, "", trackingMethod) | ||
if err != nil { | ||
return false, fmt.Errorf("failed to set app instance tracking on the namespace %s: %s", liveNs.GetName(), err) | ||
} | ||
|
||
return true, nil | ||
} | ||
} | ||
|
||
return isNewNamespace, nil | ||
} | ||
} | ||
|
||
func appendSSAAnnotation(in map[string]string) map[string]string { | ||
r := map[string]string{} | ||
for k, v := range in { | ||
r[k] = v | ||
} | ||
r[gitopscommon.AnnotationSyncOptions] = gitopscommon.SyncOptionServerSideApply | ||
return r | ||
} |
Oops, something went wrong.