-
Notifications
You must be signed in to change notification settings - Fork 7k
feat: enable metadata to be set on namespaces #10672
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d33d23a
namespace labels
pasha-codefresh dc6969f
create namespace should support annotations
pasha-codefresh e69c2df
handle also modification hook
pasha-codefresh 14e1877
regenerate entity on modify hook
pasha-codefresh e01eb88
manifests
pasha-codefresh 4426079
feat: enable metadata to be set on namespaces
blakepettersson 7cb2a9f
fix: don't always track namespaces
blakepettersson e774851
fix: always return true with `hasManagedMetadata`
blakepettersson cdd8897
docs: add clarifying docs on resource tracking
blakepettersson 33f1b0d
style: pr tweaks
blakepettersson 3f992f1
fix: re-add label unsetting
blakepettersson d76f316
Update gitops-engine to current master
leoluz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that this logic isn't present in the
syncNamespacefunction. Is there a reason to stop cleaning the instance label from the namespace?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't think there's any particular reason; think this was an artifact from when the resource tracking was always being set. I've re-added it, but what is the reason for having this? If this is significant, I'd add a test to check that this gets unset.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a bit of history (@alexmt correct me if I'm wrong :)
The tracking id is how Argo CD tracks the resources belonging to an
Application. Previously Argo CD used to use a standard label for that but that causes some conflicts with other controllers. To avoid this problem, Argo CD is now gradually switching from the standard label to a proprietary annotation. This whole logic is handled inutil/argo/resource_tracking.goAs far as I understand the previous code, it was cleaning up the label from a previously tracked namespace. This was only happening if the namespace already existed in k8s and it had the tracking label which means that the namespace yaml was provided in git. If we want to keep the current behaviour we should clean the label only if:
Applicationis configured to create namespaceApplicationis not configured to manage namespace metadataBtw, cleaning only the label in the scenario above is probably a bug we currently have. I suspect that we should clean the label AND the annotation if they are present and the
Applicationis under the 2 conditions above.FYI, I am going to bring this to the contributors meeting this week (will happen Oct 20th 8:15AM PST)