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

feat: Application resource watching #43

Merged
merged 15 commits into from
Jan 11, 2024
Merged

feat: Application resource watching #43

merged 15 commits into from
Jan 11, 2024

Conversation

sl1pm4t
Copy link
Collaborator

@sl1pm4t sl1pm4t commented Jul 21, 2023

No description provided.

@sl1pm4t sl1pm4t marked this pull request as draft July 21, 2023 06:05
Comment on lines 89 to 92
// TODO
// have any of the Source repoURLs changed?
log.Trace().Str("key", key).Msg("appwatcher: onApplicationUpdated")
Copy link
Collaborator

Choose a reason for hiding this comment

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

at this point we'll have to remove the old app (in all the various maps), then add the new one back in

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yeah that's a smart way to handle it instead of doing an upsert

log.Warn().Err(err).Msg("appwatcher: could not get key for deleted application")
}

log.Trace().Str("key", key).Msg("appwatcher: onApplicationDeleted")
Copy link
Collaborator

Choose a reason for hiding this comment

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

here we'll have to remove the app from the app directory

@@ -69,6 +71,7 @@ func init() {
flags.Bool("show-debug-info", false, "Set to true to print debug info to the footer of MR comments (KUBECHECKS_SHOW_DEBUG_INFO).")
flags.Bool("enable-conftest", false, "Set to true to enable conftest policy checking of manifests (KUBECHECKS_ENABLE_CONFTEST).")
flags.String("label-filter", "", "(Optional) If set, The label that must be set on an MR (as \"kubechecks:<value>\") for kubechecks to process the merge request webhook (KUBECHECKS_LABEL_FILTER).")
flags.String("argocd-namespace", "argocd", "The namespace to watch for Application resources")
Copy link
Collaborator

Choose a reason for hiding this comment

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

With the new Argocd releases, applications can be in different namespaces. I think it's worth using a cluster role to watch for all apps

pkg/app_watcher/appwatcher.go Outdated Show resolved Hide resolved
pkg/server/hook_handler.go Outdated Show resolved Hide resolved
pkg/server/server.go Outdated Show resolved Hide resolved
pkg/server/server.go Outdated Show resolved Hide resolved
@github-actions
Copy link

github-actions bot commented Aug 18, 2023

Temporary image deleted.

@MeNsaaH
Copy link
Collaborator

MeNsaaH commented Dec 12, 2023

Picking this up

@MeNsaaH MeNsaaH marked this pull request as ready for review January 10, 2024 11:59
} else {
s.cfg.VcsToArgoMap = argoMap
if s.appWatcher != nil {
go s.appWatcher.Run(context.Background(), 1)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we want this to be ctx, not a new context, so it gets canceled when the app gets canceled

}
}

if !isGitRepo(app.Spec.Source.RepoURL) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

might want to check app.Spec.Source != nil. not sure if it would happen in real life, but why worry?

Comment on lines +75 to +80
key, err := cache.MetaNamespaceKeyFunc(obj)
if err != nil {
log.Error().Err(err).Msg("appwatcher: could not get key for added application")
}
log.Debug().Str("key", key).Msg("appwatcher: onApplicationAdded")
Copy link
Collaborator

Choose a reason for hiding this comment

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

do we use key for anything other than logging?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Not really. Just for logging

// onAdd is the function executed when the informer notifies the
// presence of a new Application in the namespace
func (ctrl *ApplicationWatcher) onApplicationAdded(obj interface{}) {
if !canProcessApp(obj) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

each time we use this, we end up casting obj to *appv1alpha1.Application, which this function does too. it'd probably clean things up if you changed canProcessApp to return (*appv1alpha1.Application, bool). This would also let you reimplement cache.MetaNamespaceKeyFunc to just be return fmt.Sprintf("%s/%s", app.Metadata.Namespace, app.Metadata.Name) and avoid the possible error altogether.

newApp := new.(*appv1alpha1.Application)

// We want to update when any of Source or Sources parameters has changed
if !reflect.DeepEqual(oldApp.Spec.Source, newApp.Spec.Source) || !reflect.DeepEqual(oldApp.Spec.Sources, newApp.Spec.Sources) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

UpdateApp isn't a particularly expensive operation, to avoid any possible issues with DeepEqual we could always call UpdateApp whenever a change is made.

Copy link
Collaborator

Choose a reason for hiding this comment

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

The problem here is onApplicationUpdated gets called more often than I realized. It's called when any element of the app is modified, this could be .Spec, .metadata or .Status. .Status is modified on every Argocd refresh, hence why were restricting the change to just .Spec.Source. DeepEqual has a predictable behavior with Objects 😀😀, so hopefully, nothing goes south.

Comment on lines 30 to 33
func NewServer(cfg *config.ServerConfig) *Server {
return &Server{cfg: cfg}
var appWatcher *app_watcher.ApplicationWatcher
if viper.GetBool("monitor-all-applications") {
argoMap, err := config.BuildAppsMap(context.TODO())
Copy link
Collaborator

Choose a reason for hiding this comment

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

we should probably pass ctx from the parent context here as well

Copy link
Collaborator

@djeebus djeebus left a comment

Choose a reason for hiding this comment

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

Left some comments, but none of them are important, just opinions. Nice work, thanks!

@MeNsaaH MeNsaaH changed the title Re-add Application resource watching feat: Application resource watching Jan 11, 2024
@MeNsaaH MeNsaaH merged commit d0614c1 into main Jan 11, 2024
6 checks passed
@MeNsaaH MeNsaaH deleted the app-watcher branch January 11, 2024 12:00
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.

3 participants