Skip to content

Commit

Permalink
Adjustments to interface with gcp and external-cloud-provider (#15)
Browse files Browse the repository at this point in the history
* Prepare for clouds which don't yet support external-cloud-provider

* Remove warning aboug GCP cloud-provider, added unit tests

* formatting
  • Loading branch information
addyess authored Mar 6, 2024
1 parent f1a4255 commit 3a5e868
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 32 deletions.
47 changes: 15 additions & 32 deletions charms/kubernetes_snaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
45 changes: 45 additions & 0 deletions tests/unit/test_kubernetes_snaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 3a5e868

Please sign in to comment.