Skip to content

Commit

Permalink
Introduce --debug option; fix $patch=delete
Browse files Browse the repository at this point in the history
  • Loading branch information
kvaps committed Jan 10, 2025
1 parent 725ecb0 commit 486e8d2
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 10 deletions.
7 changes: 6 additions & 1 deletion pkg/commands/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var applyCmdFlags struct {
configFiles []string // -f/--files
talosVersion string
withSecrets string
debug bool
kubernetesVersion string
dryRun bool
preserve bool
Expand All @@ -54,6 +55,9 @@ var applyCmd = &cobra.Command{
if !cmd.Flags().Changed("kubernetes-version") {
applyCmdFlags.kubernetesVersion = Config.TemplateOptions.KubernetesVersion
}
if !cmd.Flags().Changed("debug") {
applyCmdFlags.debug = Config.TemplateOptions.Debug
}
if !cmd.Flags().Changed("preserve") {
applyCmdFlags.preserve = Config.UpgradeOptions.Preserve
}
Expand Down Expand Up @@ -88,6 +92,7 @@ func apply(args []string) func(ctx context.Context, c *client.Client) error {
TalosVersion: applyCmdFlags.talosVersion,
WithSecrets: applyCmdFlags.withSecrets,
KubernetesVersion: applyCmdFlags.kubernetesVersion,
Debug: applyCmdFlags.debug,
}

patches := []string{"@" + configFile}
Expand Down Expand Up @@ -188,8 +193,8 @@ func init() {
applyCmd.Flags().StringSliceVarP(&applyCmdFlags.configFiles, "file", "f", nil, "specify config files or patches in a YAML file (can specify multiple)")
applyCmd.Flags().StringVar(&applyCmdFlags.talosVersion, "talos-version", "", "the desired Talos version to generate config for (backwards compatibility, e.g. v0.8)")
applyCmd.Flags().StringVar(&applyCmdFlags.withSecrets, "with-secrets", "", "use a secrets file generated using 'gen secrets'")

applyCmd.Flags().StringVar(&applyCmdFlags.kubernetesVersion, "kubernetes-version", constants.DefaultKubernetesVersion, "desired kubernetes version to run")
applyCmd.Flags().BoolVarP(&applyCmdFlags.debug, "debug", "", false, "show only rendered patches")
applyCmd.Flags().BoolVar(&applyCmdFlags.dryRun, "dry-run", false, "check how the config change will be applied in dry-run mode")
applyCmd.Flags().DurationVar(&applyCmdFlags.configTryTimeout, "timeout", constants.ConfigTryTimeout, "the config will be rolled back after specified timeout (if try mode is selected)")
applyCmd.Flags().StringSliceVar(&applyCmdFlags.certFingerprints, "cert-fingerprint", nil, "list of server certificate fingeprints to accept (defaults to no check)")
Expand Down
1 change: 1 addition & 0 deletions pkg/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var Config struct {
WithSecrets string `yaml:"withSecrets"`
KubernetesVersion string `yaml:"kubernetesVersion"`
Full bool `yaml:"full"`
Debug bool `yaml:"debug"`
} `yaml:"templateOptions"`
ApplyOptions struct {
DryRun bool `yaml:"preserve"`
Expand Down
6 changes: 6 additions & 0 deletions pkg/commands/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var templateCmdFlags struct {
talosVersion string
withSecrets string
full bool
debug bool
offline bool
kubernetesVersion string
inplace bool
Expand Down Expand Up @@ -64,6 +65,9 @@ var templateCmd = &cobra.Command{
if !cmd.Flags().Changed("full") {
templateCmdFlags.full = Config.TemplateOptions.Full
}
if !cmd.Flags().Changed("debug") {
templateCmdFlags.debug = Config.TemplateOptions.Debug
}
if !cmd.Flags().Changed("offline") {
templateCmdFlags.offline = Config.TemplateOptions.Offline
}
Expand Down Expand Up @@ -199,6 +203,7 @@ func generateOutput(ctx context.Context, c *client.Client, args []string) (strin
TalosVersion: templateCmdFlags.talosVersion,
WithSecrets: templateCmdFlags.withSecrets,
Full: templateCmdFlags.full,
Debug: templateCmdFlags.debug,
Root: Config.RootDir,
Offline: templateCmdFlags.offline,
KubernetesVersion: templateCmdFlags.kubernetesVersion,
Expand Down Expand Up @@ -234,6 +239,7 @@ func init() {
templateCmd.Flags().StringVar(&templateCmdFlags.talosVersion, "talos-version", "", "the desired Talos version to generate config for (backwards compatibility, e.g. v0.8)")
templateCmd.Flags().StringVar(&templateCmdFlags.withSecrets, "with-secrets", "", "use a secrets file generated using 'gen secrets'")
templateCmd.Flags().BoolVarP(&templateCmdFlags.full, "full", "", false, "show full resulting config, not only patch")
templateCmd.Flags().BoolVarP(&templateCmdFlags.debug, "debug", "", false, "show only rendered patches")
templateCmd.Flags().BoolVarP(&templateCmdFlags.offline, "offline", "", false, "disable gathering information and lookup functions")
templateCmd.Flags().StringVar(&templateCmdFlags.kubernetesVersion, "kubernetes-version", constants.DefaultKubernetesVersion, "desired kubernetes version to run")

Expand Down
75 changes: 66 additions & 9 deletions pkg/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/cosi-project/runtime/pkg/resource"
"github.com/cosi-project/runtime/pkg/resource/meta"
"github.com/hashicorp/go-multierror"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/strvals"

Expand All @@ -32,7 +33,6 @@ import (
"github.com/siderolabs/talos/pkg/machinery/config/generate"
"github.com/siderolabs/talos/pkg/machinery/config/generate/secrets"
"github.com/siderolabs/talos/pkg/machinery/config/machine"
"helm.sh/helm/v3/pkg/chart"
)

// Options encapsulates all parameters necessary for rendering.
Expand All @@ -47,6 +47,7 @@ type Options struct {
TalosVersion string
WithSecrets string
Full bool
Debug bool
Root string
Offline bool
KubernetesVersion string
Expand All @@ -55,6 +56,44 @@ type Options struct {
Endpoint string
}

// debugPhase is a unified debug function that prints debug information based on the given stage and context,
// then exits the program.
func debugPhase(opts Options, patches []string, clusterName string, clusterEndpoint string, mType machine.Type) {
phase := 2
if clusterName == "" {
clusterName = "dummy"
phase = 1
}
if clusterEndpoint == "" {
clusterEndpoint = "clusterEndpoint"
phase = 1
}

fmt.Printf(
"# DEBUG(phase %d): talosctl gen config %s %s -t %s --with-secrets=%s --talos-version=%s --kubernetes-version=%s -o -",
phase, clusterName, clusterEndpoint, mType,
opts.WithSecrets, opts.TalosVersion, opts.KubernetesVersion,
)

patchOption := "--config-patch-control-plane"
if mType == machine.TypeWorker {
patchOption = "--config-patch-worker"
}

// Print patches
for _, patch := range patches {
if string(patch[0]) == "@" {
// Apply patch is always one
fmt.Printf(" %s=%s\n", patchOption, patch)
} else {
fmt.Printf("\n---")
fmt.Printf("\n# DEBUG(phase %d): %s=\n%s", phase, patchOption, patch)
}
}

os.Exit(0)
}

// FullConfigProcess handles the full process of creating and updating the Bundle.
func FullConfigProcess(ctx context.Context, opts Options, patches []string) (*bundle.Bundle, error) {
configBundle, err := InitializeConfigBundle(opts)
Expand All @@ -64,11 +103,17 @@ func FullConfigProcess(ctx context.Context, opts Options, patches []string) (*bu

loadedPatches, err := configpatcher.LoadPatches(patches)
if err != nil {
if opts.Debug {
debugPhase(opts, patches, "", "", machine.TypeUnknown)
}
return nil, err
}

err = configBundle.ApplyPatches(loadedPatches, true, true)
err = configBundle.ApplyPatches(loadedPatches, true, false)
if err != nil {
if opts.Debug {
debugPhase(opts, patches, "", "", machine.TypeUnknown)
}
return nil, fmt.Errorf("apply initial patches error: %w", err)
}

Expand All @@ -81,6 +126,10 @@ func FullConfigProcess(ctx context.Context, opts Options, patches []string) (*bu
machineType = machine.TypeWorker
}

if opts.Debug {
debugPhase(opts, patches, clusterName, clusterEndpoint.String(), machineType)
}

// Reinitializing the configuration bundle with updated parameters
updatedOpts := Options{
TalosVersion: opts.TalosVersion,
Expand All @@ -95,7 +144,7 @@ func FullConfigProcess(ctx context.Context, opts Options, patches []string) (*bu
}

// Applying updated patches
err = configBundle.ApplyPatches(loadedPatches, true, true)
err = configBundle.ApplyPatches(loadedPatches, (machineType == machine.TypeControlPlane), (machineType == machine.TypeWorker))
if err != nil {
return nil, fmt.Errorf("apply updated patches error: %w", err)
}
Expand Down Expand Up @@ -216,6 +265,7 @@ func Render(ctx context.Context, c *client.Client, opts Options) ([]byte, error)

finalConfig, err := applyPatchesAndRenderConfig(ctx, opts, configPatches, chrt)
if err != nil {
// TODO
return nil, err
}

Expand Down Expand Up @@ -344,11 +394,17 @@ func applyPatchesAndRenderConfig(ctx context.Context, opts Options, configPatche

patches, err := configpatcher.LoadPatches(configPatches)
if err != nil {
if opts.Debug {
debugPhase(opts, configPatches, "", "", machine.TypeUnknown)
}
return nil, err
}

err = configBundle.ApplyPatches(patches, true, true)
err = configBundle.ApplyPatches(patches, true, false)
if err != nil {
if opts.Debug {
debugPhase(opts, configPatches, "", "", machine.TypeUnknown)
}
return nil, err
}
machineType := configBundle.ControlPlaneCfg.Machine().Type()
Expand All @@ -358,6 +414,10 @@ func applyPatchesAndRenderConfig(ctx context.Context, opts Options, configPatche
machineType = machine.TypeWorker
}

if opts.Debug {
debugPhase(opts, configPatches, clusterName, clusterEndpoint.String(), machineType)
}

// Reload config with the correct machineType, clusterName and endpoint
configBundleOpts = []bundle.Option{
bundle.WithInputOptions(
Expand Down Expand Up @@ -404,7 +464,8 @@ func applyPatchesAndRenderConfig(ctx context.Context, opts Options, configPatche
return nil, err
}
}
err = configBundle.ApplyPatches(patches, true, true)

err = configBundle.ApplyPatches(patches, (machineType == machine.TypeControlPlane), (machineType == machine.TypeWorker))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -478,7 +539,6 @@ func extractResourceData(r resource.Resource) (map[string]interface{}, error) {
return res, fmt.Errorf("error unmarshaling yaml: %w", err)
}
res["spec"] = unmarshalledData
//res["stringSpec"] = yamlValue.(string)
} else {
return res, fmt.Errorf("field 'yaml' not found")
}
Expand All @@ -493,8 +553,6 @@ func newLookupFunction(ctx context.Context, c *client.Client) func(resource stri

var resources []map[string]interface{}

// get <type>
// get <type> <id>
callbackResource := func(parentCtx context.Context, hostname string, r resource.Resource, callError error) error {
if callError != nil {
multiErr = multierror.Append(multiErr, callError)
Expand All @@ -507,7 +565,6 @@ func newLookupFunction(ctx context.Context, c *client.Client) func(resource stri
}

resources = append(resources, res)

return nil
}
callbackRD := func(definition *meta.ResourceDefinition) error {
Expand Down

0 comments on commit 486e8d2

Please sign in to comment.