Enable deleting apps by using CLI

This commit is contained in:
Maarten de Waard 2022-09-22 16:14:49 +02:00
parent 5893ee39a3
commit caa9b2e79b
No known key found for this signature in database
GPG key ID: 1D3E893A657CC8DA
4 changed files with 95 additions and 33 deletions

View file

@ -13,6 +13,8 @@ from kubernetes.client.exceptions import ApiException
from kubernetes.utils import create_from_yaml
from kubernetes.utils.create_from_yaml import FailToCreateError
# Load the kube config once
config.load_kube_config()
def create_variables_secret(app_slug, variables_filepath):
"""Checks if a variables secret for app_name already exists, generates it if necessary.
@ -117,19 +119,31 @@ def store_kustomization(kustomization_template_filepath, app_slug):
namespace="flux-system",
plural="kustomizations",
body=kustomization_dict)
# create_from_yaml(
# api_client_instance,
# yaml_objects=[kustomization_dict],
# # All kustomizations live in the flux-system namespace
# namespace="flux-system"
# )
except FailToCreateError as ex:
print(f"Could not create {app_slug} Kustomization because of exception {ex}")
return
print(f"Kustomization created with api response: {api_response}")
def delete_kustomization(kustomization_name):
"""Deletes kustomization for an app_slug. Should also result in the
deletion of the app's HelmReleases, PVCs, OAuth2Client, etc. Nothing will
remain"""
api_client_instance = api_client.ApiClient()
custom_objects_api = client.CustomObjectsApi(api_client_instance)
body = client.V1DeleteOptions()
try:
api_response = custom_objects_api.delete_namespaced_custom_object(
group="kustomize.toolkit.fluxcd.io",
version="v1beta2",
namespace="flux-system",
plural="kustomizations",
name=kustomization_name,
body=body)
except ApiException as ex:
print(f"Could not delete {kustomization_name} Kustomization because of exception {ex}")
return False
print(f"Kustomization deleted with api response: {api_response}")
def read_template_to_dict(template_filepath, template_globals):
"""Reads a Jinja2 template that contains yaml and turns it into a dict
@ -198,7 +212,6 @@ def get_all_kustomizations(namespace='flux-system'):
:return: Kustomizations as returned by CustomObjectsApi.list_namespaced_custom_object()
:rtype: object
"""
config.load_kube_config()
api = client.CustomObjectsApi()
api_response = api.list_namespaced_custom_object(
group="kustomize.toolkit.fluxcd.io",
@ -231,7 +244,6 @@ def get_all_helmreleases(namespace='stackspin'):
:return: Helmreleases as returned by CustomObjectsApi.list_namespaced_custom_object()
:rtype: object
"""
config.load_kube_config()
api = client.CustomObjectsApi()
api_response = api.list_namespaced_custom_object(
group="helm.toolkit.fluxcd.io",
@ -244,7 +256,6 @@ def get_all_helmreleases(namespace='stackspin'):
def get_kustomization(name, namespace='flux-system'):
"""Returns all info of a Flux kustomization with name 'name'"""
config.load_kube_config()
api = client.CustomObjectsApi()
try:
resource = api.get_namespaced_custom_object(
@ -264,7 +275,6 @@ def get_kustomization(name, namespace='flux-system'):
def get_helmrelease(name, namespace='stackspin-apps'):
"""Returns all info of a Flux helmrelease with name 'name'"""
config.load_kube_config()
api = client.CustomObjectsApi()
try:
resource = api.get_namespaced_custom_object(