| // Licensed to Apache Software Foundation (ASF) under one or more contributor |
| // license agreements. See the NOTICE file distributed with |
| // this work for additional information regarding copyright |
| // ownership. Apache Software Foundation (ASF) licenses this file to you 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. |
| // |
| |
| package util |
| |
| import ( |
| "k8s.io/apimachinery/pkg/api/meta" |
| "k8s.io/client-go/discovery" |
| "k8s.io/client-go/discovery/cached/memory" |
| "k8s.io/client-go/rest" |
| "k8s.io/client-go/restmapper" |
| "k8s.io/client-go/tools/clientcmd" |
| ) |
| |
| // SimpleRESTClientGetter implements genericclioptions.RESTClientGetter, |
| // so that we can use kubectl's functions just by passing kubeConfig and namespace. |
| type SimpleRESTClientGetter struct { |
| Namespace string |
| KubeConfig string |
| } |
| |
| func NewSimpleRESTClientGetter(namespace, kubeConfig string) *SimpleRESTClientGetter { |
| return &SimpleRESTClientGetter{ |
| Namespace: namespace, |
| KubeConfig: kubeConfig, |
| } |
| } |
| |
| func (c *SimpleRESTClientGetter) ToRESTConfig() (*rest.Config, error) { |
| config, err := clientcmd.RESTConfigFromKubeConfig([]byte(c.KubeConfig)) |
| if err != nil { |
| return nil, err |
| } |
| return config, nil |
| } |
| |
| func (c *SimpleRESTClientGetter) ToDiscoveryClient() (discovery.CachedDiscoveryInterface, error) { |
| config, err := c.ToRESTConfig() |
| if err != nil { |
| return nil, err |
| } |
| |
| config.Burst = 100 |
| |
| discoveryClient, _ := discovery.NewDiscoveryClientForConfig(config) |
| return memory.NewMemCacheClient(discoveryClient), nil |
| } |
| |
| func (c *SimpleRESTClientGetter) ToRESTMapper() (meta.RESTMapper, error) { |
| discoveryClient, err := c.ToDiscoveryClient() |
| if err != nil { |
| return nil, err |
| } |
| |
| mapper := restmapper.NewDeferredDiscoveryRESTMapper(discoveryClient) |
| expander := restmapper.NewShortcutExpander(mapper, discoveryClient) |
| return expander, nil |
| } |
| |
| func (c *SimpleRESTClientGetter) ToRawKubeConfigLoader() clientcmd.ClientConfig { |
| loadingRules := clientcmd.NewDefaultClientConfigLoadingRules() |
| loadingRules.DefaultClientConfig = &clientcmd.DefaultClientConfig |
| |
| overrides := &clientcmd.ConfigOverrides{ClusterDefaults: clientcmd.ClusterDefaults} |
| overrides.Context.Namespace = c.Namespace |
| |
| return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, overrides) |
| } |