Merge pull request #91295 from ingvagabund/have-kubectl-explain-check-all-gvks

kubectl explain: detect resource group in case there are two or more groups discovered

Kubernetes-commit: e19c88fbf6f292ca5619d7974c0f79d1d6042e95
This commit is contained in:
Kubernetes Publisher 2021-03-07 08:31:42 -08:00
commit d98ca0f41f
5 changed files with 64 additions and 17 deletions

6
Godeps/Godeps.json generated
View File

@ -924,7 +924,7 @@
}, },
{ {
"ImportPath": "k8s.io/apimachinery", "ImportPath": "k8s.io/apimachinery",
"Rev": "283a3268598b" "Rev": "543ebb56644a"
}, },
{ {
"ImportPath": "k8s.io/cli-runtime", "ImportPath": "k8s.io/cli-runtime",
@ -932,11 +932,11 @@
}, },
{ {
"ImportPath": "k8s.io/client-go", "ImportPath": "k8s.io/client-go",
"Rev": "8df02b312ceb" "Rev": "fa8f4cc307b8"
}, },
{ {
"ImportPath": "k8s.io/code-generator", "ImportPath": "k8s.io/code-generator",
"Rev": "cc2553e427ca" "Rev": "5f36bce169a3"
}, },
{ {
"ImportPath": "k8s.io/component-base", "ImportPath": "k8s.io/component-base",

10
go.mod
View File

@ -34,9 +34,9 @@ require (
golang.org/x/sys v0.0.0-20210225134936-a50acf3fe073 golang.org/x/sys v0.0.0-20210225134936-a50acf3fe073
gopkg.in/yaml.v2 v2.2.8 gopkg.in/yaml.v2 v2.2.8
k8s.io/api v0.0.0-20210307052319-8e35c5077a13 k8s.io/api v0.0.0-20210307052319-8e35c5077a13
k8s.io/apimachinery v0.0.0-20210306132128-283a3268598b k8s.io/apimachinery v0.0.0-20210307091931-543ebb56644a
k8s.io/cli-runtime v0.0.0-20210306142336-0d4cd42bdbc7 k8s.io/cli-runtime v0.0.0-20210306142336-0d4cd42bdbc7
k8s.io/client-go v0.0.0-20210307052715-8df02b312ceb k8s.io/client-go v0.0.0-20210307092416-fa8f4cc307b8
k8s.io/component-base v0.0.0-20210306134456-247e7b3ba560 k8s.io/component-base v0.0.0-20210306134456-247e7b3ba560
k8s.io/component-helpers v0.0.0-20210306134759-13f6e97127a5 k8s.io/component-helpers v0.0.0-20210306134759-13f6e97127a5
k8s.io/klog/v2 v2.5.0 k8s.io/klog/v2 v2.5.0
@ -49,10 +49,10 @@ require (
replace ( replace (
k8s.io/api => k8s.io/api v0.0.0-20210307052319-8e35c5077a13 k8s.io/api => k8s.io/api v0.0.0-20210307052319-8e35c5077a13
k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20210306132128-283a3268598b k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20210307091931-543ebb56644a
k8s.io/cli-runtime => k8s.io/cli-runtime v0.0.0-20210306142336-0d4cd42bdbc7 k8s.io/cli-runtime => k8s.io/cli-runtime v0.0.0-20210306142336-0d4cd42bdbc7
k8s.io/client-go => k8s.io/client-go v0.0.0-20210307052715-8df02b312ceb k8s.io/client-go => k8s.io/client-go v0.0.0-20210307092416-fa8f4cc307b8
k8s.io/code-generator => k8s.io/code-generator v0.0.0-20210306131731-cc2553e427ca k8s.io/code-generator => k8s.io/code-generator v0.0.0-20210307061741-5f36bce169a3
k8s.io/component-base => k8s.io/component-base v0.0.0-20210306134456-247e7b3ba560 k8s.io/component-base => k8s.io/component-base v0.0.0-20210306134456-247e7b3ba560
k8s.io/component-helpers => k8s.io/component-helpers v0.0.0-20210306134759-13f6e97127a5 k8s.io/component-helpers => k8s.io/component-helpers v0.0.0-20210306134759-13f6e97127a5
k8s.io/metrics => k8s.io/metrics v0.0.0-20210306142006-6087b643ac2c k8s.io/metrics => k8s.io/metrics v0.0.0-20210306142006-6087b643ac2c

6
go.sum
View File

@ -629,10 +629,10 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k= honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
k8s.io/api v0.0.0-20210307052319-8e35c5077a13/go.mod h1:oXIlxI7P0Fm+wAl92K966U84hJ8jYH+vvhPMIFggXJQ= k8s.io/api v0.0.0-20210307052319-8e35c5077a13/go.mod h1:oXIlxI7P0Fm+wAl92K966U84hJ8jYH+vvhPMIFggXJQ=
k8s.io/apimachinery v0.0.0-20210306132128-283a3268598b/go.mod h1:2LERmYT9PI3b4uQt87vnb2UVkblBDzZhucIf8PxvJ2o= k8s.io/apimachinery v0.0.0-20210307091931-543ebb56644a/go.mod h1:2LERmYT9PI3b4uQt87vnb2UVkblBDzZhucIf8PxvJ2o=
k8s.io/cli-runtime v0.0.0-20210306142336-0d4cd42bdbc7/go.mod h1:75VZI2v5ikaS8lKia04ivLTySY9Ylvec0MIC6Ctu+4A= k8s.io/cli-runtime v0.0.0-20210306142336-0d4cd42bdbc7/go.mod h1:75VZI2v5ikaS8lKia04ivLTySY9Ylvec0MIC6Ctu+4A=
k8s.io/client-go v0.0.0-20210307052715-8df02b312ceb/go.mod h1:qv0Qrnu6UEKMfXsRgVBD0X/s4dNXOmHHWD79odx0C38= k8s.io/client-go v0.0.0-20210307092416-fa8f4cc307b8/go.mod h1:nRX9GyJtP168lusL9mb1HJYOXaQah70f9jKd3XqOJNs=
k8s.io/code-generator v0.0.0-20210306131731-cc2553e427ca/go.mod h1:+mcF5UBXubtZGDob4qNax0wjPWZG3wKwQNlGJiVCadA= k8s.io/code-generator v0.0.0-20210307061741-5f36bce169a3/go.mod h1:+mcF5UBXubtZGDob4qNax0wjPWZG3wKwQNlGJiVCadA=
k8s.io/component-base v0.0.0-20210306134456-247e7b3ba560/go.mod h1:I1nomQYfl44UYAQFJJL/eA28WOBZNwpTUseGxPE/jpE= k8s.io/component-base v0.0.0-20210306134456-247e7b3ba560/go.mod h1:I1nomQYfl44UYAQFJJL/eA28WOBZNwpTUseGxPE/jpE=
k8s.io/component-helpers v0.0.0-20210306134759-13f6e97127a5/go.mod h1:kSezxJTi9WjrBqCVq2uzr2STcQ5TMu85JuNmNpWo8to= k8s.io/component-helpers v0.0.0-20210306134759-13f6e97127a5/go.mod h1:kSezxJTi9WjrBqCVq2uzr2STcQ5TMu85JuNmNpWo8to=
k8s.io/gengo v0.0.0-20200413195148-3a45101e95ac/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0= k8s.io/gengo v0.0.0-20200413195148-3a45101e95ac/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0=

View File

@ -120,12 +120,22 @@ func (o *ExplainOptions) Run(args []string) error {
recursive := o.Recursive recursive := o.Recursive
apiVersionString := o.APIVersion apiVersionString := o.APIVersion
// TODO: After we figured out the new syntax to separate group and resource, allow var fullySpecifiedGVR schema.GroupVersionResource
// the users to use it in explain (kubectl explain <group><syntax><resource>). var fieldsPath []string
// Refer to issue #16039 for why we do this. Refer to PR #15808 that used "/" syntax. var err error
fullySpecifiedGVR, fieldsPath, err := explain.SplitAndParseResourceRequest(args[0], o.Mapper) if len(apiVersionString) == 0 {
if err != nil { fullySpecifiedGVR, fieldsPath, err = explain.SplitAndParseResourceRequestWithMatchingPrefix(args[0], o.Mapper)
return err if err != nil {
return err
}
} else {
// TODO: After we figured out the new syntax to separate group and resource, allow
// the users to use it in explain (kubectl explain <group><syntax><resource>).
// Refer to issue #16039 for why we do this. Refer to PR #15808 that used "/" syntax.
fullySpecifiedGVR, fieldsPath, err = explain.SplitAndParseResourceRequest(args[0], o.Mapper)
if err != nil {
return err
}
} }
gvk, _ := o.Mapper.KindFor(fullySpecifiedGVR) gvk, _ := o.Mapper.KindFor(fullySpecifiedGVR)

View File

@ -50,6 +50,43 @@ func SplitAndParseResourceRequest(inResource string, mapper meta.RESTMapper) (sc
if err != nil { if err != nil {
return schema.GroupVersionResource{}, nil, err return schema.GroupVersionResource{}, nil, err
} }
return gvr, fieldsPath, nil
}
// SplitAndParseResourceRequestWithMatchingPrefix separates the users input into a model and fields
// while selecting gvr whose (resource, group) prefix matches the resource
func SplitAndParseResourceRequestWithMatchingPrefix(inResource string, mapper meta.RESTMapper) (gvr schema.GroupVersionResource, fieldsPath []string, err error) {
// ignore trailing period
inResource = strings.TrimSuffix(inResource, ".")
dotParts := strings.Split(inResource, ".")
gvrs, err := mapper.ResourcesFor(schema.GroupVersionResource{Resource: dotParts[0]})
if err != nil {
return schema.GroupVersionResource{}, nil, err
}
for _, gvrItem := range gvrs {
// Find first gvr whose gr prefixes requested resource
groupResource := gvrItem.GroupResource().String()
if strings.HasPrefix(inResource, groupResource) {
resourceSuffix := inResource[len(groupResource):]
if len(resourceSuffix) > 0 {
dotParts := strings.Split(resourceSuffix, ".")
if len(dotParts) > 0 {
fieldsPath = dotParts[1:]
}
}
return gvrItem, fieldsPath, nil
}
}
// If no match, take the first (the highest priority) gvr
if len(gvrs) > 0 {
gvr = gvrs[0]
_, fieldsPath = splitDotNotation(inResource)
}
return gvr, fieldsPath, nil return gvr, fieldsPath, nil
} }