Методы программного удаления пространства имен в Kubernetes: примеры Python, JavaScript и Java

Чтобы удалить пространство имен программным способом, вам необходимо указать язык программирования и платформу, которую вы используете. Здесь я приведу примеры на трех популярных языках программирования: Python, JavaScript (Node.js) и Java.

  1. Удаление пространства имен в Python с помощью клиентской библиотеки Kubernetes Python:

    from kubernetes import client, config
    # Load the Kubernetes configuration
    config.load_kube_config()
    # Create an instance of the Kubernetes API client
    api_instance = client.CoreV1Api()
    # Specify the name of the namespace to delete
    namespace_name = "your-namespace"
    # Delete the namespace
    api_instance.delete_namespace(name=namespace_name)
  2. Удаление пространства имен в JavaScript (Node.js) с помощью официальной клиентской библиотеки Kubernetes JavaScript:

    const k8s = require('@kubernetes/client-node');
    // Load the Kubernetes configuration
    const kc = new k8s.KubeConfig();
    kc.loadFromDefault();
    // Create an instance of the Kubernetes API client
    const k8sApi = kc.makeApiClient(k8s.CoreV1Api);
    // Specify the name of the namespace to delete
    const namespaceName = 'your-namespace';
    // Delete the namespace
    k8sApi.deleteNamespace(namespaceName)
    .then(() => {
    console.log('Namespace deleted successfully.');
    })
    .catch((err) => {
    console.error('Error deleting namespace:', err);
    });
  3. Удаление пространства имен в Java с помощью официальной клиентской библиотеки Kubernetes Java:

    import io.kubernetes.client.openapi.ApiClient;
    import io.kubernetes.client.openapi.ApiException;
    import io.kubernetes.client.openapi.Configuration;
    import io.kubernetes.client.openapi.apis.CoreV1Api;
    import io.kubernetes.client.openapi.models.V1DeleteOptions;
    public class DeleteNamespaceExample {
    public static void main(String[] args) {
        // Load the Kubernetes configuration
        ApiClient client = Configuration.getDefaultApiClient();
        // Create an instance of the Kubernetes API client
        CoreV1Api api = new CoreV1Api();
        // Specify the name of the namespace to delete
        String namespaceName = "your-namespace";
        // Create delete options
        V1DeleteOptions deleteOptions = new V1DeleteOptions();
        try {
            // Delete the namespace
            api.deleteNamespace(namespaceName, deleteOptions, null, null, null, null, null);
            System.out.println("Namespace deleted successfully.");
        } catch (ApiException e) {
            System.err.println("Error deleting namespace: " + e.getResponseBody());
        }
    }
    }