| 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_aks_info |
| 14 |
|
| 15 |
version_added: "0.1.2" |
| 16 |
|
| 17 |
short_description: Get Azure Kubernetes Service facts |
| 18 |
|
| 19 |
description: |
| 20 |
- Get facts for a specific Azure Kubernetes Service or all Azure Kubernetes Services. |
| 21 |
|
| 22 |
options: |
| 23 |
name: |
| 24 |
description: |
| 25 |
- Limit results to a specific resource group. |
| 26 |
type: str |
| 27 |
resource_group: |
| 28 |
description: |
| 29 |
- The resource group to search for the desired Azure Kubernetes Service |
| 30 |
type: str |
| 31 |
tags: |
| 32 |
description: |
| 33 |
- Limit results by providing a list of tags. Format tags as 'key' or 'key:value'. |
| 34 |
type: list |
| 35 |
elements: str |
| 36 |
show_kubeconfig: |
| 37 |
description: |
| 38 |
- Show kubeconfig of the AKS cluster. |
| 39 |
- Note the operation will cost more network overhead, not recommended when listing AKS. |
| 40 |
- I(show_kubeconfig=monitoring) to lists the cluster monitoring user credentials of a managed cluster. |
| 41 |
- I(show_kubeconfig=admin) to lists the cluster admin credentials of a managed cluster. |
| 42 |
- I(show_kubeconfig=user) to lists the cluster user credentials of a managed cluster. |
| 43 |
type: str |
| 44 |
choices: |
| 45 |
- user |
| 46 |
- admin |
| 47 |
- monitoring |
| 48 |
|
| 49 |
extends_documentation_fragment: |
| 50 |
- azure.azcollection.azure |
| 51 |
|
| 52 |
author: |
| 53 |
- Yuwei Zhou (@yuwzho) |
| 54 |
''' |
| 55 |
|
| 56 |
EXAMPLES = ''' |
| 57 |
- name: Get facts for one Azure Kubernetes Service |
| 58 |
azure_rm_aks_info: |
| 59 |
name: Testing |
| 60 |
resource_group: myResourceGroup |
| 61 |
|
| 62 |
- name: Get facts for all Azure Kubernetes Services |
| 63 |
azure_rm_aks_info: |
| 64 |
|
| 65 |
- name: Get facts by tags |
| 66 |
azure_rm_aks_info: |
| 67 |
tags: |
| 68 |
- testing |
| 69 |
''' |
| 70 |
|
| 71 |
RETURN = ''' |
| 72 |
azure_aks: |
| 73 |
description: List of Azure Kubernetes Service dicts. |
| 74 |
returned: always |
| 75 |
type: list |
| 76 |
''' |
| 77 |
|
| 78 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 79 |
|
| 80 |
try: |
| 81 |
from azure.core.exceptions import ResourceNotFoundError |
| 82 |
from azure.core.exceptions import HttpResponseError |
| 83 |
except Exception: |
| 84 |
|
| 85 |
pass |
| 86 |
|
| 87 |
AZURE_OBJECT_CLASS = 'managedClusters' |
| 88 |
|
| 89 |
|
| 90 |
class AzureRMManagedClusterInfo(AzureRMModuleBase): |
| 91 |
"""Utility class to get Azure Kubernetes Service facts""" |
| 92 |
|
| 93 |
def __init__(self): |
| 94 |
|
| 95 |
self.module_args = dict( |
| 96 |
name=dict(type='str'), |
| 97 |
resource_group=dict(type='str'), |
| 98 |
tags=dict(type='list', elements='str'), |
| 99 |
show_kubeconfig=dict(type='str', choices=['user', 'admin', 'monitoring']), |
| 100 |
) |
| 101 |
|
| 102 |
self.results = dict( |
| 103 |
changed=False, |
| 104 |
aks=[], |
| 105 |
available_versions=[] |
| 106 |
) |
| 107 |
|
| 108 |
self.name = None |
| 109 |
self.resource_group = None |
| 110 |
self.tags = None |
| 111 |
self.show_kubeconfig = None |
| 112 |
|
| 113 |
super(AzureRMManagedClusterInfo, self).__init__( |
| 114 |
derived_arg_spec=self.module_args, |
| 115 |
supports_check_mode=True, |
| 116 |
supports_tags=False, |
| 117 |
facts_module=True |
| 118 |
) |
| 119 |
|
| 120 |
def exec_module(self, **kwargs): |
| 121 |
|
| 122 |
for key in self.module_args: |
| 123 |
setattr(self, key, kwargs[key]) |
| 124 |
|
| 125 |
if self.name is not None and self.resource_group is not None: |
| 126 |
self.results['aks'] = self.get_item() |
| 127 |
elif self.resource_group is not None: |
| 128 |
self.results['aks'] = self.list_by_resourcegroup() |
| 129 |
else: |
| 130 |
self.results['aks'] = self.list_items() |
| 131 |
|
| 132 |
return self.results |
| 133 |
|
| 134 |
def get_item(self): |
| 135 |
"""Get a single Azure Kubernetes Service""" |
| 136 |
|
| 137 |
self.log('Get properties for {0}'.format(self.name)) |
| 138 |
|
| 139 |
item = None |
| 140 |
result = [] |
| 141 |
|
| 142 |
try: |
| 143 |
item = self.managedcluster_client.managed_clusters.get(self.resource_group, self.name) |
| 144 |
except ResourceNotFoundError: |
| 145 |
pass |
| 146 |
|
| 147 |
if item and self.has_tags(item.tags, self.tags): |
| 148 |
result = [self.serialize_obj(item, AZURE_OBJECT_CLASS)] |
| 149 |
if self.show_kubeconfig: |
| 150 |
result[0]['kube_config'] = self.get_aks_kubeconfig(self.resource_group, self.name) |
| 151 |
|
| 152 |
return result |
| 153 |
|
| 154 |
def list_by_resourcegroup(self): |
| 155 |
"""Get all Azure Kubernetes Services""" |
| 156 |
|
| 157 |
self.log('List all Azure Kubernetes Services under resource group') |
| 158 |
|
| 159 |
try: |
| 160 |
response = self.managedcluster_client.managed_clusters.list_by_resource_group(self.resource_group) |
| 161 |
except Exception as exc: |
| 162 |
self.fail('Failed to list all items - {0}'.format(str(exc))) |
| 163 |
|
| 164 |
results = [] |
| 165 |
for item in response: |
| 166 |
if self.has_tags(item.tags, self.tags): |
| 167 |
item_dict = self.serialize_obj(item, AZURE_OBJECT_CLASS) |
| 168 |
if self.show_kubeconfig: |
| 169 |
item_dict['kube_config'] = self.get_aks_kubeconfig(self.resource_group, item.name) |
| 170 |
results.append(item_dict) |
| 171 |
|
| 172 |
return results |
| 173 |
|
| 174 |
def list_items(self): |
| 175 |
"""Get all Azure Kubernetes Services""" |
| 176 |
|
| 177 |
self.log('List all Azure Kubernetes Services') |
| 178 |
|
| 179 |
try: |
| 180 |
response = self.managedcluster_client.managed_clusters.list() |
| 181 |
except Exception as exc: |
| 182 |
self.fail('Failed to list all items - {0}'.format(str(exc))) |
| 183 |
|
| 184 |
results = [] |
| 185 |
for item in response: |
| 186 |
if self.has_tags(item.tags, self.tags): |
| 187 |
item_dict = self.serialize_obj(item, AZURE_OBJECT_CLASS) |
| 188 |
if self.show_kubeconfig: |
| 189 |
item_dict['kube_config'] = self.get_aks_kubeconfig(item.resource_group, item.name) |
| 190 |
results.append(item_dict) |
| 191 |
|
| 192 |
return results |
| 193 |
|
| 194 |
def get_aks_kubeconfig(self, resource_group, name): |
| 195 |
''' |
| 196 |
Gets kubeconfig for the specified AKS instance. |
| 197 |
|
| 198 |
:return: AKS instance kubeconfig |
| 199 |
''' |
| 200 |
if self.show_kubeconfig == 'user': |
| 201 |
try: |
| 202 |
access_profile = self.managedcluster_client.managed_clusters.list_cluster_user_credentials(self.resource_group, self.name) |
| 203 |
except HttpResponseError as ec: |
| 204 |
self.log("Lists the cluster user credentials of a managed cluster Failed, Exception as {0}".format(ec)) |
| 205 |
return [] |
| 206 |
return [item.value.decode('utf-8') for item in access_profile.kubeconfigs] |
| 207 |
elif self.show_kubeconfig == 'admin': |
| 208 |
try: |
| 209 |
access_profile = self.managedcluster_client.managed_clusters.list_cluster_admin_credentials(self.resource_group, self.name) |
| 210 |
except HttpResponseError as ec: |
| 211 |
self.log("Lists the cluster admin credentials of a managed cluster Failed, Exception as {0}".format(ec)) |
| 212 |
return [] |
| 213 |
return [item.value.decode('utf-8') for item in access_profile.kubeconfigs] |
| 214 |
elif self.show_kubeconfig == 'monitoring': |
| 215 |
try: |
| 216 |
access_profile = self.managedcluster_client.managed_clusters.list_cluster_monitoring_user_credentials(self.resource_group, self.name) |
| 217 |
except HttpResponseError as ec: |
| 218 |
self.log("Lists the cluster monitoring credentials of a managed cluster Failed, Exception as {0}".format(ec)) |
| 219 |
return [] |
| 220 |
return [item.value.decode('utf-8') for item in access_profile.kubeconfigs] |
| 221 |
else: |
| 222 |
return [] |
| 223 |
|
| 224 |
|
| 225 |
def main(): |
| 226 |
"""Main module execution code path""" |
| 227 |
|
| 228 |
AzureRMManagedClusterInfo() |
| 229 |
|
| 230 |
|
| 231 |
if __name__ == '__main__': |
| 232 |
main() |