@@ -8,6 +8,9 @@ import json
88import os
99import subprocess
1010import sys
11+ import tempfile
12+
13+ import yaml
1114
1215
1316def get_current_context ():
@@ -20,6 +23,13 @@ def get_kubeconfig_path():
2023 return os .environ .get ('KUBECONFIG' , os .path .expanduser ('~/.kube/config' ))
2124
2225
26+ def get_kubernetes_dir ():
27+ root_dir = subprocess .check_output (
28+ ['git' , 'rev-parse' , '--show-toplevel' ]
29+ ).decode ().strip ()
30+ return os .path .join (root_dir , 'kubernetes' )
31+
32+
2333def main ():
2434 parser = argparse .ArgumentParser (description = __doc__ )
2535
@@ -36,13 +46,20 @@ def main():
3646 "app-{appname}".
3747 ''' ,
3848 )
49+
50+ # This argument is optional because some Kubernetes services
51+ # (sourcegraph) don't need us to build our own Docker images
3952 parser .add_argument (
4053 'appversion' ,
54+ nargs = '?' ,
55+ default = '' ,
4156 help = 'The version of the app, usually the docker tag.' ,
4257 )
58+
4359 parser .add_argument (
44- 'dir' ,
45- help = 'The directory of Kubernetes resource templates.' ,
60+ '--secrets' ,
61+ default = '/etc/ocf-kubernetes/secrets/' ,
62+ help = 'The YAML file or directory of YAML files of Kubernetes secrets.' ,
4663 )
4764 args = parser .parse_args ()
4865
@@ -61,21 +78,31 @@ def main():
6178 ).check_returncode ()
6279
6380 # Bindings passed into the kubernetes-deploy templates
64- bindings = {
65- 'version' : args .appversion ,
66- }
81+ bindings = {'version' : args .appversion }
6782
68- # kubernetes-deploy requires that this environmental variable be set, and passes
69- # it to templates as the `current_sha` variable. We don't ever use it.
70- os .environ ['REVISION' ] = 'unused'
83+ if os .path .isfile (args .secrets ):
84+ filename = args .secrets
85+ else :
86+ filename = os .path .join (args .secrets , args .appname + '.yaml' )
7187
72- subprocess .run (
73- ['kubernetes-deploy' ,
74- namespace_name ,
75- args .kube_context ,
76- '--bindings=' + json .dumps (bindings ),
77- '--template-dir=' + args .dir ]
78- ).check_returncode ()
88+ with open (filename , 'r' ) as stream :
89+ bindings .update (yaml .safe_load (stream ))
90+
91+ # Created with 600 perms
92+ with tempfile .NamedTemporaryFile (suffix = '.json' ) as bindings_file :
93+
94+ bindings_file .write (json .dumps (bindings ).encode ())
95+
96+ # ensure the file gets written to
97+ bindings_file .flush ()
98+
99+ subprocess .run (
100+ ['kubernetes-deploy' ,
101+ namespace_name ,
102+ args .kube_context ,
103+ '--bindings=@' + bindings_file .name ,
104+ '--template-dir=' + get_kubernetes_dir ()]
105+ ).check_returncode ()
79106
80107
81108if __name__ == '__main__' :
0 commit comments