|
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 |
5 | 5 |
|
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. |
10 | 10 |
|
11 | 11 |
|
12 | 12 | import os |
@@ -41,34 +41,62 @@ def get_epilog(self): |
41 | 41 |
|
42 | 42 |
|
43 | 43 | class HelmfileRunner(CompositionConfigGenerator, object): |
44 | | - def __init__(self, ops_config, cluster_config_path): |
| 44 | + def __init__(self, ops_config, cluster_config_path, execute): |
45 | 45 | super(HelmfileRunner, self).__init__(["helmfiles"]) |
46 | 46 | logging.basicConfig(level=logging.INFO) |
47 | 47 | self.ops_config = ops_config |
48 | 48 | self.cluster_config_path = cluster_config_path |
| 49 | + self.execute = execute |
49 | 50 |
|
50 | 51 | def run(self, args): |
51 | 52 | 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, '') |
53 | 55 |
|
54 | | - compositions= self.get_sorted_compositions(config_path_prefix) |
| 56 | + compositions = self.get_sorted_compositions(config_path_prefix) |
55 | 57 | if len(compositions) == 0 or compositions[0] != "helmfiles": |
56 | 58 | raise Exception("Please provide the full path to composition=helmfiles") |
57 | 59 | composition = compositions[0] |
58 | 60 | 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) |
60 | 63 |
|
61 | 64 | command = self.get_helmfile_command(args) |
62 | 65 | return dict(command=command) |
63 | 66 |
|
| 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 | + |
64 | 92 | def generate_helmfile_config(self, path, args): |
65 | 93 | output_file = args.helmfile_path + "/hiera-generated.yaml" |
66 | 94 | 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) |
72 | 100 |
|
73 | 101 | def get_helmfile_command(self, args): |
74 | 102 | cmd = ' '.join(args.extra_args + [args.subcommand]) |
|
0 commit comments