Merge pull request #62877 from deads2k/cli-34-describer
Automatic merge from submit-queue (batch tested with PRs 62982, 63075, 63067, 62877, 63141). 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>. make describers more generic from the CLI I've made this change very small so the intent and explanation make sense to people. Clients are not generic. Client**Configs** are generic. We faced this distinction in the apiserver and it took us a little to hurdle it. When you try to provide a generic example or function, you need to provide Client**Config**, not a kube clientset. The reason is that the code you're calling may have generated their own clientset, may want to use a dynamic one, or may want to a simple restclient. As we seek to make `kubectl` primitives more generally applicable, this is an example we'll want to follow. I suspect we'll be making more changes along these veins as we tease out the generic pieces of `kubectl ` to make a friendly CLI library. @kubernetes/sig-cli-maintainers /hold Holding for a few days to make sure that people have time to read and digest. ```release-note NONE ```
This commit is contained in:
@@ -107,6 +107,7 @@ go_library(
|
||||
"//vendor/k8s.io/apimachinery/pkg/util/sets:go_default_library",
|
||||
"//vendor/k8s.io/client-go/dynamic:go_default_library",
|
||||
"//vendor/k8s.io/client-go/kubernetes:go_default_library",
|
||||
"//vendor/k8s.io/client-go/rest:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
|
@@ -47,6 +47,7 @@ import (
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
"k8s.io/client-go/dynamic"
|
||||
externalclient "k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/rest"
|
||||
"k8s.io/kubernetes/pkg/api/events"
|
||||
"k8s.io/kubernetes/pkg/api/legacyscheme"
|
||||
"k8s.io/kubernetes/pkg/api/ref"
|
||||
@@ -123,7 +124,16 @@ func (pw *prefixWriter) Flush() {
|
||||
}
|
||||
}
|
||||
|
||||
func describerMap(c clientset.Interface, externalclient externalclient.Interface) map[schema.GroupKind]printers.Describer {
|
||||
func describerMap(clientConfig *rest.Config) (map[schema.GroupKind]printers.Describer, error) {
|
||||
c, err := clientset.NewForConfig(clientConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
externalclient, err := externalclient.NewForConfig(clientConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
m := map[schema.GroupKind]printers.Describer{
|
||||
api.Kind("Pod"): &PodDescriber{c},
|
||||
api.Kind("ReplicationController"): &ReplicationControllerDescriber{c},
|
||||
@@ -164,24 +174,19 @@ func describerMap(c clientset.Interface, externalclient externalclient.Interface
|
||||
scheduling.Kind("PriorityClass"): &PriorityClassDescriber{c},
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
// DescribableResources lists all resource types we can describe.
|
||||
func DescribableResources() []string {
|
||||
keys := make([]string, 0)
|
||||
|
||||
for k := range describerMap(nil, nil) {
|
||||
resource := strings.ToLower(k.Kind)
|
||||
keys = append(keys, resource)
|
||||
}
|
||||
return keys
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// DescriberFor returns the default describe functions for each of the standard
|
||||
// Kubernetes types.
|
||||
func DescriberFor(kind schema.GroupKind, c clientset.Interface, externalclient externalclient.Interface) (printers.Describer, bool) {
|
||||
f, ok := describerMap(c, externalclient)[kind]
|
||||
func DescriberFor(kind schema.GroupKind, clientConfig *rest.Config) (printers.Describer, bool) {
|
||||
describers, err := describerMap(clientConfig)
|
||||
if err != nil {
|
||||
glog.V(1).Info(err)
|
||||
return nil, false
|
||||
}
|
||||
|
||||
f, ok := describers[kind]
|
||||
return f, ok
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user