Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change clientset to use config from config.GetConfig() #468

Merged
merged 5 commits into from
Nov 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/manager/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ go_library(
"@com_github_operator_framework_operator_sdk//pkg/metrics:go_default_library",
"@com_github_operator_framework_operator_sdk//version:go_default_library",
"@com_github_spf13_pflag//:go_default_library",
"@io_k8s_client_go//kubernetes:go_default_library",
"@io_k8s_client_go//plugin/pkg/client/auth:go_default_library",
"@io_k8s_sigs_controller_runtime//pkg/client/config:go_default_library",
"@io_k8s_sigs_controller_runtime//pkg/log:go_default_library",
Expand Down
3 changes: 2 additions & 1 deletion cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
_ "github.com/operator-framework/operator-sdk/pkg/metrics"
sdkVersion "github.com/operator-framework/operator-sdk/version"
"github.com/spf13/pflag"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"sigs.k8s.io/controller-runtime/pkg/client/config"
logf "sigs.k8s.io/controller-runtime/pkg/log"
Expand Down Expand Up @@ -91,7 +92,7 @@ func main() {
os.Exit(1)
}

clientset, err := v1alpha1.GetClientset()
clientset, err := kubernetes.NewForConfig(cfg)
if err != nil {
log.Error(err, "")
os.Exit(1)
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/contrail/v1alpha1/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ go_library(
"@io_k8s_client_go//dynamic:go_default_library",
"@io_k8s_client_go//kubernetes:go_default_library",
"@io_k8s_client_go//rest:go_default_library",
"@io_k8s_client_go//tools/clientcmd:go_default_library",
"@io_k8s_client_go//tools/remotecommand:go_default_library",
"@io_k8s_kube_openapi//pkg/common:go_default_library",
"@io_k8s_sigs_controller_runtime//pkg/client:go_default_library",
"@io_k8s_sigs_controller_runtime//pkg/client/config:go_default_library",
"@io_k8s_sigs_controller_runtime//pkg/controller/controllerutil:go_default_library",
"@io_k8s_sigs_controller_runtime//pkg/reconcile:go_default_library",
"@io_k8s_sigs_controller_runtime//pkg/runtime/scheme:go_default_library",
Expand Down
97 changes: 5 additions & 92 deletions pkg/apis/contrail/v1alpha1/exec_to_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,107 +4,20 @@ import (
"bytes"
"fmt"
"io"
"os"
"os/user"
"path/filepath"

core_v1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
"sigs.k8s.io/controller-runtime/pkg/client/config"

// "k8s.io/client-go/kubernetes/scheme"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/remotecommand"
)

const debug = false

// GetConfig creates a *rest.Config for talking to a Kubernetes apiserver.
// If --kubeconfig is set, will use the kubeconfig file at that location. Otherwise will assume running
// in cluster and use the cluster provided kubeconfig.
//
// Config precedence
//
// * --kubeconfig flag pointing at a file
//
// * KUBECONFIG environment variable pointing at a file
//
// * In-cluster config if running in cluster
//
// * $HOME/.kube/config if exists
func GetConfig() (*rest.Config, error) {
var kubeconfig, masterURL string
// If a flag is specified with the config location, use that
if len(kubeconfig) > 0 {
return clientcmd.BuildConfigFromFlags(masterURL, kubeconfig)
}
// If an env variable is specified with the config locaiton, use that
if len(os.Getenv("KUBECONFIG")) > 0 {
return clientcmd.BuildConfigFromFlags(masterURL, os.Getenv("KUBECONFIG"))
}
// If no explicit location, try the in-cluster config
if c, err := rest.InClusterConfig(); err == nil {
return c, nil
}
// If no in-cluster config, try the default location in the user's home directory
if usr, err := user.Current(); err == nil {
if c, err := clientcmd.BuildConfigFromFlags(
"", filepath.Join(usr.HomeDir, ".kube", "config")); err == nil {
return c, nil
}
}

return nil, fmt.Errorf("could not locate a kubeconfig")
}

// GetClientConfig first tries to get a config object which uses the service account kubernetes gives to pods,
// if it is called from a process running in a kubernetes environment.
// Otherwise, it tries to build config from a default kubeconfig filepath if it fails, it fallback to the default config.
// Once it get the config, it returns the same.
func GetClientConfig() (*rest.Config, error) {
config, err := rest.InClusterConfig()
if err != nil {
if debug {
fmt.Printf("Unable to create config. Error: %+v\n", err)
}
err1 := err
kubeconfig := filepath.Join(os.Getenv("HOME"), ".kube", "config")
config, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
err = fmt.Errorf("InClusterConfig as well as BuildConfigFromFlags Failed. Error in InClusterConfig: %+v\nError in BuildConfigFromFlags: %+v", err1, err)
return nil, err
}
}

return config, nil
}

// GetClientsetFromConfig takes REST config and Create a clientset based on that and return that clientset.
func GetClientsetFromConfig(config *rest.Config) (*kubernetes.Clientset, error) {
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
err = fmt.Errorf("failed creating clientset. Error: %+v", err)
return nil, err
}

return clientset, nil
}

// GetClientset first tries to get a config object which uses the service account kubernetes gives to pods,
// if it is called from a process running in a kubernetes environment.
// Otherwise, it tries to build config from a default kubeconfig filepath if it fails, it fallback to the default config.
// Once it get the config, it creates a new Clientset for the given config and returns the clientset.
func GetClientset() (*kubernetes.Clientset, error) {
config, err := GetClientConfig()
if err != nil {
return nil, err
}

return GetClientsetFromConfig(config)
}

// GetDynamicClientFromConfig takes REST config and Create a dynamic client based on that return that client.
func GetDynamicClientFromConfig(config *rest.Config) (dynamic.Interface, error) {
dynamicClient, err := dynamic.NewForConfig(config)
Expand All @@ -119,7 +32,7 @@ func GetDynamicClientFromConfig(config *rest.Config) (dynamic.Interface, error)
// Otherwise, it tries to build config from a default kubeconfig filepath if it fails, it fallback to the default config.
// Once it get the config, it creates a new Dynamic Client for the given config and returns the client.
func GetDynamicClient() (dynamic.Interface, error) {
config, err := GetClientConfig()
config, err := config.GetConfig()
if err != nil {
return nil, err
}
Expand All @@ -132,7 +45,7 @@ func GetDynamicClient() (dynamic.Interface, error) {
// Otherwise, it tries to build config from a default kubeconfig filepath if it fails, it fallback to the default config.
// Once it get the config, it
func GetRESTClient() (*rest.RESTClient, error) {
config, err := GetClientConfig()
config, err := config.GetConfig()
if err != nil {
return &rest.RESTClient{}, err
}
Expand All @@ -149,12 +62,12 @@ func GetRESTClient() (*rest.RESTClient, error) {
// string: Errors. (STDERR)
// error: If any error has occurred otherwise `nil`
func ExecToPodThroughAPI(command []string, containerName, podName, namespace string, stdin io.Reader) (string, string, error) {
config, err := GetClientConfig()
config, err := config.GetConfig()
if err != nil {
return "", "", err
}

clientset, err := GetClientsetFromConfig(config)
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return "", "", err
}
Expand Down