From 1a00187622df6d157d2f658eef5c6dfcd78a4f14 Mon Sep 17 00:00:00 2001 From: Andrei Kvapil Date: Mon, 8 Jul 2024 13:09:10 +0200 Subject: [PATCH] Fix reading nodes and endpoints from modeline (#13) Signed-off-by: Andrei Kvapil --- pkg/commands/apply.go | 17 ++++++++++++----- pkg/commands/template.go | 27 ++++++++++++++++++--------- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/pkg/commands/apply.go b/pkg/commands/apply.go index 481404c..12afd3f 100644 --- a/pkg/commands/apply.go +++ b/pkg/commands/apply.go @@ -35,6 +35,8 @@ var applyCmdFlags struct { stage bool force bool configTryTimeout time.Duration + nodesFromArgs bool + endpointsFromArgs bool } var applyCmd = &cobra.Command{ @@ -61,6 +63,13 @@ var applyCmd = &cobra.Command{ if !cmd.Flags().Changed("force") { applyCmdFlags.force = Config.UpgradeOptions.Force } + applyCmdFlags.nodesFromArgs = len(GlobalArgs.Nodes) > 0 + applyCmdFlags.endpointsFromArgs = len(GlobalArgs.Endpoints) > 0 + // Set dummy endpoint to avoid errors on building clinet + if len(GlobalArgs.Endpoints) == 0 { + GlobalArgs.Endpoints = append(GlobalArgs.Endpoints, "127.0.0.1") + } + return nil }, RunE: func(cmd *cobra.Command, args []string) error { @@ -70,10 +79,8 @@ var applyCmd = &cobra.Command{ func apply(args []string) func(ctx context.Context, c *client.Client) error { return func(ctx context.Context, c *client.Client) error { - nodesFromArgs := len(GlobalArgs.Nodes) > 0 - endpointsFromArgs := len(GlobalArgs.Endpoints) > 0 for _, configFile := range applyCmdFlags.configFiles { - if err := processModelineAndUpdateGlobals(configFile, nodesFromArgs, endpointsFromArgs, true); err != nil { + if err := processModelineAndUpdateGlobals(configFile, applyCmdFlags.nodesFromArgs, applyCmdFlags.endpointsFromArgs, true); err != nil { return err } @@ -138,10 +145,10 @@ func apply(args []string) func(ctx context.Context, c *client.Client) error { } // Reset args - if !nodesFromArgs { + if !applyCmdFlags.nodesFromArgs { GlobalArgs.Nodes = []string{} } - if !endpointsFromArgs { + if !applyCmdFlags.endpointsFromArgs { GlobalArgs.Endpoints = []string{} } } diff --git a/pkg/commands/template.go b/pkg/commands/template.go index 8404305..b1838f1 100644 --- a/pkg/commands/template.go +++ b/pkg/commands/template.go @@ -35,6 +35,9 @@ var templateCmdFlags struct { offline bool kubernetesVersion string inplace bool + nodesFromArgs bool + endpointsFromArgs bool + templatesFromArgs bool } var templateCmd = &cobra.Command{ @@ -64,6 +67,14 @@ var templateCmd = &cobra.Command{ if !cmd.Flags().Changed("offline") { templateCmdFlags.offline = Config.TemplateOptions.Offline } + templateCmdFlags.templatesFromArgs = len(templateCmdFlags.templateFiles) > 0 + templateCmdFlags.nodesFromArgs = len(GlobalArgs.Nodes) > 0 + templateCmdFlags.endpointsFromArgs = len(GlobalArgs.Endpoints) > 0 + // Set dummy endpoint to avoid errors on building clinet + if len(GlobalArgs.Endpoints) == 0 { + GlobalArgs.Endpoints = append(GlobalArgs.Endpoints, "127.0.0.1") + } + return nil }, RunE: func(cmd *cobra.Command, args []string) error { @@ -100,28 +111,26 @@ func template(args []string) func(ctx context.Context, c *client.Client) error { func templateWithFiles(args []string) func(ctx context.Context, c *client.Client) error { return func(ctx context.Context, c *client.Client) error { - templatesFromArgs := len(templateCmdFlags.templateFiles) > 0 - nodesFromArgs := len(GlobalArgs.Nodes) > 0 - endpointsFromArgs := len(GlobalArgs.Endpoints) > 0 firstFileProcessed := false for _, configFile := range templateCmdFlags.configFiles { modelineConfig, err := modeline.ReadAndParseModeline(configFile) if err != nil { return fmt.Errorf("modeline parsing failed: %v\n", err) } - if !templatesFromArgs { + if !templateCmdFlags.templatesFromArgs { if len(modelineConfig.Templates) == 0 { return fmt.Errorf("modeline does not contain templates information") } else { templateCmdFlags.templateFiles = modelineConfig.Templates } } - if !nodesFromArgs { + if !templateCmdFlags.nodesFromArgs { GlobalArgs.Nodes = modelineConfig.Nodes } - if !endpointsFromArgs { + if !templateCmdFlags.endpointsFromArgs { GlobalArgs.Endpoints = modelineConfig.Endpoints } + fmt.Printf("nodes: %v, endpoints: %v, templates: %v", GlobalArgs.Nodes, GlobalArgs.Endpoints, templateCmdFlags.templateFiles) if len(GlobalArgs.Nodes) < 1 { return errors.New("nodes are not set for the command: please use `--nodes` flag or configuration file to set the nodes to run the command against") @@ -165,13 +174,13 @@ func templateWithFiles(args []string) func(ctx context.Context, c *client.Client // Reset args firstFileProcessed = true - if !templatesFromArgs { + if !templateCmdFlags.templatesFromArgs { templateCmdFlags.templateFiles = []string{} } - if !nodesFromArgs { + if !templateCmdFlags.nodesFromArgs { GlobalArgs.Nodes = []string{} } - if !endpointsFromArgs { + if !templateCmdFlags.endpointsFromArgs { GlobalArgs.Endpoints = []string{} } }