Skip to content
This repository has been archived by the owner on Oct 12, 2023. It is now read-only.

Add git directory generator #7

Merged
merged 19 commits into from
Sep 8, 2020
1 change: 1 addition & 0 deletions api/v1alpha1/applicationset_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type ClusterGenerator struct {
type GitGenerator struct {
RepoURL string `json:"repoURL"`
Directories []GitDirectoryGeneratorItem `json:"directories,omitempty"`
Revision string `json:"revision"`
}

type GitDirectoryGeneratorItem struct {
Expand Down
5 changes: 5 additions & 0 deletions manifests/crds/argoproj.io_applicationsets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.3.0
creationTimestamp: null
name: applicationsets.argoproj.io
spec:
Expand Down Expand Up @@ -102,8 +104,11 @@ spec:
type: array
repoURL:
type: string
revision:
type: string
required:
- repoURL
- revision
type: object
list:
description: ListGenerator include items info
Expand Down
26 changes: 20 additions & 6 deletions pkg/controllers/applicationset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package controllers
import (
"context"
"github.com/argoproj-labs/applicationset/pkg/generators"
argov1alpha1 "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
"k8s.io/client-go/tools/record"

log "github.com/sirupsen/logrus"
Expand All @@ -32,8 +33,9 @@ import (
// ApplicationSetReconciler reconciles a ApplicationSet object
type ApplicationSetReconciler struct {
client.Client
Scheme *runtime.Scheme
Recorder record.EventRecorder
Scheme *runtime.Scheme
Recorder record.EventRecorder
RepoServerAddr string
}

// +kubebuilder:rbac:groups=argoproj.io,resources=applicationsets,verbs=get;list;watch;create;update;patch;delete
Expand All @@ -49,14 +51,26 @@ func (r *ApplicationSetReconciler) Reconcile(req ctrl.Request) (ctrl.Result, err
return ctrl.Result{}, client.IgnoreNotFound(err)
}

var generator generators.Generator
generator = generators.NewListGenerator()
ListGenerator := generators.NewListGenerator()
GitGenerator := generators.NewGitGenerator(r.RepoServerAddr)
for _, tmpGenerator := range applicationSetInfo.Spec.Generators {
desiredApplications, err := generator.GenerateApplications(&tmpGenerator, &applicationSetInfo)
log.Infof("desiredApplications %+v", desiredApplications)
var desiredApplications []argov1alpha1.Application
var err error
if tmpGenerator.List != nil {
desiredApplications, err = ListGenerator.GenerateApplications(&tmpGenerator, &applicationSetInfo)
}

if tmpGenerator.Git != nil {
desiredApplications, err = GitGenerator.GenerateApplications(&tmpGenerator, &applicationSetInfo)
}

if err != nil {
log.WithError(err).Error("error generating applications")
continue
}

log.Infof("desiredApplications %+v", desiredApplications)

}

return ctrl.Result{}, nil
Expand Down
84 changes: 80 additions & 4 deletions pkg/generators/git.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,101 @@
package generators

import (
"context"
"fmt"
argoprojiov1alpha1 "github.com/argoproj-labs/applicationset/api/v1alpha1"
"github.com/argoproj-labs/applicationset/pkg/utils"
argov1alpha1 "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
"github.com/argoproj/argo-cd/reposerver/apiclient"
log "github.com/sirupsen/logrus"
"path"
)

var _ Generator = (*GitGenerator)(nil)

type GitGenerator struct {
repoClientset apiclient.Clientset
}

func NewGitGenerator() Generator {
g := &GitGenerator{}
func NewGitGenerator(repoServerAddress string) Generator {
repoClientset := apiclient.NewRepoServerClientset(repoServerAddress, 5)
g := &GitGenerator{
repoClientset: repoClientset,
}
return g
}

func (g *GitGenerator) GenerateApplications(appSet *argoprojiov1alpha1.ApplicationSet) ([]argov1alpha1.Application, error) {
if appSet == nil {
func (g *GitGenerator) GenerateApplications(appSetGenerator *argoprojiov1alpha1.ApplicationSetGenerator, appSet *argoprojiov1alpha1.ApplicationSet) ([]argov1alpha1.Application, error) {
if appSetGenerator == nil || appSet == nil {
return nil, fmt.Errorf("ApplicationSet is empty")
}

if appSetGenerator.Git == nil {
return nil, fmt.Errorf("git variable empty")
}

if len(appSetGenerator.Git.Directories) > 0 {
paths, err := g.GetAllPaths(appSetGenerator.Git)
if err != nil {
return nil, err
}

res := make([]argov1alpha1.Application, len(paths))
for _, p := range paths {
app, err := g.generateApplicationFromPath(appSet, p)
if err != nil {
log.Errorf("Error %e while generating app from path: %s", err, p)
OmerKahani marked this conversation as resolved.
Show resolved Hide resolved
continue
}
res = append(res, *app)
}

return res, nil
}

return nil, nil
}

func (g *GitGenerator) generateApplicationFromPath(appSet *argoprojiov1alpha1.ApplicationSet, appPath string) (*argov1alpha1.Application, error) {
var tmplApplication argov1alpha1.Application
tmplApplication.Namespace = appSet.Spec.Template.Namespace
tmplApplication.Name = appSet.Spec.Template.Name
tmplApplication.Spec = appSet.Spec.Template.Spec

params := make(map[string]string, 2)
params["path"] = appPath
params["path.basename"] = path.Base(appPath)

tmpApplication, err := utils.RenderTemplateParams(&tmplApplication, params)

return tmpApplication, err
}

func (g *GitGenerator) GetAllPaths(GitGenerator *argoprojiov1alpha1.GitGenerator) ([]string, error) {
closer, repoClient, err := g.repoClientset.NewRepoServerClient()
defer closer.Close()
if err != nil {
return nil, err
}

listAppsRequest := &apiclient.ListAppsRequest{
Repo: &argov1alpha1.Repository{
Repo: GitGenerator.RepoURL,
},
Revision: GitGenerator.Revision,
//Path: GitGenerator.Directories,
}

appList, err := repoClient.ListApps(context.TODO(), listAppsRequest)
if err != nil {
return nil, err
}

var res []string

for name, _ := range appList.Apps {
res = append(res, name)
}

return res, nil
}