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

support app set #266

Merged
merged 2 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ Run the following commands for available flags and subcommands:
### Custom Packages

Idpbuilder supports specifying custom packages using the flag `--package-dir` flag.
This flag expects a directory (local or remote) containing ArgoCD application files.
This flag expects a directory (local or remote) containing ArgoCD application files and / or ArgoCD application set files.
In case of a remote directory, it must be a directory in a git repository,
and the URL format must be a [kustomize remote URL format](https://github.com/kubernetes-sigs/kustomize/blob/master/examples/remoteBuild.md).

Expand Down
2 changes: 2 additions & 0 deletions api/v1alpha1/custom_package_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ type ArgoCDPackageSpec struct {
ApplicationFile string `json:"applicationFile"`
Name string `json:"name"`
Namespace string `json:"namespace"`
// +kubebuilder:validation:Enum:=Application;ApplicationSet
Type string `json:"type"`
}

type CustomPackageStatus struct {
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.21.3

require (
code.gitea.io/sdk/gitea v0.16.0
github.com/cnoe-io/argocd-api v0.0.0-20240125015729-416a35fe855d
github.com/cnoe-io/argocd-api v0.0.0-20240529230753-bb9d05f9e1d8
github.com/docker/docker v24.0.7+incompatible
github.com/go-git/go-billy/v5 v5.5.0
github.com/go-git/go-git/v5 v5.12.0
Expand All @@ -17,7 +17,7 @@ require (
k8s.io/apiextensions-apiserver v0.29.1
k8s.io/apimachinery v0.29.1
k8s.io/client-go v0.29.1
k8s.io/klog/v2 v2.110.1
k8s.io/klog/v2 v2.120.1
sigs.k8s.io/controller-runtime v0.17.1
sigs.k8s.io/kind v0.23.0
sigs.k8s.io/kustomize/kyaml v0.16.0
Expand Down Expand Up @@ -52,7 +52,7 @@ require (
github.com/go-openapi/swag v0.23.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
Expand Down Expand Up @@ -97,7 +97,7 @@ require (
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/oauth2 v0.16.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/term v0.18.0 // indirect
Expand Down
13 changes: 6 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XL
github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA=
github.com/cloudflare/circl v1.3.7 h1:qlCDlTPz2n9fu58M0Nh1J/JzcFpfgkFHHX3O35r5vcU=
github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBSc8r4zxgA=
github.com/cnoe-io/argocd-api v0.0.0-20240125015729-416a35fe855d h1:cmcSrS0OYTLlGsBFshaAG29qC+PC5LBtKRhnEknlzgU=
github.com/cnoe-io/argocd-api v0.0.0-20240125015729-416a35fe855d/go.mod h1:IXG3LiEAeckMfjdwJnt6qC0ee4J4U5bleMuk1HN82ZA=
github.com/cnoe-io/argocd-api v0.0.0-20240529230753-bb9d05f9e1d8 h1:BYnQciuk2mQl3rE2B+P3u4Od7PK7PMUxUfRlz3cXIeA=
github.com/cnoe-io/argocd-api v0.0.0-20240529230753-bb9d05f9e1d8/go.mod h1:vc5mQ/ctD9dFYbWrYHyOclTdbbYU5p+QoFgJBG21RQQ=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cyphar/filepath-securejoin v0.2.4 h1:Ugdm7cg7i6ZK6x3xDF1oEu1nfkyfH53EtKeQYTC3kyg=
github.com/cyphar/filepath-securejoin v0.2.4/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxGGx79pTxQpKOJNYHHl4=
Expand Down Expand Up @@ -69,7 +69,6 @@ github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399 h1:eMj
github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399/go.mod h1:1OCfN199q1Jm3HZlxleg+Dw/mwps2Wbk9frAWm+4FII=
github.com/go-git/go-git/v5 v5.12.0 h1:7Md+ndsjrzZxbddRDZjF14qK+NN56sy6wkqaVrjZtys=
github.com/go-git/go-git/v5 v5.12.0/go.mod h1:FTM9VKtnI2m65hNI/TenDDDnUf2Q9FHnXYjuz9i5OEY=
github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/zapr v1.3.0 h1:XGdV8XW8zdwFiwOA2Dryh1gj2KRQyOOoNmBy4EplIcQ=
Expand Down Expand Up @@ -240,8 +239,8 @@ golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/oauth2 v0.16.0 h1:aDkGMBSYxElaoP81NpoUoz2oo2R2wHdZpGToUxfyQrQ=
golang.org/x/oauth2 v0.16.0/go.mod h1:hqZ+0LWXsiVoZpeld6jVt06P3adbS2Uu911W1SsJv2o=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down Expand Up @@ -335,8 +334,8 @@ k8s.io/client-go v0.29.1 h1:19B/+2NGEwnFLzt0uB5kNJnfTsbV8w6TgQRz9l7ti7A=
k8s.io/client-go v0.29.1/go.mod h1:TDG/psL9hdet0TI9mGyHJSgRkW3H9JZk2dNEUS7bRks=
k8s.io/component-base v0.29.1 h1:MUimqJPCRnnHsskTTjKD+IC1EHBbRCVyi37IoFBrkYw=
k8s.io/component-base v0.29.1/go.mod h1:fP9GFjxYrLERq1GcWWZAE3bqbNcDKDytn2srWuHTtKc=
k8s.io/klog/v2 v2.110.1 h1:U/Af64HJf7FcwMcXyKm2RPM22WZzyR7OSpYj5tg3cL0=
k8s.io/klog/v2 v2.110.1/go.mod h1:YGtd1984u+GgbuZ7e08/yBuAfKLSO0+uR1Fhi6ExXjo=
k8s.io/klog/v2 v2.120.1 h1:QXU6cPEOIslTGvZaXvFWiP9VKyeet3sawzTOvdXb4Vw=
k8s.io/klog/v2 v2.120.1/go.mod h1:3Jpz1GvMt720eyJH1ckRHK1EDfpxISzJ7I9OYgaDtPE=
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 h1:aVUu9fTY98ivBPKR9Y5w/AuzbMm96cd3YHRTU83I780=
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00/go.mod h1:AsvuZPBlUDVuCdzJ87iajxtXuR9oktsTctW/R9wwouA=
k8s.io/utils v0.0.0-20230726121419-3b25d923346b h1:sgn3ZU783SCgtaSJjpcVVlRqd6GSnlTLKgpAAttJvpI=
Expand Down
192 changes: 135 additions & 57 deletions pkg/controllers/custompackage/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"time"

argocdapplication "github.com/cnoe-io/argocd-api/api/argo/application"
argov1alpha1 "github.com/cnoe-io/argocd-api/api/argo/application/v1alpha1"
"github.com/cnoe-io/idpbuilder/api/v1alpha1"
"github.com/cnoe-io/idpbuilder/pkg/k8s"
Expand Down Expand Up @@ -77,55 +78,101 @@ func (r *Reconciler) reconcileCustomPackage(ctx context.Context, resource *v1alp
return ctrl.Result{}, fmt.Errorf("reading file %s: %w", resource.Spec.ArgoCD.ApplicationFile, err)
}

var returnedRawResource []byte
if returnedRawResource, err = util.ApplyTemplate(b, r.Config); err != nil {
return ctrl.Result{}, err
}

objs, err := k8s.ConvertYamlToObjects(r.Scheme, returnedRawResource)
objs, err := k8s.ConvertYamlToObjects(r.Scheme, b)
if err != nil {
return ctrl.Result{}, fmt.Errorf("converting yaml to object %w", err)
}
if len(objs) == 0 {
return ctrl.Result{}, fmt.Errorf("file contained 0 kubernetes objects %s", resource.Spec.ArgoCD.ApplicationFile)
}

app, ok := objs[0].(*argov1alpha1.Application)
if !ok {
return ctrl.Result{}, fmt.Errorf("object is not an ArgoCD application %s", resource.Spec.ArgoCD.ApplicationFile)
}
switch resource.Spec.ArgoCD.Type {
case argocdapplication.ApplicationKind:
app, ok := objs[0].(*argov1alpha1.Application)
if !ok {
return ctrl.Result{}, fmt.Errorf("object is not an ArgoCD application %s", resource.Spec.ArgoCD.ApplicationFile)
}

res, err := r.reconcileArgoCDApp(ctx, resource, app)
if err != nil {
return ctrl.Result{}, err
}

appName := app.GetName()
if resource.Spec.Replicate {
synced := true
repoRefs := make([]v1alpha1.ObjectRef, 0, 1)
if app.Spec.HasMultipleSources() {
for j := range app.Spec.Sources {
s := &app.Spec.Sources[j]
res, repo, sErr := r.reconcileArgoCDSource(ctx, resource, s, appName)
if sErr != nil {
return res, sErr
foundAppObj := argov1alpha1.Application{}
err = r.Client.Get(ctx, client.ObjectKeyFromObject(app), &foundAppObj)
if err != nil {
if errors.IsNotFound(err) {
err = r.Client.Create(ctx, app)
if err != nil {
return ctrl.Result{}, fmt.Errorf("creating %s app CR: %w", app.Name, err)
}
if repo != nil {
if synced {
synced = repo.Status.InternalGitRepositoryUrl != ""
}
s.RepoURL = repo.Status.InternalGitRepositoryUrl
repoRefs = append(repoRefs, v1alpha1.ObjectRef{
Namespace: repo.Namespace,
Name: repo.Name,
UID: string(repo.ObjectMeta.UID),
})

return ctrl.Result{RequeueAfter: requeueTime}, nil
}
return ctrl.Result{}, fmt.Errorf("getting argocd application object: %w", err)
}

foundAppObj.Spec = app.Spec
foundAppObj.ObjectMeta.Annotations = app.GetAnnotations()
foundAppObj.ObjectMeta.Labels = app.GetLabels()
err = r.Client.Update(ctx, &foundAppObj)
if err != nil {
return ctrl.Result{}, fmt.Errorf("updating argocd application object %s: %w", app.Name, err)
}
return res, nil

case argocdapplication.ApplicationSetKind:
// application set embeds application spec. extract it then handle git generator repoURLs.
appSet, ok := objs[0].(*argov1alpha1.ApplicationSet)
if !ok {
return ctrl.Result{}, fmt.Errorf("object is not an ArgoCD application set %s", resource.Spec.ArgoCD.ApplicationFile)
}
res, err := r.reconcileArgoCDAppSet(ctx, resource, appSet)
if err != nil {
return ctrl.Result{}, err
}
foundAppSetObj := argov1alpha1.ApplicationSet{}
err = r.Client.Get(ctx, client.ObjectKeyFromObject(appSet), &foundAppSetObj)
if err != nil {
if errors.IsNotFound(err) {
err = r.Client.Create(ctx, appSet)
if err != nil {
return ctrl.Result{}, fmt.Errorf("creating %s argocd application set CR: %w", appSet.Name, err)
}
return ctrl.Result{RequeueAfter: requeueTime}, nil
}
} else {
s := app.Spec.Source
res, repo, sErr := r.reconcileArgoCDSource(ctx, resource, s, appName)
return ctrl.Result{}, fmt.Errorf("getting argocd application set object: %w", err)
}

foundAppSetObj.Spec = appSet.Spec
Copy link
Contributor

Choose a reason for hiding this comment

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

the reconcilliation logic is duplicated here with the above case. Is it possible to dedupe?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I considered it but I don't think we'd save any lines of code or performance because we'd have to type cast in the new function then branch out anyways. I figured a bit of duplication here is acceptable.

foundAppSetObj.ObjectMeta.Annotations = appSet.GetAnnotations()
foundAppSetObj.ObjectMeta.Labels = appSet.GetLabels()
err = r.Client.Update(ctx, &foundAppSetObj)
if err != nil {
return ctrl.Result{}, fmt.Errorf("updating argocd application object %s: %w", appSet.Name, err)
}
return res, nil

default:
return ctrl.Result{}, fmt.Errorf("file is not a supported argocd kind %s", resource.Spec.ArgoCD.ApplicationFile)
}
}

func (r *Reconciler) reconcileArgoCDApp(ctx context.Context, resource *v1alpha1.CustomPackage, app *argov1alpha1.Application) (ctrl.Result, error) {
appSourcesSynced := true
repoRefs := make([]v1alpha1.ObjectRef, 0, 1)
if app.Spec.HasMultipleSources() {
notSyncedRepos := 0
for j := range app.Spec.Sources {
s := &app.Spec.Sources[j]
res, repo, sErr := r.reconcileArgoCDSource(ctx, resource, s.RepoURL, app.Name)
if sErr != nil {
return res, sErr
}
if repo != nil {
synced = repo.Status.InternalGitRepositoryUrl != ""
if repo.Status.InternalGitRepositoryUrl == "" {
notSyncedRepos += 1
}
s.RepoURL = repo.Status.InternalGitRepositoryUrl
repoRefs = append(repoRefs, v1alpha1.ObjectRef{
Namespace: repo.Namespace,
Expand All @@ -134,40 +181,69 @@ func (r *Reconciler) reconcileCustomPackage(ctx context.Context, resource *v1alp
})
}
}
resource.Status.GitRepositoryRefs = repoRefs
resource.Status.Synced = synced
appSourcesSynced = notSyncedRepos == 0
} else {
s := app.Spec.Source
res, repo, sErr := r.reconcileArgoCDSource(ctx, resource, s.RepoURL, app.Name)
if sErr != nil {
return res, sErr
}
if repo != nil {
appSourcesSynced = repo.Status.InternalGitRepositoryUrl != ""
s.RepoURL = repo.Status.InternalGitRepositoryUrl
repoRefs = append(repoRefs, v1alpha1.ObjectRef{
Namespace: repo.Namespace,
Name: repo.Name,
UID: string(repo.ObjectMeta.UID),
})
}
}
resource.Status.GitRepositoryRefs = repoRefs
resource.Status.Synced = appSourcesSynced
return ctrl.Result{RequeueAfter: requeueTime}, nil
}

foundAppObj := argov1alpha1.Application{}
err = r.Client.Get(ctx, client.ObjectKeyFromObject(app), &foundAppObj)
if err != nil {
if errors.IsNotFound(err) {
err = r.Client.Create(ctx, app)
if err != nil {
return ctrl.Result{}, fmt.Errorf("creating %s app CR: %w", appName, err)
func (r *Reconciler) reconcileArgoCDAppSet(ctx context.Context, resource *v1alpha1.CustomPackage, appSet *argov1alpha1.ApplicationSet) (ctrl.Result, error) {
notSyncedRepos := 0
for i := range appSet.Spec.Generators {
g := appSet.Spec.Generators[i]
if g.Git != nil {
res, repo, gErr := r.reconcileArgoCDSource(ctx, resource, g.Git.RepoURL, appSet.GetName())
if gErr != nil {
return res, fmt.Errorf("reconciling git generator URL %s, %s: %w", g.Git.RepoURL, resource.Spec.ArgoCD.ApplicationFile, gErr)
}
if repo != nil {
g.Git.RepoURL = repo.Status.InternalGitRepositoryUrl
if repo.Status.InternalGitRepositoryUrl == "" {
notSyncedRepos += 1
}
}

return ctrl.Result{RequeueAfter: requeueTime}, nil
}
return ctrl.Result{}, fmt.Errorf("getting argocd application object: %w", err)
}

foundAppObj.Spec = app.Spec
foundAppObj.ObjectMeta.Annotations = app.Annotations
foundAppObj.ObjectMeta.Labels = app.Labels
err = r.Client.Update(ctx, &foundAppObj)
gitGeneratorsSynced := notSyncedRepos == 0
app := argov1alpha1.Application{
ObjectMeta: metav1.ObjectMeta{Name: appSet.GetName(), Namespace: appSet.Namespace},
}
app.Spec = appSet.Spec.Template.Spec

_, err := r.reconcileArgoCDApp(ctx, resource, &app)
if err != nil {
return ctrl.Result{}, fmt.Errorf("updating argocd application object %s: %w", appName, err)
return ctrl.Result{}, fmt.Errorf("reconciling application set %s %w", resource.Spec.ArgoCD.ApplicationFile, err)
}

resource.Status.Synced = resource.Status.Synced && gitGeneratorsSynced

return ctrl.Result{RequeueAfter: requeueTime}, nil
}

func (r *Reconciler) reconcileArgoCDSource(ctx context.Context, resource *v1alpha1.CustomPackage, appSource *argov1alpha1.ApplicationSource, appName string) (ctrl.Result, *v1alpha1.GitRepository, error) {
if isCNOEScheme(appSource.RepoURL) {
// create a gitrepository custom resource, then let the git repository controller take care of the rest
func (r *Reconciler) reconcileArgoCDSource(ctx context.Context, resource *v1alpha1.CustomPackage, repoUrl, appName string) (ctrl.Result, *v1alpha1.GitRepository, error) {
if isCNOEScheme(repoUrl) {
if resource.Spec.RemoteRepository.Url == "" {
return r.reconcileArgoCDSourceFromLocal(ctx, resource, appName, appSource.RepoURL)
return r.reconcileArgoCDSourceFromLocal(ctx, resource, appName, repoUrl)
}
return r.reconcileArgoCDSourceFromRemote(ctx, resource, appName, appSource.RepoURL)
return r.reconcileArgoCDSourceFromRemote(ctx, resource, appName, repoUrl)
}
return ctrl.Result{}, nil, nil
}
Expand Down Expand Up @@ -281,8 +357,10 @@ func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
}

func (r *Reconciler) getArgoCDAppFile(ctx context.Context, resource *v1alpha1.CustomPackage) ([]byte, error) {
filePath := resource.Spec.ArgoCD.ApplicationFile

if resource.Spec.RemoteRepository.Url == "" {
return os.ReadFile(resource.Spec.ArgoCD.ApplicationFile)
return os.ReadFile(filePath)
}

cloneDir := util.RepoDir(resource.Spec.RemoteRepository.Url, r.TempDir)
Expand All @@ -293,7 +371,7 @@ func (r *Reconciler) getArgoCDAppFile(ctx context.Context, resource *v1alpha1.Cu
if err != nil {
return nil, fmt.Errorf("cloning repo, %s: %w", resource.Spec.RemoteRepository.Url, err)
}
return util.ReadWorktreeFile(wt, resource.Spec.ArgoCD.ApplicationFile)
return util.ReadWorktreeFile(wt, filePath)
}

func localRepoName(appName, dir string) string {
Expand Down
Loading
Loading