Skip to content
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

k8sClient get resources across all namespaces (#601) #854

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

jortkoopmans
Copy link
Contributor

@jortkoopmans jortkoopmans commented Sep 2, 2024

Work in progress. Updating Applications using this branch works for me, when the RBAC permissions are configured. (#601)

However, there are several considerations;

  • Clearly the original code intents to call these functions per namespace (this is clear from the tests)
  • The tests have not been fixed (yet)
  • There could be performance issues by listing/filtering all Applications across namespaces (on the other hand, traditionally all Applications were in the same namespace)
  • Alternatively an array variable could be introduced, listing namespaces to be monitored instead. These could be looped through
  • Alternatively (again), a configuration variable could be introduced, to switch from single namespace to all namespaces (explicitly for the k8sClient).
  • Since the argocd api mechanism does not have this distinction, it might make sense to keep the design similar.

@wd
Copy link

wd commented Sep 3, 2024

Great! Maybe you can consider reusing the sourceNamespace settings from ArgoCD
and they recently supported regex in the value. https://github.com/argoproj/argo-cd/pull/19017/files

@jortkoopmans
Copy link
Contributor Author

Great! Maybe you can consider reusing the sourceNamespace settings from ArgoCD and they recently supported regex in the value. https://github.com/argoproj/argo-cd/pull/19017/files

It does make sense to wanting to align the functionality to ArgoCD itself. AIU is effectively following that featureset, as we're seeing here with the 'app in any namespace' feature.
Code-wise, it would be better to share specific modules (e.g. regex.go), instead of duplicating them manually.

If we go for this approach, a fair amount of change is needed;

  • Get namespaces and filter these using a regex (also include RBAC for ns)
  • Get Applications in these namespaces (and keep that relation)
  • Modify existing Application update/patch functions to use these App + namespace combinations

Copy link
Contributor

@ishitasequeira ishitasequeira left a comment

Choose a reason for hiding this comment

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

@jortkoopmans @wd I was thinking about keeping this PR focused on fixing the currently broken apps-in-any-namespace feature and adding wildcard support in a separate PR. WDYT?

I reviewed and tested the PR in regards to fixing the currently broken apps-in-any-namespace feature and changes look good. However, the unit tests still need to be fixed.

@wd
Copy link

wd commented Sep 6, 2024

@ishitasequeira I'm good with that. I'm just trying to bring information from Argo here.

@ishitasequeira
Copy link
Contributor

@wd, It's a good callout for sure and something which can be looked into as a next step forward for the feature.

@jortkoopmans, let me know if you need any help in fixing the unit tests.

- Modify ks8Client functions to always get Application resources across all namespaces
- Add required RBAC permissions

Signed-off-by: Jort Koopmans <[email protected]>
@jortkoopmans jortkoopmans force-pushed the bugfix/601_k8sclient_all_namespaces branch from 6c9e2ee to 4902053 Compare September 16, 2024 07:28
@codecov-commenter
Copy link

codecov-commenter commented Sep 17, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 75.88%. Comparing base (13f01ec) to head (d4b988f).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #854      +/-   ##
==========================================
+ Coverage   75.47%   75.88%   +0.41%     
==========================================
  Files          31       31              
  Lines        3144     3177      +33     
==========================================
+ Hits         2373     2411      +38     
+ Misses        636      633       -3     
+ Partials      135      133       -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Fix UpdateSpec to handle partial updates without specified appNamespace
Fix tests to work with Applications across namespaces

Signed-off-by: Jort Koopmans <[email protected]>
@jortkoopmans jortkoopmans force-pushed the bugfix/601_k8sclient_all_namespaces branch from 2748faa to d440337 Compare September 17, 2024 17:08
- Error wrapping for improved reporting in higher-lvl code
- Change UpdateSpec retry to stop trying, with exponential backoff
- Add and enhance tests. Improve code coverage.

Signed-off-by: Jort Koopmans <[email protected]>
@jortkoopmans
Copy link
Contributor Author

Thank you for the feedback and guidance @wd @ishitasequeira .
I have reviewed the fix with the scope to get AIU to work across namespaces (according to guidance).

It turned out that some of the functions strictly require a namespace to be provided, I have modified several functions to deal with this correctly (and introduce some helper functions). Specifically:

  • GetApplication needs a namespace for the Application, I think it's defined here: argo-cd
  • UpdateSpec can only work when it can match the UpdateSpec with a unique Application. Note that the UpdateSpec does not have to include the namespace.

For my use case, I only need AIU to monitor the Applications (ListApplications) across namespaces, since I use it exclusively to overwrite the sha256 image hashes (and not Get or Update the Application spec). This is probably why it worked for me previously (?).

Subsequently I refactored and extended some of the tests. But feel free to amend or change this. Lastly, while testing I noticed that retrying on conflict is perpetual, I implemented maxRetries and exponential backoff to resolve that.

@jortkoopmans jortkoopmans marked this pull request as ready for review September 18, 2024 14:32
@jortkoopmans jortkoopmans changed the title k8sClient get resources across all namespaces. WIP (#601) k8sClient get resources across all namespaces (#601) Sep 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants