Skip to content

Commit

Permalink
feat: add k8s preset, use k8s snap by default in dev preset
Browse files Browse the repository at this point in the history
  • Loading branch information
jnsgruk committed Oct 30, 2024
1 parent 8ac2249 commit abf03f9
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 26 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,12 @@ sudo concierge prepare -p dev
| Preset Name | Included |
| :---------: | :--------------------------------------------------------------- |
| `dev` | `juju`, `microk8s`, `lxd` `snapcraft`, `charmcraft`, `rockcraft` |
| `k8s` | `juju`, `k8s`, `lxd`, `rockcraft`, `charmcraft` |
| `microk8s` | `juju`, `microk8s`, `lxd`, `rockcraft`, `charmcraft` |
| `machine` | `juju`, `lxd`, `snapcraft`, `charmcraft` |

Note that in the `microk8s` preset, while `lxd` is installed, it is not bootstrapped. It is installed
and initialised with enough config such that `charmcraft` can use it as a build backend.
Note that in the `microk8s`/`k8s` presets, while `lxd` is installed, it is not bootstrapped. It is
installed and initialised with enough config such that `charmcraft` can use it as a build backend.

### Config File

Expand Down
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func parseConfig(configFile string) (*Config, error) {
func getOverrides(flags *pflag.FlagSet) ConfigOverrides {
return ConfigOverrides{
JujuChannel: envOrFlagString(flags, "juju-channel"),
K8sChannel: envOrFlagString(flags, "k8s-channel"),
MicroK8sChannel: envOrFlagString(flags, "microk8s-channel"),
LXDChannel: envOrFlagString(flags, "lxd-channel"),
CharmcraftChannel: envOrFlagString(flags, "charmcraft-channel"),
Expand Down
6 changes: 3 additions & 3 deletions internal/config/config_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type jujuConfig struct {

// providerConfig represents the set of providers to be configured and bootstrapped.
type providerConfig struct {
K8s K8sConfig `mapstructure:"k8s"`
K8s k8sConfig `mapstructure:"k8s"`
LXD lxdConfig `mapstructure:"lxd"`
Google googleConfig `mapstructure:"google"`
MicroK8s microk8sConfig `mapstructure:"microk8s"`
Expand Down Expand Up @@ -50,8 +50,8 @@ type microk8sConfig struct {
Addons []string `mapstructure:"addons"`
}

// K8sConfig represents how MicroK8s should be configured on the host.
type K8sConfig struct {
// k8sConfig represents how MicroK8s should be configured on the host.
type k8sConfig struct {
Enable bool `mapstructure:"enable"`
Bootstrap bool `mapstructure:"bootstrap"`
Channel string `mapstructure:"channel"`
Expand Down
34 changes: 32 additions & 2 deletions internal/config/presets.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import "fmt"
// Preset returns a configuration preset by name.
func Preset(preset string) (*Config, error) {
switch preset {
case "k8s":
return k8sPreset, nil
case "microk8s":
return microk8sPreset, nil
case "machine":
Expand Down Expand Up @@ -55,6 +57,19 @@ var defaultMicroK8sConfig microk8sConfig = microk8sConfig{
},
}

// defaultK8sConfig is the standard K8s config used throughout presets.
var defaultK8sConfig k8sConfig = k8sConfig{
Enable: true,
Bootstrap: true,
Features: map[string]map[string]string{
"load-balancer": {
"l2-mode": "true",
"cidrs": "10.43.45.0/28",
},
"local-storage": {},
},
}

// machinePreset is a configuration preset designed to be used when testing
// machine charms.
var machinePreset *Config = &Config{
Expand All @@ -68,6 +83,21 @@ var machinePreset *Config = &Config{
},
}

// k8sPreset is a configuration preset designed to be used when testing
// k8s charms.
var k8sPreset *Config = &Config{
Juju: defaultJujuConfig,
Providers: providerConfig{
// Enable LXD so charms can be built, but don't bootstrap onto it.
LXD: lxdConfig{Enable: true},
K8s: defaultK8sConfig,
},
Host: hostConfig{
Packages: defaultPackages,
Snaps: append(defaultSnaps, "rockcraft/latest/stable"),
},
}

// microk8sPreset is a configuration preset designed to be used when testing
// k8s charms.
var microk8sPreset *Config = &Config{
Expand All @@ -88,8 +118,8 @@ var microk8sPreset *Config = &Config{
var devPreset *Config = &Config{
Juju: defaultJujuConfig,
Providers: providerConfig{
LXD: defaultLXDConfig,
MicroK8s: defaultMicroK8sConfig,
LXD: defaultLXDConfig,
K8s: defaultK8sConfig,
},
Host: hostConfig{
Packages: defaultPackages,
Expand Down
7 changes: 6 additions & 1 deletion internal/providers/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ import (
"github.com/jnsgruk/concierge/internal/runner"
)

// Default channel from which K8s is installed.
const defaultK8sChannel = "1.31/candidate"

// NewK8s constructs a new K8s provider instance.
func NewK8s(runner runner.CommandRunner, config *config.Config) *K8s {
var channel string

if config.Overrides.K8sChannel != "" {
channel = config.Overrides.K8sChannel
} else {
} else if config.Providers.K8s.Channel != "" {
channel = config.Providers.K8s.Channel
} else {
channel = defaultK8sChannel
}

return &K8s{
Expand Down
6 changes: 3 additions & 3 deletions internal/providers/k8s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestNewK8s(t *testing.T) {
noOverrides := &config.Config{}

channelInConfig := &config.Config{}
channelInConfig.Providers.K8s.Channel = "1.31/candidate"
channelInConfig.Providers.K8s.Channel = "1.32/candidate"

overrides := &config.Config{}
overrides.Overrides.K8sChannel = "1.32/edge"
Expand All @@ -39,11 +39,11 @@ func TestNewK8s(t *testing.T) {
tests := []test{
{
config: noOverrides,
expected: &K8s{Channel: "", runner: runner},
expected: &K8s{Channel: "1.31/candidate", runner: runner},
},
{
config: channelInConfig,
expected: &K8s{Channel: "1.31/candidate", runner: runner},
expected: &K8s{Channel: "1.32/candidate", runner: runner},
},
{
config: overrides,
Expand Down
6 changes: 3 additions & 3 deletions internal/providers/microk8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

// Default channel from which MicroK8s is installed when the latest strict
// version cannot be determined.
const defaultChannel = "1.31-strict/stable"
const defaultMicroK8sChannel = "1.31-strict/stable"

// NewMicroK8s constructs a new MicroK8s provider instance.
func NewMicroK8s(runner runner.CommandRunner, config *config.Config) *MicroK8s {
Expand Down Expand Up @@ -201,12 +201,12 @@ func (m *MicroK8s) setupKubectl() error {
func computeDefaultChannel() string {
// If the snapd socket doesn't exist on the system, return a default value
if _, err := os.Stat("/run/snapd.socket"); errors.Is(err, os.ErrNotExist) {
return defaultChannel
return defaultMicroK8sChannel
}

snap, _, err := snapdClient.New(nil).FindOne("microk8s")
if err != nil {
return defaultChannel
return defaultMicroK8sChannel
}

keys := []string{}
Expand Down
6 changes: 3 additions & 3 deletions tests/overrides-env/task.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ execute: |
export CONCIERGE_ROCKCRAFT_CHANNEL=latest/edge
export CONCIERGE_SNAPCRAFT_CHANNEL=latest/edge
export CONCIERGE_LXD_CHANNEL=latest/candidate
export CONCIERGE_MICROK8S_CHANNEL=1.30-strict/candidate
export CONCIERGE_K8S_CHANNEL=1.31/beta
export CONCIERGE_EXTRA_SNAPS="node/22/stable"
export CONCIERGE_EXTRA_DEBS="make"
Expand All @@ -29,8 +29,8 @@ execute: |
list="$(snap list lxd)"
echo $list | MATCH latest/candidate
list="$(snap list microk8s)"
echo $list | MATCH 1.30-strict/candidate
list="$(snap list k8s)"
echo $list | MATCH 1.31/beta
list="$(snap list node)"
echo $list | MATCH 22/stable
Expand Down
15 changes: 7 additions & 8 deletions tests/preset-dev/task.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,23 @@ execute: |
"$SPREAD_PATH"/concierge --trace prepare -p dev
# Check that relevant snaps are installed
for s in juju microk8s lxd kubectl jq yq charmcraft rockcraft snapcraft; do
for s in juju k8s lxd kubectl jq yq charmcraft rockcraft snapcraft; do
snap list "$s" | MATCH $s
done
# Check the relevant debs are installed
command -v pip | MATCH /usr/bin/pip
python3 -m venv -h | head -n1 | grep -q -e "usage: venv"
# Ensure MicroK8s is configured as expected
sudo microk8s status --format yaml | yq '.addons[] | select(.name=="hostpath-storage") | .status'
sudo microk8s status --format yaml | yq '.addons[] | select(.name=="dns") | .status'
sudo microk8s status --format yaml | yq '.addons[] | select(.name=="metallb") | .status'
sudo k8s status --output-format yaml | yq '.dns.enabled' | MATCH true
sudo k8s status --output-format yaml | yq '.load-balancer.enabled' | MATCH true
sudo k8s status --output-format yaml | yq '.load-balancer.message' | MATCH "enabled, L2 mode"
sudo k8s get | yq '.load-balancer.cidrs' | MATCH "10.43.45.0/28"
# Ensure that kubectl was configured correctly
kubectl config current-context | MATCH "microk8s"
kubectl config current-context | MATCH "k8s"
# Ensure the juju controllers are bootstrapped and have models
for i in concierge-microk8s:admin/testing concierge-lxd:admin/testing; do
for i in concierge-k8s:admin/testing concierge-lxd:admin/testing; do
juju switch $i
juju model-defaults | grep test-mode | tr -s " " | MATCH "test-mode false true"
juju model-defaults | grep automatically-retry-hooks | tr -s " " | MATCH "automatically-retry-hooks true false"
Expand Down
40 changes: 40 additions & 0 deletions tests/preset-k8s/task.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
summary: Run concierge with the k8s preset
systems:
- ubuntu-24.04

execute: |
pushd "${SPREAD_PATH}/${SPREAD_TASK}"
"$SPREAD_PATH"/concierge --trace prepare -p k8s
# Check that relevant snaps are installed
for s in juju k8s kubectl jq yq charmcraft rockcraft; do
snap list "$s" | MATCH $s
done
# Check the relevant debs are installed
command -v pip | MATCH /usr/bin/pip
python3 -m venv -h | head -n1 | grep -q -e "usage: venv"
sudo k8s status --output-format yaml | yq '.dns.enabled' | MATCH true
sudo k8s status --output-format yaml | yq '.load-balancer.enabled' | MATCH true
sudo k8s status --output-format yaml | yq '.load-balancer.message' | MATCH "enabled, L2 mode"
sudo k8s get | yq '.load-balancer.cidrs' | MATCH "10.43.45.0/28"
kubectl config current-context | MATCH "k8s"
juju controllers | tail -n1 | MATCH concierge-k8s
juju models | tail -n1 | MATCH testing
# Ensure the juju controller is bootstrapped and has models
juju switch concierge-k8s:admin/testing
juju model-defaults | grep test-mode | tr -s " " | MATCH "test-mode false true"
juju model-defaults | grep automatically-retry-hooks | tr -s " " | MATCH "automatically-retry-hooks true false"
# Check that even though we installed/initialised LXD, we didn't bootstrap it
juju controllers | NOMATCH lxd-concierge
restore: |
if [[ -z "${CI:-}" ]]; then
"$SPREAD_PATH"/concierge --trace restore
fi
2 changes: 1 addition & 1 deletion tests/restore/task.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ execute: |
# Check that relevant snaps are removed
list="$(snap list)"
for s in juju microk8s lxd kubectl jq yq charmcraft rockcraft snapcraft; do
for s in juju k8s lxd kubectl jq yq charmcraft rockcraft snapcraft; do
echo "$list" | NOMATCH "$s"
done
Expand Down

0 comments on commit abf03f9

Please sign in to comment.