mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-04 23:17:50 +00:00
Merge pull request #17316 from deads2k/gv-ClientForVersion
Auto commit by PR queue bot
This commit is contained in:
@@ -18,6 +18,7 @@ package util
|
||||
|
||||
import (
|
||||
"k8s.io/kubernetes/pkg/api/registered"
|
||||
"k8s.io/kubernetes/pkg/api/unversioned"
|
||||
client "k8s.io/kubernetes/pkg/client/unversioned"
|
||||
"k8s.io/kubernetes/pkg/client/unversioned/clientcmd"
|
||||
)
|
||||
@@ -60,18 +61,37 @@ func (c *ClientCache) ClientConfigForVersion(version string) (*client.Config, er
|
||||
}
|
||||
// TODO: have a better config copy method
|
||||
config := *c.defaultConfig
|
||||
negotiatedVersion, err := client.NegotiateVersion(c.defaultClient, &config, version, registered.RegisteredVersions)
|
||||
|
||||
// TODO these fall out when we finish the refactor
|
||||
var preferredGV *unversioned.GroupVersion
|
||||
if len(version) > 0 {
|
||||
gv, err := unversioned.ParseGroupVersion(version)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
preferredGV = &gv
|
||||
}
|
||||
registeredGVs := []unversioned.GroupVersion{}
|
||||
for _, gvString := range registered.RegisteredVersions {
|
||||
gv, err := unversioned.ParseGroupVersion(gvString)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
registeredGVs = append(registeredGVs, gv)
|
||||
}
|
||||
|
||||
negotiatedVersion, err := client.NegotiateVersion(c.defaultClient, &config, preferredGV, registeredGVs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config.Version = negotiatedVersion
|
||||
config.GroupVersion = negotiatedVersion
|
||||
client.SetKubernetesDefaults(&config)
|
||||
c.configs[version] = &config
|
||||
|
||||
// `version` does not necessarily equal `config.Version`. However, we know that we call this method again with
|
||||
// `config.Version`, we should get the the config we've just built.
|
||||
configCopy := config
|
||||
c.configs[config.Version] = &configCopy
|
||||
c.configs[config.GroupVersion.String()] = &configCopy
|
||||
|
||||
return &config, nil
|
||||
}
|
||||
@@ -91,6 +111,6 @@ func (c *ClientCache) ClientForVersion(version string) (*client.Client, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c.clients[config.Version] = client
|
||||
c.clients[config.GroupVersion.String()] = client
|
||||
return client, nil
|
||||
}
|
||||
|
||||
@@ -133,7 +133,10 @@ func NewFactory(optionalClientConfig clientcmd.ClientConfig) *Factory {
|
||||
Object: func() (meta.RESTMapper, runtime.ObjectTyper) {
|
||||
cfg, err := clientConfig.ClientConfig()
|
||||
CheckErr(err)
|
||||
cmdApiVersion := cfg.Version
|
||||
cmdApiVersion := ""
|
||||
if cfg.GroupVersion != nil {
|
||||
cmdApiVersion = cfg.GroupVersion.String()
|
||||
}
|
||||
|
||||
return kubectl.OutputVersionMapper{RESTMapper: mapper, OutputVersion: cmdApiVersion}, api.Scheme
|
||||
},
|
||||
@@ -576,7 +579,10 @@ func (f *Factory) PrinterForMapping(cmd *cobra.Command, mapping *meta.RESTMappin
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defaultVersion := clientConfig.Version
|
||||
defaultVersion := ""
|
||||
if clientConfig.GroupVersion != nil {
|
||||
defaultVersion = clientConfig.GroupVersion.String()
|
||||
}
|
||||
|
||||
version := OutputVersion(cmd, defaultVersion)
|
||||
if len(version) == 0 {
|
||||
|
||||
@@ -22,6 +22,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"k8s.io/kubernetes/pkg/api/meta"
|
||||
"k8s.io/kubernetes/pkg/api/unversioned"
|
||||
"k8s.io/kubernetes/pkg/kubectl"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
@@ -75,6 +76,7 @@ func ValidateOutputArgs(cmd *cobra.Command) error {
|
||||
}
|
||||
|
||||
// OutputVersion returns the preferred output version for generic content (JSON, YAML, or templates)
|
||||
// TODO, when this has no callers, replace it with OutputVersionFromGroupVersion. Also this shoudl return a GroupVersion
|
||||
func OutputVersion(cmd *cobra.Command, defaultVersion string) string {
|
||||
outputVersion := GetFlagString(cmd, "output-version")
|
||||
if len(outputVersion) == 0 {
|
||||
@@ -83,6 +85,15 @@ func OutputVersion(cmd *cobra.Command, defaultVersion string) string {
|
||||
return outputVersion
|
||||
}
|
||||
|
||||
// OutputVersionFromGroupVersion returns the preferred output version for generic content (JSON, YAML, or templates)
|
||||
func OutputVersionFromGroupVersion(cmd *cobra.Command, defaultGV *unversioned.GroupVersion) string {
|
||||
outputVersion := GetFlagString(cmd, "output-version")
|
||||
if len(outputVersion) == 0 && defaultGV != nil {
|
||||
outputVersion = defaultGV.String()
|
||||
}
|
||||
return outputVersion
|
||||
}
|
||||
|
||||
// PrinterForCommand returns the default printer for this command.
|
||||
// Requires that printer flags have been added to cmd (see AddPrinterFlags).
|
||||
func PrinterForCommand(cmd *cobra.Command) (kubectl.ResourcePrinter, bool, error) {
|
||||
|
||||
Reference in New Issue
Block a user