Skip to content

Commit 4072b43

Browse files
authored
Generate kube config (#56)
Signed-off-by: costimuraru <[email protected]>
1 parent 0a0fefb commit 4072b43

File tree

1 file changed

+45
-17
lines changed

1 file changed

+45
-17
lines changed

src/ops/cli/helmfile.py

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
#Copyright 2019 Adobe. All rights reserved.
2-
#This file is licensed to you under the Apache License, Version 2.0 (the "License");
3-
#you may not use this file except in compliance with the License. You may obtain a copy
4-
#of the License at http://www.apache.org/licenses/LICENSE-2.0
1+
# Copyright 2019 Adobe. All rights reserved.
2+
# This file is licensed to you under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License. You may obtain a copy
4+
# of the License at http://www.apache.org/licenses/LICENSE-2.0
55

6-
#Unless required by applicable law or agreed to in writing, software distributed under
7-
#the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
8-
#OF ANY KIND, either express or implied. See the License for the specific language
9-
#governing permissions and limitations under the License.
6+
# Unless required by applicable law or agreed to in writing, software distributed under
7+
# the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
8+
# OF ANY KIND, either express or implied. See the License for the specific language
9+
# governing permissions and limitations under the License.
1010

1111

1212
import os
@@ -41,34 +41,62 @@ def get_epilog(self):
4141

4242

4343
class HelmfileRunner(CompositionConfigGenerator, object):
44-
def __init__(self, ops_config, cluster_config_path):
44+
def __init__(self, ops_config, cluster_config_path, execute):
4545
super(HelmfileRunner, self).__init__(["helmfiles"])
4646
logging.basicConfig(level=logging.INFO)
4747
self.ops_config = ops_config
4848
self.cluster_config_path = cluster_config_path
49+
self.execute = execute
4950

5051
def run(self, args):
5152
config_path_prefix = os.path.join(self.cluster_config_path, '')
52-
args.helmfile_path = '../ee-k8s-infra/compositions/helmfiles' if args.helmfile_path is None else os.path.join(args.helmfile_path, '')
53+
default_helmfiles = '../ee-k8s-infra/compositions/helmfiles'
54+
args.helmfile_path = default_helmfiles if args.helmfile_path is None else os.path.join(args.helmfile_path, '')
5355

54-
compositions= self.get_sorted_compositions(config_path_prefix)
56+
compositions = self.get_sorted_compositions(config_path_prefix)
5557
if len(compositions) == 0 or compositions[0] != "helmfiles":
5658
raise Exception("Please provide the full path to composition=helmfiles")
5759
composition = compositions[0]
5860
conf_path = self.get_config_path_for_composition(config_path_prefix, composition)
59-
self.generate_helmfile_config(conf_path, args)
61+
data = self.generate_helmfile_config(conf_path, args)
62+
self.setup_kube_config(data)
6063

6164
command = self.get_helmfile_command(args)
6265
return dict(command=command)
6366

67+
def setup_kube_config(self, data):
68+
if 'cluster' in data and 'fqdn' in data['cluster'] and 'eks' in data['cluster']['fqdn']:
69+
cluster_name = data['cluster']['fqdn']
70+
aws_profile = data['account']['name']
71+
region = data['region']['location']
72+
file_location = self.generate_eks_kube_config(cluster_name, aws_profile, region)
73+
os.environ['KUBECONFIG'] = file_location
74+
75+
def generate_eks_kube_config(self, cluster_name, aws_profile, region):
76+
file_location = self.get_tmp_file()
77+
cmd = "aws eks update-kubeconfig --name {} --profile {} --region {} --kubeconfig {}".format(cluster_name,
78+
aws_profile,
79+
region,
80+
file_location)
81+
return_code = self.execute(dict(command=cmd))
82+
if return_code != 0:
83+
raise Exception("Unable to generate EKS kube config. Exit code was {}".format(return_code))
84+
return file_location
85+
86+
@staticmethod
87+
def get_tmp_file():
88+
import tempfile
89+
with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
90+
return tmp_file.name
91+
6492
def generate_helmfile_config(self, path, args):
6593
output_file = args.helmfile_path + "/hiera-generated.yaml"
6694
logger.info('Generating helmfiles config %s', output_file)
67-
self.generator.process(path=path,
68-
filters=["helm"],
69-
output_format="yaml",
70-
output_file=output_file,
71-
print_data=True)
95+
return self.generator.process(path=path,
96+
filters=["helm", "account", "region", "cluster"],
97+
output_format="yaml",
98+
output_file=output_file,
99+
print_data=True)
72100

73101
def get_helmfile_command(self, args):
74102
cmd = ' '.join(args.extra_args + [args.subcommand])

0 commit comments

Comments
 (0)