...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/helm/restclientgetter.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/helm

     1  // Copyright 2022 Chaos Mesh 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  
    16  package helm
    17  
    18  import (
    19  	"github.com/pkg/errors"
    20  	"k8s.io/apimachinery/pkg/api/meta"
    21  	"k8s.io/cli-runtime/pkg/genericclioptions"
    22  	"k8s.io/client-go/discovery"
    23  	"k8s.io/client-go/discovery/cached/memory"
    24  	"k8s.io/client-go/rest"
    25  	"k8s.io/client-go/restmapper"
    26  	"k8s.io/client-go/tools/clientcmd"
    27  )
    28  
    29  var _ genericclioptions.RESTClientGetter = &restClientGetter{}
    30  
    31  type restClientGetter struct {
    32  	clientConfig clientcmd.ClientConfig
    33  }
    34  
    35  func NewRESTClientGetter(clientConfig clientcmd.ClientConfig) genericclioptions.RESTClientGetter {
    36  	return &restClientGetter{
    37  		clientConfig,
    38  	}
    39  }
    40  
    41  func (getter *restClientGetter) ToRESTConfig() (*rest.Config, error) {
    42  	return getter.clientConfig.ClientConfig()
    43  }
    44  
    45  func (getter *restClientGetter) ToDiscoveryClient() (discovery.CachedDiscoveryInterface, error) {
    46  	restConfig, err := getter.clientConfig.ClientConfig()
    47  	if err != nil {
    48  		return nil, errors.Wrap(err, "get rest config from client config")
    49  	}
    50  
    51  	client, err := discovery.NewDiscoveryClientForConfig(restConfig)
    52  	if err != nil {
    53  		return nil, errors.Wrap(err, "create discovery client")
    54  	}
    55  	return memory.NewMemCacheClient(client), nil
    56  }
    57  
    58  func (getter *restClientGetter) ToRESTMapper() (meta.RESTMapper, error) {
    59  	discoveryClient, err := getter.ToDiscoveryClient()
    60  	if err != nil {
    61  		return nil, err
    62  	}
    63  
    64  	mapper := restmapper.NewDeferredDiscoveryRESTMapper(discoveryClient)
    65  	expander := restmapper.NewShortcutExpander(mapper, discoveryClient)
    66  	return expander, nil
    67  }
    68  
    69  func (getter *restClientGetter) ToRawKubeConfigLoader() clientcmd.ClientConfig {
    70  	return getter.clientConfig
    71  }
    72