Skip to content

Commit

Permalink
refactor(*): move getKubeClient to utils/kubernetes
Browse files Browse the repository at this point in the history
  • Loading branch information
qianlei90 committed Oct 8, 2023
1 parent bafcc0d commit b9f636d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 30 deletions.
4 changes: 4 additions & 0 deletions cluster-autoscaler/config/autoscaling_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,12 @@ type AutoscalingOptions struct {
AWSUseStaticInstanceList bool
// GCEOptions contain autoscaling options specific to GCE cloud provider.
GCEOptions GCEOptions
// Kubernetes master location.
Kubernetes string
// Path to kube configuration if available
KubeConfigPath string
// Content type of requests sent to APIServer.
KubeAPIContentType string
// Burst setting for kubernetes client
KubeClientBurst int
// QPS setting for kubernetes client
Expand Down
35 changes: 5 additions & 30 deletions cluster-autoscaler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"flag"
"fmt"
"net/http"
"net/url"
"os"
"os/signal"
"strconv"
Expand Down Expand Up @@ -63,7 +62,6 @@ import (
"k8s.io/client-go/informers"
kube_client "k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/leaderelection"
"k8s.io/client-go/tools/leaderelection/resourcelock"
kube_flag "k8s.io/component-base/cli/flag"
Expand Down Expand Up @@ -353,7 +351,9 @@ func createAutoscalingOptions() config.AutoscalingOptions {
StatusTaints: *statusTaintsFlag,
BalancingExtraIgnoredLabels: *balancingIgnoreLabelsFlag,
BalancingLabels: *balancingLabelsFlag,
Kubernetes: *kubernetes,
KubeConfigPath: *kubeConfigFile,
KubeAPIContentType: *kubeAPIContentType,
KubeClientBurst: *kubeClientBurst,
KubeClientQPS: *kubeClientQPS,
NodeDeletionDelayTimeout: *nodeDeletionDelayTimeout,
Expand Down Expand Up @@ -393,31 +393,6 @@ func createAutoscalingOptions() config.AutoscalingOptions {
}
}

func getKubeConfig() *rest.Config {
if *kubeConfigFile != "" {
klog.V(1).Infof("Using kubeconfig file: %s", *kubeConfigFile)
// use the current context in kubeconfig
config, err := clientcmd.BuildConfigFromFlags("", *kubeConfigFile)
if err != nil {
klog.Fatalf("Failed to build config: %v", err)
}
return config
}
url, err := url.Parse(*kubernetes)
if err != nil {
klog.Fatalf("Failed to parse Kubernetes url: %v", err)
}

kubeConfig, err := config.GetKubeClientConfig(url)
if err != nil {
klog.Fatalf("Failed to build Kubernetes client configuration: %v", err)
}

kubeConfig.ContentType = *kubeAPIContentType

return kubeConfig
}

func createKubeClient(kubeConfig *rest.Config) kube_client.Interface {
return kube_client.NewForConfigOrDie(kubeConfig)
}
Expand All @@ -441,7 +416,7 @@ func buildAutoscaler(debuggingSnapshotter debuggingsnapshot.DebuggingSnapshotter
// Create basic config from flags.
autoscalingOptions := createAutoscalingOptions()

kubeClientConfig := getKubeConfig()
kubeClientConfig := kube_util.GetKubeConfig(autoscalingOptions)
kubeClientConfig.Burst = autoscalingOptions.KubeClientBurst
kubeClientConfig.QPS = float32(autoscalingOptions.KubeClientQPS)
kubeClient := createKubeClient(kubeClientConfig)
Expand All @@ -455,7 +430,7 @@ func buildAutoscaler(debuggingSnapshotter debuggingsnapshot.DebuggingSnapshotter
}
informerFactory := informers.NewSharedInformerFactoryWithOptions(kubeClient, 0, informers.WithTransform(trim))

eventsKubeClient := createKubeClient(getKubeConfig())
eventsKubeClient := createKubeClient(kube_util.GetKubeConfig(autoscalingOptions))

predicateChecker, err := predicatechecker.NewSchedulerBasedPredicateChecker(informerFactory, autoscalingOptions.SchedulerConfig)
if err != nil {
Expand Down Expand Up @@ -624,7 +599,7 @@ func main() {
klog.Fatalf("Unable to get hostname: %v", err)
}

kubeClient := createKubeClient(getKubeConfig())
kubeClient := createKubeClient(kube_util.GetKubeConfig(createAutoscalingOptions()))

// Validate that the client is ok.
_, err = kubeClient.CoreV1().Nodes().List(ctx.TODO(), metav1.ListOptions{})
Expand Down
56 changes: 56 additions & 0 deletions cluster-autoscaler/utils/kubernetes/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package kubernetes

import (
"net/url"

"k8s.io/autoscaler/cluster-autoscaler/config"

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

// GetKubeConfig returns the rest config from AutoscalingOptions.
func GetKubeConfig(opts config.AutoscalingOptions) *rest.Config {
var kubeConfig *rest.Config
var err error

if opts.KubeConfigPath != "" {
klog.V(1).Infof("Using kubeconfig file: %s", opts.KubeConfigPath)
// use the current context in kubeconfig
kubeConfig, err = clientcmd.BuildConfigFromFlags("", opts.KubeConfigPath)
if err != nil {
klog.Fatalf("Failed to build config: %v", err)
}
} else {
url, err := url.Parse(opts.Kubernetes)
if err != nil {
klog.Fatalf("Failed to parse Kubernetes url: %v", err)
}

kubeConfig, err = config.GetKubeClientConfig(url)
if err != nil {
klog.Fatalf("Failed to build Kubernetes client configuration: %v", err)
}
}

kubeConfig.ContentType = opts.KubeAPIContentType

return kubeConfig
}

0 comments on commit b9f636d

Please sign in to comment.