Skip to content

Commit

Permalink
Don't store the state in the charm
Browse files Browse the repository at this point in the history
  • Loading branch information
bschimke95 committed Nov 19, 2024
1 parent e2293e8 commit 309efb5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
15 changes: 6 additions & 9 deletions charms/worker/k8s/charmcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -161,32 +161,29 @@ config:
CIDR to use for Kubernetes services. After deployment it is
only possible to increase the size of the IP range. It is not possible to
change or shrink the address range after deployment.
local_storage_enabled:
local-storage-enabled:
type: boolean
default: true
description: |
Enable local storage provisioning. This will create a storage class
named "local-storage" that uses the hostPath provisioner. This is
useful for development and testing purposes. It is not recommended for
production use.
local_storage_local_path:
local-storage-local-path:
type: string
default: ""
default: "/var/snap/k8s/common/rawfile-storage"
description: |
The path on the host where local storage will be provisioned. This
path must be writable by the kubelet. This is only used if
local-storage.enabled is set to true.
local_storage_reclaim_policy:
local-storage-reclaim-policy:
type: string
default: Delete
description: |
The reclaim policy for local storage. This can be either "Delete" or
"Retain". If set to "Delete", the storage will be deleted when the
PersistentVolumeClaim is deleted. If set to "Retain", the storage will
be retained when the PersistentVolumeClaim is deleted.
local_storage_set_default:
type: boolean
description: |
Set the local storage class as the default storage class. This is only
used if local-storage.enabled is set to true.
actions:
get-kubeconfig:
Expand Down
25 changes: 15 additions & 10 deletions charms/worker/k8s/src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def __init__(self, *args):
self.labeller = LabelMaker(
self, kubeconfig_path=self._internal_kubeconfig, kubectl=KUBECTL_PATH
)
self._stored.set_default(is_dying=False, cluster_name=str(), cluster_config={})
self._stored.set_default(is_dying=False, cluster_name=str())

self.cos_agent = COSAgentProvider(
self,
Expand Down Expand Up @@ -379,10 +379,11 @@ def _get_cluster_config_from_charm_config(self) -> UserFacingClusterConfig:
UserFacingClusterConfig: The expected cluster configuration.
"""
local_storage = LocalStorageConfig(
enabled=self.config.get("local_storage_enabled"),
local_path=self.config.get("local_storage_local_path"),
reclaim_policy=self.config.get("local_storage_reclaim_policy"),
set_default=self.config.get("local_storage_set_default"),
enabled=self.config.get("local-storage-enabled"),
local_path=self.config.get("local-storage-local-path"),
reclaim_policy=self.config.get("local-storage-reclaim-policy"),
# Note(ben): set_default is intentionally omitted, see:
# https://github.com/canonical/k8s-operator/pull/169/files#r1847378214
)

return UserFacingClusterConfig(
Expand Down Expand Up @@ -415,16 +416,20 @@ def _update_cluster_config(self):
status.add(ops.MaintenanceStatus("Updating cluster configuration"))
log.info("Updating cluster configuration")

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

if self._stored.cluster_config == cluster_config:
if snap_cluster_config == charm_cluster_config:
return

update_request = UpdateClusterConfigRequest(config=cluster_config)
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)

self._stored.cluster_config = cluster_config

def _configure_datastore(self, config: Union[BootstrapConfig, UpdateClusterConfigRequest]):
"""Configure the datastore for the Kubernetes cluster.
Expand Down

0 comments on commit 309efb5

Please sign in to comment.