github.imxd.top/operator-framework/operator-sdk@v0.8.2/pkg/helm/client/client.go (about)

     1  // Copyright 2018 The Operator-SDK Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package client
    16  
    17  import (
    18  	"k8s.io/apimachinery/pkg/api/meta"
    19  	"k8s.io/client-go/discovery"
    20  	"k8s.io/client-go/discovery/cached"
    21  	"k8s.io/client-go/rest"
    22  	"k8s.io/client-go/tools/clientcmd"
    23  	"k8s.io/helm/pkg/kube"
    24  	"sigs.k8s.io/controller-runtime/pkg/manager"
    25  )
    26  
    27  // NewFromManager returns a Kubernetes client that can be used with
    28  // a Tiller server.
    29  func NewFromManager(mgr manager.Manager) (*kube.Client, error) {
    30  	c, err := newClientGetter(mgr)
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  	return kube.New(c), nil
    35  }
    36  
    37  type clientGetter struct {
    38  	restConfig      *rest.Config
    39  	discoveryClient discovery.CachedDiscoveryInterface
    40  	restMapper      meta.RESTMapper
    41  }
    42  
    43  func (c *clientGetter) ToRESTConfig() (*rest.Config, error) {
    44  	return c.restConfig, nil
    45  }
    46  
    47  func (c *clientGetter) ToDiscoveryClient() (discovery.CachedDiscoveryInterface, error) {
    48  	return c.discoveryClient, nil
    49  }
    50  
    51  func (c *clientGetter) ToRESTMapper() (meta.RESTMapper, error) {
    52  	return c.restMapper, nil
    53  }
    54  
    55  func (c *clientGetter) ToRawKubeConfigLoader() clientcmd.ClientConfig {
    56  	return nil
    57  }
    58  
    59  func newClientGetter(mgr manager.Manager) (*clientGetter, error) {
    60  	cfg := mgr.GetConfig()
    61  	dc, err := discovery.NewDiscoveryClientForConfig(cfg)
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  	cdc := cached.NewMemCacheClient(dc)
    66  	rm := mgr.GetRESTMapper()
    67  
    68  	return &clientGetter{
    69  		restConfig:      cfg,
    70  		discoveryClient: cdc,
    71  		restMapper:      rm,
    72  	}, nil
    73  }