2014-09-30 00:15:00 +00:00
/ *
2015-05-01 16:19:44 +00:00
Copyright 2014 The Kubernetes Authors All rights reserved .
2014-09-30 00:15:00 +00:00
Licensed under the Apache License , Version 2.0 ( the "License" ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
http : //www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an "AS IS" BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
* /
2015-08-12 17:35:07 +00:00
package unversioned
2014-09-30 00:15:00 +00:00
import (
"fmt"
2015-08-05 22:03:47 +00:00
"k8s.io/kubernetes/pkg/api"
2015-10-12 20:48:14 +00:00
"k8s.io/kubernetes/pkg/api/unversioned"
2016-01-13 22:40:56 +00:00
"k8s.io/kubernetes/pkg/apimachinery/registered"
2016-04-15 22:30:15 +00:00
"k8s.io/kubernetes/pkg/apis/apps"
2016-02-09 12:53:14 +00:00
"k8s.io/kubernetes/pkg/apis/autoscaling"
2016-02-17 23:07:38 +00:00
"k8s.io/kubernetes/pkg/apis/batch"
2015-12-08 14:21:04 +00:00
"k8s.io/kubernetes/pkg/apis/extensions"
2016-02-12 18:58:43 +00:00
"k8s.io/kubernetes/pkg/client/restclient"
2016-02-25 18:38:35 +00:00
"k8s.io/kubernetes/pkg/client/typed/discovery"
2015-09-09 17:45:01 +00:00
"k8s.io/kubernetes/pkg/util/sets"
2015-08-05 22:03:47 +00:00
"k8s.io/kubernetes/pkg/version"
2016-03-06 02:00:36 +00:00
// Import solely to initialize client auth plugins.
_ "k8s.io/kubernetes/plugin/pkg/client/auth"
2014-09-30 00:15:00 +00:00
)
2016-01-06 23:59:54 +00:00
const (
legacyAPIPath = "/api"
defaultAPIPath = "/apis"
)
2014-09-30 00:15:00 +00:00
// New creates a Kubernetes client for the given config. This client works with pods,
2015-08-07 06:29:31 +00:00
// replication controllers, daemons, and services. It allows operations such as list, get, update
2014-09-30 00:15:00 +00:00
// and delete on these objects. An error is returned if the provided configuration
// is not valid.
2016-02-12 18:58:43 +00:00
func New ( c * restclient . Config ) ( * Client , error ) {
2014-10-03 02:03:52 +00:00
config := * c
2015-01-06 17:36:08 +00:00
if err := SetKubernetesDefaults ( & config ) ; err != nil {
return nil , err
2014-10-03 02:03:52 +00:00
}
2016-02-12 18:58:43 +00:00
client , err := restclient . RESTClientFor ( & config )
2014-09-30 00:15:00 +00:00
if err != nil {
return nil , err
}
2015-09-11 22:46:18 +00:00
2015-10-15 23:34:30 +00:00
discoveryConfig := * c
2016-02-25 18:38:35 +00:00
discoveryClient , err := discovery . NewDiscoveryClientForConfig ( & discoveryConfig )
2015-10-15 23:34:30 +00:00
if err != nil {
return nil , err
}
2016-02-09 12:53:14 +00:00
var autoscalingClient * AutoscalingClient
if registered . IsRegistered ( autoscaling . GroupName ) {
autoscalingConfig := * c
autoscalingClient , err = NewAutoscaling ( & autoscalingConfig )
if err != nil {
return nil , err
}
2015-09-11 22:46:18 +00:00
}
2016-02-09 12:53:14 +00:00
2016-02-17 23:07:38 +00:00
var batchClient * BatchClient
if registered . IsRegistered ( batch . GroupName ) {
batchConfig := * c
batchClient , err = NewBatch ( & batchConfig )
if err != nil {
return nil , err
}
}
2016-02-09 12:53:14 +00:00
var extensionsClient * ExtensionsClient
if registered . IsRegistered ( extensions . GroupName ) {
extensionsConfig := * c
extensionsClient , err = NewExtensions ( & extensionsConfig )
if err != nil {
return nil , err
}
2015-08-31 16:29:18 +00:00
}
2016-04-15 22:30:15 +00:00
var appsClient * AppsClient
if registered . IsRegistered ( apps . GroupName ) {
appsConfig := * c
appsClient , err = NewApps ( & appsConfig )
if err != nil {
return nil , err
}
}
2015-10-15 23:34:30 +00:00
2016-04-15 22:30:15 +00:00
return & Client { RESTClient : client , AutoscalingClient : autoscalingClient , BatchClient : batchClient , ExtensionsClient : extensionsClient , DiscoveryClient : discoveryClient , AppsClient : appsClient } , nil
2014-09-30 00:15:00 +00:00
}
2015-06-13 02:06:18 +00:00
// MatchesServerVersion queries the server to compares the build version
// (git hash) of the client with the server's build version. It returns an error
// if it failed to contact the server or if the versions are not an exact match.
2016-02-12 18:58:43 +00:00
func MatchesServerVersion ( client * Client , c * restclient . Config ) error {
2015-06-17 03:04:51 +00:00
var err error
if client == nil {
client , err = New ( c )
if err != nil {
return err
}
2015-01-07 20:59:22 +00:00
}
2016-01-21 02:33:34 +00:00
cVer := version . Get ( )
sVer , err := client . Discovery ( ) . ServerVersion ( )
2015-01-07 20:59:22 +00:00
if err != nil {
return fmt . Errorf ( "couldn't read version from server: %v\n" , err )
}
2016-01-21 02:33:34 +00:00
// GitVersion includes GitCommit and GitTreeState, but best to be safe?
if cVer . GitVersion != sVer . GitVersion || cVer . GitCommit != sVer . GitCommit || cVer . GitTreeState != cVer . GitTreeState {
return fmt . Errorf ( "server version (%#v) differs from client version (%#v)!\n" , sVer , cVer )
2015-01-07 20:59:22 +00:00
}
return nil
}
2015-06-13 02:06:18 +00:00
// NegotiateVersion queries the server's supported api versions to find
// a version that both client and server support.
2015-06-17 03:04:51 +00:00
// - If no version is provided, try registered client versions in order of
2015-06-13 02:06:18 +00:00
// preference.
// - If version is provided, but not default config (explicitly requested via
// commandline flag), and is unsupported by the server, print a warning to
// stderr and try client's registered versions in order of preference.
// - If version is config default, and the server does not support it,
// return an error.
2016-02-12 18:58:43 +00:00
func NegotiateVersion ( client * Client , c * restclient . Config , requestedGV * unversioned . GroupVersion , clientRegisteredGVs [ ] unversioned . GroupVersion ) ( * unversioned . GroupVersion , error ) {
2015-06-17 03:04:51 +00:00
var err error
if client == nil {
client , err = New ( c )
if err != nil {
2015-11-13 21:20:54 +00:00
return nil , err
2015-06-17 03:04:51 +00:00
}
2015-06-13 02:06:18 +00:00
}
2015-09-09 17:45:01 +00:00
clientVersions := sets . String { }
2015-11-13 21:20:54 +00:00
for _ , gv := range clientRegisteredGVs {
clientVersions . Insert ( gv . String ( ) )
2015-06-13 02:06:18 +00:00
}
2015-12-11 01:51:32 +00:00
groups , err := client . ServerGroups ( )
2015-06-13 02:06:18 +00:00
if err != nil {
2015-10-20 15:45:46 +00:00
// This is almost always a connection error, and higher level code should treat this as a generic error,
// not a negotiation specific error.
2015-11-13 21:20:54 +00:00
return nil , err
2015-06-13 02:06:18 +00:00
}
2016-02-25 18:38:35 +00:00
versions := unversioned . ExtractGroupVersions ( groups )
2015-09-09 17:45:01 +00:00
serverVersions := sets . String { }
2015-12-11 01:51:32 +00:00
for _ , v := range versions {
2015-06-13 02:06:18 +00:00
serverVersions . Insert ( v )
}
2015-11-13 21:20:54 +00:00
2015-06-13 02:06:18 +00:00
// If no version requested, use config version (may also be empty).
2015-11-13 21:20:54 +00:00
// make a copy of the original so we don't risk mutating input here or in the returned value
var preferredGV * unversioned . GroupVersion
switch {
case requestedGV != nil :
t := * requestedGV
preferredGV = & t
case c . GroupVersion != nil :
t := * c . GroupVersion
preferredGV = & t
2015-06-13 02:06:18 +00:00
}
2015-11-13 21:20:54 +00:00
2015-06-13 02:06:18 +00:00
// If version explicitly requested verify that both client and server support it.
// If server does not support warn, but try to negotiate a lower version.
2015-11-13 21:20:54 +00:00
if preferredGV != nil {
if ! clientVersions . Has ( preferredGV . String ( ) ) {
return nil , fmt . Errorf ( "client does not support API version %q; client supported API versions: %v" , preferredGV , clientVersions )
2015-06-13 02:06:18 +00:00
}
2015-11-13 21:20:54 +00:00
if serverVersions . Has ( preferredGV . String ( ) ) {
return preferredGV , nil
2015-06-13 02:06:18 +00:00
}
// If we are using an explicit config version the server does not support, fail.
2015-11-13 21:20:54 +00:00
if ( c . GroupVersion != nil ) && ( * preferredGV == * c . GroupVersion ) {
return nil , fmt . Errorf ( "server does not support API version %q" , preferredGV )
2015-06-13 02:06:18 +00:00
}
}
2015-11-13 21:20:54 +00:00
for _ , clientGV := range clientRegisteredGVs {
if serverVersions . Has ( clientGV . String ( ) ) {
2015-06-13 02:06:18 +00:00
// Version was not explicitly requested in command config (--api-version).
// Ok to fall back to a supported version with a warning.
2015-10-01 04:18:50 +00:00
// TODO: caesarxuchao: enable the warning message when we have
// proper fix. Please refer to issue #14895.
// if len(version) != 0 {
// glog.Warningf("Server does not support API version '%s'. Falling back to '%s'.", version, clientVersion)
// }
2015-11-13 21:20:54 +00:00
t := clientGV
return & t , nil
2015-06-13 02:06:18 +00:00
}
}
2015-11-13 21:20:54 +00:00
return nil , fmt . Errorf ( "failed to negotiate an api version; server supports: %v, client supports: %v" ,
serverVersions , clientVersions )
2015-06-13 02:06:18 +00:00
}
2014-09-30 00:15:00 +00:00
// NewOrDie creates a Kubernetes client and panics if the provided API version is not recognized.
2016-02-12 18:58:43 +00:00
func NewOrDie ( c * restclient . Config ) * Client {
2014-09-30 00:15:00 +00:00
client , err := New ( c )
if err != nil {
panic ( err )
}
return client
}
2015-05-29 21:31:00 +00:00
// NewInCluster is a shortcut for calling InClusterConfig() and then New().
func NewInCluster ( ) ( * Client , error ) {
2016-02-12 18:58:43 +00:00
cc , err := restclient . InClusterConfig ( )
2015-05-29 21:31:00 +00:00
if err != nil {
return nil , err
}
return New ( cc )
}
2015-01-06 17:36:08 +00:00
// SetKubernetesDefaults sets default values on the provided client config for accessing the
// Kubernetes API or returns an error if any of the defaults are impossible or invalid.
2015-09-24 01:17:09 +00:00
// TODO: this method needs to be split into one that sets defaults per group, expected to be fix in PR "Refactoring clientcache.go and helper.go #14592"
2016-02-12 18:58:43 +00:00
func SetKubernetesDefaults ( config * restclient . Config ) error {
2016-01-06 23:59:54 +00:00
if config . APIPath == "" {
config . APIPath = legacyAPIPath
2015-01-06 17:36:08 +00:00
}
2016-01-13 22:40:56 +00:00
g , err := registered . Group ( api . GroupName )
2015-12-11 01:51:32 +00:00
if err != nil {
return err
2015-01-06 17:36:08 +00:00
}
2015-12-11 01:51:32 +00:00
// TODO: Unconditionally set the config.Version, until we fix the config.
copyGroupVersion := g . GroupVersion
config . GroupVersion = & copyGroupVersion
2015-01-06 17:36:08 +00:00
if config . Codec == nil {
2015-12-21 05:32:52 +00:00
config . Codec = api . Codecs . LegacyCodec ( * config . GroupVersion )
2015-01-06 17:36:08 +00:00
}
2016-01-15 05:00:58 +00:00
2016-02-12 18:58:43 +00:00
return restclient . SetKubernetesDefaults ( config )
2016-01-15 05:00:58 +00:00
}