Skip to content

Commit

Permalink
Merge pull request Azure#10 from pdaru/fleet-extension
Browse files Browse the repository at this point in the history
update style
  • Loading branch information
pdaru authored Aug 19, 2022
2 parents 9dd68b2 + 4271e5c commit 12ff9b0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 36 deletions.
14 changes: 8 additions & 6 deletions src/fleet/azext_fleet/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
from knack.log import get_logger
from knack.prompting import NoTTYException, prompt_y_n
from knack.util import CLIError
from knack.log import get_logger

logger = get_logger(__name__)


def print_or_merge_credentials(path, kubeconfig, overwrite_existing, context_name):
"""Merge an unencrypted kubeconfig into the file at the specified path, or print it to
stdout if the path is "-".
Expand Down Expand Up @@ -48,6 +48,7 @@ def print_or_merge_credentials(path, kubeconfig, overwrite_existing, context_nam
additional_file.close()
os.remove(temp_path)


def _merge_kubernetes_configurations(existing_file, addition_file, replace, context_name=None):
existing = _load_kubernetes_configuration(existing_file)
addition = _load_kubernetes_configuration(addition_file)
Expand All @@ -70,7 +71,7 @@ def _merge_kubernetes_configurations(existing_file, addition_file, replace, cont

if addition is None:
raise CLIError(
'failed to load additional configuration from {}'.format(addition_file))
f'failed to load additional configuration from {addition_file}')

if existing is None:
existing = addition
Expand All @@ -94,10 +95,10 @@ def _merge_kubernetes_configurations(existing_file, addition_file, replace, cont
yaml.safe_dump(existing, stream, default_flow_style=False)

current_context = addition.get('current-context', 'UNKNOWN')
msg = 'Merged "{}" as current context in {}'.format(
current_context, existing_file)
msg = f'Merged "{current_context}" as current context in {existing_file}'
print(msg)


def _handle_merge(existing, addition, key, replace):
if not addition[key]:
return
Expand All @@ -124,12 +125,13 @@ def _handle_merge(existing, addition, key, replace):
raise CLIError(msg.format(i['name'], key))
existing[key].append(i)


def _load_kubernetes_configuration(filename):
try:
with open(filename) as stream:
return yaml.safe_load(stream)
except (IOError, OSError) as ex:
if getattr(ex, 'errno', 0) == errno.ENOENT:
raise CLIError('{} does not exist'.format(filename))
raise CLIError(f'{filename} does not exist')
except (yaml.parser.ParserError, UnicodeDecodeError) as ex:
raise CLIError('Error parsing {} ({})'.format(filename, str(ex)))
raise CLIError(f'Error parsing {filename} ({str(ex)})')
5 changes: 2 additions & 3 deletions src/fleet/azext_fleet/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import os
from argcomplete.completers import FilesCompleter
from azure.cli.core.commands.parameters import (
get_resource_name_completion_list,
tags_type,
file_type
)
Expand All @@ -22,10 +21,10 @@ def load_arguments(self, _):
with self.argument_context('fleet create') as c:
c.argument('tags', tags_type)
c.argument('dns_name_prefix', options_list=['--dns-name-prefix', '-p'])

with self.argument_context('fleet patch') as c:
c.argument('tags', tags_type)

with self.argument_context('fleet get-credentials') as c:
c.argument('context_name', options_list=['--context'], help='If specified, overwrite the default context name.')
c.argument('path', options_list=['--file', '-f'], type=file_type, completer=FilesCompleter(), default=os.path.join(os.path.expanduser('~'), '.kube', 'config'))
Expand Down
64 changes: 37 additions & 27 deletions src/fleet/azext_fleet/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ def create_fleet(cmd,
if not name_part[0].isalpha():
name_part = (str('a') + name_part)[0:10]
resource_group_part = re.sub('[^A-Za-z0-9-]', '', resource_group_name)[0:16]
dns_name_prefix = '{}-{}-{}'.format(name_part, resource_group_part, subscription_id[0:6])
dns_name_prefix = f'{name_part}-{resource_group_part}-{subscription_id[0:6]}'

fleetHubProfile = FleetHubProfile(dns_prefix=dns_name_prefix)
rg_location = get_rg_location(cmd.cli_ctx, resource_group_name)
if location is None:
location = rg_location

fleet = Fleet(
location=location,
tags=tags,
Expand All @@ -60,10 +60,11 @@ def create_fleet(cmd,

return sdk_no_wait(no_wait, client.begin_create_or_update, resource_group_name, name, fleet)

def patch_fleet(cmd,
client,
resource_group_name,
name,

def patch_fleet(cmd,
client,
resource_group_name,
name,
tags=None):
FleetPatch = cmd.get_models(
"FleetPatch",
Expand All @@ -73,40 +74,45 @@ def patch_fleet(cmd,
fleetPatch = FleetPatch(tags=tags)
return client.update(resource_group_name, name, None, fleetPatch)

def get_fleet(cmd, # pylint: disable=unused-argument
client,
resource_group_name,

def get_fleet(cmd, # pylint: disable=unused-argument
client,
resource_group_name,
name):
logger.info('Getting fleet: {0} for resource group: {1}'.format(name, resource_group_name))
logger.info('Getting fleet: %s for resource group: %s', name, resource_group_name)
return client.get(resource_group_name, name)

def list_fleet_by_resource_group(cmd, # pylint: disable=unused-argument
client,

def list_fleet_by_resource_group(cmd, # pylint: disable=unused-argument
client,
resource_group_name):
logger.info('Listing fleet for resource group: {0}'.format(resource_group_name))
logger.info('Listing fleet for resource group: %s', resource_group_name)
return client.list_by_resource_group(resource_group_name)

def list_fleet_by_subscription(cmd, # pylint: disable=unused-argument

def list_fleet_by_subscription(cmd, # pylint: disable=unused-argument
client):
subscription_id = get_subscription_id(cmd.cli_ctx)
logger.info('Listing fleet for subscription id: {0}'.format(subscription_id))
logger.info('Listing fleet for subscription id: %s', subscription_id)
return client.list()


def delete_fleet(cmd, # pylint: disable=unused-argument
client,
resource_group_name,
name,
no_wait=False):
return sdk_no_wait(no_wait, client.begin_delete, resource_group_name, name)

def get_credentials(cmd, # pylint: disable=unused-argument
client,
resource_group_name,
name,
path=os.path.join(os.path.expanduser(
'~'), '.kube', 'config'),
overwrite_existing=False,
context_name=None):

def get_credentials(cmd, # pylint: disable=unused-argument
client,
resource_group_name,
name,
path=os.path.join(os.path.expanduser(
'~'), '.kube', 'config'),
overwrite_existing=False,
context_name=None):
credentialResults = client.list_credentials(resource_group_name, name)
if not credentialResults:
raise CLIError("No Kubernetes credentials found.")
Expand All @@ -119,6 +125,7 @@ def get_credentials(cmd, # pylint: disable=unused-argument
except (IndexError, ValueError):
raise CLIError("Fail to find kubeconfig file.")


def join_fleet_member(cmd,
client,
resource_group_name,
Expand All @@ -134,20 +141,23 @@ def join_fleet_member(cmd,
fleetMember = FleetMember(cluster_resource_id=member_cluster_id)
return sdk_no_wait(no_wait, client.begin_create_or_update, resource_group_name, fleet_name, name, fleetMember)


def list_fleet_member(cmd, # pylint: disable=unused-argument
client,
resource_group_name,
fleet_name):
logger.info('Listing fleet member(s) for fleet: {0} for resource group: {1}'.format(fleet_name, resource_group_name))
logger.info('Listing fleet member(s) for fleet: %s for resource group: %s', fleet_name, resource_group_name)
return client.list_by_fleet(resource_group_name, fleet_name)


def get_fleet_member(cmd, # pylint: disable=unused-argument
client,
resource_group_name,
fleet_name,
client,
resource_group_name,
fleet_name,
name):
return client.get(resource_group_name, fleet_name, name)


def remove_fleet_member(cmd, # pylint: disable=unused-argument
client,
resource_group_name,
Expand Down

0 comments on commit 12ff9b0

Please sign in to comment.