Kubernetes Helper Class Reference

class smarter.common.helpers.k8s_helpers.KubernetesHelper(kubeconfig=None, configured=False, **kwargs)[source]

Bases: SmarterHelperMixin

Helper class for interacting with Kubernetes clusters.

This class provides a set of utility methods to manage and verify Kubernetes resources such as Ingresses, Certificates, and Secrets, as well as to apply manifests and update kubeconfig files for EKS clusters. It is designed to be used as a singleton and integrates with AWS EKS and cert-manager.

_kubeconfig

The loaded kubeconfig as a dictionary.

Type:

dict

_configured

Indicates whether the kubeconfig has been configured.

Type:

bool

Parameters:
  • kubeconfig (dict, optional) – The kubeconfig dictionary to use. If not provided, a default is used.

  • configured (bool, optional) – Whether the helper is already configured.

__init__(kubeconfig=None, configured=False, **kwargs)[source]
apply_manifest(manifest)[source]

Apply a Kubernetes manifest to the cluster.

Parameters:

manifest (str) – The Kubernetes manifest as a string.

Raises:

KubernetesHelperException – If applying the manifest fails.

Returns:

None

Return type:

None

property configured: bool

Return whether the kubeconfig has been configured.

Returns:

True if configured, False otherwise.

Return type:

bool

delete_certificate(certificate_name, namespace)[source]

Delete a cert-manager certificate resource from the cluster. command: - kubectl delete certificate education.3141-5926-5359.api.example.com-tls -n smarter-platform-prod

Parameters:
  • certificate_name (str) – The name of the certificate.

  • namespace (str) – The namespace of the certificate.

Returns:

True if the certificate was deleted, False otherwise.

Return type:

bool

delete_ingress(ingress_name, namespace)[source]

Delete an Ingress resource from the cluster. command: - kubectl delete ingress education.3141-5926-5359.api.example.com -n smarter-platform-prod

Parameters:
  • ingress_name (str) – The name of the ingress.

  • namespace (str) – The namespace of the ingress.

Returns:

True if the ingress was deleted, False otherwise.

Return type:

bool

delete_ingress_resources(hostname, namespace)[source]

Delete an ingress and all child resources from the cluster. commands: - kubectl delete ingress education.3141-5926-5359.api.example.com -n smarter-platform-prod - kubectl delete certificate education.3141-5926-5359.api.example.com-tls -n smarter-platform-prod - kubectl delete secret education.3141-5926-5359.api.example.com-tls -n smarter-platform-prod

Parameters:
  • hostname (str) – The hostname of the ingress.

  • namespace (str) – The namespace of the ingress.

Returns:

A tuple of booleans indicating whether the ingress, certificate, and secret were deleted.

Return type:

Tuple[bool, bool, bool]

delete_secret(secret_name, namespace)[source]

Delete a secret resource from the cluster. commands: - kubectl delete secret education.3141-5926-5359.api.example.com-tls -n smarter-platform-prod

Parameters:
  • secret_name (str) – The name of the secret.

  • namespace (str) – The namespace of the secret.

Returns:

True if the secret was deleted, False otherwise.

Return type:

bool

get_namespaces()[source]

Get all namespaces in the Kubernetes cluster.

Returns:

A dictionary of namespaces.

Return type:

Optional[dict]

property kubeconfig: dict

Return the kubeconfig file as a dictionary.

Returns:

The kubeconfig dictionary.

Return type:

dict

property kubeconfig_path: str

Return the path to the kubeconfig file.

Returns:

The kubeconfig file path.

Return type:

str

property namespace_verified: bool

Return whether the Kubernetes namespace has been verified.

Returns:

True if the namespace has been verified, False otherwise.

Return type:

bool

property ready: bool

Return whether the Kubernetes helper is ready for use. Returns True if Kubernetes is configured, AND, the Kubernetes namespace exists.

Returns:

True if ready, False otherwise.

Return type:

bool

smarter_settings.environment_namespace

update_kubeconfig()[source]

Generate a fresh kubeconfig file for the EKS cluster.

This method uses the AWS CLI to update the kubeconfig file for the specified EKS cluster.

Raises:

KubernetesHelperException – If the kubeconfig update fails.

Returns:

True if the kubeconfig was updated successfully, False otherwise.

Return type:

bool

verify_certificate(name, namespace)[source]

Verify that a cert-manager certificate resource exists in the cluster and is in a ready state.

command: - kubectl get certificate smarter.3141-5926-5359.api.example.com-tls -n smarter-platform-prod -o json

parse json response and check for the following: - status.conditions.type == Ready

Parameters:
  • name (str) – The name of the certificate.

  • namespace (str) – The namespace of the certificate.

Returns:

True if the certificate exists and is ready, False otherwise.

Return type:

bool

verify_ingress(name, namespace)[source]

Verify that an Ingress resource exists in the cluster.

commands: - kubectl get ingress smarter.3141-5926-5359.api.example.com -n smarter-platform-prod -o json

Parameters:
  • name (str) – The name of the ingress.

  • namespace (str) – The namespace of the ingress.

Returns:

True if the ingress exists, False otherwise.

Return type:

bool

verify_ingress_resources(hostname, namespace)[source]

Verify that an ingress and all child resources exist in the cluster.

commands: - kubectl get ingress education.3141-5926-5359.api.example.com -n smarter-platform-prod -o json - kubectl get certificate education.3141-5926-5359.api.example.com-tls -n smarter-platform-prod -o json - kubectl get secret education.3141-5926-5359.api.example.com-tls -n smarter-platform-prod -o json

Parameters:
  • hostname (str) – The hostname of the ingress.

  • namespace (str) – The namespace of the ingress.

Returns:

A tuple of booleans indicating whether the ingress, certificate, and secret were verified.

Return type:

Tuple[bool, bool, bool]

verify_namespace(namespace)[source]

Verify that a namespace exists in the cluster.

Parameters:

namespace (str) – The name of the namespace.

Returns:

True if the namespace exists, False otherwise.

Return type:

bool

verify_secret(name, namespace)[source]

Verify that a secret resource exists in the cluster. command: - kubectl get secret smarter.3141-5926-5359.api.example.com-tls -n smarter-platform-prod -o json

Parameters:
  • name (str) – The name of the secret.

  • namespace (str) – The namespace of the secret.

Returns:

True if the secret exists, False otherwise.

Return type:

bool