Skip to content

Commit

Permalink
[operator] Add kube clientcmd util
Browse files Browse the repository at this point in the history
  • Loading branch information
mfordjody committed Dec 8, 2024
1 parent 68bf1dd commit c56d188
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions pkg/util/kube/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package kube

import (
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"

"os"
)

func DefaultRestConfig(kubeconfig, context string, fns ...func(config *rest.Config)) (*rest.Config, error) {
}

Check failure on line 12 in pkg/util/kube/util.go

View workflow job for this annotation

GitHub Actions / Go Test

missing return

func BuildClientConfig(kubeconfig, context string) (*rest.Config, error) {
c, err := BuildClientCmd(kubeconfig, context).ClientConfig()

Check failure on line 15 in pkg/util/kube/util.go

View workflow job for this annotation

GitHub Actions / Go Test

c declared and not used
if err != nil {
return nil, err
}
return

Check failure on line 19 in pkg/util/kube/util.go

View workflow job for this annotation

GitHub Actions / Go Test

not enough return values
}

func SetRestDefaults(config *rest.Config) (*rest.Config) {
if config.GroupVersion == nil || config.GroupVersion.Empty() {
config.GroupVersion = &corev1.SchemeGroupVersion
}
if len(config.APIPath) == 0 {
if len(config.GroupVersion.Group) == 0 {
config.APIPath = "/api"
} else {
config.APIPath = "/apis"
}
}
if config.NegotiatedSerializer == nil {
}
return config
}

func BuildClientCmd(kubeconfig, context string, overrides ...func(configOverrides *clientcmd.ConfigOverrides)) clientcmd.ClientConfig {
if kubeconfig != "" {
info, err := os.Stat(kubeconfig)
if err != nil || info.Size() == 0 {
kubeconfig = ""
}
}
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
loadingRules.DefaultClientConfig = &clientcmd.DefaultClientConfig
loadingRules.ExplicitPath = kubeconfig
configOverrides := &clientcmd.ConfigOverrides{
ClusterDefaults: clientcmd.ClusterDefaults,
CurrentContext: context,
}
for _, fn := range overrides {
fn(configOverrides)
}
return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides)
}

0 comments on commit c56d188

Please sign in to comment.