This repository has been archived by the owner on Oct 12, 2023. It is now read-only.
/
util.go
179 lines (155 loc) · 5.71 KB
/
util.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package utils
import (
"encoding/json"
"fmt"
"io"
"reflect"
"sort"
"strconv"
"strings"
argoprojiov1alpha1 "github.com/argoproj/applicationset/api/v1alpha1"
argov1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/valyala/fasttemplate"
)
type Renderer interface {
RenderTemplateParams(tmpl *argov1alpha1.Application, syncPolicy *argoprojiov1alpha1.ApplicationSetSyncPolicy, params map[string]string) (*argov1alpha1.Application, error)
}
type Render struct {
}
func (r *Render) RenderTemplateParams(tmpl *argov1alpha1.Application, syncPolicy *argoprojiov1alpha1.ApplicationSetSyncPolicy, params map[string]string) (*argov1alpha1.Application, error) {
if tmpl == nil {
return nil, fmt.Errorf("application template is empty ")
}
if len(params) == 0 {
return tmpl, nil
}
tmplBytes, err := json.Marshal(tmpl)
if err != nil {
return nil, err
}
fstTmpl := fasttemplate.New(string(tmplBytes), "{{", "}}")
replacedTmplStr, err := r.replace(fstTmpl, params, true)
if err != nil {
return nil, err
}
var replacedTmpl argov1alpha1.Application
err = json.Unmarshal([]byte(replacedTmplStr), &replacedTmpl)
if err != nil {
return nil, err
}
// Add the 'resources-finalizer' finalizer if:
// The template application doesn't have any finalizers, and:
// a) there is no syncPolicy, or
// b) there IS a syncPolicy, but preserveResourcesOnDeletion is set to false
// See TestRenderTemplateParamsFinalizers in util_test.go for test-based definition of behaviour
if (syncPolicy == nil || !syncPolicy.PreserveResourcesOnDeletion) &&
(replacedTmpl.ObjectMeta.Finalizers == nil || len(replacedTmpl.ObjectMeta.Finalizers) == 0) {
replacedTmpl.ObjectMeta.Finalizers = []string{"resources-finalizer.argocd.argoproj.io"}
}
return &replacedTmpl, nil
}
// Replace executes basic string substitution of a template with replacement values.
// 'allowUnresolved' indicates whether or not it is acceptable to have unresolved variables
// remaining in the substituted template.
func (r *Render) replace(fstTmpl *fasttemplate.Template, replaceMap map[string]string, allowUnresolved bool) (string, error) {
var unresolvedErr error
replacedTmpl := fstTmpl.ExecuteFuncString(func(w io.Writer, tag string) (int, error) {
trimmedTag := strings.TrimSpace(tag)
replacement, ok := replaceMap[trimmedTag]
if len(trimmedTag) == 0 || !ok {
if allowUnresolved {
// just write the same string back
return w.Write([]byte(fmt.Sprintf("{{%s}}", tag)))
}
unresolvedErr = errors.Errorf("failed to resolve {{%s}}", tag)
return 0, nil
}
// The following escapes any special characters (e.g. newlines, tabs, etc...)
// in preparation for substitution
replacement = strconv.Quote(replacement)
replacement = replacement[1 : len(replacement)-1]
return w.Write([]byte(replacement))
})
if unresolvedErr != nil {
return "", unresolvedErr
}
return replacedTmpl, nil
}
// Log a warning if there are unrecognized generators
func CheckInvalidGenerators(applicationSetInfo *argoprojiov1alpha1.ApplicationSet) {
hasInvalidGenerators, invalidGenerators := invalidGenerators(applicationSetInfo)
if len(invalidGenerators) > 0 {
gnames := []string{}
for n := range invalidGenerators {
gnames = append(gnames, n)
}
sort.Strings(gnames)
aname := applicationSetInfo.ObjectMeta.Name
msg := "ApplicationSet %s contains unrecognized generators: %s"
log.Warnf(msg, aname, strings.Join(gnames, ", "))
} else if hasInvalidGenerators {
name := applicationSetInfo.ObjectMeta.Name
msg := "ApplicationSet %s contains unrecognized generators"
log.Warnf(msg, name)
}
}
// Return true if there are unknown generators specified in the application set. If we can discover the names
// of these generators, return the names as the keys in a map
func invalidGenerators(applicationSetInfo *argoprojiov1alpha1.ApplicationSet) (bool, map[string]bool) {
names := make(map[string]bool)
hasInvalidGenerators := false
for index, generator := range applicationSetInfo.Spec.Generators {
v := reflect.Indirect(reflect.ValueOf(generator))
found := false
for i := 0; i < v.NumField(); i++ {
field := v.Field(i)
if !field.CanInterface() {
continue
}
if !reflect.ValueOf(field.Interface()).IsNil() {
found = true
break
}
}
if !found {
hasInvalidGenerators = true
addInvalidGeneratorNames(names, applicationSetInfo, index)
}
}
return hasInvalidGenerators, names
}
func addInvalidGeneratorNames(names map[string]bool, applicationSetInfo *argoprojiov1alpha1.ApplicationSet, index int) {
// The generator names are stored in the "kubectl.kubernetes.io/last-applied-configuration" annotation
config := applicationSetInfo.ObjectMeta.Annotations["kubectl.kubernetes.io/last-applied-configuration"]
var values map[string]interface{}
err := json.Unmarshal([]byte(config), &values)
if err != nil {
log.Warnf("couldn't unmarshal kubectl.kubernetes.io/last-applied-configuration: %+v", config)
return
}
spec, ok := values["spec"].(map[string]interface{})
if !ok {
log.Warn("coundn't get spec from kubectl.kubernetes.io/last-applied-configuration annotation")
return
}
generators, ok := spec["generators"].([]interface{})
if !ok {
log.Warn("coundn't get generators from kubectl.kubernetes.io/last-applied-configuration annotation")
return
}
if index >= len(generators) {
log.Warnf("index %d out of range %d for generator in kubectl.kubernetes.io/last-applied-configuration", index, len(generators))
return
}
generator, ok := generators[index].(map[string]interface{})
if !ok {
log.Warn("coundn't get generator from kubectl.kubernetes.io/last-applied-configuration annotation")
return
}
for key := range generator {
names[key] = true
break
}
}