Skip to content

Commit

Permalink
pkg/utils: move GetKubeconfig from pkg/apihelper here
Browse files Browse the repository at this point in the history
This change is part of an effort to remove the pkg/apihelper package.
GetKubeconfig is useful helper functionality shared accross the codebase
so move it into a "safe" location.
  • Loading branch information
marquiz committed Jan 24, 2024
1 parent ea4504a commit acf815f
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 15 deletions.
9 changes: 0 additions & 9 deletions pkg/apihelper/k8shelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"k8s.io/apimachinery/pkg/types"
k8sclient "k8s.io/client-go/kubernetes"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)

// K8sHelpers implements APIHelpers
Expand Down Expand Up @@ -110,11 +109,3 @@ func (h K8sHelpers) GetPod(cli *k8sclient.Clientset, namespace string, podName s

return pod, nil
}

// GetKubeconfig returns the kubeconfig for the cluster
func GetKubeconfig(path string) (*restclient.Config, error) {
if path == "" {
return restclient.InClusterConfig()
}
return clientcmd.BuildConfigFromFlags("", path)
}
3 changes: 1 addition & 2 deletions pkg/nfd-gc/nfd-gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"k8s.io/client-go/tools/cache"
"k8s.io/klog/v2"

"sigs.k8s.io/node-feature-discovery/pkg/apihelper"
nfdv1alpha1 "sigs.k8s.io/node-feature-discovery/pkg/apis/nfd/v1alpha1"
nfdclientset "sigs.k8s.io/node-feature-discovery/pkg/generated/clientset/versioned"
"sigs.k8s.io/node-feature-discovery/pkg/utils"
Expand Down Expand Up @@ -59,7 +58,7 @@ type nfdGarbageCollector struct {
}

func New(args *Args) (NfdGarbageCollector, error) {
kubeconfig, err := apihelper.GetKubeconfig(args.Kubeconfig)
kubeconfig, err := utils.GetKubeconfig(args.Kubeconfig)

Check warning on line 61 in pkg/nfd-gc/nfd-gc.go

View check run for this annotation

Codecov / codecov/patch

pkg/nfd-gc/nfd-gc.go#L61

Added line #L61 was not covered by tests
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/nfd-master/nfd-master.go
Original file line number Diff line number Diff line change
Expand Up @@ -1140,7 +1140,7 @@ func (m *nfdMaster) updateNodeObject(cli *kubernetes.Clientset, nodeName string,
func (m *nfdMaster) getKubeconfig() (*restclient.Config, error) {
var err error
if m.kubeconfig == nil {
m.kubeconfig, err = apihelper.GetKubeconfig(m.args.Kubeconfig)
m.kubeconfig, err = utils.GetKubeconfig(m.args.Kubeconfig)

Check warning on line 1143 in pkg/nfd-master/nfd-master.go

View check run for this annotation

Codecov / codecov/patch

pkg/nfd-master/nfd-master.go#L1143

Added line #L1143 was not covered by tests
}
return m.kubeconfig, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/nfd-topology-updater/nfd-topology-updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (w *nfdTopologyUpdater) Run() error {
return fmt.Errorf("failed to get PodResource Client: %w", err)
}

kubeconfig, err := apihelper.GetKubeconfig(w.args.KubeConfigFile)
kubeconfig, err := utils.GetKubeconfig(w.args.KubeConfigFile)

Check warning on line 134 in pkg/nfd-topology-updater/nfd-topology-updater.go

View check run for this annotation

Codecov / codecov/patch

pkg/nfd-topology-updater/nfd-topology-updater.go#L134

Added line #L134 was not covered by tests
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/nfd-worker/nfd-worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import (

apiequality "k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/node-feature-discovery/pkg/apihelper"
nfdv1alpha1 "sigs.k8s.io/node-feature-discovery/pkg/apis/nfd/v1alpha1"
nfdclient "sigs.k8s.io/node-feature-discovery/pkg/generated/clientset/versioned"
pb "sigs.k8s.io/node-feature-discovery/pkg/labeler"
Expand Down Expand Up @@ -747,7 +746,7 @@ func (m *nfdWorker) getNfdClient() (*nfdclient.Clientset, error) {
return m.nfdClient, nil
}

kubeconfig, err := apihelper.GetKubeconfig(m.args.Kubeconfig)
kubeconfig, err := utils.GetKubeconfig(m.args.Kubeconfig)

Check warning on line 749 in pkg/nfd-worker/nfd-worker.go

View check run for this annotation

Codecov / codecov/patch

pkg/nfd-worker/nfd-worker.go#L749

Added line #L749 was not covered by tests
if err != nil {
return nil, err
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/utils/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ package utils
import (
"os"
"strings"

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

var nodeName string
Expand All @@ -43,3 +46,11 @@ func GetKubernetesNamespace() string {
}
return os.Getenv("KUBERNETES_NAMESPACE")
}

// GetKubeconfig returns the kubeconfig for the cluster
func GetKubeconfig(path string) (*restclient.Config, error) {
if path == "" {
return restclient.InClusterConfig()
}
return clientcmd.BuildConfigFromFlags("", path)

Check warning on line 55 in pkg/utils/kubernetes.go

View check run for this annotation

Codecov / codecov/patch

pkg/utils/kubernetes.go#L51-L55

Added lines #L51 - L55 were not covered by tests
}

0 comments on commit acf815f

Please sign in to comment.