Skip to content

Commit

Permalink
Merge pull request #1209 from kubecost/ramich-kubeclient
Browse files Browse the repository at this point in the history
Add options to load kubeconfig
  • Loading branch information
dramich committed May 16, 2022
2 parents 3364c7f + b0cb003 commit 7d4584f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 28 deletions.
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)
}

0 comments on commit 7d4584f

Please sign in to comment.