Skip to content

Commit

Permalink
Merge cloud-provider and feature config
Browse files Browse the repository at this point in the history
  • Loading branch information
bschimke95 committed Nov 20, 2024
1 parent e16c44d commit 17d8a37
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 74 deletions.
14 changes: 7 additions & 7 deletions charms/worker/k8s/lib/charms/k8s/v0/k8sd_api_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ class ClusterMember(BaseModel):
datastore_role: Optional[str] = Field(default=None, alias="datastore-role")


class DNSConfig(BaseModel):
class DNSConfig(BaseModel, allow_population_by_field_name=True):
"""Configuration for the DNS settings of the cluster.
Attributes:
Expand All @@ -210,7 +210,7 @@ class DNSConfig(BaseModel):
upstream_nameservers: Optional[List[str]] = Field(default=None, alias="upstream-nameservers")


class IngressConfig(BaseModel):
class IngressConfig(BaseModel, allow_population_by_field_name=True):
"""Configuration for the ingress settings of the cluster.
Attributes:
Expand All @@ -224,7 +224,7 @@ class IngressConfig(BaseModel):
enable_proxy_protocol: Optional[bool] = Field(default=None, alias="enable-proxy-protocol")


class LoadBalancerConfig(BaseModel):
class LoadBalancerConfig(BaseModel, allow_population_by_field_name=True):
"""Configuration for the load balancer settings of the cluster.
Attributes:
Expand All @@ -250,7 +250,7 @@ class LoadBalancerConfig(BaseModel):
bgp_peer_port: Optional[int] = Field(default=None, alias="bgp-peer-port")


class LocalStorageConfig(BaseModel):
class LocalStorageConfig(BaseModel, allow_population_by_field_name=True):
"""Configuration for the local storage settings of the cluster.
Attributes:
Expand All @@ -266,7 +266,7 @@ class LocalStorageConfig(BaseModel):
set_default: Optional[bool] = Field(default=None, alias="set-default")


class NetworkConfig(BaseModel):
class NetworkConfig(BaseModel, allow_population_by_field_name=True):
"""Configuration for the network settings of the cluster.
Attributes:
Expand All @@ -276,7 +276,7 @@ class NetworkConfig(BaseModel):
enabled: Optional[bool] = Field(default=None)


class GatewayConfig(BaseModel):
class GatewayConfig(BaseModel, allow_population_by_field_name=True):
"""Configuration for the gateway settings of the cluster.
Attributes:
Expand All @@ -286,7 +286,7 @@ class GatewayConfig(BaseModel):
enabled: Optional[bool] = Field(default=None)


