Skip to content

Commit

Permalink
k8s: deduplicate passing context and namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
trehn committed Apr 19, 2018
1 parent 2f28152 commit 48a151c
Showing 1 changed file with 16 additions and 30 deletions.
46 changes: 16 additions & 30 deletions bundlewrap/items/kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,28 +62,13 @@ def cdict(self):

def fix(self, status):
if status.must_be_deleted:
cmdline = [
"kubectl",
"--context={}".format(self.node.kubectl_context),
"delete",
self.KIND,
self.resource_name,
]
if self.namespace:
cmdline.append("--namespace={}".format(self.namespace))
result = run_local(cmdline)
result = run_local(self._kubectl + ["delete", self.KIND, self.resource_name])
log_error(result)
else:
cmdline = [
"kubectl",
"--context={}".format(self.node.kubectl_context),
"apply",
"-f",
"-",
]
if self.namespace:
cmdline.append("--namespace={}".format(self.namespace))
result = run_local(cmdline, data_stdin=self.manifest.encode('utf-8'))
result = run_local(
self._kubectl + ["apply", "-f", "-"],
data_stdin=self.manifest.encode('utf-8'),
)
log_error(result)

def get_auto_deps(self, items, _secrets=True):
Expand Down Expand Up @@ -111,6 +96,16 @@ def get_auto_deps(self, items, _secrets=True):
deps.append(item.id)
return deps

@property
def _kubectl(self):
cmdline = [
"kubectl",
"--context={}".format(self.node.kubectl_context),
]
if self.namespace:
cmdline.append("--namespace={}".format(self.namespace))
return cmdline

@property
def _manifest_dict(self):
if self.attributes['manifest_processor'] == 'jinja2':
Expand Down Expand Up @@ -171,16 +166,7 @@ def resource_name(self):
return self.name.split("/", 1)[-1]

def sdict(self):
result = run_local([
"kubectl",
"--context={}".format(self.node.kubectl_context),
"--namespace={}".format(self.namespace),
"get",
"-o",
"json",
self.KIND,
self.resource_name,
])
result = run_local(self._kubectl + ["get", "-o", "json", self.KIND, self.resource_name])
if result.return_code == 0:
full_json_response = json.loads(result.stdout)
if full_json_response.get("status", {}).get("phase") == "Terminating":
Expand Down

0 comments on commit 48a151c

Please sign in to comment.