Skip to content

Commit

Permalink
feat: add --config-patch parameter to talosctl gen config
Browse files Browse the repository at this point in the history
Fixes: #3410

Same as in `talosctl cluster create`. Will apply RFC6902 json patch
during the config generation if specified.

Signed-off-by: Artem Chernyshev <artem.0xD2@gmail.com>
  • Loading branch information
Unix4ever authored and talos-bot committed Apr 2, 2021
1 parent e664362 commit 39c6dbc
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 2 deletions.
20 changes: 18 additions & 2 deletions cmd/talosctl/cmd/mgmt/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"path/filepath"
"strings"

jsonpatch "github.com/evanphx/json-patch"
"github.com/spf13/cobra"
talosnet "github.com/talos-systems/net"
"gopkg.in/yaml.v3"
Expand All @@ -35,6 +36,7 @@ var genConfigCmdFlags struct {
installDisk string
installImage string
outputDir string
configPatch string
registryMirrors []string
persistConfig bool
withExamples bool
Expand Down Expand Up @@ -143,7 +145,7 @@ func genV1Alpha1Config(args []string) error {
genOptions = append(genOptions, generate.WithVersionContract(versionContract))
}

configBundle, err := bundle.NewConfigBundle(
configBundleOpts := []bundle.Option{
bundle.WithInputOptions(
&bundle.InputOptions{
ClusterName: args[0],
Expand All @@ -158,7 +160,20 @@ func genV1Alpha1Config(args []string) error {
),
},
),
)
}

if genConfigCmdFlags.configPatch != "" {
var jsonPatch jsonpatch.Patch

jsonPatch, err = jsonpatch.DecodePatch([]byte(genConfigCmdFlags.configPatch))
if err != nil {
return fmt.Errorf("error parsing config JSON patch: %w", err)
}

configBundleOpts = append(configBundleOpts, bundle.WithJSONPatch(jsonPatch))
}

configBundle, err := bundle.NewConfigBundle(configBundleOpts...)
if err != nil {
return fmt.Errorf("failed to generate config bundle: %w", err)
}
Expand Down Expand Up @@ -205,6 +220,7 @@ func init() {
genConfigCmd.Flags().StringVar(&genConfigCmdFlags.talosVersion, "talos-version", "", "the desired Talos version to generate config for (backwards compatibility, e.g. v0.8)")
genConfigCmd.Flags().StringVar(&genConfigCmdFlags.kubernetesVersion, "kubernetes-version", "", "desired kubernetes version to run")
genConfigCmd.Flags().StringVarP(&genConfigCmdFlags.outputDir, "output-dir", "o", "", "destination to output generated files")
genConfigCmd.Flags().StringVar(&genConfigCmdFlags.configPatch, "config-patch", "", "patch generated machineconfigs")
genConfigCmd.Flags().StringSliceVar(&genConfigCmdFlags.registryMirrors, "registry-mirror", []string{}, "list of registry mirrors to use in format: <registry host>=<mirror URL>")
genConfigCmd.Flags().BoolVarP(&genConfigCmdFlags.persistConfig, "persist", "p", true, "the desired persist value for configs")
genConfigCmd.Flags().BoolVarP(&genConfigCmdFlags.withExamples, "with-examples", "", true, "renders all machine configs with the commented examples")
Expand Down
22 changes: 22 additions & 0 deletions internal/integration/cli/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
package cli

import (
"encoding/json"
"io/ioutil"
"os"
"regexp"

"github.com/talos-systems/talos/internal/integration/base"
"github.com/talos-systems/talos/pkg/machinery/config/configloader"
)

// GenSuite verifies dmesg command.
Expand Down Expand Up @@ -132,6 +134,26 @@ func (suite *GenSuite) TestGenConfigURLValidation() {
base.StderrShouldMatch(regexp.MustCompile(regexp.QuoteMeta(`try: "https://192.168.0.1:2000"`))))
}

// TestGenConfigPatch verify that gen config --config-patch works.
func (suite *GenSuite) TestGenConfigPatch() {
patch, err := json.Marshal([]map[string]interface{}{
{
"op": "replace",
"path": "/cluster/clusterName",
"value": "bar",
},
})

suite.Assert().NoError(err)

suite.RunCLI([]string{"gen", "config", "foo", "https://192.168.0.1:6443", "--config-patch", string(patch)})

cfg, err := configloader.NewFromFile("controlplane.yaml")

suite.Assert().NoError(err)
suite.Assert().Equal("bar", cfg.Cluster().Name())
}

func init() {
allSuites = append(allSuites, new(GenSuite))
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ created talosconfig
> If you think this should be added to the docs, please [create a issue](https://github.com/talos-systems/talos/issues).
At this point, you can modify the generated configs to your liking.
Optionally, you can specify `--config-patch` with RFC6902 jsonpatch which will be applied during the config generation.

#### Validate the Configuration Files

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ created talosconfig

Now add the required shebang (e.g. `#!talos`) at the top of `init.yaml`, `controlplane.yaml`, and `join.yaml`
At this point, you can modify the generated configs to your liking.
Optionally, you can specify `--config-patch` with RFC6902 jsonpatch which will be applied during the config generation.

#### Validate the Configuration Files

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ created talosconfig
```

At this point, you can modify the generated configs to your liking.
Optionally, you can specify `--config-patch` with RFC6902 jsonpatch which will be applied during the config generation.

#### Validate the Configuration Files

Expand Down
1 change: 1 addition & 0 deletions website/content/docs/v0.10/Cloud Platforms/aws.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ created talosconfig
```

At this point, you can modify the generated configs to your liking.
Optionally, you can specify `--config-patch` with RFC6902 jsonpatch which will be applied during the config generation.

#### Validate the Configuration Files

Expand Down
1 change: 1 addition & 0 deletions website/content/docs/v0.10/Cloud Platforms/digitalocean.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ created talosconfig
```

At this point, you can modify the generated configs to your liking.
Optionally, you can specify `--config-patch` with RFC6902 jsonpatch which will be applied during the config generation.

#### Validate the Configuration Files

Expand Down
2 changes: 2 additions & 0 deletions website/content/docs/v0.10/Cloud Platforms/gcp.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ LB_PUBLIC_IP=$(gcloud compute forwarding-rules describe talos-fwd-rule \
talosctl gen config talos-k8s-gcp-tutorial https://${LB_PUBLIC_IP}:443
```

Additionally, you can specify `--config-patch` with RFC6902 jsonpatch which will be applied during the config generation.

### Compute Creation

We are now ready to create our GCP nodes.
Expand Down
2 changes: 2 additions & 0 deletions website/content/docs/v0.10/Cloud Platforms/openstack.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ LB_PUBLIC_IP=$(openstack loadbalancer show talos-control-plane -f json | jq -r .
talosctl gen config talos-k8s-openstack-tutorial https://${LB_PUBLIC_IP}:6443
```

Additionally, you can specify `--config-patch` with RFC6902 jsonpatch which will be applied during the config generation.

### Compute Creation

We are now ready to create our Openstack nodes.
Expand Down
1 change: 1 addition & 0 deletions website/content/docs/v0.10/Reference/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,7 @@ talosctl gen config <cluster name> <cluster endpoint> [flags]

```
--additional-sans strings additional Subject-Alt-Names for the APIServer certificate
--config-patch string patch generated machineconfigs
--dns-domain string the dns domain to use for cluster (default "cluster.local")
-h, --help help for config
--install-disk string the disk to install to (default "/dev/sda")
Expand Down
1 change: 1 addition & 0 deletions website/content/docs/v0.10/Virtualized Platforms/vmware.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ created talosconfig
```

At this point, you can modify the generated configs to your liking.
Optionally, you can specify `--config-patch` with RFC6902 jsonpatch which will be applied during the config generation.

#### Validate the Configuration Files

Expand Down

0 comments on commit 39c6dbc

Please sign in to comment.