From a985f769e76660339a7fe7cc2dfb4a9a824fe498 Mon Sep 17 00:00:00 2001 From: Berkay Tekin Oz Date: Fri, 24 May 2024 16:04:29 +0300 Subject: [PATCH 1/3] Use k8sd backend and sync by default --- snap/hooks/configure | 4 +++- snap/hooks/install | 2 -- src/k8s/cmd/k8s/k8s_x_snapd_config.go | 2 +- src/k8s/pkg/utils/experimental/snapdconfig/k8s_to_snapd.go | 2 +- src/k8s/pkg/utils/experimental/snapdconfig/meta.go | 4 ++-- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/snap/hooks/configure b/snap/hooks/configure index e428bddc6..b153e19c4 100755 --- a/snap/hooks/configure +++ b/snap/hooks/configure @@ -4,4 +4,6 @@ k8s::common::setup_env -k8s::cmd::k8s x-snapd-config reconcile +if [ -e "$SNAP_COMMON/var/lib/k8sd/state/control.socket" ]; then + k8s::cmd::k8s x-snapd-config reconcile +fi diff --git a/snap/hooks/install b/snap/hooks/install index 24dc9c6d5..4ecea6b3c 100755 --- a/snap/hooks/install +++ b/snap/hooks/install @@ -4,8 +4,6 @@ k8s::common::setup_env -# disable snap set/get by default -k8s::cmd::k8s x-snapd-config disable # k8s has a REST interface to initialize a cluster. # In order to interact with the REST API the k8sd service diff --git a/src/k8s/cmd/k8s/k8s_x_snapd_config.go b/src/k8s/cmd/k8s/k8s_x_snapd_config.go index c6ee24b8c..6e56a0b23 100644 --- a/src/k8s/cmd/k8s/k8s_x_snapd_config.go +++ b/src/k8s/cmd/k8s/k8s_x_snapd_config.go @@ -78,7 +78,7 @@ func newXSnapdConfigCmd(env cmdutil.ExecutionEnvironment) *cobra.Command { } } - mode.Orb = "snapd" + mode.Orb = "k8sd" if err := snapdconfig.SetMeta(cmd.Context(), env.Snap, mode); err != nil { cmd.PrintErrf("Error: failed to set snapd configuration: %v\n", err) env.Exit(1) diff --git a/src/k8s/pkg/utils/experimental/snapdconfig/k8s_to_snapd.go b/src/k8s/pkg/utils/experimental/snapdconfig/k8s_to_snapd.go index b871b5e4b..de1459ee3 100644 --- a/src/k8s/pkg/utils/experimental/snapdconfig/k8s_to_snapd.go +++ b/src/k8s/pkg/utils/experimental/snapdconfig/k8s_to_snapd.go @@ -13,7 +13,7 @@ import ( func SetSnapdFromK8sd(ctx context.Context, config apiv1.UserFacingClusterConfig, snap snap.Snap) error { var sets []string for key, cfg := range map[string]any{ - "meta": Meta{Orb: "snapd", APIVersion: "1.30"}, + "meta": Meta{Orb: "k8sd", APIVersion: "1.30"}, "dns": config.DNS, "network": config.Network, "local-storage": config.LocalStorage, diff --git a/src/k8s/pkg/utils/experimental/snapdconfig/meta.go b/src/k8s/pkg/utils/experimental/snapdconfig/meta.go index 74f20d8f4..3926daabe 100644 --- a/src/k8s/pkg/utils/experimental/snapdconfig/meta.go +++ b/src/k8s/pkg/utils/experimental/snapdconfig/meta.go @@ -33,9 +33,9 @@ func ParseMeta(ctx context.Context, s snap.Snap) (Meta, bool, error) { return Meta{}, false, fmt.Errorf("failed to parse snap config mode: %w", err) } - // default meta.orb is none + // default meta.orb is k8sd if parse.Meta.Orb == "" { - parse.Meta.Orb = "none" + parse.Meta.Orb = "k8sd" } switch parse.Meta.Orb { case "k8sd", "snapd", "none": From e6279adfe30fcaf4f8416d58f84ab1b43769e687 Mon Sep 17 00:00:00 2001 From: Berkay Tekin Oz Date: Tue, 28 May 2024 09:08:35 +0300 Subject: [PATCH 2/3] Adjusting changes, moving enablement to bootstrap/join hooks --- snap/hooks/configure | 4 +--- snap/hooks/install | 2 ++ src/k8s/cmd/k8s/k8s_x_snapd_config.go | 2 +- src/k8s/pkg/k8sd/app/hooks_bootstrap.go | 5 +++++ src/k8s/pkg/k8sd/app/hooks_join.go | 5 +++++ src/k8s/pkg/utils/experimental/snapdconfig/k8s_to_snapd.go | 2 +- 6 files changed, 15 insertions(+), 5 deletions(-) diff --git a/snap/hooks/configure b/snap/hooks/configure index b153e19c4..e428bddc6 100755 --- a/snap/hooks/configure +++ b/snap/hooks/configure @@ -4,6 +4,4 @@ k8s::common::setup_env -if [ -e "$SNAP_COMMON/var/lib/k8sd/state/control.socket" ]; then - k8s::cmd::k8s x-snapd-config reconcile -fi +k8s::cmd::k8s x-snapd-config reconcile diff --git a/snap/hooks/install b/snap/hooks/install index 4ecea6b3c..24dc9c6d5 100755 --- a/snap/hooks/install +++ b/snap/hooks/install @@ -4,6 +4,8 @@ k8s::common::setup_env +# disable snap set/get by default +k8s::cmd::k8s x-snapd-config disable # k8s has a REST interface to initialize a cluster. # In order to interact with the REST API the k8sd service diff --git a/src/k8s/cmd/k8s/k8s_x_snapd_config.go b/src/k8s/cmd/k8s/k8s_x_snapd_config.go index 6e56a0b23..c6ee24b8c 100644 --- a/src/k8s/cmd/k8s/k8s_x_snapd_config.go +++ b/src/k8s/cmd/k8s/k8s_x_snapd_config.go @@ -78,7 +78,7 @@ func newXSnapdConfigCmd(env cmdutil.ExecutionEnvironment) *cobra.Command { } } - mode.Orb = "k8sd" + mode.Orb = "snapd" if err := snapdconfig.SetMeta(cmd.Context(), env.Snap, mode); err != nil { cmd.PrintErrf("Error: failed to set snapd configuration: %v\n", err) env.Exit(1) diff --git a/src/k8s/pkg/k8sd/app/hooks_bootstrap.go b/src/k8s/pkg/k8sd/app/hooks_bootstrap.go index 001dc2af0..41be0ccc7 100644 --- a/src/k8s/pkg/k8sd/app/hooks_bootstrap.go +++ b/src/k8s/pkg/k8sd/app/hooks_bootstrap.go @@ -18,6 +18,7 @@ import ( "github.com/canonical/k8s/pkg/k8sd/types" snaputil "github.com/canonical/k8s/pkg/snap/util" "github.com/canonical/k8s/pkg/utils" + "github.com/canonical/k8s/pkg/utils/experimental/snapdconfig" "github.com/canonical/microcluster/state" ) @@ -334,6 +335,10 @@ func (a *App) onBootstrapControlPlane(s *state.State, bootstrapConfig apiv1.Boot return fmt.Errorf("database transaction to update cluster configuration failed: %w", err) } + if err := snapdconfig.SetSnapdFromK8sd(s.Context, cfg.ToUserFacing(), snap); err != nil { + return fmt.Errorf("failed to set snapd configuration from k8sd: %w", err) + } + // Start services if err := startControlPlaneServices(s.Context, snap, cfg.Datastore.GetType()); err != nil { return fmt.Errorf("failed to start services: %w", err) diff --git a/src/k8s/pkg/k8sd/app/hooks_join.go b/src/k8s/pkg/k8sd/app/hooks_join.go index 430a10d91..90e896c3c 100644 --- a/src/k8s/pkg/k8sd/app/hooks_join.go +++ b/src/k8s/pkg/k8sd/app/hooks_join.go @@ -9,6 +9,7 @@ import ( "github.com/canonical/k8s/pkg/k8sd/pki" "github.com/canonical/k8s/pkg/k8sd/setup" "github.com/canonical/k8s/pkg/utils" + "github.com/canonical/k8s/pkg/utils/experimental/snapdconfig" "github.com/canonical/microcluster/state" ) @@ -144,6 +145,10 @@ func (a *App) onPostJoin(s *state.State, initConfig map[string]string) error { return fmt.Errorf("failed to configure services: %w", err) } + if err := snapdconfig.SetSnapdFromK8sd(s.Context, cfg.ToUserFacing(), snap); err != nil { + return fmt.Errorf("failed to set snapd configuration from k8sd: %w", err) + } + // Start services if err := startControlPlaneServices(s.Context, snap, cfg.Datastore.GetType()); err != nil { return fmt.Errorf("failed to start services: %w", err) diff --git a/src/k8s/pkg/utils/experimental/snapdconfig/k8s_to_snapd.go b/src/k8s/pkg/utils/experimental/snapdconfig/k8s_to_snapd.go index de1459ee3..b871b5e4b 100644 --- a/src/k8s/pkg/utils/experimental/snapdconfig/k8s_to_snapd.go +++ b/src/k8s/pkg/utils/experimental/snapdconfig/k8s_to_snapd.go @@ -13,7 +13,7 @@ import ( func SetSnapdFromK8sd(ctx context.Context, config apiv1.UserFacingClusterConfig, snap snap.Snap) error { var sets []string for key, cfg := range map[string]any{ - "meta": Meta{Orb: "k8sd", APIVersion: "1.30"}, + "meta": Meta{Orb: "snapd", APIVersion: "1.30"}, "dns": config.DNS, "network": config.Network, "local-storage": config.LocalStorage, From 0db5923c90c67b9a457eae19d2e1f7284ae6c8e0 Mon Sep 17 00:00:00 2001 From: Berkay Tekin Oz Date: Tue, 28 May 2024 13:50:06 +0300 Subject: [PATCH 3/3] Revert default orb setting in ParseMeta --- src/k8s/pkg/utils/experimental/snapdconfig/meta.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/k8s/pkg/utils/experimental/snapdconfig/meta.go b/src/k8s/pkg/utils/experimental/snapdconfig/meta.go index 3926daabe..74f20d8f4 100644 --- a/src/k8s/pkg/utils/experimental/snapdconfig/meta.go +++ b/src/k8s/pkg/utils/experimental/snapdconfig/meta.go @@ -33,9 +33,9 @@ func ParseMeta(ctx context.Context, s snap.Snap) (Meta, bool, error) { return Meta{}, false, fmt.Errorf("failed to parse snap config mode: %w", err) } - // default meta.orb is k8sd + // default meta.orb is none if parse.Meta.Orb == "" { - parse.Meta.Orb = "k8sd" + parse.Meta.Orb = "none" } switch parse.Meta.Orb { case "k8sd", "snapd", "none":