-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: generate an app-scaffold files
- Loading branch information
1 parent
2cce2ca
commit 239dc5c
Showing
6 changed files
with
212 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package generate | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
type Files struct { | ||
data map[string]bytes.Buffer | ||
} | ||
|
||
func (f *Files) Add(file string, content bytes.Buffer) { | ||
if f.data == nil { | ||
f.data = map[string]bytes.Buffer{} | ||
} | ||
|
||
f.data[file] = content | ||
} | ||
|
||
func (f *Files) Save(filePrefix string) error { | ||
for file, content := range f.data { | ||
name := filepath.Join(filePrefix, file) | ||
|
||
if err := os.MkdirAll(filepath.Dir(name), 0755); err != nil { | ||
return errors.Wrap(err, "failed to create directory") | ||
} | ||
|
||
if err := os.WriteFile(name, content.Bytes(), 0644); err != nil { | ||
return errors.Wrap(err, "failed to write file") | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package generate | ||
|
||
import ( | ||
"bytes" | ||
"embed" | ||
"fmt" | ||
"io/fs" | ||
"path/filepath" | ||
"strings" | ||
"text/template" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
//go:embed scaffold | ||
var scaffoldFS embed.FS | ||
|
||
type ScaffoldData struct { | ||
AppName string | ||
DeploymentName string | ||
Description string | ||
Environment string | ||
Namespace string | ||
} | ||
|
||
func AppScaffold(appName string, environments []string, outputPath string) error { | ||
for _, env := range environments { | ||
files, err := generateAppScaffoldEnvironmentFiles(appName, env) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := files.Save(filepath.Join(outputPath, env)); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func generateAppScaffoldEnvironmentFiles(appName, environment string) (*Files, error) { | ||
tpl, err := template.New("tpl").ParseFS(scaffoldFS, "scaffold/*.yaml", "scaffold/**/*.yaml") | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to create template") | ||
} | ||
|
||
data := ScaffoldData{ | ||
AppName: appName, | ||
DeploymentName: fmt.Sprintf("%s-environment-%s", environment, appName), | ||
Description: fmt.Sprintf("%s example application", appName), | ||
Environment: environment, | ||
Namespace: environment, | ||
} | ||
|
||
files := &Files{} | ||
err = fs.WalkDir(scaffoldFS, ".", func(path string, d fs.DirEntry, err error) error { | ||
if err != nil { | ||
return errors.Wrap(err, "error walking directory") | ||
} | ||
|
||
if d.IsDir() { | ||
return nil | ||
} | ||
|
||
// Get the file name without the root path | ||
s := strings.Split(path, string(filepath.Separator)) | ||
file := strings.Join(s[1:], string(filepath.Separator)) | ||
|
||
// Parse any template variables in the file name | ||
fileTpl, err := tpl.Parse(file) | ||
if err != nil { | ||
return errors.Wrap(err, "error parsing file name") | ||
} | ||
|
||
var fileNameOutput bytes.Buffer | ||
if err := fileTpl.Execute(&fileNameOutput, data); err != nil { | ||
return errors.Wrap(err, "error executing file name") | ||
} | ||
|
||
// Parse the contents of the file | ||
var fileContent bytes.Buffer | ||
if err := tpl.ExecuteTemplate(&fileContent, d.Name(), data); err != nil { | ||
return errors.Wrap(err, "error executing template") | ||
} | ||
|
||
// Now store everything for output | ||
files.Add(fileNameOutput.String(), fileContent) | ||
|
||
return nil | ||
}) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "error walking directory") | ||
} | ||
|
||
return files, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
apiVersion: argoproj.io/v1alpha1 | ||
kind: Application | ||
metadata: | ||
name: "{{ .DeploymentName }}" | ||
namespace: argocd | ||
finalizers: | ||
- resources-finalizer.argocd.argoproj.io | ||
annotations: | ||
argocd.argoproj.io/sync-wave: '45' | ||
spec: | ||
project: default | ||
source: | ||
repoURL: <GITOPS_REPO_URL> | ||
path: "registry/environments/{{ .Environment }}/{{ .AppName }}" | ||
targetRevision: HEAD | ||
destination: | ||
name: in-cluster | ||
namespace: "{{ .Namespace }}" | ||
syncPolicy: | ||
automated: | ||
prune: true | ||
selfHeal: true | ||
syncOptions: | ||
- CreateNamespace=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
apiVersion: v2 | ||
description: "{{ .Description }}" | ||
name: "{{ .AppName }}" | ||
type: application | ||
version: 1.0.0 | ||
dependencies: | ||
- name: "{{ .AppName }}" | ||
repository: http://chartmuseum.chartmuseum.svc.cluster.local:8080 | ||
version: 0.0.1-rc.awaiting-ci |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# This is a generated file. These values may not correspond to your own chart's values | ||
|
||
"{{ .AppName }}": | ||
annotations: | | ||
linkerd.io/inject: "enabled" | ||
labels: | | ||
mirror.linkerd.io/exported: "true" | ||
image: | ||
repository: "<CONTAINER_REGISTRY_URL>/{{ .AppName }}" | ||
imagePullSecrets: | ||
- name: docker-config | ||
ingress: | ||
className: nginx | ||
enabled: true | ||
annotations: | ||
<CERT_MANAGER_ISSUER_ANNOTATION_1> | ||
<CERT_MANAGER_ISSUER_ANNOTATION_2> | ||
<CERT_MANAGER_ISSUER_ANNOTATION_3> | ||
<CERT_MANAGER_ISSUER_ANNOTATION_4> | ||
nginx.ingress.kubernetes.io/service-upstream: "true" | ||
hosts: | ||
- host: "{{ .AppName }}-{{ .Environment }}.<DOMAIN_NAME>" | ||
paths: | ||
- path: / | ||
pathType: Prefix | ||
tls: | ||
- secretName: "{{ .AppName }}-tls" | ||
hosts: | ||
- "{{ .AppName }}-{{ .Environment }}.<DOMAIN_NAME>" |