-
Notifications
You must be signed in to change notification settings - Fork 5
/
kustomize.py
executable file
·154 lines (103 loc) · 3.9 KB
/
kustomize.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
import base64
import hashlib
import os
import yaml
REPONAME = "sample-docs"
PHASE = "demo-dev"
IMAGENAME = "nalbam/docs"
VERSION = "v0.0.0"
def parse_args():
p = argparse.ArgumentParser(description="GitOps")
p.add_argument("-r", "--reponame", default=REPONAME, help="reponame")
p.add_argument("-p", "--phase", default=PHASE, help="phase")
p.add_argument("-n", "--imagename", default=IMAGENAME, help="imagename")
p.add_argument("-v", "--version", default=VERSION, help="version")
return p.parse_args()
def replace_deployment(args, cm_hasg, sec_hash):
filepath = "{}/{}/deployment.yaml".format(args.reponame, args.phase)
filehash = ""
if os.path.exists(filepath):
print("replace", filepath)
doc = None
with open(filepath, "r") as file:
doc = yaml.safe_load(file)
# pod
doc["spec"]["template"]["metadata"]["labels"]["version"] = args.version
# datadog
doc["metadata"]["labels"]["tags.datadoghq.com/version"] = args.version
doc["spec"]["template"]["metadata"]["labels"][
"tags.datadoghq.com/version"
] = args.version
containers = doc["spec"]["template"]["spec"]["containers"]
containers[0]["image"] = "{}:{}".format(args.imagename, args.version)
if "env" in containers[0]:
for i, env in enumerate(containers[0]["env"]):
if env["name"] == "CONFIGMAP_HASH":
env["value"] = cm_hasg
if env["name"] == "SECRET_HASH":
env["value"] = sec_hash
if doc != None:
with open(filepath, "w") as file:
yaml.safe_dump(doc, file)
return filehash
def replace_service_preview(args):
filepath = "{}/{}/service-preview.yaml".format(args.reponame, args.phase)
filehash = ""
if os.path.exists(filepath):
print("replace", filepath)
doc = None
with open(filepath, "r") as file:
doc = yaml.safe_load(file)
# replace
doc["spec"]["selector"]["version"] = args.version
if doc != None:
with open(filepath, "w") as file:
yaml.safe_dump(doc, file)
with open(filepath, "rb") as file:
filehash = hashlib.md5(file.read()).hexdigest()
return filehash
def replace_configmap(args):
filepath = "{}/{}/configmap.yaml".format(args.reponame, args.phase)
filehash = ""
if os.path.exists(filepath):
print("replace", filepath)
doc = None
with open(filepath, "r") as file:
doc = yaml.safe_load(file)
# replace
doc["data"]["VERSION"] = args.version
if doc != None:
with open(filepath, "w") as file:
yaml.safe_dump(doc, file)
with open(filepath, "rb") as file:
filehash = hashlib.md5(file.read()).hexdigest()
return filehash
def replace_secret(args):
filepath = "{}/{}/secret.yaml".format(args.reponame, args.phase)
filehash = ""
if os.path.exists(filepath):
print("replace", filepath)
doc = None
with open(filepath, "r") as file:
doc = yaml.safe_load(file)
# replace
doc["data"]["SECRET_VERSION"] = base64.b64encode(
args.version.encode("utf-8")
)
if doc != None:
with open(filepath, "w") as file:
yaml.safe_dump(doc, file)
with open(filepath, "rb") as file:
filehash = hashlib.md5(file.read()).hexdigest()
return filehash
def main():
args = parse_args()
chash = replace_configmap(args)
shash = replace_secret(args)
replace_deployment(args, chash, shash)
replace_service_preview(args)
if __name__ == "__main__":
main()