Skip to content
This repository was archived by the owner on Dec 17, 2025. It is now read-only.

feat: Ability to create custom labels for namespaces created with syncOptions CreateNamespace#443

Merged
jannfis merged 5 commits intoargoproj:masterfrom
pasha-codefresh:namespace-labels
Aug 18, 2022
Merged

feat: Ability to create custom labels for namespaces created with syncOptions CreateNamespace#443
jannfis merged 5 commits intoargoproj:masterfrom
pasha-codefresh:namespace-labels

Conversation

@pasha-codefresh
Copy link
Copy Markdown
Member

@pasha-codefresh pasha-codefresh commented Aug 11, 2022

Signed-off-by: pashavictorovich <pavel@codefresh.io>
@pasha-codefresh pasha-codefresh changed the title namespace labels hook feat: Ability to create custom labels for namespaces created with syncOptions CreateNamespace Aug 11, 2022
@codecov
Copy link
Copy Markdown

codecov bot commented Aug 11, 2022

Codecov Report

Merging #443 (df041e5) into master (2bc3fef) will increase coverage by 0.05%.
The diff coverage is 60.00%.

@@            Coverage Diff             @@
##           master     #443      +/-   ##
==========================================
+ Coverage   55.22%   55.27%   +0.05%     
==========================================
  Files          41       41              
  Lines        4449     4452       +3     
==========================================
+ Hits         2457     2461       +4     
+ Misses       1803     1802       -1     
  Partials      189      189              
Impacted Files Coverage Δ
pkg/sync/sync_context.go 72.20% <60.00%> (+0.21%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

Comment on lines +797 to +800
unstructuredObjCopy := unstructuredObj.DeepCopy()
if sc.namespaceCreator(unstructuredObjCopy) {
tasks = append(tasks, &syncTask{phase: common.SyncPhasePreSync, targetObj: unstructuredObjCopy, liveObj: nil})
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My expectation as a user would be that when I set the createNamespaceMetadata field, all auto-created namespaces will be updated with that metadata.

What if in WithNamespaceCreation we accepted multiple namespaceModifier functions and run them in series where they are currently being run?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@crenshaw-dev Do you want to add ns metadata to the already created auto namespace?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, use case is

User creates an app with autocreate namespaces
User syncs the app, and namespaces are auto-created
User adds a new label to the createNamespaceMetadata.labels field
User syncs the app, and the label is applied to already-auto-created namespaces

Working on this

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ashutosh16 @crenshaw-dev this use case is covered now

https://github.com/argoproj/argo-cd/pull/10288/files#diff-627c32a4f31d0f97e6ddc1f19ab3add7fb7791e0d1b0503d78c6c049e8cb4865R260

when the app is going to be synced namespaceModifier will be called and labels will be added to namespace

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

Signed-off-by: pashavictorovich <pavel@codefresh.io>
@pasha-codefresh pasha-codefresh marked this pull request as ready for review August 16, 2022 11:21
Signed-off-by: pashavictorovich <pavel@codefresh.io>
@pasha-codefresh pasha-codefresh requested review from ashutosh16 and crenshaw-dev and removed request for ashutosh16 August 16, 2022 11:32
Copy link
Copy Markdown
Member

@crenshaw-dev crenshaw-dev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

Copy link
Copy Markdown
Member

@jannfis jannfis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a few questions

Comment on lines +797 to +800
unstructuredObjCopy := unstructuredObj.DeepCopy()
if sc.namespaceCreator(unstructuredObjCopy) {
tasks = append(tasks, &syncTask{phase: common.SyncPhasePreSync, targetObj: unstructuredObjCopy, liveObj: nil})
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a good reason for using a copy of unstructuredObj here instead of just using the original?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it can be removed

"encoding/json"
"errors"
"fmt"
errors2 "k8s.io/apimachinery/pkg/api/errors"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a nit: Could we please rename the import to apierrors or something more obvious than errors2?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it was autogenerated, sry


// WithNamespaceCreation will create non-exist namespace
func WithNamespaceCreation(createNamespace bool, namespaceModifier func(*unstructured.Unstructured) bool) SyncOpt {
func WithNamespaceCreation(createNamespace bool, namespaceModifier func(*unstructured.Unstructured) bool, namespaceCreator func(*unstructured.Unstructured) bool) SyncOpt {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense to make a unique type of func (*unstructured.Unstructured) bool, as the method signature becomes a little unreadable :)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Снимок экрана 2022-08-16 в 17 01 44
Not sure that it starts look better, maybe you can advice better naming

Signed-off-by: pashavictorovich <pavel@codefresh.io>
Signed-off-by: pashavictorovich <pavel@codefresh.io>
@sonarqubecloud
Copy link
Copy Markdown

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@jannfis
Copy link
Copy Markdown
Member

jannfis commented Aug 16, 2022

Also, looking at the appropriate code in Argo CD, is there a reason we need a new modifier function and do not reuse the existing one here: https://github.com/argoproj/argo-cd/blob/2d544b42c9c41be85a7fcb25535e3013d2ae3350/controller/sync.go#L243?

This would save us an API change in gitops-engine.

@pasha-codefresh
Copy link
Copy Markdown
Member Author

pasha-codefresh commented Aug 16, 2022

because modifier is calling only if namespace already exist, this hook is not calling when namespace not exist
https://github.com/argoproj/gitops-engine/pull/443/files#diff-74e77a4722803a7d3f87b956ec81b82d3c5a0dd50de299922aa2208fec57e6e9R796

@jannfis

Copy link
Copy Markdown
Member

@jannfis jannfis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for coming back so late.

LGTM.

@jannfis jannfis merged commit a56a803 into argoproj:master Aug 18, 2022
pasha-codefresh added a commit to pasha-codefresh/gitops-engine that referenced this pull request Aug 23, 2022
…with syncOptions CreateNamespace (argoproj#443)"

This reverts commit a56a803.
leoluz pushed a commit that referenced this pull request Aug 23, 2022
#455)

* Revert "feat: Ability to create custom labels for namespaces created with syncOptions CreateNamespace (#443)"

This reverts commit a56a803.

* remove import

Signed-off-by: pashavictorovich <pavel@codefresh.io>

* fix test

Signed-off-by: pashavictorovich <pavel@codefresh.io>

Signed-off-by: pashavictorovich <pavel@codefresh.io>
blakepettersson added a commit to blakepettersson/gitops-engine that referenced this pull request Sep 22, 2022
This builds upon argoproj#443; the main difference here is that we keep the
existing API and do a bit more logic in the downstream client to
determine whether this namespace should be updated or not.

Signed-off-by: Blake Pettersson <blake.pettersson@gmail.com>
blakepettersson added a commit to blakepettersson/gitops-engine that referenced this pull request Sep 22, 2022
This builds upon argoproj#443; the main difference here is that we keep the
existing API and do a bit more logic in the downstream client to
determine whether this namespace should be updated or not.

Signed-off-by: Blake Pettersson <blake.pettersson@gmail.com>
blakepettersson added a commit to blakepettersson/gitops-engine that referenced this pull request Oct 4, 2022
This builds upon argoproj#443; the main difference here is that we keep the
existing API and do a bit more logic in the downstream client to
determine whether this namespace should be updated or not.

Signed-off-by: Blake Pettersson <blake.pettersson@gmail.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants