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

Add options to load kubeconfig #1209

Merged
merged 1 commit into from
May 16, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 2 additions & 14 deletions pkg/cmd/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/kubecost/cost-model/pkg/costmodel"
"github.com/kubecost/cost-model/pkg/costmodel/clusters"
"github.com/kubecost/cost-model/pkg/env"
"github.com/kubecost/cost-model/pkg/kubeconfig"
"github.com/kubecost/cost-model/pkg/log"
"github.com/kubecost/cost-model/pkg/prom"
"github.com/kubecost/cost-model/pkg/util/watcher"
Expand All @@ -23,8 +24,6 @@ import (

"github.com/rs/cors"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)

// AgentOpts contain configuration options that can be passed to the Execute() method
Expand All @@ -50,18 +49,7 @@ func newKubernetesClusterCache() (kubernetes.Interface, clustercache.ClusterCach
var err error

// Kubernetes API setup
var kc *rest.Config
if kubeconfig := env.GetKubeConfigPath(); kubeconfig != "" {
kc, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
} else {
kc, err = rest.InClusterConfig()
}

if err != nil {
return nil, nil, err
}

kubeClientset, err := kubernetes.NewForConfig(kc)
kubeClientset, err := kubeconfig.LoadKubeClient("")
if err != nil {
return nil, nil, err
}
Expand Down
17 changes: 3 additions & 14 deletions pkg/costmodel/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"time"

"github.com/kubecost/cost-model/pkg/config"
"github.com/kubecost/cost-model/pkg/kubeconfig"
"github.com/kubecost/cost-model/pkg/metrics"
"github.com/kubecost/cost-model/pkg/services"
"github.com/kubecost/cost-model/pkg/util/httputil"
Expand Down Expand Up @@ -46,8 +47,6 @@ import (
"github.com/patrickmn/go-cache"

"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)

var sanitizePolicy = bluemonday.UGCPolicy()
Expand Down Expand Up @@ -1442,19 +1441,9 @@ func Initialize(additionalConfigWatchers ...*watcher.ConfigMapWatcher) *Accesses
log.Infof("Using scrape interval of %f", scrapeInterval.Seconds())

// Kubernetes API setup
var kc *rest.Config
if kubeconfig := env.GetKubeConfigPath(); kubeconfig != "" {
kc, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
} else {
kc, err = rest.InClusterConfig()
}

kubeClientset, err := kubeconfig.LoadKubeClient("")
if err != nil {
panic(err.Error())
}
kubeClientset, err := kubernetes.NewForConfig(kc)
if err != nil {
panic(err.Error())
log.Fatalf("Failed to build Kubernetes client: %s", err.Error())
}

// Create ConfigFileManager for synchronization of shared configuration
Expand Down
29 changes: 29 additions & 0 deletions pkg/kubeconfig/loader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package kubeconfig

import (
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)

// LoadKubeconfig attempts to load a kubeconfig based on default locations.
// If a path is passed in then only that path is checked and will error
// if not found
func LoadKubeconfig(path string) (*rest.Config, error) {
// Use the default load order: KUBECONFIG env > $HOME/.kube/config > In cluster config
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
if path != "" {
loadingRules.ExplicitPath = path
}
loader := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, &clientcmd.ConfigOverrides{})
return loader.ClientConfig()
}

// LoadKubeClient accepts a path to a kubeconfig to load and returns the clientset
func LoadKubeClient(path string) (*kubernetes.Clientset, error) {
config, err := LoadKubeconfig(path)
if err != nil {
return nil, err
}
return kubernetes.NewForConfig(config)
}