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

#164 Send events on image change #167

Merged
merged 11 commits into from Mar 26, 2021
Merged

Conversation

magmax
Copy link
Contributor

@magmax magmax commented Mar 21, 2021

Sends an event on any image change (fixes #164)

I thought in re-using some argo-cd code, but finally I decided to copy and adapt it. It is the code related to send the event.

The namespace used is the one where argocd-image-updater is running in order to reduce the permissions required (a Role instead a ClusterRole).

I had to add some argo-cd dependencies to the kubernetes package, what is... ugly. Maybe you know a better way to do it.

Finally, understand this is my first golang contribution... I'm open to changes XD

NOTE: I didn't tested it under real fire. I have no docker repository accessible from my laptop.

@CLAassistant
Copy link

CLAassistant commented Mar 21, 2021

CLA assistant check
All committers have signed the CLA.

@codecov
Copy link

codecov bot commented Mar 21, 2021

Codecov Report

Merging #167 (8d2b15f) into master (21df300) will increase coverage by 0.65%.
The diff coverage is 90.90%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #167      +/-   ##
==========================================
+ Coverage   68.20%   68.85%   +0.65%     
==========================================
  Files          19       19              
  Lines        1343     1384      +41     
==========================================
+ Hits          916      953      +37     
- Misses        344      346       +2     
- Partials       83       85       +2     
Impacted Files Coverage Δ
pkg/argocd/update.go 72.26% <86.66%> (+0.66%) ⬆️
pkg/kube/kubernetes.go 85.29% <93.10%> (+5.29%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 21df300...8d2b15f. Read the comment docs.

Copy link
Contributor

@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.

Hey, awesome @magmax! Pretty great for your first lines of Go, actually! I only have a few comments (see inline) and also the following:

  • We need to give the ServiceAccount for the Image Updater appropriate Kubernetes RBAC permissions to create Events. The Role is part of the manifests and can be found here. Can you please provide proper authorization there?
  • I feel that emitting events should be enabled by default, but optionally can be disabled. It would be great to have a command line switch to disable it (e.g. --disable-kube-events). The main command can be found here and the flags are defined here. We provide default values to most flags that are override-able using environment variables (e.g. using env.GetBoolVal()), and then map those to keys in the argocd-image-updater-config ConfigMap, as shown here. I think this one should be settable by IMAGE_UPDATER_KUBE_EVENTS with a ConfigMap key of kube.events.

pkg/argocd/update.go Outdated Show resolved Hide resolved
@magmax
Copy link
Contributor Author

magmax commented Mar 25, 2021

I did the change.
My brains's python section says that I should have created an object for that.
The C section says it won't work due to memory weaks and misunderstanding.
But the Golang section says that everything is fine XD

I didn't tested it under real fire because I have no repository accessible from this computer and it would take me a while to do it.

Comment on lines 260 to 261
annotations[fmt.Sprintf("%s/oldtag", c.imageName)] = c.oldTag.TagName
annotations[fmt.Sprintf("%s/newtag", c.imageName)] = c.newTag.TagName
Copy link
Contributor

Choose a reason for hiding this comment

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

On a second thought, I'm not sure whether this will work reliably. The name parts of annotations are rather strict, and must follow a rule:

regex used for validation is '([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]') with an optional DNS subdomain prefix and '/' (e.g. 'example.com/MyName')

Also, the name must be max 63 characters long. I think we must either make sure that the image name is properly normalized, look for another name for the annotation (and put the image name in its value) or think about a completely another solution (e.g. putting the details into the event's Message).

Is there a specific reason that you use an annotation for storing the updated images?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not really. I want just to "pass parameters" between the argocd-image-updater and a listener through events.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

New approach. Now annotations have less randomness and a fixed size around 40 characters

Copy link
Contributor

Choose a reason for hiding this comment

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

Awesome! I tested it successfully, the event now looks like:

Name:             guestbook-multi.166fdad08646fd49
Namespace:        argocd
Labels:           <none>
Annotations:      argocd-image-updater.image-0/image-name: heptio-images/ks-guestbook-demo
                  argocd-image-updater.image-0/new-tag: 0.2
                  argocd-image-updater.image-0/old-tag: 0.1
                  argocd-image-updater.image-1/image-name: coredns/coredns
                  argocd-image-updater.image-1/new-tag: 1.8.3
                  argocd-image-updater.image-1/old-tag: 1.8.0
API Version:      v1               
Count:            1   
...
Involved Object:                
  API Version:       argoproj.io/v1alpha1
  Kind:              Application
  Name:              guestbook-multi                                                                     
  Namespace:         argocd                                                                                                                                                                                        
...
Message:             Successfully updated application 'guestbook-multi'
...
Source:
  Component:  ArgocdImageUpdater
Type:         Normal

I'm not sure whether it's intended to use the image name without the registry prefix (the Image Updater's API is possibly not good documented in that regard), but I'd suggest to change

changeList = append(changeList, change{updateableImage.ImageName, updateableImage.ImageTag, containerImageNew.ImageTag})

to

changeList = append(changeList, change{updateableImage.GetFullNameWithoutTag(), updateableImage.ImageTag, containerImageNew.ImageTag})

at the place where you store the image information to be propagated to the event.

And I also guess this would be the last change required before merge 🎉

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm thinking in using annotations to compose a message on slack. So allowing the user to decide might be a really cool idea :)

So, I finally decided to add both.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm fine with that solution as well 👍

Copy link
Contributor

@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.

LGTM!

Thanks for this contribution @magmax - much appreciated!

Also, you said it's your first steps into Golang, but I have to say great job on the code and thanks for constructively working through the review with me!

@jannfis jannfis merged commit 274d19c into argoproj-labs:master Mar 26, 2021
@magmax
Copy link
Contributor Author

magmax commented Mar 26, 2021

Thank you very much for your comments and support.

@axot
Copy link

axot commented Mar 24, 2022

If argocd is running in a different namespace as involvedObject.namespace, the event can't be sent.

"Event could not be sent: Event "xxxx" is invalid: involvedObject.namespace: Invalid value: "yyyy": does not match event.namespace" application=zzzz

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.

Create events for changes
4 participants