diff options
| author | jannfis <jann@mistrust.net> | 2020-08-12 18:26:26 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-08-12 18:26:26 +0200 |
| commit | bb2deb125bcf0c8de73561b3ff06e4661847e26f (patch) | |
| tree | 4096b08119c6872a27110bb8ee029321de9e557c /cmd | |
| parent | 526e78703986c5ee990ecbc039543aa8dcd32268 (diff) | |
refactor: Move update logic to argocd package and provide more tests (#45)
* refactor: Move update logic to argocd package and provide more tests
* Fix typos
Diffstat (limited to 'cmd')
| -rw-r--r-- | cmd/main.go | 148 |
1 files changed, 3 insertions, 145 deletions
diff --git a/cmd/main.go b/cmd/main.go index 5ece20f..70064f0 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -13,7 +13,6 @@ import ( "github.com/argoproj-labs/argocd-image-updater/pkg/client" "github.com/argoproj-labs/argocd-image-updater/pkg/env" "github.com/argoproj-labs/argocd-image-updater/pkg/health" - "github.com/argoproj-labs/argocd-image-updater/pkg/image" "github.com/argoproj-labs/argocd-image-updater/pkg/log" "github.com/argoproj-labs/argocd-image-updater/pkg/registry" "github.com/argoproj-labs/argocd-image-updater/pkg/version" @@ -41,150 +40,9 @@ type ImageUpdaterConfig struct { RegistriesConf string } -// Stores some statistics about the results of a run -type ImageUpdaterResult struct { - NumApplicationsProcessed int - NumImagesUpdated int - NumImagesConsidered int - NumSkipped int - NumErrors int -} - -// Update all images of a single application. Will run in a goroutine. -func updateApplication(argoClient argocd.ArgoCD, kubeClient *client.KubernetesClient, curApplication *argocd.ApplicationImages, dryRun bool) ImageUpdaterResult { - result := ImageUpdaterResult{} - app := curApplication.Application.GetName() - - // Get all images that are deployed with the current application - applicationImages := argocd.GetImagesFromApplication(&curApplication.Application) - - result.NumApplicationsProcessed += 1 - - // Loop through all images of current application, and check whether one of - // its images is eligible for updating. - // - // Whether an image qualifies for update is dependent on semantic version - // constraints which are part of the application's annotation values. - // - for _, applicationImage := range applicationImages { - updateableImage := curApplication.Images.ContainsImage(applicationImage, false) - if updateableImage == nil { - log.WithContext().AddField("application", app).Debugf("Image %s not in list of allowed images, skipping", applicationImage.ImageName) - result.NumSkipped += 1 - continue - } - - result.NumImagesConsidered += 1 - - imgCtx := log.WithContext(). - AddField("application", app). - AddField("registry", applicationImage.RegistryURL). - AddField("image_name", applicationImage.ImageName). - AddField("image_tag", applicationImage.ImageTag) - - imgCtx.Debugf("Considering this image for update") - - rep, err := registry.GetRegistryEndpoint(applicationImage.RegistryURL) - if err != nil { - imgCtx.Errorf("Could not get registry endpoint from configuration: %v", err) - result.NumErrors += 1 - continue - } - - var vc image.VersionConstraint - if updateableImage.ImageTag != nil { - vc.Constraint = updateableImage.ImageTag.TagName - imgCtx.Debugf("Using version constraint '%s' when looking for a new tag", vc.Constraint) - } else { - imgCtx.Debugf("Using no version constraint when looking for a new tag") - } - - vc.SortMode = updateableImage.GetParameterUpdateStrategy(curApplication.Application.Annotations) - - ep, err := registry.GetRegistryEndpoint(updateableImage.RegistryURL) - if err != nil { - imgCtx.Errorf("Could not get registry endpoint: %v", err) - continue - } - - err = ep.SetEndpointCredentials(kubeClient) - if err != nil { - imgCtx.Errorf("Could not set registry endpoint credentials: %v", err) - continue - } - - regClient, err := registry.NewClient(ep) - if err != nil { - imgCtx.Errorf("Could not create registry client: %v", err) - continue - } - - // Get list of available image tags from the repository - tags, err := rep.GetTags(applicationImage, regClient, &vc) - if err != nil { - imgCtx.Errorf("Could not get tags from registry: %v", err) - result.NumErrors += 1 - continue - } - - imgCtx.Tracef("List of available tags found: %v", tags.Tags()) - - // Get the latest available tag matching any constraint that might be set - // for allowed updates. - latest, err := applicationImage.GetNewestVersionFromTags(&vc, tags) - if err != nil { - imgCtx.Errorf("Unable to find newest version from available tags: %v", err) - result.NumErrors += 1 - continue - } - - // If we have no latest tag information, it means there was no tag which - // has met our version constraint (or there was no semantic versioned tag - // at all in the repository) - if latest == nil { - imgCtx.Debugf("No suitable image tag for upgrade found in list of available tags.") - result.NumSkipped += 1 - continue - } - - // If the latest tag does not match image's current tag, it means we have - // an update candidate. - if applicationImage.ImageTag.TagName != latest.TagName { - if dryRun { - imgCtx.Infof("Would upgrade image to %s, but this is a dry run. Skipping.", applicationImage.WithTag(latest).String()) - continue - } - - imgCtx.Infof("Upgrading image to %s", applicationImage.WithTag(latest).String()) - - if appType := argocd.GetApplicationType(&curApplication.Application); appType == argocd.ApplicationTypeKustomize { - err = argocd.SetKustomizeImage(argoClient, &curApplication.Application, updateableImage.WithTag(latest)) - } else if appType == argocd.ApplicationTypeHelm { - err = argocd.SetHelmImage(argoClient, &curApplication.Application, updateableImage.WithTag(latest)) - } else { - result.NumErrors += 1 - err = fmt.Errorf("Could not update application %s - neither Helm nor Kustomize application", app) - } - - if err != nil { - imgCtx.Errorf("Error while trying to update image: %v", err) - result.NumErrors += 1 - continue - } else { - imgCtx.Infof("Successfully updated image '%s' to '%s'", applicationImage.GetFullNameWithTag(), applicationImage.WithTag(latest).GetFullNameWithTag()) - result.NumImagesUpdated += 1 - } - } else { - imgCtx.Debugf("Image '%s' already on latest allowed version", applicationImage.GetFullNameWithTag()) - } - } - - return result -} - // Main loop for argocd-image-controller -func runImageUpdater(cfg *ImageUpdaterConfig) (ImageUpdaterResult, error) { - result := ImageUpdaterResult{} +func runImageUpdater(cfg *ImageUpdaterConfig) (argocd.ImageUpdaterResult, error) { + result := argocd.ImageUpdaterResult{} argoClient, err := argocd.NewClient(&cfg.ClientOpts) if err != nil { return result, err @@ -231,7 +89,7 @@ func runImageUpdater(cfg *ImageUpdaterConfig) (ImageUpdaterResult, error) { go func(app string, curApplication argocd.ApplicationImages) { defer sem.Release(1) log.Debugf("Processing application %s", app) - res := updateApplication(cfg.ArgoClient, cfg.KubeClient, &curApplication, cfg.DryRun) + res := argocd.UpdateApplication(registry.NewClient, cfg.ArgoClient, cfg.KubeClient, &curApplication, cfg.DryRun) result.NumApplicationsProcessed += 1 result.NumErrors += res.NumErrors result.NumImagesConsidered += res.NumImagesConsidered |
