Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subordinate charm config PoC #160

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion charms/worker/k8s/charmcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ config:
type: string
description: |
Labels can be used to organize and to select subsets of nodes in the
cluster. Declare node labels in key=value format, separated by spaces.
cluster. Declare node labels in key=value format, separated by spaces.
register-with-taints:
type: string
default: ""
Expand Down Expand Up @@ -178,6 +178,9 @@ provides:
interface: containerd
ceph-k8s-info:
interface: kubernetes-info
feature:
interface: k8s-snap-feature
scope: container

requires:
etcd:
Expand Down
39 changes: 39 additions & 0 deletions charms/worker/k8s/src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import socket
import subprocess
import uuid
import json
from functools import cached_property
from pathlib import Path
from time import sleep
Expand All @@ -38,6 +39,7 @@
from charms.interface_external_cloud_provider import ExternalCloudProvider
from charms.k8s.v0.k8sd_api_manager import (
BootstrapConfig,
LoadBalancerConfig,
ControlPlaneNodeJoinConfig,
CreateClusterRequest,
DNSConfig,
Expand Down Expand Up @@ -149,10 +151,47 @@ def __init__(self, *args):
)

self.framework.observe(self.on.update_status, self._on_update_status)
self.framework.observe(self.on.feature_relation_changed, self._on_feature_relation_changed)

eaudetcobello marked this conversation as resolved.
Show resolved Hide resolved
if self.is_control_plane:
self.etcd = EtcdReactiveRequires(self)
self.framework.observe(self.on.get_kubeconfig_action, self._get_external_kubeconfig)

def _on_feature_relation_changed(self, event: ops.RelationChangedEvent):
eaudetcobello marked this conversation as resolved.
Show resolved Hide resolved
relation_data = event.relation.data.get(event.unit) #type: ignore
if not relation_data:
log.warning("No relation data found for unit %s", event.unit)
return

eaudetcobello marked this conversation as resolved.
Show resolved Hide resolved
feature_name = relation_data.get("feature-name")
feature_version = relation_data.get("feature-version") # library version
feature_attributes = relation_data.get("feature-attributes")
eaudetcobello marked this conversation as resolved.
Show resolved Hide resolved

feature_config_classes: Dict[str, type] = {
"load-balancer": LoadBalancerConfig,
"local-storage": LocalStorageConfig,
}

if feature_name and feature_version and feature_attributes:
feature_attributes = json.loads(feature_attributes)

config_class = feature_config_classes.get(feature_name)
if config_class:
feature_config = config_class(**feature_attributes)
eaudetcobello marked this conversation as resolved.
Show resolved Hide resolved

feature_name = feature_name.replace("-", "_")
config = UserFacingClusterConfig(**{feature_name: feature_config})
update_request = UpdateClusterConfigRequest(config=config)
self.api_manager.update_cluster_config(update_request)

log.info("Got feature of type [%s], with version [%s] and attributes [%s]", feature_name, feature_version, json.dumps(feature_config.dict()))
else:
log.warning("Unsupported feature name: %s", feature_name)
eaudetcobello marked this conversation as resolved.
Show resolved Hide resolved
else:
log.warning("Feature relation data is incomplete: %s", relation_data)

log.info("Relation %s changed: %s", event.relation.name, relation_data)

def _k8s_info(self, event: ops.EventBase):
"""Send cluster information on the kubernetes-info relation.

Expand Down
28 changes: 28 additions & 0 deletions redeploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
set -x

Check failure on line 1 in redeploy.sh

View workflow job for this annotation

GitHub Actions / unit-tests / Shell scripts lint

Tips depend on target shell and yours is unknown. Add a shebang or a 'shell' directive.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
set -x
# Copyright 2024 Canonical Ltd.
# See LICENSE file for licensing details.
set -x

eaudetcobello marked this conversation as resolved.
Show resolved Hide resolved

DIR=$(realpath $(dirname "${0}"))

Check warning on line 3 in redeploy.sh

View workflow job for this annotation

GitHub Actions / unit-tests / Shell scripts lint

Quote this to prevent word splitting.
LB_DIR=$DIR/../k8s-load-balancer

echo "Run charmcraft pack for k8s"
charmcraft pack -p charms/worker/k8s

echo "Run charmcraft pack for k8s-load-balancer"
cd $LB_DIR

Check warning on line 10 in redeploy.sh

View workflow job for this annotation

GitHub Actions / unit-tests / Shell scripts lint

Use 'cd ... || exit' or 'cd ... || return' in case cd fails.
charmcraft pack

cd $DIR

Check warning on line 13 in redeploy.sh

View workflow job for this annotation

GitHub Actions / unit-tests / Shell scripts lint

Use 'cd ... || exit' or 'cd ... || return' in case cd fails.

echo "Removing k8s k8s-load-balancer relation..."
juju remove-relation k8s k8s-load-balancer

echo "Removing k8s-load-balancer application..."
juju remove-application k8s-load-balancer

echo "Refresh k8s application with new charm"
juju refresh k8s --switch $DIR/k8s_ubuntu-20.04-amd64_ubuntu-22.04-amd64_ubuntu-24.04-amd64.charm --force-units --force

echo "Refresh k8s-load-balancer application with new charm"
juju deploy $LB_DIR/k8s-load-balancer_ubuntu-22.04-amd64.charm

echo "Add relation"
juju integrate k8s k8s-load-balancer
Loading