Merge pull request #11761 from johngmyers/override-tweak

Adjustments to SpecOverride
This commit is contained in:
Kubernetes Prow Robot 2021-06-14 22:50:01 -07:00 committed by GitHub
commit 222fd4be15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 8 deletions

View File

@ -27,6 +27,7 @@ import (
"strings" "strings"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/pflag"
apierrors "k8s.io/apimachinery/pkg/api/errors" apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
"k8s.io/klog/v2" "k8s.io/klog/v2"
@ -70,8 +71,8 @@ type CreateClusterOptions struct {
// SSHPublicKeys is a map of the SSH public keys we should configure; required on AWS, not required on GCE // SSHPublicKeys is a map of the SSH public keys we should configure; required on AWS, not required on GCE
SSHPublicKeys map[string][]byte SSHPublicKeys map[string][]byte
// Overrides allows setting values directly in the spec. // Sets allows setting values directly in the spec.
Overrides []string Sets []string
// Unsets allows unsetting values directly in the spec. // Unsets allows unsetting values directly in the spec.
Unsets []string Unsets []string
@ -302,7 +303,7 @@ func NewCmdCreateCluster(f *util.Factory, out io.Writer) *cobra.Command {
cmd.Flags().StringVarP(&options.Output, "output", "o", options.Output, "Output format. One of json|yaml. Used with the --dry-run flag.") cmd.Flags().StringVarP(&options.Output, "output", "o", options.Output, "Output format. One of json|yaml. Used with the --dry-run flag.")
if featureflag.SpecOverrideFlag.Enabled() { if featureflag.SpecOverrideFlag.Enabled() {
cmd.Flags().StringSliceVar(&options.Overrides, "override", options.Overrides, "Directly configure values in the spec") cmd.Flags().StringSliceVar(&options.Sets, "set", options.Sets, "Directly set values in the spec")
cmd.Flags().StringSliceVar(&options.Unsets, "unset", options.Unsets, "Directly unset values in the spec") cmd.Flags().StringSliceVar(&options.Unsets, "unset", options.Unsets, "Directly unset values in the spec")
} }
@ -336,6 +337,15 @@ func NewCmdCreateCluster(f *util.Factory, out io.Writer) *cobra.Command {
cmd.Flags().BoolVar(&options.OpenstackLBOctavia, "os-octavia", options.OpenstackLBOctavia, "If true octavia loadbalancer api will be used") cmd.Flags().BoolVar(&options.OpenstackLBOctavia, "os-octavia", options.OpenstackLBOctavia, "If true octavia loadbalancer api will be used")
cmd.Flags().StringVar(&options.OpenstackDNSServers, "os-dns-servers", options.OpenstackDNSServers, "comma separated list of DNS Servers which is used in network") cmd.Flags().StringVar(&options.OpenstackDNSServers, "os-dns-servers", options.OpenstackDNSServers, "comma separated list of DNS Servers which is used in network")
cmd.Flags().StringVar(&options.OpenstackNetworkID, "os-network", options.OpenstackNetworkID, "The ID of the existing OpenStack network to use") cmd.Flags().StringVar(&options.OpenstackNetworkID, "os-network", options.OpenstackNetworkID, "The ID of the existing OpenStack network to use")
cmd.Flags().SetNormalizeFunc(func(f *pflag.FlagSet, name string) pflag.NormalizedName {
switch name {
case "override":
name = "set"
}
return pflag.NormalizedName(name)
})
return cmd return cmd
} }
@ -516,7 +526,7 @@ func RunCreateCluster(ctx context.Context, f *util.Factory, out io.Writer, c *Cr
if err := commands.UnsetClusterFields(c.Unsets, cluster); err != nil { if err := commands.UnsetClusterFields(c.Unsets, cluster); err != nil {
return err return err
} }
if err := commands.SetClusterFields(c.Overrides, cluster); err != nil { if err := commands.SetClusterFields(c.Sets, cluster); err != nil {
return err return err
} }

View File

@ -4,5 +4,5 @@ Zones:
CloudProvider: aws CloudProvider: aws
Networking: cni Networking: cni
KubernetesVersion: v1.21.0 KubernetesVersion: v1.21.0
Overrides: Sets:
- cluster.spec.nodePortAccess=1.2.3.4/32,10.20.30.0/24 - cluster.spec.nodePortAccess=1.2.3.4/32,10.20.30.0/24

View File

@ -95,12 +95,12 @@ func setPrimitive(v reflect.Value, newValue string) error {
} }
if v.Type().Kind() == reflect.Slice { if v.Type().Kind() == reflect.Slice {
// Because this function generally sets values, we overwrite instead of appending. // To support multiple values, we split on commas.
// Then to support multiple values, we split on commas.
// We have no way to escape a comma currently; but in general we prefer having a slice in the schema, // We have no way to escape a comma currently; but in general we prefer having a slice in the schema,
// rather than having values that need to be parsed, so we may not need it. // rather than having values that need to be parsed, so we may not need it.
tokens := strings.Split(newValue, ",") tokens := strings.Split(newValue, ",")
valueArray := reflect.MakeSlice(v.Type(), 0, len(tokens)) valueArray := reflect.MakeSlice(v.Type(), 0, v.Len()+len(tokens))
valueArray = reflect.AppendSlice(valueArray, v)
for _, s := range tokens { for _, s := range tokens {
valueItem := reflect.New(v.Type().Elem()) valueItem := reflect.New(v.Type().Elem())
if err := setPrimitive(valueItem.Elem(), s); err != nil { if err := setPrimitive(valueItem.Elem(), s); err != nil {

View File

@ -163,6 +163,13 @@ func TestSet(t *testing.T) {
Path: "spec.containers[0].enumSlice", Path: "spec.containers[0].enumSlice",
Value: "ABC,DEF", Value: "ABC,DEF",
}, },
{
Name: "append enum slice",
Input: "{ 'spec': { 'containers': [ { 'enumSlice': [ 'ABC', 'DEF' ] } ] } }",
Expected: "{ 'spec': { 'containers': [ { 'enumSlice': [ 'ABC', 'DEF', 'GHI', 'JKL' ] } ] } }",
Path: "spec.containers[0].enumSlice",
Value: "GHI,JKL",
},
// Not sure if we should do this... // Not sure if we should do this...
// { // {
// Name: "creating missing array elements", // Name: "creating missing array elements",