Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
nabuskey committed May 29, 2024
1 parent 4b953ea commit deb5e10
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 30 deletions.
8 changes: 5 additions & 3 deletions api/v1alpha1/custom_package_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ type RemoteRepositorySpec struct {

type ArgoCDPackageSpec struct {
// ApplicationFile specifies the absolute path to the ArgoCD application file
ApplicationFile string `json:"applicationFile"`
Name string `json:"name"`
Namespace string `json:"namespace"`
ApplicationFile string `json:"applicationFile"`
ApplicationSetFile string `json:"applicationSetFile"`
Name string `json:"name"`
Namespace string `json:"namespace"`
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-20240529000425-926dd47185f1
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-alpha.0.20240325200702-a64c3ab43010
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 @@ -26,8 +26,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-20240529000425-926dd47185f1 h1:n8GUS1AZnCLUQ77agVjFxmcJ/OoEn481H6U/PRX+3Ic=
github.com/cnoe-io/argocd-api v0.0.0-20240529000425-926dd47185f1/go.mod h1:vc5mQ/ctD9dFYbWrYHyOclTdbbYU5p+QoFgJBG21RQQ=
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cyphar/filepath-securejoin v0.2.4 h1:Ugdm7cg7i6ZK6x3xDF1oEu1nfkyfH53EtKeQYTC3kyg=
Expand Down Expand Up @@ -72,7 +72,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 @@ -250,8 +249,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 @@ -347,8 +346,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
50 changes: 38 additions & 12 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 @@ -90,18 +91,35 @@ func (r *Reconciler) reconcileCustomPackage(ctx context.Context, resource *v1alp
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)
var appSpec *argov1alpha1.ApplicationSpec
var app client.Object

switch resource.Spec.ArgoCD.Type {
case argocdapplication.ApplicationKind:
a, ok := objs[0].(*argov1alpha1.Application)
if !ok {
return ctrl.Result{}, fmt.Errorf("object is not an ArgoCD application %s", resource.Spec.ArgoCD.ApplicationFile)
}
appSpec = &a.Spec
case argocdapplication.ApplicationSetKind:
{
a, ok := objs[0].(*argov1alpha1.ApplicationSet)
if !ok {
return ctrl.Result{}, fmt.Errorf("object is not an ArgoCD application set %s", resource.Spec.ArgoCD.ApplicationSetFile)
}
appSpec = &a.Spec.Template.Spec
}
default:
return ctrl.Result{}, fmt.Errorf("file is not a supported argocd kind %s %s", resource.Spec.ArgoCD.ApplicationFile, resource.Spec.ArgoCD.ApplicationSetFile)
}

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]
if appSpec.HasMultipleSources() {
for j := range appSpec.Sources {
s := &appSpec.Sources[j]
res, repo, sErr := r.reconcileArgoCDSource(ctx, resource, s, appName)
if sErr != nil {
return res, sErr
Expand All @@ -119,7 +137,7 @@ func (r *Reconciler) reconcileCustomPackage(ctx context.Context, resource *v1alp
}
}
} else {
s := app.Spec.Source
s := appSpec.Source
res, repo, sErr := r.reconcileArgoCDSource(ctx, resource, s, appName)
if sErr != nil {
return res, sErr
Expand Down Expand Up @@ -152,16 +170,17 @@ func (r *Reconciler) reconcileCustomPackage(ctx context.Context, resource *v1alp
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
foundAppObj.Spec = *appSpec
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", appName, err)
}
return ctrl.Result{RequeueAfter: requeueTime}, nil
}

// 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, appSource *argov1alpha1.ApplicationSource, appName string) (ctrl.Result, *v1alpha1.GitRepository, error) {
if isCNOEScheme(appSource.RepoURL) {
if resource.Spec.RemoteRepository.Url == "" {
Expand Down Expand Up @@ -281,8 +300,15 @@ func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
}

func (r *Reconciler) getArgoCDAppFile(ctx context.Context, resource *v1alpha1.CustomPackage) ([]byte, error) {
var filePath string
if resource.Spec.ArgoCD.Type == argocdapplication.ApplicationSetKind {
filePath = resource.Spec.ArgoCD.ApplicationSetFile
} else if resource.Spec.ArgoCD.Type == argocdapplication.ApplicationKind {
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 +319,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
16 changes: 12 additions & 4 deletions pkg/controllers/localbuild/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ func (r *LocalbuildReconciler) reconcileCustomPkg(
}

if isSupportedArgoCDTypes(gvk) {
kind := o.GetKind()
appName := o.GetName()
appNS := o.GetNamespace()
customPkg := &v1alpha1.CustomPackage{
Expand Down Expand Up @@ -336,12 +337,19 @@ func (r *LocalbuildReconciler) reconcileCustomPkg(
Namespace: resource.Status.Gitea.AdminUserSecretNamespace,
},
ArgoCD: v1alpha1.ArgoCDPackageSpec{
ApplicationFile: filePath,
Name: appName,
Namespace: appNS,
Name: appName,
Namespace: appNS,
Type: kind,
},
}

if kind == argocdapp.ApplicationKind {
customPkg.Spec.ArgoCD.ApplicationFile = filePath
}
if kind == argocdapp.ApplicationSetKind {
customPkg.Spec.ArgoCD.ApplicationSetFile = filePath
}

if remote != nil {
customPkg.Spec.RemoteRepository = v1alpha1.RemoteRepositorySpec{
Url: remote.CloneUrl(),
Expand Down Expand Up @@ -497,7 +505,7 @@ func isSupportedArgoCDTypes(gvk *schema.GroupVersionKind) bool {
if gvk == nil {
return false
}
return gvk.Kind == argocdapp.ApplicationKind && gvk.Group == argocdapp.Group
return gvk.Group == argocdapp.Group && (gvk.Kind == argocdapp.ApplicationKind || gvk.Kind == argocdapp.ApplicationSetKind)
}

func GetEmbeddedRawInstallResources(name string, templateData any, config v1alpha1.PackageCustomization, scheme *runtime.Scheme) ([][]byte, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,20 @@ spec:
description: ApplicationFile specifies the absolute path to the
ArgoCD application file
type: string
applicationSetFile:
type: string
name:
type: string
namespace:
type: string
type:
type: string
required:
- applicationFile
- applicationSetFile
- name
- namespace
- type
type: object
gitServerAuthSecretRef:
properties:
Expand Down

0 comments on commit deb5e10

Please sign in to comment.