-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow changing cluster configuration through
snap set k8s
(#399)
- Loading branch information
1 parent
db5015e
commit 1ee1537
Showing
16 changed files
with
320 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/bin/bash -e | ||
|
||
. $SNAP/k8s/lib.sh | ||
|
||
k8s::common::setup_env | ||
|
||
k8s::cmd::k8s x-snapd-config reconcile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package k8s | ||
|
||
import ( | ||
apiv1 "github.com/canonical/k8s/api/v1" | ||
cmdutil "github.com/canonical/k8s/cmd/util" | ||
"github.com/canonical/k8s/pkg/utils/experimental/snapdconfig" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func newXSnapdConfigCmd(env cmdutil.ExecutionEnvironment) *cobra.Command { | ||
disableCmd := &cobra.Command{ | ||
Use: "disable", | ||
Short: "Disable the use of snap get/set to manage the cluster configuration", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if err := snapdconfig.Disable(cmd.Context(), env.Snap); err != nil { | ||
cmd.PrintErrf("Error: failed to disable snapd configuration: %v\n", err) | ||
env.Exit(1) | ||
} | ||
}, | ||
} | ||
reconcileCmd := &cobra.Command{ | ||
Use: "reconcile", | ||
Short: "Reconcile the cluster configuration changes from k8s {set,get} <-> snap {set,get} k8s", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
mode, empty, err := snapdconfig.ParseMeta(cmd.Context(), env.Snap) | ||
if err != nil { | ||
if !empty { | ||
cmd.PrintErrf("Error: failed to parse meta configuration: %v\n", err) | ||
env.Exit(1) | ||
return | ||
} | ||
|
||
cmd.PrintErrf("Warning: failed to parse meta configuration: %v\n", err) | ||
cmd.PrintErrf("Warning: ignoring further errors to prevent infinite loop\n") | ||
if setErr := snapdconfig.SetMeta(cmd.Context(), env.Snap, snapdconfig.Meta{ | ||
APIVersion: "1.30", | ||
Orb: "none", | ||
Error: err.Error(), | ||
}); setErr != nil { | ||
cmd.PrintErrf("Warning: failed to set meta configuration to safe defaults: %v\n", setErr) | ||
} | ||
return | ||
} | ||
|
||
switch mode.Orb { | ||
case "none": | ||
cmd.PrintErrln("Warning: meta.orb is none, skipping reconcile actions") | ||
return | ||
case "k8sd": | ||
client, err := env.Client(cmd.Context()) | ||
if err != nil { | ||
cmd.PrintErrf("Error: failed to create k8sd client: %v\n", err) | ||
env.Exit(1) | ||
return | ||
} | ||
config, err := client.GetClusterConfig(cmd.Context(), apiv1.GetClusterConfigRequest{}) | ||
if err != nil { | ||
cmd.PrintErrf("Error: failed to retrieve cluster configuration: %v\n", err) | ||
env.Exit(1) | ||
return | ||
} | ||
if err := snapdconfig.SetSnapdFromK8sd(cmd.Context(), config, env.Snap); err != nil { | ||
cmd.PrintErrf("Error: failed to update snapd state: %v\n", err) | ||
env.Exit(1) | ||
return | ||
} | ||
case "snapd": | ||
client, err := env.Client(cmd.Context()) | ||
if err != nil { | ||
cmd.PrintErrf("Error: failed to create k8sd client: %v\n", err) | ||
env.Exit(1) | ||
return | ||
} | ||
if err := snapdconfig.SetK8sdFromSnapd(cmd.Context(), client, env.Snap); err != nil { | ||
cmd.PrintErrf("Error: failed to update k8sd state: %v\n", err) | ||
env.Exit(1) | ||
return | ||
} | ||
} | ||
|
||
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) | ||
return | ||
} | ||
}, | ||
} | ||
cmd := &cobra.Command{ | ||
Use: "x-snapd-config", | ||
Short: "Manage snapd configuration", | ||
Hidden: true, | ||
} | ||
|
||
cmd.AddCommand(reconcileCmd) | ||
cmd.AddCommand(disableCmd) | ||
|
||
return cmd | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package snapdconfig | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/canonical/k8s/pkg/snap" | ||
) | ||
|
||
func Disable(ctx context.Context, s snap.Snap) error { | ||
b, err := json.Marshal(Meta{Orb: "none", APIVersion: "1.30"}) | ||
if err != nil { | ||
return fmt.Errorf("failed to marshal config: %w", err) | ||
} | ||
if err := s.SnapctlSet(ctx, fmt.Sprintf("meta=%s", string(b)), "dns!", "network!", "gateway!", "ingress!", "load-balancer!", "local-storage!"); err != nil { | ||
return fmt.Errorf("failed to snapctl set: %w", err) | ||
} | ||
return nil | ||
} |
36 changes: 36 additions & 0 deletions
36
src/k8s/pkg/utils/experimental/snapdconfig/k8s_to_snapd.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package snapdconfig | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
|
||
apiv1 "github.com/canonical/k8s/api/v1" | ||
"github.com/canonical/k8s/pkg/snap" | ||
) | ||
|
||
// SetSnapdFromK8sd uses snapctl to update the local snapd configuration with the new k8sd cluster configuration. | ||
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"}, | ||
"dns": config.DNS, | ||
"network": config.Network, | ||
"local-storage": config.LocalStorage, | ||
"load-balancer": config.LoadBalancer, | ||
"ingress": config.Ingress, | ||
"gateway": config.Gateway, | ||
} { | ||
b, err := json.Marshal(cfg) | ||
if err != nil { | ||
return fmt.Errorf("failed to marshal %s config: %w", key, err) | ||
} | ||
sets = append(sets, fmt.Sprintf("%s=%s", key, string(b))) | ||
} | ||
|
||
if err := snap.SnapctlSet(ctx, sets...); err != nil { | ||
return fmt.Errorf("failed to set snapd configuration: %w", err) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.