-
Notifications
You must be signed in to change notification settings - Fork 10
feat: store presets as YAML rather than Go #152
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
Merged
tonyandrewmeyer
merged 12 commits into
canonical:main
from
tonyandrewmeyer:feat-presets-as-yaml
Feb 26, 2026
Merged
Changes from 3 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c4e531f
feat: store the presets as YAML rather than Go
tonyandrewmeyer 8206b21
Update .goreleaser.yaml
tonyandrewmeyer dab3a50
Address review comments.
tonyandrewmeyer 46374c0
Remove pointless test.
tonyandrewmeyer 2145dd1
Snaps with no explicit channel default to latest/stable, via snapd be…
tonyandrewmeyer eafcdf8
Update README.
tonyandrewmeyer 31f4330
Fix optional channel by converting nil to an empty map before unmarsh…
tonyandrewmeyer 2364712
Use any not interface{}
tonyandrewmeyer a6d001b
Apply suggestion from @tonyandrewmeyer
tonyandrewmeyer f2e7b4e
Merge branch 'main' into feat-presets-as-yaml
tonyandrewmeyer f44499c
Merge branch 'main' into feat-presets-as-yaml
tonyandrewmeyer 0c6c5f9
Address review comments.
tonyandrewmeyer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,162 +1,59 @@ | ||
| package config | ||
|
|
||
| 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": | ||
| return machinePreset, nil | ||
| case "dev": | ||
| return devPreset, nil | ||
| case "crafts": | ||
| return craftsPreset, nil | ||
| default: | ||
| return nil, fmt.Errorf("unknown preset '%s'", preset) | ||
| import ( | ||
| "bytes" | ||
| "errors" | ||
| "fmt" | ||
| "io/fs" | ||
| "sort" | ||
| "strings" | ||
|
|
||
| "github.com/canonical/concierge/presets" | ||
| "github.com/spf13/viper" | ||
| ) | ||
|
|
||
| // ValidPresets returns the sorted list of available preset names. | ||
| func ValidPresets() []string { | ||
| entries, err := presets.FS.ReadDir(".") | ||
| if err != nil { | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| // defaultJujuConfig is the default Juju config for all presets. | ||
| var defaultJujuConfig jujuConfig = jujuConfig{ | ||
| Disable: false, | ||
| ModelDefaults: map[string]string{ | ||
| "test-mode": "true", | ||
| "automatically-retry-hooks": "false", | ||
| }, | ||
| } | ||
|
|
||
| // defaultPackages is the set of packages installed for all presets. | ||
| var defaultPackages []string = []string{ | ||
| "gnome-keyring", | ||
| "python3-pip", | ||
| "python3-venv", | ||
| } | ||
|
|
||
| // defaultSnaps is the set of snaps installed for all presets. | ||
| var defaultSnaps map[string]SnapConfig = map[string]SnapConfig{ | ||
| "charmcraft": {Channel: "latest/stable"}, | ||
| "jq": {Channel: "latest/stable"}, | ||
| "yq": {Channel: "latest/stable"}, | ||
| } | ||
|
|
||
| // defaultLXDConfig is the standard LXD config used throughout presets. | ||
| var defaultLXDConfig lxdConfig = lxdConfig{ | ||
| Enable: true, | ||
| Bootstrap: true, | ||
| } | ||
|
|
||
| // defaultMicroK8sConfig is the standard MicroK8s config used throughout presets. | ||
| var defaultMicroK8sConfig microk8sConfig = microk8sConfig{ | ||
| Enable: true, | ||
| Bootstrap: true, | ||
| Addons: []string{ | ||
| "hostpath-storage", | ||
| "dns", | ||
| "rbac", | ||
| "metallb:10.64.140.43-10.64.140.49", | ||
| }, | ||
| } | ||
|
|
||
| // defaultK8sConfig is the standard K8s config used throughout presets. | ||
| var defaultK8sConfig k8sConfig = k8sConfig{ | ||
| Enable: true, | ||
| Bootstrap: true, | ||
| BootstrapConstraints: map[string]string{"root-disk": "2G"}, | ||
| Features: map[string]map[string]string{ | ||
| "load-balancer": { | ||
| "l2-mode": "true", | ||
| "cidrs": "10.43.45.0/28", | ||
| }, | ||
| "local-storage": {}, | ||
| "network": {}, | ||
| }, | ||
| } | ||
|
|
||
| // machinePreset is a configuration preset designed to be used when testing | ||
| // machine charms. | ||
| var machinePreset *Config = &Config{ | ||
| Juju: defaultJujuConfig, | ||
| Providers: providerConfig{ | ||
| LXD: defaultLXDConfig, | ||
| }, | ||
| Host: hostConfig{ | ||
| Packages: defaultPackages, | ||
| Snaps: MergeMaps(defaultSnaps, map[string]SnapConfig{ | ||
| "snapcraft": {Channel: "latest/stable"}, | ||
| }), | ||
| }, | ||
| var names []string | ||
| for _, e := range entries { | ||
| if !e.IsDir() && strings.HasSuffix(e.Name(), ".yaml") { | ||
| names = append(names, strings.TrimSuffix(e.Name(), ".yaml")) | ||
| } | ||
| } | ||
| sort.Strings(names) | ||
| return names | ||
| } | ||
|
|
||
| // 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: MergeMaps(defaultSnaps, map[string]SnapConfig{ | ||
| "rockcraft": {Channel: "latest/stable"}, | ||
| }), | ||
| }, | ||
| // Preset returns a configuration preset by name. | ||
| func Preset(preset string) (*Config, error) { | ||
| filename := preset + ".yaml" | ||
| data, err := presets.FS.ReadFile(filename) | ||
| if err != nil { | ||
| if errors.Is(err, fs.ErrNotExist) { | ||
| return nil, fmt.Errorf("unknown preset '%s'", preset) | ||
| } | ||
| return nil, fmt.Errorf("failed to read preset '%s': %w", preset, err) | ||
| } | ||
| return loadPreset(data) | ||
| } | ||
|
|
||
| // microk8sPreset is a configuration preset designed to be used when testing | ||
| // k8s charms. | ||
| var microk8sPreset *Config = &Config{ | ||
| Juju: defaultJujuConfig, | ||
| Providers: providerConfig{ | ||
| // Enable LXD so charms can be built, but don't bootstrap onto it. | ||
| LXD: lxdConfig{Enable: true}, | ||
| MicroK8s: defaultMicroK8sConfig, | ||
| }, | ||
| Host: hostConfig{ | ||
| Packages: defaultPackages, | ||
| Snaps: MergeMaps(defaultSnaps, map[string]SnapConfig{ | ||
| "rockcraft": {Channel: "latest/stable"}, | ||
| }), | ||
| }, | ||
| } | ||
| // loadPreset parses YAML data into a Config using a fresh Viper instance. | ||
| func loadPreset(data []byte) (*Config, error) { | ||
tonyandrewmeyer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| v := viper.New() | ||
| v.SetConfigType("yaml") | ||
|
|
||
| // devPreset combines both the LXD and K8s presets, designed to be used by | ||
| // developers when iterating on charms. | ||
| var devPreset *Config = &Config{ | ||
| Juju: defaultJujuConfig, | ||
| Providers: providerConfig{ | ||
| LXD: defaultLXDConfig, | ||
| K8s: defaultK8sConfig, | ||
| }, | ||
| Host: hostConfig{ | ||
| Packages: defaultPackages, | ||
| Snaps: MergeMaps(defaultSnaps, map[string]SnapConfig{ | ||
| "rockcraft": {Channel: "latest/stable"}, | ||
| "snapcraft": {Channel: "latest/stable"}, | ||
| "jhack": {Channel: "latest/stable", Connections: []string{"jhack:dot-local-share-juju"}}, | ||
| }), | ||
| }, | ||
| } | ||
| if err := v.ReadConfig(bytes.NewReader(data)); err != nil { | ||
| return nil, fmt.Errorf("failed to parse preset: %w", err) | ||
| } | ||
|
|
||
| // craftsPreset installs each of the crafts, and configures LXD, but disables Juju. | ||
| // Useful for workflows where only artifacts need to be built. | ||
| var craftsPreset *Config = &Config{ | ||
| Juju: jujuConfig{ | ||
| Disable: true, | ||
| }, | ||
| Providers: providerConfig{ | ||
| LXD: defaultLXDConfig, | ||
| }, | ||
| Host: hostConfig{ | ||
| Packages: defaultPackages, | ||
| Snaps: MergeMaps(defaultSnaps, map[string]SnapConfig{ | ||
| "rockcraft": {Channel: "latest/stable"}, | ||
| "snapcraft": {Channel: "latest/stable"}, | ||
| }), | ||
| }, | ||
| conf := &Config{} | ||
| if err := v.Unmarshal(conf); err != nil { | ||
| return nil, fmt.Errorf("failed to unmarshal preset: %w", err) | ||
| } | ||
| return conf, nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.