diff --git a/pkg/argocd/argocd.go b/pkg/argocd/argocd.go index f064eaaa..fe4810dd 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 723d6978..37697e77 100644 --- a/pkg/argocd/update.go +++ b/pkg/argocd/update.go @@ -288,6 +288,18 @@ func UpdateApplication(updateConf *UpdateConfiguration, state *SyncIterationStat if needsUpdate(updateableImage, applicationImage, latest) { imgCtx.Infof("Setting new image to %s", applicationImage.WithTag(latest).GetFullNameWithTag()) + + // Check if new image is alredy set in Application Spec when write back is set to argocd + // and compare with new image + appImage, err := getAppImage(&updateConf.UpdateApp.Application, applicationImage.WithTag(latest)) + if err != nil { + continue + } + if appImage == applicationImage.WithTag(latest).GetFullNameWithTag() { + imgCtx.Infof("New image %s already set in spec", applicationImage.WithTag(latest).GetFullNameWithTag()) + continue + } + needUpdate = true err = setAppImage(&updateConf.UpdateApp.Application, applicationImage.WithTag(latest)) @@ -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 {