Merge pull request #10652 from JanetKuo/kubectl-o-name

Add '-o name' to mutations
This commit is contained in:
Mike Danese
2015-07-29 09:46:10 -07:00
27 changed files with 241 additions and 56 deletions

View File

@@ -146,4 +146,5 @@ type RESTMapper interface {
VersionAndKindForResource(resource string) (defaultVersion, kind string, err error)
RESTMapping(kind string, versions ...string) (*RESTMapping, error)
AliasesForResource(resource string) ([]string, bool)
ResourceSingularizer(resource string) (singular string, err error)
}

View File

@@ -77,6 +77,8 @@ type DefaultRESTMapper struct {
reverse map[typeMeta]string
scopes map[typeMeta]RESTScope
versions []string
plurals map[string]string
singulars map[string]string
interfacesFunc VersionInterfacesFunc
}
@@ -93,6 +95,8 @@ func NewDefaultRESTMapper(versions []string, f VersionInterfacesFunc) *DefaultRE
mapping := make(map[string]typeMeta)
reverse := make(map[typeMeta]string)
scopes := make(map[typeMeta]RESTScope)
plurals := make(map[string]string)
singulars := make(map[string]string)
// TODO: verify name mappings work correctly when versions differ
return &DefaultRESTMapper{
@@ -100,12 +104,16 @@ func NewDefaultRESTMapper(versions []string, f VersionInterfacesFunc) *DefaultRE
reverse: reverse,
scopes: scopes,
versions: versions,
plurals: plurals,
singulars: singulars,
interfacesFunc: f,
}
}
func (m *DefaultRESTMapper) Add(scope RESTScope, kind string, version string, mixedCase bool) {
plural, singular := kindToResource(kind, mixedCase)
m.plurals[singular] = plural
m.singulars[plural] = singular
meta := typeMeta{APIVersion: version, Kind: kind}
_, ok1 := m.mapping[plural]
_, ok2 := m.mapping[strings.ToLower(plural)]
@@ -133,7 +141,7 @@ func kindToResource(kind string, mixedCase bool) (plural, singular string) {
singular = strings.ToLower(kind)
}
if strings.HasSuffix(singular, "status") {
plural = strings.TrimSuffix(singular, "status") + "statuses"
plural = singular + "es"
} else {
switch string(singular[len(singular)-1]) {
case "s":
@@ -147,6 +155,16 @@ func kindToResource(kind string, mixedCase bool) (plural, singular string) {
return
}
// ResourceSingularizer implements RESTMapper
// It converts a resource name from plural to singular (e.g., from pods to pod)
func (m *DefaultRESTMapper) ResourceSingularizer(resource string) (singular string, err error) {
singular, ok := m.singulars[resource]
if !ok {
return resource, fmt.Errorf("no singular of resource %q has been defined", resource)
}
return singular, nil
}
// VersionAndKindForResource implements RESTMapper
func (m *DefaultRESTMapper) VersionAndKindForResource(resource string) (defaultVersion, kind string, err error) {
meta, ok := m.mapping[strings.ToLower(resource)]
@@ -249,6 +267,18 @@ func (m *DefaultRESTMapper) AliasesForResource(alias string) ([]string, bool) {
// MultiRESTMapper is a wrapper for multiple RESTMappers.
type MultiRESTMapper []RESTMapper
// ResourceSingularizer converts a REST resource name from plural to singular (e.g., from pods to pod)
// This implementation supports multiple REST schemas and return the first match.
func (m MultiRESTMapper) ResourceSingularizer(resource string) (singular string, err error) {
for _, t := range m {
singular, err = t.ResourceSingularizer(resource)
if err == nil {
return
}
}
return
}
// VersionAndKindForResource provides the Version and Kind mappings for the
// REST resources. This implementation supports multiple REST schemas and return
// the first match.

View File

@@ -124,6 +124,40 @@ func TestKindToResource(t *testing.T) {
}
}
func TestRESTMapperResourceSingularizer(t *testing.T) {
testCases := []struct {
Kind, APIVersion string
MixedCase bool
Plural string
Singular string
}{
{Kind: "Pod", APIVersion: "test", MixedCase: true, Plural: "pods", Singular: "pod"},
{Kind: "Pod", APIVersion: "test", MixedCase: false, Plural: "pods", Singular: "pod"},
{Kind: "ReplicationController", APIVersion: "test", MixedCase: true, Plural: "replicationControllers", Singular: "replicationController"},
{Kind: "ReplicationController", APIVersion: "test", MixedCase: false, Plural: "replicationcontrollers", Singular: "replicationcontroller"},
{Kind: "ImageRepository", APIVersion: "test", MixedCase: true, Plural: "imageRepositories", Singular: "imageRepository"},
{Kind: "ImageRepository", APIVersion: "test", MixedCase: false, Plural: "imagerepositories", Singular: "imagerepository"},
{Kind: "Status", APIVersion: "test", MixedCase: true, Plural: "statuses", Singular: "status"},
{Kind: "Status", APIVersion: "test", MixedCase: false, Plural: "statuses", Singular: "status"},
{Kind: "lowercase", APIVersion: "test", MixedCase: false, Plural: "lowercases", Singular: "lowercase"},
// Don't add extra s if the original object is already plural
{Kind: "lowercases", APIVersion: "test", MixedCase: false, Plural: "lowercases", Singular: "lowercases"},
}
for i, testCase := range testCases {
mapper := NewDefaultRESTMapper([]string{"test"}, fakeInterfaces)
// create singular/plural mapping
mapper.Add(RESTScopeNamespace, testCase.Kind, testCase.APIVersion, testCase.MixedCase)
singular, _ := mapper.ResourceSingularizer(testCase.Plural)
if singular != testCase.Singular {
t.Errorf("%d: mismatched singular: %s, should be %s", i, singular, testCase.Singular)
}
}
}
func TestRESTMapperRESTMapping(t *testing.T) {
testCases := []struct {
Kind string