Unverified Commit dc315b8e authored by Kubernetes Submit Queue's avatar Kubernetes Submit Queue Committed by GitHub

Merge pull request #55254 from mtaufen/iterative-manifest-url-header

Automatic merge from submit-queue (batch tested with PRs 55254, 55525, 50108, 54674, 55263). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. ColonSeparatedMultimapStringString: allow multiple Set invocations with default override The first call to Set will clear the map before adding entries; subsequent calls will simply append to the map. This makes it possible to override default values with a command-line option rather than appending to defaults, while still allowing the distribution of key-value pairs across multiple flag invocations. For example: `--flag "a:hello" --flag "b:again" --flag "b:beautiful" --flag "c:world"` results in `{"a": ["hello"], "b": ["again", "beautiful"], "c": ["world"]}` Related: #53833, https://github.com/kubernetes/kubernetes/pull/54643#discussion_r148651257 ```release-note NONE ```
parents 56e62b68 1347c094
...@@ -371,7 +371,7 @@ func AddKubeletConfigFlags(fs *pflag.FlagSet, c *kubeletconfig.KubeletConfigurat ...@@ -371,7 +371,7 @@ func AddKubeletConfigFlags(fs *pflag.FlagSet, c *kubeletconfig.KubeletConfigurat
fs.DurationVar(&c.FileCheckFrequency.Duration, "file-check-frequency", c.FileCheckFrequency.Duration, "Duration between checking config files for new data") fs.DurationVar(&c.FileCheckFrequency.Duration, "file-check-frequency", c.FileCheckFrequency.Duration, "Duration between checking config files for new data")
fs.DurationVar(&c.HTTPCheckFrequency.Duration, "http-check-frequency", c.HTTPCheckFrequency.Duration, "Duration between checking http for new data") fs.DurationVar(&c.HTTPCheckFrequency.Duration, "http-check-frequency", c.HTTPCheckFrequency.Duration, "Duration between checking http for new data")
fs.StringVar(&c.ManifestURL, "manifest-url", c.ManifestURL, "URL for accessing the container manifest") fs.StringVar(&c.ManifestURL, "manifest-url", c.ManifestURL, "URL for accessing the container manifest")
fs.Var(flag.ColonSeparatedMultimapStringString(c.ManifestURLHeader), "manifest-url-header", "Comma-separated list of HTTP headers to use when accessing the manifest URL. Multiple headers with the same name will be added in the same order provided. For example: `a:hello,b:again,c:world,b:beautiful`") fs.Var(flag.NewColonSeparatedMultimapStringString(&c.ManifestURLHeader), "manifest-url-header", "Comma-separated list of HTTP headers to use when accessing the manifest URL. Multiple headers with the same name will be added in the same order provided. This flag can be repeatedly invoked. For example: `--manifest-url-header 'a:hello,b:again,c:world' --manifest-url-header 'b:beautiful'`")
fs.BoolVar(&c.EnableServer, "enable-server", c.EnableServer, "Enable the Kubelet's server") fs.BoolVar(&c.EnableServer, "enable-server", c.EnableServer, "Enable the Kubelet's server")
fs.Var(componentconfig.IPVar{Val: &c.Address}, "address", "The IP address for the Kubelet to serve on (set to 0.0.0.0 for all interfaces)") fs.Var(componentconfig.IPVar{Val: &c.Address}, "address", "The IP address for the Kubelet to serve on (set to 0.0.0.0 for all interfaces)")
fs.Int32Var(&c.Port, "port", c.Port, "The port for the Kubelet to serve on.") fs.Int32Var(&c.Port, "port", c.Port, "The port for the Kubelet to serve on.")
......
...@@ -231,9 +231,6 @@ func SetDefaults_KubeletConfiguration(obj *KubeletConfiguration) { ...@@ -231,9 +231,6 @@ func SetDefaults_KubeletConfiguration(obj *KubeletConfiguration) {
if obj.EnforceNodeAllocatable == nil { if obj.EnforceNodeAllocatable == nil {
obj.EnforceNodeAllocatable = defaultNodeAllocatableEnforcement obj.EnforceNodeAllocatable = defaultNodeAllocatableEnforcement
} }
if obj.ManifestURLHeader == nil {
obj.ManifestURLHeader = make(map[string][]string)
}
} }
func boolVar(b bool) *bool { func boolVar(b bool) *bool {
......
...@@ -28,13 +28,30 @@ import ( ...@@ -28,13 +28,30 @@ import (
// slice of strings associated with that key. Items in the list associated with a given // slice of strings associated with that key. Items in the list associated with a given
// key will appear in the order provided. // key will appear in the order provided.
// For example: `a:hello,b:again,c:world,b:beautiful` results in `{"a": ["hello"], "b": ["again", "beautiful"], "c": ["world"]}` // For example: `a:hello,b:again,c:world,b:beautiful` results in `{"a": ["hello"], "b": ["again", "beautiful"], "c": ["world"]}`
type ColonSeparatedMultimapStringString map[string][]string // The first call to Set will clear the map before adding entries; subsequent calls will simply append to the map.
// This makes it possible to override default values with a command-line option rather than appending to defaults,
// while still allowing the distribution of key-value pairs across multiple flag invocations.
// For example: `--flag "a:hello" --flag "b:again" --flag "b:beautiful" --flag "c:world"` results in `{"a": ["hello"], "b": ["again", "beautiful"], "c": ["world"]}`
type ColonSeparatedMultimapStringString struct {
Multimap *map[string][]string
initialized bool // set to true after the first Set call
}
// NewColonSeparatedMultimapStringString takes a pointer to a map[string][]string and returns the
// ColonSeparatedMultimapStringString flag parsing shim for that map.
func NewColonSeparatedMultimapStringString(m *map[string][]string) *ColonSeparatedMultimapStringString {
return &ColonSeparatedMultimapStringString{Multimap: m}
}
// Set implements github.com/spf13/pflag.Value // Set implements github.com/spf13/pflag.Value
func (m ColonSeparatedMultimapStringString) Set(value string) error { func (m *ColonSeparatedMultimapStringString) Set(value string) error {
// clear old values if m.Multimap == nil {
for k := range m { return fmt.Errorf("no target (nil pointer to map[string][]string)")
delete(m, k) }
if !m.initialized || *m.Multimap == nil {
// clear default values, or allocate if no existing map
*m.Multimap = make(map[string][]string)
m.initialized = true
} }
for _, pair := range strings.Split(value, ",") { for _, pair := range strings.Split(value, ",") {
if len(pair) == 0 { if len(pair) == 0 {
...@@ -46,19 +63,19 @@ func (m ColonSeparatedMultimapStringString) Set(value string) error { ...@@ -46,19 +63,19 @@ func (m ColonSeparatedMultimapStringString) Set(value string) error {
} }
k := strings.TrimSpace(kv[0]) k := strings.TrimSpace(kv[0])
v := strings.TrimSpace(kv[1]) v := strings.TrimSpace(kv[1])
m[k] = append(m[k], v) (*m.Multimap)[k] = append((*m.Multimap)[k], v)
} }
return nil return nil
} }
// String implements github.com/spf13/pflag.Value // String implements github.com/spf13/pflag.Value
func (m ColonSeparatedMultimapStringString) String() string { func (m *ColonSeparatedMultimapStringString) String() string {
type kv struct { type kv struct {
k string k string
v string v string
} }
kvs := make([]kv, 0, len(m)) kvs := make([]kv, 0, len(*m.Multimap))
for k, vs := range m { for k, vs := range *m.Multimap {
for i := range vs { for i := range vs {
kvs = append(kvs, kv{k: k, v: vs[i]}) kvs = append(kvs, kv{k: k, v: vs[i]})
} }
...@@ -75,6 +92,11 @@ func (m ColonSeparatedMultimapStringString) String() string { ...@@ -75,6 +92,11 @@ func (m ColonSeparatedMultimapStringString) String() string {
} }
// Type implements github.com/spf13/pflag.Value // Type implements github.com/spf13/pflag.Value
func (m ColonSeparatedMultimapStringString) Type() string { func (m *ColonSeparatedMultimapStringString) Type() string {
return "colonSeparatedMultimapStringString" return "colonSeparatedMultimapStringString"
} }
// Empty implements OmitEmpty
func (m *ColonSeparatedMultimapStringString) Empty() bool {
return len(*m.Multimap) == 0
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment