-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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
Add set instancegroup
command
#10593
Merged
k8s-ci-robot
merged 3 commits into
kubernetes:master
from
gabrieljackson:set-instancegroup-cmd-redux
Jan 25, 2021
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
Copyright 2021 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
"k8s.io/kubectl/pkg/util/i18n" | ||
"k8s.io/kubectl/pkg/util/templates" | ||
|
||
"k8s.io/kops/cmd/kops/util" | ||
"k8s.io/kops/pkg/commands" | ||
) | ||
|
||
var ( | ||
setInstancegroupLong = templates.LongDesc(i18n.T(`Set an instance group field value. | ||
|
||
This command changes the desired instance group configuration in the registry. | ||
|
||
kops set does not update the cloud resources; to apply the changes use "kops update cluster".`)) | ||
|
||
setInstancegroupExample = templates.Examples(i18n.T(` | ||
# Set instance group to run image custom-ami-image | ||
kops set instancegroup --name k8s-cluster.example.com nodes spec.image=custom-ami-image | ||
`)) | ||
) | ||
|
||
// NewCmdSetInstancegroup builds a cobra command for the kops set instancegroup command. | ||
func NewCmdSetInstancegroup(f *util.Factory, out io.Writer) *cobra.Command { | ||
options := &commands.SetInstanceGroupOptions{} | ||
|
||
cmd := &cobra.Command{ | ||
Use: "instancegroup", | ||
Aliases: []string{"instancegroups", "ig"}, | ||
Short: i18n.T("Set instancegroup fields."), | ||
Long: setInstancegroupLong, | ||
Example: setInstancegroupExample, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
ctx := context.TODO() | ||
|
||
for i, arg := range args { | ||
index := strings.Index(arg, "=") | ||
|
||
if i == 0 { | ||
if index != -1 { | ||
exitWithError(fmt.Errorf("Specify name of instance group to edit")) | ||
} | ||
options.InstanceGroupName = arg | ||
} else { | ||
if index == -1 { | ||
exitWithError(fmt.Errorf("unrecognized parameter %q (missing '=')", arg)) | ||
return | ||
} | ||
options.Fields = append(options.Fields, arg) | ||
} | ||
} | ||
|
||
options.ClusterName = rootCommand.ClusterName() | ||
|
||
if err := commands.RunSetInstancegroup(ctx, f, cmd, out, options); err != nil { | ||
exitWithError(err) | ||
} | ||
}, | ||
} | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
|
||
<!--- This file is automatically generated by make gen-cli-docs; changes should be made in the go CLI command code (under cmd/kops) --> | ||
|
||
## kops set instancegroup | ||
|
||
Set instancegroup fields. | ||
|
||
### Synopsis | ||
|
||
Set an instance group field value. | ||
|
||
This command changes the desired instance group configuration in the registry. | ||
|
||
kops set does not update the cloud resources; to apply the changes use "kops update cluster". | ||
|
||
``` | ||
kops set instancegroup [flags] | ||
``` | ||
|
||
### Examples | ||
|
||
``` | ||
# Set instance group to run image custom-ami-image | ||
kops set instancegroup --name k8s-cluster.example.com nodes spec.image=custom-ami-image | ||
``` | ||
|
||
### Options | ||
|
||
``` | ||
-h, --help help for instancegroup | ||
``` | ||
|
||
### Options inherited from parent commands | ||
|
||
``` | ||
--add_dir_header If true, adds the file directory to the header of the log messages | ||
--alsologtostderr log to standard error as well as files | ||
--config string yaml config file (default is $HOME/.kops.yaml) | ||
--log_backtrace_at traceLocation when logging hits line file:N, emit a stack trace (default :0) | ||
--log_dir string If non-empty, write log files in this directory | ||
--log_file string If non-empty, use this log file | ||
--log_file_max_size uint Defines the maximum size a log file can grow to. Unit is megabytes. If the value is 0, the maximum file size is unlimited. (default 1800) | ||
--logtostderr log to standard error instead of files (default true) | ||
--name string Name of cluster. Overrides KOPS_CLUSTER_NAME environment variable | ||
--one_output If true, only write logs to their native severity level (vs also writing to each lower severity level | ||
--skip_headers If true, avoid header prefixes in the log messages | ||
--skip_log_headers If true, avoid headers when opening log files | ||
--state string Location of state storage (kops 'config' file). Overrides KOPS_STATE_STORE environment variable | ||
--stderrthreshold severity logs at or above this threshold go to stderr (default 2) | ||
-v, --v Level number for the log level verbosity | ||
--vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging | ||
``` | ||
|
||
### SEE ALSO | ||
|
||
* [kops set](kops_set.md) - Set fields on clusters and other resources. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
Copyright 2019 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package commands | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
"k8s.io/apimachinery/pkg/util/validation/field" | ||
|
||
"k8s.io/kops/cmd/kops/util" | ||
api "k8s.io/kops/pkg/apis/kops" | ||
"k8s.io/kops/pkg/featureflag" | ||
"k8s.io/kops/util/pkg/reflectutils" | ||
) | ||
|
||
// SetInstanceGroupOptions contains the options for setting configuration on an | ||
// instance group. | ||
type SetInstanceGroupOptions struct { | ||
Fields []string | ||
ClusterName string | ||
InstanceGroupName string | ||
} | ||
|
||
// RunSetInstancegroup implements the set instancegroup command logic. | ||
func RunSetInstancegroup(ctx context.Context, f *util.Factory, cmd *cobra.Command, out io.Writer, options *SetInstanceGroupOptions) error { | ||
if !featureflag.SpecOverrideFlag.Enabled() { | ||
return fmt.Errorf("set instancegroup is currently feature gated; set `export KOPS_FEATURE_FLAGS=SpecOverrideFlag`") | ||
} | ||
|
||
if options.ClusterName == "" { | ||
return field.Required(field.NewPath("clusterName"), "Cluster name is required") | ||
} | ||
if options.InstanceGroupName == "" { | ||
return field.Required(field.NewPath("instancegroupName"), "Instance Group name is required") | ||
} | ||
|
||
clientset, err := f.Clientset() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
cluster, err := clientset.GetCluster(ctx, options.ClusterName) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
instanceGroups, err := ReadAllInstanceGroups(ctx, clientset, cluster) | ||
if err != nil { | ||
return err | ||
} | ||
var instanceGroupToUpdate *api.InstanceGroup | ||
for _, instanceGroup := range instanceGroups { | ||
if instanceGroup.GetName() == options.InstanceGroupName { | ||
instanceGroupToUpdate = instanceGroup | ||
} | ||
} | ||
if instanceGroupToUpdate == nil { | ||
return fmt.Errorf("unable to find instance group with name %q", options.InstanceGroupName) | ||
} | ||
|
||
err = SetInstancegroupFields(options.Fields, instanceGroupToUpdate) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = UpdateInstanceGroup(ctx, clientset, cluster, instanceGroups, instanceGroupToUpdate) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// SetInstancegroupFields sets field values in the instance group. | ||
func SetInstancegroupFields(fields []string, instanceGroup *api.InstanceGroup) error { | ||
for _, field := range fields { | ||
kv := strings.SplitN(field, "=", 2) | ||
if len(kv) != 2 { | ||
return fmt.Errorf("unhandled field: %q", field) | ||
} | ||
|
||
key := kv[0] | ||
key = strings.TrimPrefix(key, "instancegroup.") | ||
|
||
if err := reflectutils.SetString(instanceGroup, key, kv[1]); err != nil { | ||
return fmt.Errorf("failed to set %s=%s: %v", kv[0], kv[1], err) | ||
} | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these can be removed because they're only operating on the Cluster object rather than the InstanceGroup we're modifying.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am pretty sure I need to keep this if I am going to do
DeepValidate
in the following lines. If this isn't the correct method of checking the resources though I can change it, but I didn't see another similar validation method for just instancegroups.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah you're right, I missed that. thanks! I think in this case we can follow what
kops edit instancegroup
does which is largely the same:kops/cmd/kops/edit_instancegroup.go
Lines 167 to 183 in 97c40a3
but it only validates with
CrossValidateInstanceGroup
rather thanDeepValidate
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the pointer. I modified the validation call to use the same one as you referenced.