-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Kris Budde <[email protected]>
- Loading branch information
Showing
20 changed files
with
414 additions
and
159 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
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,110 @@ | ||
package myks | ||
|
||
import ( | ||
"bytes" | ||
_ "embed" | ||
"path/filepath" | ||
"text/template" | ||
|
||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
const ArgoCDStepName = "argocd" | ||
|
||
//go:embed templates/argocd/application.ytt.yaml | ||
var argocd_application_template []byte | ||
|
||
const argocd_data_values_schema = ` | ||
#@data/values | ||
--- | ||
argocd: | ||
app: | ||
name: "{{ .AppName }}" | ||
source: | ||
path: "{{ .AppPath }}" | ||
repoURL: "{{ .RepoURL }}" | ||
targetRevision: "{{ .TargetRevision }}" | ||
` | ||
|
||
func (a *Application) renderArgoCD() (err error) { | ||
if !a.argoCDEnabled { | ||
log.Debug().Msg(a.Msg(ArgoCDStepName, "ArgoCD is disabled")) | ||
return | ||
} | ||
|
||
schemaFile, err := a.argoCDPrepareSchema() | ||
if err != nil { | ||
return | ||
} | ||
|
||
// 0. Global data values schema and library files are added later in the a.yttS call | ||
// 1. Dynamyc ArgoCD data values | ||
yttFiles := []string{schemaFile} | ||
// 2. Collection of application main data values and schemas | ||
yttFiles = append(yttFiles, a.yttDataFiles...) | ||
// 3. Collection of environment argocd-specific data values and schemas, and overlays | ||
yttFiles = append(yttFiles, a.e.collectBySubpath(filepath.Join("_env", a.e.g.ArgoCDDataDirName))...) | ||
// 4. Collection of application argocd-specific data values and schemas, and overlays | ||
yttFiles = append(yttFiles, a.e.collectBySubpath(filepath.Join("_apps", a.Name, a.e.g.ArgoCDDataDirName))...) | ||
|
||
res, err := a.yttS( | ||
"argocd", | ||
"create ArgoCD application yaml", | ||
yttFiles, | ||
bytes.NewReader(argocd_application_template), | ||
) | ||
if err != nil { | ||
log.Error().Err(err). | ||
Str("stdout", res.Stdout). | ||
Str("stderr", res.Stderr). | ||
Msg(a.Msg("argocd", "failed to render ArgoCD Application yaml")) | ||
return | ||
} | ||
|
||
filepath := filepath.Join(a.getArgoCDDestinationDir(), "app-"+a.Name+".yaml") | ||
err = writeFile(filepath, []byte(res.Stdout)) | ||
if err != nil { | ||
return | ||
} | ||
|
||
return | ||
} | ||
|
||
func (a *Application) argoCDPrepareSchema() (filename string, err error) { | ||
const name = "argocd_data_schema.ytt.yaml" | ||
|
||
tmpl, err := template.New(name).Parse(string(argocd_data_values_schema)) | ||
if err != nil { | ||
return | ||
} | ||
|
||
type Data struct { | ||
AppName string | ||
AppPath string | ||
RepoURL string | ||
TargetRevision string | ||
} | ||
|
||
data := Data{ | ||
AppName: a.Name, | ||
AppPath: a.getDestinationDir(), | ||
RepoURL: a.e.g.GitRepoUrl, | ||
TargetRevision: a.e.g.GitRepoBranch, | ||
} | ||
|
||
buf := &bytes.Buffer{} | ||
err = tmpl.Execute(buf, data) | ||
if err != nil { | ||
return | ||
} | ||
|
||
err = a.writeTempFile(name, buf.String()) | ||
|
||
filename = a.expandTempPath(name) | ||
|
||
return | ||
} | ||
|
||
func (a *Application) getArgoCDDestinationDir() string { | ||
return filepath.Join(a.e.g.RootDir, a.e.g.RenderedDir, "argocd", a.e.Id) | ||
} |
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
12 changes: 12 additions & 0 deletions
12
internal/myks/assets/envs/_env/argocd/annotations.overlay.ytt.yaml
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,12 @@ | ||
#@ load("@ytt:overlay", "overlay") | ||
#@ load("@ytt:data", "data") | ||
|
||
#! Match all ArgoCD resources: Application, AppProject, Secret. | ||
#@overlay/match by=overlay.all, expects="1+" | ||
--- | ||
#@overlay/match missing_ok=True | ||
#@overlay/match-child-defaults missing_ok=True | ||
metadata: | ||
annotations: | ||
myks.dev/environment: #@ data.values.environment.id | ||
app.kubernetes.io/source: #@ data.values.myks.gitRepoUrl |
15 changes: 15 additions & 0 deletions
15
internal/myks/assets/envs/_env/argocd/secret.overlay.ytt.yaml
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,15 @@ | ||
#@ load("@ytt:overlay", "overlay") | ||
--- | ||
#@ def secret_fragment(): | ||
kind: Secret | ||
metadata: | ||
labels: | ||
argocd.argoproj.io/secret-type: cluster | ||
#@ end | ||
|
||
#@overlay/match by=overlay.subset(secret_fragment()), expects="0+" | ||
--- | ||
#! See https://argo-cd.readthedocs.io/en/release-2.8/operator-manual/declarative-setup/#clusters | ||
stringData: | ||
config: ARGOCD_CLUSTER_CONNECT_CONFIG | ||
server: ARGOCD_CLUSTER_SERVER_URL |
9 changes: 9 additions & 0 deletions
9
internal/myks/assets/envs/mykso/dev/_apps/httpbingo/argocd/overlay.ytt.yaml
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 @@ | ||
#@ load("@ytt:overlay", "overlay") | ||
|
||
#@overlay/match by=overlay.subset({"kind": "Application"}) | ||
--- | ||
spec: | ||
syncPolicy: | ||
automated: | ||
#! Disable self-healing of the application to allow manual changes. | ||
selfHeal: false |
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 |
---|---|---|
|
@@ -4,5 +4,4 @@ environment: | |
id: mykso-dev | ||
applications: | ||
- proto: argocd | ||
- proto: argocd-apps | ||
- proto: httpbingo |
10 changes: 0 additions & 10 deletions
10
internal/myks/assets/prototypes/argocd-apps/app-data.ytt.yaml
This file was deleted.
Oops, something went wrong.
57 changes: 0 additions & 57 deletions
57
internal/myks/assets/prototypes/argocd-apps/ytt/all.ytt.yaml
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.