コード例 #1
0
ファイル: clientcache.go プロジェクト: johscheuer/kubernetes
// also looks up the discovery client.  We can't do this during init because the flags won't have been set
// because this is constructed pre-command execution before the command tree is even set up
func (c *ClientCache) getDefaultConfig() (restclient.Config, discovery.DiscoveryInterface, error) {
	c.defaultConfigLock.Lock()
	defer c.defaultConfigLock.Unlock()

	if c.defaultConfig != nil && c.discoveryClient != nil {
		return *c.defaultConfig, c.discoveryClient, nil
	}

	config, err := c.loader.ClientConfig()
	if err != nil {
		return restclient.Config{}, nil, err
	}
	discoveryClient, err := c.discoveryClientFactory.DiscoveryClient()
	if err != nil {
		return restclient.Config{}, nil, err
	}
	if c.matchVersion {
		if err := discovery.MatchesServerVersion(discoveryClient); err != nil {
			return restclient.Config{}, nil, err
		}
	}

	c.defaultConfig = config
	c.discoveryClient = discoveryClient
	return *c.defaultConfig, c.discoveryClient, nil
}
コード例 #2
0
// ClientConfigForVersion returns the correct config for a server
func (c *ClientCache) ClientConfigForVersion(version *unversioned.GroupVersion) (*restclient.Config, error) {
	if c.defaultConfig == nil {
		config, err := c.loader.ClientConfig()
		if err != nil {
			return nil, err
		}
		c.defaultConfig = config
		if c.matchVersion {
			if err := discovery.MatchesServerVersion(c.discoveryClient, config); err != nil {
				return nil, err
			}
		}
	}
	if version != nil {
		if config, ok := c.configs[*version]; ok {
			return config, nil
		}
	}

	// TODO: have a better config copy method
	config := *c.defaultConfig

	// TODO these fall out when we finish the refactor
	var preferredGV *unversioned.GroupVersion
	if version != nil {
		versionCopy := *version
		preferredGV = &versionCopy
	}

	oldclient.SetKubernetesDefaults(&config)
	negotiatedVersion, err := discovery.NegotiateVersion(c.discoveryClient, &config, preferredGV, registered.EnabledVersions())
	if err != nil {
		return nil, err
	}
	config.GroupVersion = negotiatedVersion

	if version != nil {
		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 config we've just built.
	configCopy := config
	c.configs[*config.GroupVersion] = &configCopy

	return &config, nil
}