Skip to content

Commit

Permalink
add support for auto discovering AppProjects and making sure they pri…
Browse files Browse the repository at this point in the history
…nt before Applications
  • Loading branch information
Josh Wolf committed Jul 30, 2020
1 parent 0de5731 commit cd65b92
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
42 changes: 33 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
autoAppsFlag = "autoapps"
argoAPIVersion = "argoproj.io/v1alpha1"
argoAppKind = "Application"
argoProjectKind = "AppProject"
autoAppsEnvPrefix = "AUTOAPPS_"
autoAppsAnnotationSkipDetector = "autoapps-skip-discovery"
)
Expand Down Expand Up @@ -44,12 +45,15 @@ func (g *Generate) Run(cmd *cobra.Command, args []string) error {
logrus.Fatal("You must specify a --basePath!")
}

apps, err := walkForApps(g.BasePath)
projects, apps, err := walkForArgo(g.BasePath)
if err != nil {
logrus.Errorf("Failed to collect apps: %v", err)
}

// Remove any apps that don't want to be included
// Print out rendered projects to stdout for ArgoCD to read
fmt.Print(strings.Join(projects, "\n---\n"))

fmt.Println("---")

// Print out rendered apps to stdout for ArgoCD to read
fmt.Print(strings.Join(apps, "\n---\n"))
Expand All @@ -65,8 +69,10 @@ func main() {
cli.Main(root)
}

func walkForApps(base string) (apps []string, err error) {
func walkForArgo(base string) (projects []string, apps []string, err error) {
var appsData [][]byte
var projectData [][]byte

err = filepath.Walk(base, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
Expand All @@ -79,42 +85,60 @@ func walkForApps(base string) (apps []string, err error) {
return err
}

if ok := isAutoApp(data); ok {
// Check AppProjects
if ok := isAutoApp(data, argoAPIVersion, argoProjectKind); ok {
projectData = append(appsData, data)
}

// Check Applications
if ok := isAutoApp(data, argoAPIVersion, argoAppKind); ok {
appsData = append(appsData, data)
}
}
return nil
})

if err != nil {
return apps, err
return projects, apps, err
}

// Render project template
for _, data := range projectData {
rendered := renderTemplate(string(data))

// Determine if we need to skip it once it's read
include := isAutoApp([]byte(rendered), argoAPIVersion, argoProjectKind)

if include {
projects = append(projects, rendered)
}
}

// Render apps template
for _, data := range appsData {
rendered := renderTemplate(string(data))

// Determine if we need to skip it once it's read
include := isAutoApp([]byte(rendered))
include := isAutoApp([]byte(rendered), argoAPIVersion, argoAppKind)

if include {
apps = append(apps, rendered)
}
}

return apps, nil
return projects, apps, nil
}

// isAutoApp returns true/false based on whether or not a valid yaml file is a non skipped valid Application CR
func isAutoApp(data []byte) bool {
func isAutoApp(data []byte, apiVersion string, kind string) bool {
var a App
isApp := false

err := yaml.Unmarshal(data, &a)
if err != nil {}

// Check if this is an app
if a.ApiVersion == argoAPIVersion && a.Kind == argoAppKind {
if a.ApiVersion == apiVersion && a.Kind == kind {
isApp = true
}

Expand Down
13 changes: 13 additions & 0 deletions testdata/sub/project.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: argoproj.io/v1alpha1
kind: AppProject
metadata:
name: tester
annotations:
autoapps-skip-discovery: "true"
spec:
clusterResourceWhitelist:
- group: '*'
kind: '*'
namespaceResourceBlacklist:
- group: ""
kind: ""

0 comments on commit cd65b92

Please sign in to comment.