Skip to content

Commit

Permalink
Use provider eksctl method to run eksctl because the binary is not in…
Browse files Browse the repository at this point in the history
… PATH;
  • Loading branch information
ycliuhw committed Jun 2, 2020
1 parent 1f195d8 commit 8bb2a22
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
6 changes: 4 additions & 2 deletions acceptancetests/jujupy/k8s_provider/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from contextlib import contextmanager
from enum import Enum
from pprint import pformat
from shlex import quote
from time import sleep

from jujupy.client import temp_bootstrap_env
Expand Down Expand Up @@ -211,13 +212,14 @@ def patch_configmap(self, namespace, cm_name, key, value):
cm['data'] = data
self.kubectl_apply(json.dumps(cm))

def sh(self, *args):
args = [str(arg) for arg in args]
def sh(self, *args, shell=False):
args = [quote(str(arg)) if shell else str(arg) for arg in args]
logger.debug('sh -> %s', ' '.join(args))
return subprocess.check_output(
# cmd should be a list of str.
args,
stderr=subprocess.STDOUT,
shell=shell,
).decode('UTF-8').strip()

def _ensure_kubectl_bin(self):
Expand Down
23 changes: 12 additions & 11 deletions acceptancetests/jujupy/k8s_provider/eks.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ def __init__(self, bs_manager, timeout=1800):
super().__init__(bs_manager, timeout)

self.cluster_name = self.client.env.controller.name # use controller name for cluster name
suffix = os.environ.get('BUILD_NUMBER', None)
if suffix is not None:
self.cluster_name += '-%s' % suffix
self._eksctl_bin = os.path.join(self.juju_home, 'eksctl')
self._ensure_eksctl_bin()
self.default_storage_class_name = ''
Expand All @@ -65,7 +68,7 @@ def __init_client(self, env):

# list all running clusters.
logger.info(
'running eks clusters in %s: \n\t- %s', self.location,
'Running eks clusters in %s: \n\t- %s', self.location,
'\n\t- '.join([c['name'] for c in self.list_clusters(self.location)])
)

Expand All @@ -79,17 +82,15 @@ def _ensure_eksctl_bin(self):
else:
self.sh(
'''curl --silent --location "https://github.com/weaveworks/eksctl/releases/latest/download/eksctl_$(uname -s)_amd64.tar.gz" | tar xz -C /tmp && mv /tmp/eksctl %s
''' % self._eksctl_bin)
''' % self._eksctl_bin, shell=True)

def eksctl(self, *args):
return self.sh(self._eksctl_bin, *args)

def _tear_down_substrate(self):
logger.info("Deleting the EKS instance {0}".format(self.cluster_name))
try:
o = self.eksctl(
'delete', 'cluster', self.cluster_name, '--region', self.location,
)
o = self.eksctl('delete', 'cluster', self.cluster_name, '--region', self.location)
logger.info("cluster %s has been deleted -> \n%s", self.cluster_name, o)
except Exception as e:
if is_404(e):
Expand All @@ -103,9 +104,9 @@ def list_clusters(self, region):
)

def _ensure_kube_dir(self):
logger.info("writing kubeconfig to %s" % self.kube_config_path)
self.sh(
'eksctl', 'utils', 'write-kubeconfig', '--cluster', self.cluster_name,
logger.info("Writing kubeconfig to %s" % self.kube_config_path)
self.eksctl(
'utils', 'write-kubeconfig', '--cluster', self.cluster_name,
'--region', self.location, '--kubeconfig', self.kube_config_path,
)

Expand All @@ -130,10 +131,10 @@ def log_remaining(remaining, msg=''):
if remaining % 30 == 0:
msg += ' timeout in %ss...' % remaining
logger.info(msg)

# do pre cleanup;
self._tear_down_substrate()

for remaining in until_timeout(600):
# wait for the existing cluster to be deleted.
try:
Expand All @@ -146,7 +147,7 @@ def log_remaining(remaining, msg=''):
log_remaining(remaining)

# provision cluster.
logger.info('creating cluster -> %s', self.cluster_name)
logger.info('Creating cluster -> %s', self.cluster_name)
try:
o = self.eksctl(
'create', 'cluster',
Expand Down

0 comments on commit 8bb2a22

Please sign in to comment.