Skip to content
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

Introduce --debug option; fix $patch=delete #31

Merged
merged 1 commit into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Comment on lines +59 to +95
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider making the debug output less disruptive.

The debugPhase function currently exits the program using os.Exit(0), which could be disruptive to the application flow. Consider:

  1. Making the exit behavior configurable
  2. Returning the debug information instead of printing directly
  3. Using a logger interface for better control over output

This would make the debug functionality more flexible and less intrusive.

-func debugPhase(opts Options, patches []string, clusterName string, clusterEndpoint string, mType machine.Type) {
+func debugPhase(opts Options, patches []string, clusterName string, clusterEndpoint string, mType machine.Type) string {
     phase := 2
     if clusterName == "" {
         clusterName = "dummy"
         phase = 1
     }
     if clusterEndpoint == "" {
         clusterEndpoint = "clusterEndpoint"
         phase = 1
     }

-    fmt.Printf(
+    output := fmt.Sprintf(
         "# 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]) == "@" {
-            fmt.Printf(" %s=%s\n", patchOption, patch)
+            output += fmt.Sprintf(" %s=%s\n", patchOption, patch)
         } else {
-            fmt.Printf("\n---")
-            fmt.Printf("\n# DEBUG(phase %d): %s=\n%s", phase, patchOption, patch)
+            output += fmt.Sprintf("\n---\n# DEBUG(phase %d): %s=\n%s", phase, patchOption, patch)
         }
     }

-    os.Exit(0)
+    return output
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// 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)
}
// 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) string {
phase := 2
if clusterName == "" {
clusterName = "dummy"
phase = 1
}
if clusterEndpoint == "" {
clusterEndpoint = "clusterEndpoint"
phase = 1
}
output := fmt.Sprintf(
"# 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]) == "@" {
output += fmt.Sprintf(" %s=%s\n", patchOption, patch)
} else {
output += fmt.Sprintf("\n---\n# DEBUG(phase %d): %s=\n%s", phase, patchOption, patch)
}
}
return output
}


// 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
Loading