Skip to content

Commit 318b852

Browse files
committed
feat: add k8s preset
1 parent 8ac2249 commit 318b852

File tree

8 files changed

+89
-12
lines changed

8 files changed

+89
-12
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,12 @@ sudo concierge prepare -p dev
113113
| Preset Name | Included |
114114
| :---------: | :--------------------------------------------------------------- |
115115
| `dev` | `juju`, `microk8s`, `lxd` `snapcraft`, `charmcraft`, `rockcraft` |
116+
| `k8s` | `juju`, `k8s`, `lxd`, `rockcraft`, `charmcraft` |
116117
| `microk8s` | `juju`, `microk8s`, `lxd`, `rockcraft`, `charmcraft` |
117118
| `machine` | `juju`, `lxd`, `snapcraft`, `charmcraft` |
118119

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

122123
### Config File
123124

internal/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ func parseConfig(configFile string) (*Config, error) {
106106
func getOverrides(flags *pflag.FlagSet) ConfigOverrides {
107107
return ConfigOverrides{
108108
JujuChannel: envOrFlagString(flags, "juju-channel"),
109+
K8sChannel: envOrFlagString(flags, "k8s-channel"),
109110
MicroK8sChannel: envOrFlagString(flags, "microk8s-channel"),
110111
LXDChannel: envOrFlagString(flags, "lxd-channel"),
111112
CharmcraftChannel: envOrFlagString(flags, "charmcraft-channel"),

internal/config/config_format.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type jujuConfig struct {
2222

2323
// providerConfig represents the set of providers to be configured and bootstrapped.
2424
type providerConfig struct {
25-
K8s K8sConfig `mapstructure:"k8s"`
25+
K8s k8sConfig `mapstructure:"k8s"`
2626
LXD lxdConfig `mapstructure:"lxd"`
2727
Google googleConfig `mapstructure:"google"`
2828
MicroK8s microk8sConfig `mapstructure:"microk8s"`
@@ -50,8 +50,8 @@ type microk8sConfig struct {
5050
Addons []string `mapstructure:"addons"`
5151
}
5252

53-
// K8sConfig represents how MicroK8s should be configured on the host.
54-
type K8sConfig struct {
53+
// k8sConfig represents how MicroK8s should be configured on the host.
54+
type k8sConfig struct {
5555
Enable bool `mapstructure:"enable"`
5656
Bootstrap bool `mapstructure:"bootstrap"`
5757
Channel string `mapstructure:"channel"`

internal/config/presets.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import "fmt"
55
// Preset returns a configuration preset by name.
66
func Preset(preset string) (*Config, error) {
77
switch preset {
8+
case "k8s":
9+
return k8sPreset, nil
810
case "microk8s":
911
return microk8sPreset, nil
1012
case "machine":
@@ -55,6 +57,19 @@ var defaultMicroK8sConfig microk8sConfig = microk8sConfig{
5557
},
5658
}
5759

60+
// defaultK8sConfig is the standard K8s config used throughout presets.
61+
var defaultK8sConfig k8sConfig = k8sConfig{
62+
Enable: true,
63+
Bootstrap: true,
64+
Features: map[string]map[string]string{
65+
"load-balancer": {
66+
"l2-mode": "true",
67+
"cidrs": "10.43.45.0/28",
68+
},
69+
"local-storage": {},
70+
},
71+
}
72+
5873
// machinePreset is a configuration preset designed to be used when testing
5974
// machine charms.
6075
var machinePreset *Config = &Config{
@@ -68,6 +83,21 @@ var machinePreset *Config = &Config{
6883
},
6984
}
7085

86+
// k8sPreset is a configuration preset designed to be used when testing
87+
// k8s charms.
88+
var k8sPreset *Config = &Config{
89+
Juju: defaultJujuConfig,
90+
Providers: providerConfig{
91+
// Enable LXD so charms can be built, but don't bootstrap onto it.
92+
LXD: lxdConfig{Enable: true},
93+
K8s: defaultK8sConfig,
94+
},
95+
Host: hostConfig{
96+
Packages: defaultPackages,
97+
Snaps: append(defaultSnaps, "rockcraft/latest/stable"),
98+
},
99+
}
100+
71101
// microk8sPreset is a configuration preset designed to be used when testing
72102
// k8s charms.
73103
var microk8sPreset *Config = &Config{

internal/providers/k8s.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,19 @@ import (
1111
"github.com/jnsgruk/concierge/internal/runner"
1212
)
1313

14+
// Default channel from which K8s is installed.
15+
const defaultK8sChannel = "1.31/candidate"
16+
1417
// NewK8s constructs a new K8s provider instance.
1518
func NewK8s(runner runner.CommandRunner, config *config.Config) *K8s {
1619
var channel string
1720

1821
if config.Overrides.K8sChannel != "" {
1922
channel = config.Overrides.K8sChannel
20-
} else {
23+
} else if config.Providers.K8s.Channel != "" {
2124
channel = config.Providers.K8s.Channel
25+
} else {
26+
channel = defaultK8sChannel
2227
}
2328

2429
return &K8s{

internal/providers/k8s_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestNewK8s(t *testing.T) {
2828
noOverrides := &config.Config{}
2929

3030
channelInConfig := &config.Config{}
31-
channelInConfig.Providers.K8s.Channel = "1.31/candidate"
31+
channelInConfig.Providers.K8s.Channel = "1.32/candidate"
3232

3333
overrides := &config.Config{}
3434
overrides.Overrides.K8sChannel = "1.32/edge"
@@ -39,11 +39,11 @@ func TestNewK8s(t *testing.T) {
3939
tests := []test{
4040
{
4141
config: noOverrides,
42-
expected: &K8s{Channel: "", runner: runner},
42+
expected: &K8s{Channel: "1.31/candidate", runner: runner},
4343
},
4444
{
4545
config: channelInConfig,
46-
expected: &K8s{Channel: "1.31/candidate", runner: runner},
46+
expected: &K8s{Channel: "1.32/candidate", runner: runner},
4747
},
4848
{
4949
config: overrides,

internal/providers/microk8s.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818

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

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

207207
snap, _, err := snapdClient.New(nil).FindOne("microk8s")
208208
if err != nil {
209-
return defaultChannel
209+
return defaultMicroK8sChannel
210210
}
211211

212212
keys := []string{}

tests/preset-k8s/task.yaml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
summary: Run concierge with the k8s preset
2+
systems:
3+
- ubuntu-24.04
4+
5+
execute: |
6+
pushd "${SPREAD_PATH}/${SPREAD_TASK}"
7+
8+
"$SPREAD_PATH"/concierge --trace prepare -p k8s
9+
10+
# Check that relevant snaps are installed
11+
for s in juju k8s kubectl jq yq charmcraft rockcraft; do
12+
snap list "$s" | MATCH $s
13+
done
14+
15+
# Check the relevant debs are installed
16+
command -v pip | MATCH /usr/bin/pip
17+
python3 -m venv -h | head -n1 | grep -q -e "usage: venv"
18+
19+
sudo k8s status --output-format yaml | yq '.dns.enabled' | MATCH true
20+
sudo k8s status --output-format yaml | yq '.load-balancer.enabled' | MATCH true
21+
sudo k8s status --output-format yaml | yq '.load-balancer.message' | MATCH "enabled, L2 mode"
22+
sudo k8s get | yq '.load-balancer.cidrs' | MATCH "10.43.45.0/28"
23+
24+
kubectl config current-context | MATCH "k8s"
25+
26+
juju controllers | tail -n1 | MATCH concierge-k8s
27+
juju models | tail -n1 | MATCH testing
28+
29+
# Ensure the juju controller is bootstrapped and has models
30+
juju switch concierge-k8s:admin/testing
31+
juju model-defaults | grep test-mode | tr -s " " | MATCH "test-mode false true"
32+
juju model-defaults | grep automatically-retry-hooks | tr -s " " | MATCH "automatically-retry-hooks true false"
33+
34+
# Check that even though we installed/initialised LXD, we didn't bootstrap it
35+
juju controllers | NOMATCH lxd-concierge
36+
37+
restore: |
38+
if [[ -z "${CI:-}" ]]; then
39+
"$SPREAD_PATH"/concierge --trace restore
40+
fi

0 commit comments

Comments
 (0)