github.com/timstclair/heapster@v0.20.0-alpha1/Godeps/_workspace/src/k8s.io/kubernetes/pkg/client/unversioned/extensions.go (about)

     1  /*
     2  Copyright 2015 The Kubernetes Authors All rights reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package unversioned
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  	"strings"
    23  
    24  	"k8s.io/kubernetes/pkg/api/latest"
    25  	"k8s.io/kubernetes/pkg/api/unversioned"
    26  	"k8s.io/kubernetes/pkg/version"
    27  )
    28  
    29  // Interface holds the experimental methods for clients of Kubernetes
    30  // to allow mock testing.
    31  // Features of Extensions group are not supported and may be changed or removed in
    32  // incompatible ways at any time.
    33  type ExtensionsInterface interface {
    34  	VersionInterface
    35  	HorizontalPodAutoscalersNamespacer
    36  	ScaleNamespacer
    37  	DaemonSetsNamespacer
    38  	DeploymentsNamespacer
    39  	JobsNamespacer
    40  	IngressNamespacer
    41  }
    42  
    43  // ExtensionsClient is used to interact with experimental Kubernetes features.
    44  // Features of Extensions group are not supported and may be changed or removed in
    45  // incompatible ways at any time.
    46  type ExtensionsClient struct {
    47  	*RESTClient
    48  }
    49  
    50  // ServerVersion retrieves and parses the server's version.
    51  func (c *ExtensionsClient) ServerVersion() (*version.Info, error) {
    52  	body, err := c.Get().AbsPath("/version").Do().Raw()
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  	var info version.Info
    57  	err = json.Unmarshal(body, &info)
    58  	if err != nil {
    59  		return nil, fmt.Errorf("got '%s': %v", string(body), err)
    60  	}
    61  	return &info, nil
    62  }
    63  
    64  // ServerAPIVersions retrieves and parses the list of experimental API versions the
    65  // server supports.
    66  func (c *ExtensionsClient) ServerAPIVersions() (*unversioned.APIVersions, error) {
    67  	body, err := c.Get().AbsPath("/apis/extensions").Do().Raw()
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  	var v unversioned.APIVersions
    72  	err = json.Unmarshal(body, &v)
    73  	if err != nil {
    74  		return nil, fmt.Errorf("got '%s': %v", string(body), err)
    75  	}
    76  	return &v, nil
    77  }
    78  
    79  func (c *ExtensionsClient) HorizontalPodAutoscalers(namespace string) HorizontalPodAutoscalerInterface {
    80  	return newHorizontalPodAutoscalers(c, namespace)
    81  }
    82  
    83  func (c *ExtensionsClient) Scales(namespace string) ScaleInterface {
    84  	return newScales(c, namespace)
    85  }
    86  
    87  func (c *ExtensionsClient) DaemonSets(namespace string) DaemonSetInterface {
    88  	return newDaemonSets(c, namespace)
    89  }
    90  
    91  func (c *ExtensionsClient) Deployments(namespace string) DeploymentInterface {
    92  	return newDeployments(c, namespace)
    93  }
    94  
    95  func (c *ExtensionsClient) Jobs(namespace string) JobInterface {
    96  	return newJobs(c, namespace)
    97  }
    98  
    99  func (c *ExtensionsClient) Ingress(namespace string) IngressInterface {
   100  	return newIngress(c, namespace)
   101  }
   102  
   103  // NewExtensions creates a new ExtensionsClient for the given config. This client
   104  // provides access to experimental Kubernetes features.
   105  // Features of Extensions group are not supported and may be changed or removed in
   106  // incompatible ways at any time.
   107  func NewExtensions(c *Config) (*ExtensionsClient, error) {
   108  	config := *c
   109  	if err := setExtensionsDefaults(&config); err != nil {
   110  		return nil, err
   111  	}
   112  	client, err := RESTClientFor(&config)
   113  	if err != nil {
   114  		return nil, err
   115  	}
   116  	return &ExtensionsClient{client}, nil
   117  }
   118  
   119  // NewExtensionsOrDie creates a new ExtensionsClient for the given config and
   120  // panics if there is an error in the config.
   121  // Features of Extensions group are not supported and may be changed or removed in
   122  // incompatible ways at any time.
   123  func NewExtensionsOrDie(c *Config) *ExtensionsClient {
   124  	client, err := NewExtensions(c)
   125  	if err != nil {
   126  		panic(err)
   127  	}
   128  	return client
   129  }
   130  
   131  func setExtensionsDefaults(config *Config) error {
   132  	// if experimental group is not registered, return an error
   133  	g, err := latest.Group("extensions")
   134  	if err != nil {
   135  		return err
   136  	}
   137  	config.Prefix = "apis/"
   138  	if config.UserAgent == "" {
   139  		config.UserAgent = DefaultKubernetesUserAgent()
   140  	}
   141  	// TODO: Unconditionally set the config.Version, until we fix the config.
   142  	//if config.Version == "" {
   143  	gv, err := unversioned.ParseGroupVersion(g.GroupVersion)
   144  	if err != nil {
   145  		return err
   146  	}
   147  	config.GroupVersion = &gv
   148  	//}
   149  
   150  	versionInterfaces, err := g.InterfacesFor(config.GroupVersion.String())
   151  	if err != nil {
   152  		return fmt.Errorf("Extensions API group/version '%v' is not recognized (valid values: %s)",
   153  			config.GroupVersion, strings.Join(latest.GroupOrDie("extensions").Versions, ", "))
   154  	}
   155  	config.Codec = versionInterfaces.Codec
   156  	if config.QPS == 0 {
   157  		config.QPS = 5
   158  	}
   159  	if config.Burst == 0 {
   160  		config.Burst = 10
   161  	}
   162  	return nil
   163  }