/
webhooks.go
102 lines (88 loc) · 2.84 KB
/
webhooks.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
package main
import (
"bytes"
"fmt"
"os"
"path"
"strings"
"github.com/stoewer/go-strcase"
yamlutil "k8s.io/apimachinery/pkg/util/yaml"
)
const yamlBufferSize = 100
// whManifest represents webhook/manifests.yaml
type whManifest struct {
APIVersion string `yaml:"apiVersion"`
Kind string `yaml:"kind"`
Metadata struct {
CreationTimestamp interface{} `yaml:"creationTimestamp"`
Name string `yaml:"name"`
} `yaml:"metadata"`
whManifestProp `yaml:",inline"`
}
// whManifestProp separate struct to render Webhooks prop only
type whManifestProp struct {
Webhooks []struct {
AdmissionReviewVersions []string `yaml:"admissionReviewVersions"`
ClientConfig struct {
Service struct {
Name string `yaml:"name"`
Namespace string `yaml:"namespace"`
Path string `yaml:"path"`
} `yaml:"service"`
} `yaml:"clientConfig"`
FailurePolicy string `yaml:"failurePolicy"`
Name string `yaml:"name"`
Rules []struct {
APIGroups []string `yaml:"apiGroups"`
APIVersions []string `yaml:"apiVersions"`
Operations []string `yaml:"operations"`
Resources []string `yaml:"resources"`
} `yaml:"rules"`
SideEffects string `yaml:"sideEffects"`
} `yaml:"webhooks"`
}
// updateWebhooks creates charts for webhooks using operators files
// as a result it creates two separate "charts" for mutating and validating webhooks
func updateWebhooks(operatorPath, operatorCharts string) error {
file, err := os.ReadFile(path.Join(operatorPath, "config/webhook/manifests.yaml"))
if err != nil {
return err
}
decoder := yamlutil.NewYAMLOrJSONDecoder(bytes.NewReader(file), yamlBufferSize)
for {
var wh whManifest
err = decoder.Decode(&wh)
if err != nil {
break
}
b, err := marshalCompactYaml(&wh.whManifestProp)
if err != nil {
return err
}
// Renders manifest template
data := fmt.Sprintf(manifestTemplate, wh.Kind, wh.Metadata.Name, b.String())
// Replaces name and namespace with inclusions
data = strings.ReplaceAll(data, `name: webhook-service`, `name: {{ include "aiven-operator.fullname" . }}-webhook-service`)
data = strings.ReplaceAll(data, `namespace: system`, `namespace: {{ include "aiven-operator.namespace" . }}`)
// Creates files according to Metadata.Name in snake case
filePath := path.Join(operatorCharts, "templates", strcase.SnakeCase(wh.Metadata.Name)+".yaml")
err = writeFile(filePath, []byte(data))
if err != nil {
return err
}
}
return nil
}
var manifestTemplate = `{{- if .Values.webhooks.enabled }}
apiVersion: admissionregistration.k8s.io/v1
kind: %s
metadata:
annotations:
{{- include "aiven-operator.ca_injection_annotation" . | nindent 4 }}
name: {{ include "aiven-operator.fullname" . }}-%s
namespace: {{ include "aiven-operator.namespace" . }}
labels:
{{- include "aiven-operator.labels" . | nindent 4 }}
%s
{{- end }}
`