diff --git a/charms/kubernetes_snaps.py b/charms/kubernetes_snaps.py index 5aff777..d4b51a7 100644 --- a/charms/kubernetes_snaps.py +++ b/charms/kubernetes_snaps.py @@ -81,9 +81,9 @@ def configure_apiserver( api_opts["service-account-issuer"] = "https://kubernetes.default.svc" api_opts["service-account-signing-key-file"] = str(service_account_key_path) api_opts["service-account-key-file"] = str(service_account_key_path) - api_opts[ - "kubelet-preferred-address-types" - ] = "InternalIP,Hostname,InternalDNS,ExternalDNS,ExternalIP" + api_opts["kubelet-preferred-address-types"] = ( + "InternalIP,Hostname,InternalDNS,ExternalDNS,ExternalIP" + ) # TODO: encryption at rest # api_opts["encryption-provider-config"] = str(encryption_config_path()) @@ -159,16 +159,10 @@ def configure_apiserver( api_opts["client-ca-file"] = "/root/cdk/ca.crt" if external_cloud_provider.has_xcp: + log.info("KubeApi: Uses an External Cloud Provider") api_opts["cloud-provider"] = "external" - - if external_cloud_provider.name == "gce": - # TODO: We need a charm for external-cloud-provider for GCP - # based on https://github.com/kubernetes/cloud-provider-gcp/ - log.warning( - "GCP cloud-provider is currently available in-tree " - "but can be avoided configuring here if we use the " - "external tree provider" - ) + else: + log.info("KubeApi: No Cloud Features") api_opts["feature-gates"] = ",".join(feature_gates) @@ -229,16 +223,10 @@ def configure_controller_manager( feature_gates = ["RotateKubeletServerCertificate=true"] if external_cloud_provider.has_xcp: + log.info("KubeController: Uses an External Cloud Provider") controller_opts["cloud-provider"] = "external" - - if external_cloud_provider.name == "gce": - # TODO: We need a charm for external-cloud-provider for GCP - # based on https://github.com/kubernetes/cloud-provider-gcp/ - log.warning( - "GCP cloud-provider is currently available in-tree " - "but can be avoided configuring here if we use the " - "external tree provider" - ) + else: + log.info("KubeController: No Cloud Features") controller_opts["feature-gates"] = ",".join(feature_gates) @@ -371,23 +359,18 @@ def configure_kubelet( kubelet_opts["hostname-override"] = get_node_name(fqdn) kubelet_opts["config"] = "/root/cdk/kubelet/config.yaml" if external_cloud_provider.has_xcp: + log.info("Kubelet: Uses an External Cloud Provider") kubelet_opts["cloud-provider"] = "external" - - if external_cloud_provider.name == "gce": - # TODO: We need a charm for external-cloud-provider for GCP - # based on https://github.com/kubernetes/cloud-provider-gcp/ - log.warning( - "GCP cloud-provider is currently available in-tree " - "but can be avoided configuring here if we use the " - "external tree provider" - ) + else: + log.info("Kubelet: No Cloud Features") # Add kubelet-extra-config. This needs to happen last so that it # overrides any config provided by the charm. merge_extra_config(kubelet_config, extra_config) - os.makedirs("/root/cdk/kubelet", exist_ok=True) - with open("/root/cdk/kubelet/config.yaml", "w") as f: + p = Path("/root/cdk/kubelet/config.yaml") + p.parent.mkdir(parents=True, exist_ok=True) + with p.open("w") as f: f.write("# Generated by charm, do not edit\n") yaml.dump(kubelet_config, f) diff --git a/tests/unit/test_kubernetes_snaps.py b/tests/unit/test_kubernetes_snaps.py index da1fbe0..38d844f 100644 --- a/tests/unit/test_kubernetes_snaps.py +++ b/tests/unit/test_kubernetes_snaps.py @@ -61,3 +61,48 @@ def test_is_channel_swap(subprocess_call, subprocess_check_output): assert kubernetes_snaps.is_channel_swap("my-snap", "1.28/stable") assert not kubernetes_snaps.is_channel_swap("my-snap", "1.29/stable") assert kubernetes_snaps.is_channel_swap("my-snap", "1.30/stable") + + +@pytest.fixture(params=[None, "external"]) +def external_cloud(request): + cloud = mock.MagicMock() + cloud.has_xcp = request.param == "external" + cloud.in_tree.return_value = {} + yield cloud + + +@mock.patch("charms.kubernetes_snaps.configure_kubernetes_service") +@mock.patch("charms.kubernetes_snaps.Path") +def test_configure_kubelet( + mock_path, + configure_kubernetes_service, + external_cloud, +): + kubernetes_snaps.configure_kubelet( + "container_runtime_endpoint", + "dns_domain", + "dns_ip", + {}, + {}, + external_cloud, + "kubeconfig", + "node_ip", + "registry.io", + ["taint:NoExecute"], + ) + configure_kubernetes_service.assert_called_once() + service, args, extra = configure_kubernetes_service.call_args[0] + assert service == "kubelet" + assert extra == {} + expected_args = { + "kubeconfig": "kubeconfig", + "v": "0", + "node-ip": "node_ip", + "container-runtime-endpoint": "container_runtime_endpoint", + "hostname-override": kubernetes_snaps.get_node_name(), + "config": "/root/cdk/kubelet/config.yaml", + **external_cloud.in_tree.return_value, + } + if external_cloud.has_xcp: + expected_args["cloud-provider"] = "external" + assert expected_args == args