mirror of
https://github.com/k8sgpt-ai/k8sgpt.git
synced 2025-04-28 11:25:03 +00:00
* chore: change license to Apache-2 Signed-off-by: Thomas Schuetz <thomas.schuetz@t-sc.eu>
77 lines
2.0 KiB
Go
77 lines
2.0 KiB
Go
/*
|
|
Copyright 2023 The K8sGPT Authors.
|
|
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.
|
|
*/
|
|
|
|
package kubernetes
|
|
|
|
import (
|
|
"k8s.io/apimachinery/pkg/runtime/serializer"
|
|
"k8s.io/client-go/kubernetes"
|
|
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
|
|
"k8s.io/client-go/rest"
|
|
"k8s.io/client-go/tools/clientcmd"
|
|
"k8s.io/kubectl/pkg/scheme"
|
|
)
|
|
|
|
type Client struct {
|
|
Client kubernetes.Interface
|
|
RestClient rest.Interface
|
|
Config *rest.Config
|
|
}
|
|
|
|
func (c *Client) GetConfig() *rest.Config {
|
|
return c.Config
|
|
}
|
|
|
|
func (c *Client) GetClient() kubernetes.Interface {
|
|
return c.Client
|
|
}
|
|
|
|
func (c *Client) GetRestClient() rest.Interface {
|
|
return c.RestClient
|
|
}
|
|
|
|
func NewClient(kubecontext string, kubeconfig string) (*Client, error) {
|
|
var config *rest.Config
|
|
config, err := rest.InClusterConfig()
|
|
if err != nil {
|
|
clientConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
|
|
&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfig},
|
|
&clientcmd.ConfigOverrides{
|
|
CurrentContext: kubecontext,
|
|
})
|
|
// create the clientset
|
|
config, err = clientConfig.ClientConfig()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
clientSet, err := kubernetes.NewForConfig(config)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
config.APIPath = "/api"
|
|
config.GroupVersion = &scheme.Scheme.PrioritizedVersionsForGroup("")[0]
|
|
config.NegotiatedSerializer = serializer.WithoutConversionCodecFactory{CodecFactory: scheme.Codecs}
|
|
|
|
restClient, err := rest.RESTClientFor(config)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &Client{
|
|
Client: clientSet,
|
|
RestClient: restClient,
|
|
Config: config,
|
|
}, nil
|
|
}
|