class MetricsServerConfig(BaseModel):
class MetricsServerConfig(BaseModel, allow_population_by_field_name=True):
"""Configuration for the metrics server settings of the cluster.
Attributes:
Expand Down
75 changes: 9 additions & 66 deletions charms/worker/k8s/src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,7 @@ def _bootstrap_k8s_snap(self):

bootstrap_config = BootstrapConfig.construct()
self._configure_datastore(bootstrap_config)
self._configure_cloud_provider(bootstrap_config)
self._configure_cluster_config(bootstrap_config)
bootstrap_config.cluster_config = self._assemble_cluster_config()
bootstrap_config.service_cidr = str(self.config["service-cidr"])
bootstrap_config.control_plane_taints = str(self.config["register-with-taints"]).split()
bootstrap_config.extra_sans = [_get_public_address()]
Expand Down Expand Up @@ -400,8 +399,8 @@ def _get_valid_annotations(self) -> Optional[dict]:

return annotations

def _get_cluster_config_from_charm_config(self) -> UserFacingClusterConfig:
"""Retrieve the cluster config from charm configuration.
def _assemble_cluster_config(self) -> UserFacingClusterConfig:
"""Retrieve the cluster config from charm configuration and charm relations.
Returns:
UserFacingClusterConfig: The expected cluster configuration.
Expand All @@ -414,50 +413,16 @@ def _get_cluster_config_from_charm_config(self) -> UserFacingClusterConfig:
# https://github.com/canonical/k8s-operator/pull/169/files#r1847378214
)

cloud_provider = None
if self.xcp.has_xcp:
cloud_provider = "external"

return UserFacingClusterConfig(
local_storage=local_storage,
annotations=self._get_valid_annotations(),
cloud_provider=cloud_provider,
)

def _configure_cluster_config(self, config: BootstrapConfig):
"""Configure the cluster config for the Canonical Kubernetes cluster.
Args:
config (BootstrapConfig): Configuration object to bootstrap the cluster.
"""
if not config.cluster_config:
config.cluster_config = self._get_cluster_config_from_charm_config()
else:
charm_cluster_config = self._get_cluster_config_from_charm_config()
# Merge the charm and user cluster configurations
# The existing configuration takes precedence over the charm configuration as
# this information is retrieved elsewhere (e.g. cloud-provider).
merged_data = {**charm_cluster_config.dict(), **config.cluster_config.dict()}
config.cluster_config = UserFacingClusterConfig(**merged_data)

@status.on_error(
ops.BlockedStatus("Invalid Cluster configuration"),
AssertionError,
)
def _update_cluster_config(self):
"""Update the cluster configuration for the Canonical Kubernetes cluster."""
status.add(ops.MaintenanceStatus("Updating cluster configuration"))
log.info("Updating cluster configuration")

charm_cluster_config = self._get_cluster_config_from_charm_config()
snap_cluster_config = self.api_manager.get_cluster_status().metadata.status.config

if snap_cluster_config == charm_cluster_config:
return

log.info(
"Snap config %s different from charm config %s. Updating snap config...",
snap_cluster_config,
charm_cluster_config,
)
update_request = UpdateClusterConfigRequest(config=charm_cluster_config)
self.api_manager.update_cluster_config(update_request)

def _configure_datastore(self, config: Union[BootstrapConfig, UpdateClusterConfigRequest]):
"""Configure the datastore for the Kubernetes cluster.
Expand Down Expand Up @@ -504,27 +469,6 @@ def _configure_datastore(self, config: Union[BootstrapConfig, UpdateClusterConfi
elif datastore == "dqlite":
log.info("Using dqlite as datastore")

def _configure_cloud_provider(
self, config: Union[BootstrapConfig, UpdateClusterConfigRequest]
):
"""Configure the cloud-provider for the Kubernetes cluster.
Args:
config (BootstrapConfig): The bootstrap configuration object for
the Kubernetes cluster that is being configured. This object
will be modified in-place.
"""
if self.xcp.has_xcp:
log.info("Using external as cloud-provider")
if isinstance(config, BootstrapConfig):
if not (ufcg := config.cluster_config):
ufcg = config.cluster_config = UserFacingClusterConfig()
elif isinstance(config, UpdateClusterConfigRequest):
if not (ufcg := config.config):
ufcg = config.config = UserFacingClusterConfig()

ufcg.cloud_provider = "external"

def _revoke_cluster_tokens(self, event: ops.EventBase):
"""Revoke tokens for the units in the cluster and k8s-cluster relations.
Expand Down Expand Up @@ -636,7 +580,7 @@ def _ensure_cluster_config(self):
update_request = UpdateClusterConfigRequest()

self._configure_datastore(update_request)
self._configure_cloud_provider(update_request)
update_request.config = self._assemble_cluster_config()
configure_kube_control(self)
self.api_manager.update_cluster_config(update_request)

Expand Down Expand Up @@ -815,7 +759,6 @@ def _reconcile(self, event: ops.EventBase):
self._k8s_info(event)
self._bootstrap_k8s_snap()
self._enable_functionalities()
self._update_cluster_config()
self._create_cluster_tokens()
self._create_cos_tokens()
self._apply_cos_requirements()
Expand Down
1 change: 0 additions & 1 deletion charms/worker/k8s/tests/unit/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ def mock_reconciler_handlers(harness):
handler_names |= {
"_bootstrap_k8s_snap",
"_enable_functionalities",
"_update_cluster_config",
"_create_cluster_tokens",
"_create_cos_tokens",
"_apply_cos_requirements",
Expand Down

0 comments on commit 17d8a37

Please sign in to comment.