-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
apiserver: add enable subresource options
Signed-off-by: Iceber Gu <[email protected]>
- Loading branch information
Showing
4 changed files
with
118 additions
and
19 deletions.
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
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,74 @@ | ||
package kubeapiserver | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/spf13/pflag" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/apimachinery/pkg/util/sets" | ||
) | ||
|
||
type Options struct { | ||
AllowedProxySubresources []string | ||
} | ||
|
||
func NewOptions() *Options { | ||
return &Options{} | ||
} | ||
|
||
func (o *Options) AddFlags(fs *pflag.FlagSet) { | ||
var resources []string | ||
for r, srs := range supportedProxyCoreSubresources { | ||
for _, sr := range srs { | ||
resources = append(resources, r+"/"+sr) | ||
} | ||
} | ||
fs.StringSliceVar(&o.AllowedProxySubresources, "allowed-proxy-subresources", o.AllowedProxySubresources, ""+ | ||
"List of subresources that support proxying requests to the specified cluster, formatted as '[resource/subresource],[subresource],...'. "+ | ||
fmt.Sprintf("Supported proxy subresources include %q", strings.Join(resources, ",")), | ||
) | ||
} | ||
|
||
var supportedProxyCoreSubresources = map[string][]string{ | ||
"pods": {"proxy", "log", "exec", "attach", "portfowrd"}, | ||
"nodes": {"proxy"}, | ||
"services": {"proxy"}, | ||
} | ||
|
||
func (o *Options) Config() (*ExtraConfig, error) { | ||
subresources := make(map[schema.GroupResource]sets.Set[string]) | ||
|
||
for _, subresource := range o.AllowedProxySubresources { | ||
var resource string | ||
switch slice := strings.Split(strings.TrimSpace(subresource), "/"); len(slice) { | ||
case 1: | ||
subresource = slice[0] | ||
case 2: | ||
resource, subresource = slice[0], slice[1] | ||
default: | ||
return nil, fmt.Errorf("--allowed-proxy-subresources: invalid format %q", subresource) | ||
} | ||
|
||
var matched bool | ||
for r, srs := range supportedProxyCoreSubresources { | ||
for _, sr := range srs { | ||
if (resource == "" || resource == r) && subresource == sr { | ||
gr := schema.GroupResource{Group: "", Resource: r} | ||
set := subresources[gr] | ||
if set == nil { | ||
set = sets.New[string]() | ||
subresources[gr] = set | ||
} | ||
set.Insert(sr) | ||
matched = true | ||
break | ||
} | ||
} | ||
} | ||
if !matched { | ||
return nil, fmt.Errorf("--allowed-proxy-subresources: unsupported subresources or invalid format %q", subresource) | ||
} | ||
} | ||
return &ExtraConfig{AllowedProxySubresources: subresources}, nil | ||
} |