Skip to content

Commit

Permalink
Added namespace command
Browse files Browse the repository at this point in the history
Changed image name to include namespace
  • Loading branch information
keet committed Dec 11, 2018
1 parent cc0ebe0 commit a690d36
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 12 deletions.
8 changes: 4 additions & 4 deletions kubesh.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@
}
},
"templates": {
"aks_short_image_name": "$APP/__CONTAINER__",
"aks_image_name": "$CONTAINER_REGISTRY/$APP/__CONTAINER__",
"aks_short_image_name": "$NAMESPACE/$APP/__CONTAINER__",
"aks_image_name": "$CONTAINER_REGISTRY/$NAMESPACE/$APP/__CONTAINER__",
"docker_image_name": "__DIRECTORY_NAME_____CONTAINER__",
"gcr_image_name": "$CONTAINER_REGISTRY/$PROJECT/$APP/__CONTAINER__",
"minikube_image_name": "localhost:5000/$APP/__CONTAINER__"
"gcr_image_name": "$CONTAINER_REGISTRY/$PROJECT/$NAMESPACE/$APP/__CONTAINER__",
"minikube_image_name": "localhost:5000/$NAMESPACE/$APP/__CONTAINER__"
}
}
8 changes: 4 additions & 4 deletions library/aks.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ class AKS(Kubernetes):
@property
def image_name_template(self) -> str:
if self.templates is None:
return '$CONTAINER_REGISTRY/$APP/__CONTAINER__'
return '$CONTAINER_REGISTRY/$NAMESPACE/$APP/__CONTAINER__'
else:
return self.templates.get('aks_image_name', '$CONTAINER_REGISTRY/$APP/__CONTAINER__')
return self.templates.get('aks_image_name', '$CONTAINER_REGISTRY/$NAMESPACE/$APP/__CONTAINER__')

@property
def short_image_name_template(self) -> str:
Expand Down Expand Up @@ -57,11 +57,11 @@ def auth(self):
self._azure_login(variables['AZURE_APP_ID'], variables['AZURE_PASSWORD'], variables['AZURE_TENANT'])
self._azure_acr_login(variables['AZURE_CONTAINER_REGISTRY_NAME'])

@WrapPrint('Logging in azure... ', 'done')
@WrapPrint('Logging into azure... ', 'done')
def _azure_login(self, app_id: str, password: str, tenant: str):
call(['az', 'login', '--service-principal', '-u', app_id, '-p', password, '--tenant', tenant], stdout=PIPE)

@WrapPrint('Logging in azure container registry... ', 'done')
@WrapPrint('Logging into azure container registry... ', 'done')
def _azure_acr_login(self, name: str):
call(['az', 'acr', 'login', '--name', name], stdout=PIPE)

Expand Down
4 changes: 4 additions & 0 deletions library/argparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def __setup_environment_parsers(self, parent_parser):
containers = get_services(setting['deployment_filename'])
else:
containers = get_containers(setting['dockerfile_filename'])
containers.extend(setting.get('containers', []))
self.__setup_operation_parsers(parser, setting['type'], containers)

def __setup_operation_parsers(self, parent_parser, type, containers):
Expand All @@ -38,6 +39,9 @@ def __setup_operation_parsers(self, parent_parser, type, containers):
# Stop
parser = subparsers.add_parser('stop', help='Stop deployment.')
parser.add_argument('containers', nargs='*', help='Containers to stop')
# Namespace
if type != 'docker':
parser = subparsers.add_parser('namespace', help='Sets kubectl to the environment\'s namespace.')
# Logs
parser = subparsers.add_parser('logs', help='Show logs of container in deployment.')
if type == 'docker':
Expand Down
4 changes: 2 additions & 2 deletions library/gke.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ class GKE(Kubernetes):
@property
def image_name_template(self) -> str:
if self.templates is None:
return '$CONTAINER_REGISTRY/$PROJECT/$APP/__CONTAINER__'
return '$CONTAINER_REGISTRY/$PROJECT/$NAMESPACE/$APP/__CONTAINER__'
else:
return self.templates.get('gcr_image_name', '$CONTAINER_REGISTRY/$PROJECT/$APP/__CONTAINER__')
return self.templates.get('gcr_image_name', '$CONTAINER_REGISTRY/$PROJECT/$NAMESPACE/$APP/__CONTAINER__')

def get_build_image_name(self, container: str) -> str:
image_name = generate_image_name(container, self.image_name_template)
Expand Down
4 changes: 4 additions & 0 deletions library/kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ def stop(self, containers=[]):
self._load_image_name(containers)
run(['kubectl', 'delete', '-f', '-'], input=load_deployment_file(self.deployment_filename), encoding='UTF-8')

def namespace(self):
current_context = check_output(['kubectl', 'config', 'current-context']).decode('UTF-8').replace('\n', '')
call(['kubectl', 'config', 'set-context', current_context, '--namespace=' + os.getenv('NAMESPACE')])

def logs(self, containers=[], pod=None, container=None, follow=False):
command = ['kubectl', 'logs', pod, container]
if follow:
Expand Down
9 changes: 7 additions & 2 deletions library/minikube.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ class Minikube(Environment):
@property
def image_name_template(self) -> str:
if self.templates is None:
return 'localhost:5000/$APP/__CONTAINER__'
return 'localhost:5000/$NAMESPACE/$APP/__CONTAINER__'
else:
return self.templates.get('minikube_image_name', 'localhost:5000/$APP/__CONTAINER__')
return self.templates.get('minikube_image_name', 'localhost:5000/$NAMESPACE/$APP/__CONTAINER__')

@minikube_health_checker
@Condition('containers', get_containers, 'dockerfile_filename')
Expand Down Expand Up @@ -82,6 +82,11 @@ def stop(self, containers=[]):
self._load_image_names(containers)
run(['kubectl', 'delete', '-f', '-'], input=load_deployment_file(self.deployment_filename), encoding='UTF-8')

@minikube_health_checker
def namespace(self):
current_context = check_output(['kubectl', 'config', 'current-context']).decode('UTF-8').replace('\n', '')
call(['kubectl', 'config', 'set-context', current_context, '--namespace=' + os.getenv('NAMESPACE')])

@minikube_health_checker
def logs(self, containers=[], pod=None, container=None, follow=False):
command = ['kubectl', 'logs', pod, container]
Expand Down

0 comments on commit a690d36

Please sign in to comment.