Skip to content

Commit

Permalink
Create a script to make updating application resources easier. (kubef…
Browse files Browse the repository at this point in the history
…low#596)

* This is a replacement for
  https://github.com/kubeflow/manifests/blob/master/hack/update-instance-labels.sh

* We don't want to assume all applications are at the same version
* This script makes it easier to set a different version for a specific
  application.
  • Loading branch information
jlewi authored Feb 4, 2020
1 parent 26ff3af commit 57e8adc
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions py/kubeflow/testing/tools/applications.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
"""A CLI for updating YAML files defining application CRs.
This tool makes it easier to update and maintain the Application CRs
for each application.
"""

import fire
import logging
import os
import yaml

class AppManager:
@staticmethod
def update(version, directory=None):
"""Update the application resources to the specified version.
Args:
version: The version to set
directory: (Optional). Directory to run in defaults to current directory.
"""

if not directory:
directory = os.getcwd()

logging.info(f"Looking for application.yaml files in {directory}")

for base, _, files in os.walk(directory):
for f in files:
if f != "application.yaml":
continue

path = os.path.join(base, f)

logging.info(f"Processing {path}")

with open(path) as hf:
app = yaml.load(hf)

labels = app["spec"]["selector"]["matchLabels"]
app_name = labels.get("app.kubernetes.io/name", "")

labels["app.kubernetes.io/version"] = version
labels["app.kubernetes.io/instance"] = (
f"{app_name}-{version}")

with open(path, "w") as hf:
yaml.dump(app, hf)

# We also need to modify the kustomization file to add the labels
kustomization_path = os.path.join(base, "kustomization.yaml")

if not os.path.exists(kustomization_path):
logging.warning(f"kustomizatin file: {kustomization_path} doesn't "
f"exist")

with open(kustomization_path) as hf:
kustomize = yaml.load(hf)

if "commonLabels" not in kustomize:
kustomize["commonLabels"] = {}

kustomize["commonLabels"]["app.kubernetes.io/version"] = version
kustomize["commonLabels"]["app.kubernetes.io/instance"] = (
f"{app_name}-{version}")

logging.info(f"Updating {kustomization_path}")

with open(kustomization_path, "w") as hf:
yaml.dump(kustomize, hf)

if __name__ == "__main__":

logging.basicConfig(level=logging.INFO,
format=('%(levelname)s|%(asctime)s'
'|%(message)s|%(pathname)s|%(lineno)d|'),
datefmt='%Y-%m-%dT%H:%M:%S',
)

fire.Fire(AppManager)

0 comments on commit 57e8adc

Please sign in to comment.