This repository has been archived by the owner on Aug 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
patcher.go
128 lines (110 loc) · 3.63 KB
/
patcher.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
package main
import (
"fmt"
"k8s.io/api/admission/v1beta1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"log"
"strings"
)
const (
ConsulTemplateInjectAnnotation = "trendyol.com/consul-template-inject"
ConsulTemplateConsulAddressAnnotation = "trendyol.com/consul-template-consul-addr"
ConsulTemplateFilePathAnnotation = "trendyol.com/consul-template-output-file"
ConsulTemplateTemplateConfigMapNameAnnotation = "trendyol.com/consul-template-template-config-map-name"
)
var (
podResource = metav1.GroupVersionResource{Version: "v1", Resource: "pods",}
)
type patchOperation struct {
Op string `json:"op"`
Path string `json:"path"`
Value interface{} `json:"value,omitempty"`
}
func generatePodPatches(req *v1beta1.AdmissionRequest) (patches, error) {
if req.Resource != podResource {
log.Printf("expect resource to be %s", podResource)
return nil, fmt.Errorf("expect resource to be %s", podResource)
}
rawObj := req.Object.Raw
pod := new(corev1.Pod)
if _, _, err := universalDeserializer.Decode(rawObj, nil, pod); err != nil {
return nil, fmt.Errorf("could not deserialize pod object: %v", err)
}
podAnnotations, err := newPodAnnotations(pod)
if err != nil {
return nil, err
}
patches := patches{}
injectStatus := podAnnotations[ConsulTemplateInjectAnnotation].Value
if injectStatus == "false" {
log.Printf("config injection status false for pod: %s", pod.Name)
return patches, nil
}
templateFile := "/conf/init.tmpl"
outputFile := podAnnotations[ConsulTemplateFilePathAnnotation].Value
templateConfigMapName := podAnnotations[ConsulTemplateTemplateConfigMapNameAnnotation].Value
consulAddr := podAnnotations[ConsulTemplateConsulAddressAnnotation].Value
lastSlashIndex := strings.LastIndex(outputFile, "/")
outputFolderPath := outputFile[:lastSlashIndex]
patches.addVolumes(pod, []corev1.Volume{
{
Name: "consul-template-input",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: templateConfigMapName,
},
},
},
},
{
Name: "consul-template-output",
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{
Medium: "Memory",
},
},
},
}, )
patches.addVolumeMounts(pod, []corev1.VolumeMount{
{
Name: "consul-template-output",
MountPath: outputFolderPath,
},
}, )
patches.addInitContainers(pod, []corev1.Container{
{
Name: "trendyol-consul-template-init",
Image: "trendyoltech/trendyol-consul-template:latest",
Env: []corev1.EnvVar{
{Name: "CONSUL_TEMPLATE_PROCESS_FLAGS", Value: "-once"},
{Name: "CONSUL_ADDR", Value: consulAddr},
{Name: "CONSUL_TEMPLATE_TEMPLATE_PATH", Value: templateFile},
{Name: "CONSUL_TEMPLATE_OUTPUT_PATH", Value: outputFile},
},
VolumeMounts: []corev1.VolumeMount{
{Name: "consul-template-input", MountPath: "/conf"},
{Name: "consul-template-output", MountPath: outputFolderPath},
},
ImagePullPolicy: "IfNotPresent",
},
}, )
patches.addContainers(pod, []corev1.Container{
{
Name: "trendyol-consul-template-sidecar",
Image: "trendyoltech/trendyol-consul-template:latest",
Env: []corev1.EnvVar{
{Name: "CONSUL_ADDR", Value: consulAddr},
{Name: "CONSUL_TEMPLATE_TEMPLATE_PATH", Value: templateFile},
{Name: "CONSUL_TEMPLATE_OUTPUT_PATH", Value: outputFile},
},
VolumeMounts: []corev1.VolumeMount{
{Name: "consul-template-input", MountPath: "/conf"},
{Name: "consul-template-output", MountPath: outputFolderPath},
},
ImagePullPolicy: "IfNotPresent",
},
}, )
return patches, nil
}