This repository has been archived by the owner on Aug 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
145 lines (119 loc) · 3.48 KB
/
plugin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Package main is the ArgoCD extension of argoCD as a ConfigManagementPlugin that permits templating manifests with ytt
package main
import (
"fmt"
"os"
"strings"
"github.com/bitfield/script"
)
//nolint:gochecknoglobals // simpler to debug using these globals
var (
indent = " "
debugContent = make([]string, 0, 300)
)
func toDebug(str string) {
for _, s := range strings.Split(str, "\n") {
debugContent = append(debugContent, fmt.Sprintf("%s%s", indent, s))
}
}
func renderDebugAndExit() {
fmt.Print(`
---
apiVersion: v1
kind: ConfigMap
metadata:
name: argocd-cmp-ytt-error
namespace: debug
stringData:
debug.txt: |
`)
for _, str := range debugContent {
fmt.Println(str)
}
os.Exit(0)
}
func renderHelmChart() {
appNs, _ := os.LookupEnv("ARGOCD_APP_NAMESPACE")
appName, _ := os.LookupEnv("ARGOCD_APP_NAME")
helmReleaseName, customReleaseDefined := os.LookupEnv("ARGOCD_ENV_HELM_RELEASE_NAME")
if customReleaseDefined {
appName = helmReleaseName
}
if appNs == "" {
toDebug("#! the namespace of the application isn't defined. not possible to render the Helm Chart without that.")
renderDebugAndExit()
}
// Helm Chart detected. we only apply ytt on values.yaml and then use helm to template
stdout, err := script.Exec("ytt --data-values-env=ARGOCD_ENV --file values.yaml --output-files .").String()
if err != nil {
toDebug(fmt.Sprintf("#! cmp-ytt: an error occurred while rendering the ytt template: %v", err))
toDebug(stdout)
renderDebugAndExit()
}
stdout, err = script.Exec("helm dependency build").String()
if err != nil {
toDebug(fmt.Sprintf("#! cmp-ytt: error while pulling helm depencies: %v", err))
toDebug(stdout)
renderDebugAndExit()
}
stdout, err = script.Exec(fmt.Sprintf("helm template %s --namespace %s .", appName, appNs)).String()
if err != nil {
toDebug("#! cmp-ytt: error while templating/rendering the Helm Chart")
toDebug(stdout)
renderDebugAndExit()
}
fmt.Print(stdout)
}
func renderKustomization() {
// Kustomization detected - we apply ytt on all files and then use kustomize build
stdout, err := script.Exec("ytt --data-values-env=ARGOCD_ENV --file . --output-files .").String()
if err != nil {
toDebug(fmt.Sprintf("#! cmp-ytt: an error occurred while rendering the ytt template: %v", err))
toDebug(stdout)
renderDebugAndExit()
}
stdout, err = script.Exec("kustomize build .").String()
if err != nil {
toDebug("#! error while running kustomize build:")
toDebug(stdout)
renderDebugAndExit()
}
fmt.Print(stdout)
}
func main() {
files, err := os.ReadDir(".")
if err != nil {
toDebug("# Couldn't read files!")
toDebug(err.Error())
renderDebugAndExit()
}
for _, f := range files {
if f.IsDir() {
continue
}
if f.Name() == "Chart.yaml" {
renderHelmChart()
return
} else if isKustomization(f.Name()) {
renderKustomization()
return
}
}
// no Helm Chart nor Kustomization detected - rendering plain manifests
stdout, err := script.Exec("ytt --data-values-env=ARGOCD_ENV --file .").String()
if err != nil {
toDebug(fmt.Sprintf("#! cmp-ytt: an error occurred while rendering the ytt template: %v", err))
toDebug(stdout)
}
fmt.Print(stdout)
}
func isKustomization(path string) bool {
// source: https://github.com/argoproj/argo-cd/blob/v2.4.14/util/kustomize/kustomize.go#L208
var kustomizationNames = []string{"kustomization.yaml", "kustomization.yml", "Kustomization"}
for _, kustomization := range kustomizationNames {
if path == kustomization {
return true
}
}
return false
}