Skip to content

Commit

Permalink
Fix reading nodes and endpoints from modeline
Browse files Browse the repository at this point in the history
Signed-off-by: Andrei Kvapil <[email protected]>
  • Loading branch information
kvaps committed Jul 8, 2024
1 parent 4328b43 commit fd945fb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
17 changes: 12 additions & 5 deletions pkg/commands/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ var applyCmdFlags struct {
stage bool
force bool
configTryTimeout time.Duration
nodesFromArgs bool
endpointsFromArgs bool
}

var applyCmd = &cobra.Command{
Expand All @@ -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 {
Expand All @@ -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
}

Expand Down Expand Up @@ -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{}
}
}
Expand Down
27 changes: 18 additions & 9 deletions pkg/commands/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ var templateCmdFlags struct {
offline bool
kubernetesVersion string
inplace bool
nodesFromArgs bool
endpointsFromArgs bool
templatesFromArgs bool
}

var templateCmd = &cobra.Command{
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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{}
}
}
Expand Down

0 comments on commit fd945fb

Please sign in to comment.