Skip to content

Commit

Permalink
feat: add multisources support (argoproj-labs#513)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tronix117 committed Mar 30, 2023
1 parent 50f3cc7 commit 8d231f9
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 32 deletions.
50 changes: 35 additions & 15 deletions pkg/argocd/argocd.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (client *k8sClient) UpdateSpec(ctx context.Context, spec *application.Appli
if err != nil {
return nil, err
}
app.Spec = spec.Spec
app.Spec = *spec.Spec

updatedApp, err := client.kubeClient.ApplicationsClientset.ArgoprojV1alpha1().Applications(client.kubeClient.Namespace).Update(ctx, app, v1.UpdateOptions{})
if err != nil {
Expand Down Expand Up @@ -179,14 +179,23 @@ func FilterApplicationsForUpdate(apps []v1alpha1.Application, patterns []string,
logCtx := log.WithContext().AddField("application", app.GetName())
// Check whether application has our annotation set
annotations := app.GetAnnotations()

var sourceType v1alpha1.ApplicationSourceType

if len(app.Status.SourceTypes) > 0 {
sourceType = app.Status.SourceTypes[0]
} else {
sourceType = app.Status.SourceType
}

if _, ok := annotations[common.ImageUpdaterAnnotation]; !ok {
logCtx.Tracef("skipping app '%s' of type '%s' because required annotation is missing", app.GetName(), app.Status.SourceType)
logCtx.Tracef("skipping app '%s' of type '%s' because required annotation is missing", app.GetName(), sourceType)
continue
}

// Check for valid application type
if !IsValidApplicationType(&app) {
logCtx.Warnf("skipping app '%s' of type '%s' because it's not of supported source type", app.GetName(), app.Status.SourceType)
logCtx.Warnf("skipping app '%s' of type '%s' because it's not of supported source type", app.GetName(), sourceType)
continue
}

Expand All @@ -202,7 +211,7 @@ func FilterApplicationsForUpdate(apps []v1alpha1.Application, patterns []string,
continue
}

logCtx.Tracef("processing app '%s' of type '%v'", app.GetName(), app.Status.SourceType)
logCtx.Tracef("processing app '%s' of type '%v'", app.GetName(), sourceType)
imageList := parseImageList(annotations)
appImages := ApplicationImages{}
appImages.Application = app
Expand Down Expand Up @@ -425,15 +434,17 @@ func SetHelmImage(app *v1alpha1.Application, newImage *image.ContainerImage) err
}
}

if app.Spec.Source.Helm == nil {
app.Spec.Source.Helm = &v1alpha1.ApplicationSourceHelm{}
// This case seems not possible, but we check it anyway
helmSpec := app.Spec.GetSource().Helm
if helmSpec == nil {
helmSpec = &v1alpha1.ApplicationSourceHelm{}
}

if app.Spec.Source.Helm.Parameters == nil {
app.Spec.Source.Helm.Parameters = make([]v1alpha1.HelmParameter, 0)
if helmSpec.Parameters == nil {
helmSpec.Parameters = make([]v1alpha1.HelmParameter, 0)
}

app.Spec.Source.Helm.Parameters = mergeHelmParams(app.Spec.Source.Helm.Parameters, mergeParams)
helmSpec.Parameters = mergeHelmParams(helmSpec.Parameters, mergeParams)

return nil
}
Expand All @@ -454,22 +465,24 @@ func SetKustomizeImage(app *v1alpha1.Application, newImage *image.ContainerImage

log.WithContext().AddField("application", app.GetName()).Tracef("Setting Kustomize parameter %s", ksImageParam)

if app.Spec.Source.Kustomize == nil {
app.Spec.Source.Kustomize = &v1alpha1.ApplicationSourceKustomize{}
// This case seems not possible, but we check it anyway
kustomizeSpec := app.Spec.GetSource().Kustomize
if kustomizeSpec == nil {
kustomizeSpec = &v1alpha1.ApplicationSourceKustomize{}
}

for i, kImg := range app.Spec.Source.Kustomize.Images {
for i, kImg := range kustomizeSpec.Images {
curr := image.NewFromIdentifier(string(kImg))
override := image.NewFromIdentifier(ksImageParam)

if curr.ImageName == override.ImageName {
curr.ImageAlias = override.ImageAlias
app.Spec.Source.Kustomize.Images[i] = v1alpha1.KustomizeImage(override.String())
kustomizeSpec.Images[i] = v1alpha1.KustomizeImage(override.String())
}

}

app.Spec.Source.Kustomize.MergeImage(v1alpha1.KustomizeImage(ksImageParam))
kustomizeSpec.MergeImage(v1alpha1.KustomizeImage(ksImageParam))

return nil
}
Expand Down Expand Up @@ -518,7 +531,14 @@ func IsValidApplicationType(app *v1alpha1.Application) bool {

// getApplicationType returns the type of the application
func getApplicationType(app *v1alpha1.Application) ApplicationType {
sourceType := app.Status.SourceType
var sourceType v1alpha1.ApplicationSourceType

if len(app.Status.SourceTypes) > 0 {
sourceType = app.Status.SourceTypes[0]
} else {
sourceType = app.Status.SourceType
}

if st, set := app.Annotations[common.WriteBackTargetAnnotation]; set &&
strings.HasPrefix(st, common.KustomizationPrefix) {
sourceType = v1alpha1.ApplicationSourceTypeKustomize
Expand Down
8 changes: 4 additions & 4 deletions pkg/argocd/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func commitChangesGit(app *v1alpha1.Application, wbc *WriteBackConfig, changeLis
logCtx := log.WithContext().AddField("application", app.GetName())
creds, err := wbc.GetCreds(app)
if err != nil {
return fmt.Errorf("could not get creds for repo '%s': %v", app.Spec.Source.RepoURL, err)
return fmt.Errorf("could not get creds for repo '%s': %v", app.Spec.GetSource().RepoURL, err)
}
var gitC git.Client
if wbc.GitClient == nil {
Expand All @@ -145,7 +145,7 @@ func commitChangesGit(app *v1alpha1.Application, wbc *WriteBackConfig, changeLis
logCtx.Errorf("could not remove temp dir: %v", err)
}
}()
gitC, err = git.NewClientExt(app.Spec.Source.RepoURL, tempRoot, creds, false, false, "")
gitC, err = git.NewClientExt(app.Spec.GetSource().RepoURL, tempRoot, creds, false, false, "")
if err != nil {
return err
}
Expand Down Expand Up @@ -173,7 +173,7 @@ func commitChangesGit(app *v1alpha1.Application, wbc *WriteBackConfig, changeLis
// config, or taken from the application spec's targetRevision. If the
// target revision is set to the special value HEAD, or is the empty
// string, we'll try to resolve it to a branch name.
checkOutBranch := app.Spec.Source.TargetRevision
checkOutBranch := app.Spec.GetSource().TargetRevision
if wbc.GitBranch != "" {
checkOutBranch = wbc.GitBranch
}
Expand Down Expand Up @@ -314,7 +314,7 @@ func writeKustomization(app *v1alpha1.Application, wbc *WriteBackConfig, gitC gi
return fmt.Errorf("could not find kustomization in %s", base), false
}

filterFunc, err := imagesFilter(app.Spec.Source.Kustomize.Images)
filterFunc, err := imagesFilter(app.Spec.GetSource().Kustomize.Images)
if err != nil {
return err, false
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/argocd/gitcreds.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ func getCredsFromArgoCD(app *v1alpha1.Application, kubeClient *kube.KubernetesCl

settingsMgr := settings.NewSettingsManager(ctx, kubeClient.Clientset, kubeClient.Namespace)
argocdDB := db.NewDB(kubeClient.Namespace, settingsMgr, kubeClient.Clientset)
repo, err := argocdDB.GetRepository(ctx, app.Spec.Source.RepoURL)
repo, err := argocdDB.GetRepository(ctx, app.Spec.GetSource().RepoURL)
if err != nil {
return nil, err
}
if !repo.HasCredentials() {
return nil, fmt.Errorf("credentials for '%s' are not configured in Argo CD settings", app.Spec.Source.RepoURL)
return nil, fmt.Errorf("credentials for '%s' are not configured in Argo CD settings", app.Spec.GetSource().RepoURL)
}
return repo.GetGitCreds(argoGit.NoopCredsStore{}), nil
}
Expand All @@ -61,13 +61,13 @@ func getCredsFromSecret(app *v1alpha1.Application, credentialsSecret string, kub
return nil, fmt.Errorf("secret ref must be in format 'namespace/name', but is '%s'", credentialsSecret)
}

if ok, _ := git.IsSSHURL(app.Spec.Source.RepoURL); ok {
if ok, _ := git.IsSSHURL(app.Spec.GetSource().RepoURL); ok {
var sshPrivateKey []byte
if sshPrivateKey, ok = credentials["sshPrivateKey"]; !ok {
return nil, fmt.Errorf("invalid secret %s: does not contain field sshPrivateKey", credentialsSecret)
}
return git.NewSSHCreds(string(sshPrivateKey), "", true), nil
} else if git.IsHTTPSURL(app.Spec.Source.RepoURL) {
} else if git.IsHTTPSURL(app.Spec.GetSource().RepoURL) {
var username, password []byte
if username, ok = credentials["username"]; !ok {
return nil, fmt.Errorf("invalid secret %s: does not contain field username", credentialsSecret)
Expand Down
14 changes: 7 additions & 7 deletions pkg/argocd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,22 +382,22 @@ func marshalParamsOverride(app *v1alpha1.Application) ([]byte, error) {
appType := GetApplicationType(app)
switch appType {
case ApplicationTypeKustomize:
if app.Spec.Source.Kustomize == nil {
if app.Spec.GetSource().Kustomize == nil {
return []byte{}, nil
}
params := kustomizeOverride{
Kustomize: kustomizeImages{
Images: &app.Spec.Source.Kustomize.Images,
Images: &app.Spec.GetSource().Kustomize.Images,
},
}
override, err = yaml.Marshal(params)
case ApplicationTypeHelm:
if app.Spec.Source.Helm == nil {
if app.Spec.GetSource().Helm == nil {
return []byte{}, nil
}
params := helmOverride{
Helm: helmParameters{
Parameters: app.Spec.Source.Helm.Parameters,
Parameters: app.Spec.GetSource().Helm.Parameters,
},
}
override, err = yaml.Marshal(params)
Expand All @@ -416,7 +416,7 @@ func getWriteBackConfig(app *v1alpha1.Application, kubeClient *kube.KubernetesCl
// Default write-back is to use Argo CD API
wbc.Method = WriteBackApplication
wbc.ArgoClient = argoClient
wbc.Target = parseDefaultTarget(app.Name, app.Spec.Source.Path)
wbc.Target = parseDefaultTarget(app.Name, app.Spec.GetSource().Path)

// If we have no update method, just return our default
method, ok := app.Annotations[common.WriteBackMethodAnnotation]
Expand All @@ -436,7 +436,7 @@ func getWriteBackConfig(app *v1alpha1.Application, kubeClient *kube.KubernetesCl
case "git":
wbc.Method = WriteBackGit
if target, ok := app.Annotations[common.WriteBackTargetAnnotation]; ok && strings.HasPrefix(target, common.KustomizationPrefix) {
wbc.KustomizeBase = parseTarget(target, app.Spec.Source.Path)
wbc.KustomizeBase = parseTarget(target, app.Spec.GetSource().Path)
}
if err := parseGitConfig(app, kubeClient, wbc, creds); err != nil {
return nil, err
Expand Down Expand Up @@ -486,7 +486,7 @@ func parseGitConfig(app *v1alpha1.Application, kubeClient *kube.KubernetesClient

func commitChangesLocked(app *v1alpha1.Application, wbc *WriteBackConfig, state *SyncIterationState, changeList []ChangeEntry) error {
if wbc.RequiresLocking() {
lock := state.GetRepositoryLock(app.Spec.Source.RepoURL)
lock := state.GetRepositoryLock(app.Spec.GetSource().RepoURL)
lock.Lock()
defer lock.Unlock()
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/argocd/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ func Test_UpdateApplication(t *testing.T) {
UpdateApp: appImages,
DryRun: false,
}, NewSyncIterationState())
assert.Equal(t, v1alpha1.KustomizeImage("gcr.io/jannfis/foobar:1.0.3"), appImages.Application.Spec.Source.Kustomize.Images[0])
assert.Equal(t, v1alpha1.KustomizeImage("gcr.io/jannfis/barbar:1.0.3"), appImages.Application.Spec.Source.Kustomize.Images[1])
assert.Equal(t, v1alpha1.KustomizeImage("gcr.io/jannfis/foobar:1.0.3"), appImages.Application.Spec.GetSource().Kustomize.Images[0])
assert.Equal(t, v1alpha1.KustomizeImage("gcr.io/jannfis/barbar:1.0.3"), appImages.Application.Spec.GetSource().Kustomize.Images[1])
assert.Equal(t, 0, res.NumErrors)
assert.Equal(t, 0, res.NumSkipped)
assert.Equal(t, 1, res.NumApplicationsProcessed)
Expand Down

0 comments on commit 8d231f9

Please sign in to comment.