summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--pkg/argocd/argocd.go79
-rw-r--r--pkg/argocd/update.go34
2 files changed, 108 insertions, 5 deletions
diff --git a/pkg/argocd/argocd.go b/pkg/argocd/argocd.go
index f064eaa..fe4810d 100644
--- a/pkg/argocd/argocd.go
+++ b/pkg/argocd/argocd.go
@@ -374,6 +374,55 @@ func mergeHelmParams(src []v1alpha1.HelmParameter, merge []v1alpha1.HelmParamete
return retParams
}
+// GetHelmImage gets the image set in Application source matching new image
+// or an empty string if match is not found
+func GetHelmImage(app *v1alpha1.Application, newImage *image.ContainerImage) (string, error) {
+
+ if appType := getApplicationType(app); appType != ApplicationTypeHelm {
+ return "", fmt.Errorf("cannot set Helm params on non-Helm application")
+ }
+
+ var hpImageName, hpImageTag, hpImageSpec string
+
+ hpImageSpec = newImage.GetParameterHelmImageSpec(app.Annotations)
+ hpImageName = newImage.GetParameterHelmImageName(app.Annotations)
+ hpImageTag = newImage.GetParameterHelmImageTag(app.Annotations)
+
+ if hpImageSpec == "" {
+ if hpImageName == "" {
+ hpImageName = common.DefaultHelmImageName
+ }
+ if hpImageTag == "" {
+ hpImageTag = common.DefaultHelmImageTag
+ }
+ }
+
+ appSource := getApplicationSource(app)
+
+ if appSource.Helm == nil {
+ return "", nil
+ }
+
+ if appSource.Helm.Parameters == nil {
+ return "", nil
+ }
+
+ if hpImageSpec != "" {
+ if p := getHelmParam(appSource.Helm.Parameters, hpImageSpec); p != nil {
+ return p.Value, nil
+ }
+ } else {
+ imageName := getHelmParam(appSource.Helm.Parameters, hpImageName)
+ imageTag := getHelmParam(appSource.Helm.Parameters, hpImageTag)
+ if imageName == nil || imageTag == nil {
+ return "", nil
+ }
+ return imageName.Value + ":" + imageTag.Value, nil
+ }
+
+ return "", nil
+}
+
// SetHelmImage sets image parameters for a Helm application
func SetHelmImage(app *v1alpha1.Application, newImage *image.ContainerImage) error {
if appType := getApplicationType(app); appType != ApplicationTypeHelm {
@@ -438,6 +487,36 @@ func SetHelmImage(app *v1alpha1.Application, newImage *image.ContainerImage) err
return nil
}
+// GetKustomizeImage gets the image set in Application source matching new image
+// or an empty string if match is not found
+func GetKustomizeImage(app *v1alpha1.Application, newImage *image.ContainerImage) (string, error) {
+ if appType := getApplicationType(app); appType != ApplicationTypeKustomize {
+ return "", fmt.Errorf("cannot set Kustomize image on non-Kustomize application")
+ }
+
+ ksImageName := newImage.GetParameterKustomizeImageName(app.Annotations)
+
+ appSource := getApplicationSource(app)
+
+ if appSource.Kustomize == nil {
+ return "", nil
+ }
+
+ ksImages := appSource.Kustomize.Images
+
+ if ksImages == nil {
+ return "", nil
+ }
+
+ for _, a := range ksImages {
+ if a.Match(v1alpha1.KustomizeImage(ksImageName)) {
+ return string(a), nil
+ }
+ }
+
+ return "", nil
+}
+
// SetKustomizeImage sets a Kustomize image for given application
func SetKustomizeImage(app *v1alpha1.Application, newImage *image.ContainerImage) error {
if appType := getApplicationType(app); appType != ApplicationTypeKustomize {
diff --git a/pkg/argocd/update.go b/pkg/argocd/update.go
index 723d697..046a2bb 100644
--- a/pkg/argocd/update.go
+++ b/pkg/argocd/update.go
@@ -286,20 +286,32 @@ func UpdateApplication(updateConf *UpdateConfiguration, state *SyncIterationStat
}
if needsUpdate(updateableImage, applicationImage, latest) {
+ appImageWithTag := applicationImage.WithTag(latest)
+ appImageFullNameWithTag := appImageWithTag.GetFullNameWithTag()
+
+ // Check if new image is already set in Application Spec when write back is set to argocd
+ // and compare with new image
+ appImageSpec, err := getAppImage(&updateConf.UpdateApp.Application, appImageWithTag)
+ if err != nil {
+ continue
+ }
+ if appImageSpec == appImageFullNameWithTag {
+ imgCtx.Infof("New image %s already set in spec", appImageFullNameWithTag)
+ continue
+ }
- imgCtx.Infof("Setting new image to %s", applicationImage.WithTag(latest).GetFullNameWithTag())
needUpdate = true
+ imgCtx.Infof("Setting new image to %s", appImageFullNameWithTag)
- err = setAppImage(&updateConf.UpdateApp.Application, applicationImage.WithTag(latest))
+ err = setAppImage(&updateConf.UpdateApp.Application, appImageWithTag)
if err != nil {
imgCtx.Errorf("Error while trying to update image: %v", err)
result.NumErrors += 1
continue
} else {
- containerImageNew := applicationImage.WithTag(latest)
- imgCtx.Infof("Successfully updated image '%s' to '%s', but pending spec update (dry run=%v)", updateableImage.GetFullNameWithTag(), containerImageNew.GetFullNameWithTag(), updateConf.DryRun)
- changeList = append(changeList, ChangeEntry{containerImageNew, updateableImage.ImageTag, containerImageNew.ImageTag})
+ imgCtx.Infof("Successfully updated image '%s' to '%s', but pending spec update (dry run=%v)", updateableImage.GetFullNameWithTag(), appImageFullNameWithTag, updateConf.DryRun)
+ changeList = append(changeList, ChangeEntry{appImageWithTag, updateableImage.ImageTag, appImageWithTag.ImageTag})
result.NumImagesUpdated += 1
}
} else {
@@ -382,6 +394,18 @@ func needsUpdate(updateableImage *image.ContainerImage, applicationImage *image.
return !updateableImage.ImageTag.Equals(latest) || applicationImage.KustomizeImage != nil && applicationImage.DiffersFrom(updateableImage, false)
}
+func getAppImage(app *v1alpha1.Application, img *image.ContainerImage) (string, error) {
+ var err error
+ if appType := GetApplicationType(app); appType == ApplicationTypeKustomize {
+ return GetKustomizeImage(app, img)
+ } else if appType == ApplicationTypeHelm {
+ return GetHelmImage(app, img)
+ } else {
+ err = fmt.Errorf("could not update application %s - neither Helm nor Kustomize application", app)
+ return "", err
+ }
+}
+
func setAppImage(app *v1alpha1.Application, img *image.ContainerImage) error {
var err error
if appType := GetApplicationType(app); appType == ApplicationTypeKustomize {