55 lines
1.5 KiB
Go
55 lines
1.5 KiB
Go
/*
|
|
Copyright 2021 The Dapr 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 (
|
|
"flag"
|
|
"path/filepath"
|
|
|
|
"k8s.io/client-go/kubernetes"
|
|
"k8s.io/client-go/rest"
|
|
"k8s.io/client-go/tools/clientcmd"
|
|
"k8s.io/client-go/util/homedir"
|
|
)
|
|
|
|
//nolint:gochecknoglobals
|
|
var kubeconfig *string
|
|
|
|
//nolint:gochecknoinits
|
|
func init() {
|
|
if home := homedir.HomeDir(); home != "" {
|
|
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
|
|
} else {
|
|
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
|
|
}
|
|
}
|
|
|
|
// GetKubeClient returns a kubernetes client.
|
|
func GetKubeClient() (*kubernetes.Clientset, error) {
|
|
flag.Parse()
|
|
conf, err := rest.InClusterConfig()
|
|
if err != nil {
|
|
conf, err = clientcmd.BuildConfigFromFlags("", *kubeconfig)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
clientset, err := kubernetes.NewForConfig(conf)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return clientset, nil
|
|
}
|