mirror of https://github.com/k3s-io/k3s
Merge pull request #6573 from kargakis/setup-resource-aliases
Support setting up aliases for groups of resourcespull/6/head
commit
3bc42f1635
|
@ -5,3 +5,5 @@ port: 9042
|
|||
containerPort: 9042
|
||||
selector:
|
||||
name: cassandra
|
||||
labels:
|
||||
name: cassandra
|
||||
|
|
|
@ -615,6 +615,18 @@ __EOF__
|
|||
kube::test::get_object_assert 'nodes/127.0.0.1 service/kubernetes' "{{range.items}}{{$id_field}}:{{end}}" '127.0.0.1:kubernetes:'
|
||||
|
||||
|
||||
#####################
|
||||
# Resource aliasing #
|
||||
#####################
|
||||
|
||||
kube::log::status "Testing resource aliasing"
|
||||
kubectl create -f examples/cassandra/cassandra.yaml
|
||||
kubectl create -f examples/cassandra/cassandra-controller.yaml
|
||||
kubectl create -f examples/cassandra/cassandra-service.yaml
|
||||
kube::test::get_object_assert "all -l'name=cassandra'" "{{range.items}}{{$id_field}}:{{end}}" 'cassandra:cassandra:cassandra:'
|
||||
kubectl delete all -l name=cassandra
|
||||
|
||||
|
||||
###########
|
||||
# Swagger #
|
||||
###########
|
||||
|
|
|
@ -61,6 +61,9 @@ var SelfLinker = runtime.SelfLinker(accessor)
|
|||
// Kubernetes versions.
|
||||
var RESTMapper meta.RESTMapper
|
||||
|
||||
// userResources is a group of resources mostly used by a kubectl user
|
||||
var userResources = []string{"rc", "svc", "pods", "pvc"}
|
||||
|
||||
// InterfacesFor returns the default Codec and ResourceVersioner for a given version
|
||||
// string, or an error if the version is not known.
|
||||
func InterfacesFor(version string) (*meta.VersionInterfaces, error) {
|
||||
|
@ -124,6 +127,9 @@ func init() {
|
|||
"PersistentVolume": true,
|
||||
}
|
||||
|
||||
// setup aliases for groups of resources
|
||||
mapper.AddResourceAlias("all", userResources...)
|
||||
|
||||
// these kinds should be excluded from the list of resources
|
||||
ignoredKinds := util.NewStringSet(
|
||||
"ListOptions",
|
||||
|
|
|
@ -147,4 +147,5 @@ type RESTMapping struct {
|
|||
type RESTMapper interface {
|
||||
VersionAndKindForResource(resource string) (defaultVersion, kind string, err error)
|
||||
RESTMapping(kind string, versions ...string) (*RESTMapping, error)
|
||||
AliasesForResource(resource string) ([]string, bool)
|
||||
}
|
||||
|
|
|
@ -226,3 +226,22 @@ func (m *DefaultRESTMapper) RESTMapping(kind string, versions ...string) (*RESTM
|
|||
MetadataAccessor: interfaces.MetadataAccessor,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// aliasToResource is used for mapping aliases to resources
|
||||
var aliasToResource = map[string][]string{}
|
||||
|
||||
// AddResourceAlias maps aliases to resources
|
||||
func (m *DefaultRESTMapper) AddResourceAlias(alias string, resources ...string) {
|
||||
if len(resources) == 0 {
|
||||
return
|
||||
}
|
||||
aliasToResource[alias] = resources
|
||||
}
|
||||
|
||||
// AliasesForResource returns whether a resource has an alias or not
|
||||
func (m *DefaultRESTMapper) AliasesForResource(alias string) ([]string, bool) {
|
||||
if res, ok := aliasToResource[alias]; ok {
|
||||
return res, true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
|
|
@ -230,6 +230,7 @@ func (b *Builder) SelectAllParam(selectAll bool) *Builder {
|
|||
// When two or more arguments are received, they must be a single type and resource name(s).
|
||||
// The allowEmptySelector permits to select all the resources (via Everything func).
|
||||
func (b *Builder) ResourceTypeOrNameArgs(allowEmptySelector bool, args ...string) *Builder {
|
||||
args = b.replaceAliases(args)
|
||||
if ok, err := hasCombinedTypeArgs(args); ok {
|
||||
if err != nil {
|
||||
b.errs = append(b.errs, err)
|
||||
|
@ -269,6 +270,18 @@ func (b *Builder) ResourceTypeOrNameArgs(allowEmptySelector bool, args ...string
|
|||
return b
|
||||
}
|
||||
|
||||
func (b *Builder) replaceAliases(args []string) []string {
|
||||
replaced := []string{}
|
||||
for _, arg := range args {
|
||||
if aliases, ok := b.mapper.AliasesForResource(arg); ok {
|
||||
arg = strings.Join(aliases, ",")
|
||||
}
|
||||
replaced = append(replaced, arg)
|
||||
}
|
||||
|
||||
return replaced
|
||||
}
|
||||
|
||||
func hasCombinedTypeArgs(args []string) (bool, error) {
|
||||
hasSlash := 0
|
||||
for _, s := range args {
|
||||
|
|
|
@ -783,3 +783,36 @@ func TestReceiveMultipleErrors(t *testing.T) {
|
|||
t.Errorf("unexpected errors %v", errs)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReplaceAliases(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
args []string
|
||||
expected []string
|
||||
}{
|
||||
{
|
||||
name: "no-replacement",
|
||||
args: []string{"service", "pods", "rc"},
|
||||
expected: []string{"service", "pods", "rc"},
|
||||
},
|
||||
{
|
||||
name: "all-replacement",
|
||||
args: []string{"all"},
|
||||
expected: []string{"rc,svc,pods,pvc"},
|
||||
},
|
||||
}
|
||||
|
||||
b := NewBuilder(latest.RESTMapper, api.Scheme, fakeClient())
|
||||
|
||||
for _, test := range tests {
|
||||
replaced := b.replaceAliases(test.args)
|
||||
if len(replaced) != len(test.expected) {
|
||||
t.Errorf("%s: unexpected args length: expected %d, got %d", test.name, len(test.expected), len(replaced))
|
||||
}
|
||||
for i, arg := range test.expected {
|
||||
if arg != replaced[i] {
|
||||
t.Errorf("%s: unexpected argument: expected %s, got %s", test.name, arg, replaced[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue