| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
|
| 11 |
DOCUMENTATION = ''' |
| 12 |
--- |
| 13 |
module: azure_rm_openshiftmanagedclusterkubeconfig_info |
| 14 |
version_added: '1.17.0' |
| 15 |
short_description: Get admin kubeconfig of Azure Red Hat OpenShift Managed Cluster |
| 16 |
description: |
| 17 |
- get kubeconfig of Azure Red Hat OpenShift Managed Cluster instance. |
| 18 |
options: |
| 19 |
resource_group: |
| 20 |
description: |
| 21 |
- The name of the resource group. |
| 22 |
required: true |
| 23 |
type: str |
| 24 |
name: |
| 25 |
description: |
| 26 |
- Resource name. |
| 27 |
required: true |
| 28 |
type: str |
| 29 |
path: |
| 30 |
description: |
| 31 |
- Destination filepath of kubeconfig file |
| 32 |
required: false |
| 33 |
type: str |
| 34 |
extends_documentation_fragment: |
| 35 |
- azure.azcollection.azure |
| 36 |
author: |
| 37 |
- Maxim Babushkin (@maxbab) |
| 38 |
''' |
| 39 |
|
| 40 |
EXAMPLES = ''' |
| 41 |
- name: Obtain kubeconfig file of ARO cluster |
| 42 |
azure_rm_openshiftmanagedclusterkubeconfig_info: |
| 43 |
name: myCluster |
| 44 |
resource_group: myResourceGroup |
| 45 |
register: kubeconf |
| 46 |
|
| 47 |
- name: Print registered kubeconfig file |
| 48 |
debug: |
| 49 |
msg: "{{ kubeconf['kubeconfig'] }}" |
| 50 |
|
| 51 |
- name: Fetch kubeconfig and save it as mycluster_kubeconfig filename |
| 52 |
azure_rm_openshiftmanagedclusterkubeconfig_info: |
| 53 |
name: myCluster |
| 54 |
resource_group: myResourceGroup |
| 55 |
path: ./files/mycluster_kubeconfig |
| 56 |
|
| 57 |
- name: Fetch kubeconfig and save it to specified directory (file will be named as kubeconfig by default) |
| 58 |
azure_rm_openshiftmanagedclusterkubeconfig_info: |
| 59 |
name: myCluster |
| 60 |
resource_group: myResourceGroup |
| 61 |
path: ./files/ |
| 62 |
''' |
| 63 |
|
| 64 |
RETURN = ''' |
| 65 |
kubeconfig: |
| 66 |
description: |
| 67 |
- kubeconfig value |
| 68 |
returned: always |
| 69 |
type: str |
| 70 |
''' |
| 71 |
|
| 72 |
import base64 |
| 73 |
import filecmp |
| 74 |
import json |
| 75 |
import os |
| 76 |
import tempfile |
| 77 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt |
| 78 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_rest import GenericRestClient |
| 79 |
|
| 80 |
|
| 81 |
class Actions: |
| 82 |
NoAction, Create, Update, Delete = range(4) |
| 83 |
|
| 84 |
|
| 85 |
class AzureRMOpenShiftManagedClustersKubeconfigInfo(AzureRMModuleBaseExt): |
| 86 |
def __init__(self): |
| 87 |
self.module_arg_spec = dict( |
| 88 |
resource_group=dict( |
| 89 |
type='str', required=True |
| 90 |
), |
| 91 |
name=dict( |
| 92 |
type='str', required=True |
| 93 |
), |
| 94 |
path=dict( |
| 95 |
type='str', required=False |
| 96 |
) |
| 97 |
) |
| 98 |
|
| 99 |
self.resource_group = None |
| 100 |
self.name = None |
| 101 |
self.path = None |
| 102 |
|
| 103 |
self.results = dict(changed=False) |
| 104 |
self.mgmt_client = None |
| 105 |
self.state = None |
| 106 |
self.url = None |
| 107 |
self.status_code = [200] |
| 108 |
|
| 109 |
self.query_parameters = {} |
| 110 |
self.query_parameters['api-version'] = '2021-09-01-preview' |
| 111 |
self.header_parameters = {} |
| 112 |
self.header_parameters['Content-Type'] = 'application/json; charset=utf-8' |
| 113 |
|
| 114 |
self.mgmt_client = None |
| 115 |
super(AzureRMOpenShiftManagedClustersKubeconfigInfo, self).__init__(self.module_arg_spec, supports_check_mode=True, supports_tags=False) |
| 116 |
|
| 117 |
def exec_module(self, **kwargs): |
| 118 |
|
| 119 |
for key in self.module_arg_spec: |
| 120 |
setattr(self, key, kwargs[key]) |
| 121 |
|
| 122 |
self.mgmt_client = self.get_mgmt_svc_client(GenericRestClient, |
| 123 |
base_url=self._cloud_environment.endpoints.resource_manager) |
| 124 |
self.results = self.get_kubeconfig() |
| 125 |
if self.path and self.path_is_valid(): |
| 126 |
self.write_kubeconfig_to_file() |
| 127 |
return self.results |
| 128 |
|
| 129 |
def get_kubeconfig(self): |
| 130 |
response = None |
| 131 |
results = {} |
| 132 |
|
| 133 |
self.url = ('/subscriptions' + |
| 134 |
'/{{ subscription_id }}' + |
| 135 |
'/resourceGroups' + |
| 136 |
'/{{ resource_group }}' + |
| 137 |
'/providers' + |
| 138 |
'/Microsoft.RedHatOpenShift' + |
| 139 |
'/openShiftClusters' + |
| 140 |
'/{{ cluster_name }}' + |
| 141 |
'/listAdminCredentials') |
| 142 |
self.url = self.url.replace('{{ subscription_id }}', self.subscription_id) |
| 143 |
self.url = self.url.replace('{{ resource_group }}', self.resource_group) |
| 144 |
self.url = self.url.replace('{{ cluster_name }}', self.name) |
| 145 |
self.log("Fetch for kubeconfig from the cluster.") |
| 146 |
try: |
| 147 |
response = self.mgmt_client.query(self.url, |
| 148 |
'POST', |
| 149 |
self.query_parameters, |
| 150 |
self.header_parameters, |
| 151 |
None, |
| 152 |
self.status_code, |
| 153 |
600, |
| 154 |
30) |
| 155 |
results = json.loads(response.body()) |
| 156 |
except Exception as e: |
| 157 |
self.log('Could not get info for @(Model.ModuleOperationNameUpper).') |
| 158 |
return self.format_item(results) |
| 159 |
|
| 160 |
def format_item(self, item): |
| 161 |
d = { |
| 162 |
'kubeconfig': item.get('kubeconfig'), |
| 163 |
} |
| 164 |
return d |
| 165 |
|
| 166 |
def path_is_valid(self): |
| 167 |
if not os.path.basename(self.path): |
| 168 |
if os.path.isdir(self.path): |
| 169 |
self.log("Path is dir. Appending file name.") |
| 170 |
self.path += "kubeconfig" |
| 171 |
else: |
| 172 |
try: |
| 173 |
self.log('Attempting to makedirs {0}'.format(self.path)) |
| 174 |
os.makedirs(self.path) |
| 175 |
except IOError as exc: |
| 176 |
self.fail("Failed to create directory {0} - {1}".format(self.path, str(exc))) |
| 177 |
self.path += "kubeconfig" |
| 178 |
else: |
| 179 |
file_name = os.path.basename(self.path) |
| 180 |
path = self.path.replace(file_name, '') |
| 181 |
self.log('Checking path {0}'. format(path)) |
| 182 |
|
| 183 |
if path and not os.path.isdir(path): |
| 184 |
try: |
| 185 |
self.log('Attempting to makedirs {0}'. format(path)) |
| 186 |
os.makedirs(path) |
| 187 |
except IOError as exc: |
| 188 |
self.fail("Failed to create directory {0} - {1}".format(path, str(exc))) |
| 189 |
self.log("Validated path - {0}". format(self.path)) |
| 190 |
return True |
| 191 |
|
| 192 |
def write_kubeconfig_to_file(self): |
| 193 |
decoded_bytes = base64.b64decode(self.results['kubeconfig']) |
| 194 |
decoded_string = decoded_bytes.decode("utf-8") |
| 195 |
|
| 196 |
if os.path.exists(self.path): |
| 197 |
self.log('Existing kubeconfig file found. Compare, to decide if needs to override') |
| 198 |
|
| 199 |
|
| 200 |
tmp_kubeconfig = tempfile.TemporaryFile(mode='w') |
| 201 |
tmp_kubeconfig.write(decoded_string) |
| 202 |
tmp_kubeconfig.seek(0) |
| 203 |
|
| 204 |
|
| 205 |
if filecmp.cmp(tmp_kubeconfig.name, self.path): |
| 206 |
self.log("Files are identical. No need to override.") |
| 207 |
self.results['changed'] = False |
| 208 |
return |
| 209 |
|
| 210 |
self.log("Create {0} kubeconfig file.".format(self.path)) |
| 211 |
try: |
| 212 |
with open(self.path, "w") as file: |
| 213 |
file.write(decoded_string) |
| 214 |
except Exception as exc: |
| 215 |
self.fail("Failed to write kubeconfig output to file - {0} to {1} - {2}".format(self.results['kubeconfig'], |
| 216 |
self.path, exc)) |
| 217 |
self.log("The {0} kubeconfig file has been created.") |
| 218 |
self.results['changed'] = True |
| 219 |
return |
| 220 |
|
| 221 |
|
| 222 |
def main(): |
| 223 |
AzureRMOpenShiftManagedClustersKubeconfigInfo() |
| 224 |
|
| 225 |
|
| 226 |
if __name__ == '__main__': |
| 227 |
main() |