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

refactor(*): move getKubeClient to utils/kubernetes #6294

Merged
merged 1 commit into from
Nov 29, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,15 @@ func newProvider(
func BuildClusterAPI(opts config.AutoscalingOptions, do cloudprovider.NodeGroupDiscoveryOptions, rl *cloudprovider.ResourceLimiter) cloudprovider.CloudProvider {
managementKubeconfig := opts.CloudConfig
if managementKubeconfig == "" && !opts.ClusterAPICloudConfigAuthoritative {
managementKubeconfig = opts.KubeConfigPath
managementKubeconfig = opts.KubeClientOpts.KubeConfigPath
}

managementConfig, err := clientcmd.BuildConfigFromFlags("", managementKubeconfig)
if err != nil {
klog.Fatalf("cannot build management cluster config: %v", err)
}

workloadKubeconfig := opts.KubeConfigPath
workloadKubeconfig := opts.KubeClientOpts.KubeConfigPath

workloadConfig, err := clientcmd.BuildConfigFromFlags("", workloadKubeconfig)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ func newKamateraCloudProvider(config io.Reader, rl *cloudprovider.ResourceLimite
}

func getKubeConfig(opts config.AutoscalingOptions) *rest.Config {
klog.V(1).Infof("Using kubeconfig file: %s", opts.KubeConfigPath)
kubeConfig, err := clientcmd.BuildConfigFromFlags("", opts.KubeConfigPath)
klog.V(1).Infof("Using kubeconfig file: %s", opts.KubeClientOpts.KubeConfigPath)
kubeConfig, err := clientcmd.BuildConfigFromFlags("", opts.KubeClientOpts.KubeConfigPath)
if err != nil {
klog.Fatalf("Failed to build kubeConfig: %v", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ func BuildOCI(opts config.AutoscalingOptions, do cloudprovider.NodeGroupDiscover
}

func getKubeConfig(opts config.AutoscalingOptions) *rest.Config {
klog.V(1).Infof("Using kubeconfig file: %s", opts.KubeConfigPath)
kubeConfig, err := clientcmd.BuildConfigFromFlags("", opts.KubeConfigPath)
klog.V(1).Infof("Using kubeconfig file: %s", opts.KubeClientOpts.KubeConfigPath)
kubeConfig, err := clientcmd.BuildConfigFromFlags("", opts.KubeClientOpts.KubeConfigPath)
if err != nil {
klog.Fatalf("Failed to build kubeConfig: %v", err)
}
Expand Down
14 changes: 12 additions & 2 deletions cluster-autoscaler/config/autoscaling_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ type AutoscalingOptions struct {
AWSUseStaticInstanceList bool
// GCEOptions contain autoscaling options specific to GCE cloud provider.
GCEOptions GCEOptions
// Path to kube configuration if available
KubeConfigPath string
// KubeClientOpts specify options for kube client
KubeClientOpts KubeClientOptions
// ClusterAPICloudConfigAuthoritative tells the Cluster API provider to treat the CloudConfig option as authoritative and
// not use KubeConfigPath as a fallback when it is not provided.
ClusterAPICloudConfigAuthoritative bool
Expand Down Expand Up @@ -274,3 +274,13 @@ type AutoscalingOptions struct {
// based on the latency between the CA and the api-server
DynamicNodeDeleteDelayAfterTaintEnabled bool
}

// KubeClientOptions specify options for kube client
type KubeClientOptions struct {
// Master specifies master location.
Master string
// Path to kube configuration if available
KubeConfigPath string
// APIContentType specifies type of requests sent to APIServer.
APIContentType string
}
46 changes: 9 additions & 37 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 @@ -61,9 +60,7 @@ import (
"k8s.io/autoscaler/cluster-autoscaler/utils/units"
"k8s.io/autoscaler/cluster-autoscaler/version"
"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,9 +350,13 @@ func createAutoscalingOptions() config.AutoscalingOptions {
StatusTaints: *statusTaintsFlag,
BalancingExtraIgnoredLabels: *balancingIgnoreLabelsFlag,
BalancingLabels: *balancingLabelsFlag,
KubeConfigPath: *kubeConfigFile,
NodeDeletionDelayTimeout: *nodeDeletionDelayTimeout,
AWSUseStaticInstanceList: *awsUseStaticInstanceList,
KubeClientOpts: config.KubeClientOptions{
Master: *kubernetes,
KubeConfigPath: *kubeConfigFile,
APIContentType: *kubeAPIContentType,
},
NodeDeletionDelayTimeout: *nodeDeletionDelayTimeout,
AWSUseStaticInstanceList: *awsUseStaticInstanceList,
GCEOptions: config.GCEOptions{
ConcurrentRefreshes: *concurrentGceRefreshes,
MigInstancesMinRefreshWaitTime: *gceMigInstancesMinRefreshWaitTime,
Expand Down Expand Up @@ -391,35 +392,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)
}

func registerSignalHandlers(autoscaler core.Autoscaler) {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, os.Interrupt, os.Kill, syscall.SIGTERM, syscall.SIGQUIT)
Expand All @@ -439,7 +411,7 @@ func buildAutoscaler(debuggingSnapshotter debuggingsnapshot.DebuggingSnapshotter
// Create basic config from flags.
autoscalingOptions := createAutoscalingOptions()

kubeClient := createKubeClient(getKubeConfig())
kubeClient := kube_util.CreateKubeClient(autoscalingOptions.KubeClientOpts)

// Informer transform to trim ManagedFields for memory efficiency.
trim := func(obj interface{}) (interface{}, error) {
Expand Down Expand Up @@ -618,7 +590,7 @@ func main() {
klog.Fatalf("Unable to get hostname: %v", err)
}

kubeClient := createKubeClient(getKubeConfig())
kubeClient := kube_util.CreateKubeClient(createAutoscalingOptions().KubeClientOpts)

// Validate that the client is ok.
_, err = kubeClient.CoreV1().Nodes().List(ctx.TODO(), metav1.ListOptions{})
Expand Down
68 changes: 68 additions & 0 deletions cluster-autoscaler/utils/kubernetes/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
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"

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

const (
failedToBuildConfigErr = "Failed to build config"
failedToParseK8sUrlErr = "Failed to parse Kubernetes url"
failedToBuildClientConfigErr = "Failed to build Kubernetes client configuration"
)

// CreateKubeClient creates kube client based on AutoscalingOptions.KubeClientOptions
func CreateKubeClient(opts config.KubeClientOptions) kube_client.Interface {
return kube_client.NewForConfigOrDie(getKubeConfig(opts))
}

// getKubeConfig returns the rest config from AutoscalingOptions.KubeClientOptions.
func getKubeConfig(opts config.KubeClientOptions) *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("%v: %v", failedToBuildConfigErr, err)
}
} else {
url, err := url.Parse(opts.Master)
if err != nil {
klog.Fatalf("%v: %v", failedToParseK8sUrlErr, err)
}

kubeConfig, err = config.GetKubeClientConfig(url)
if err != nil {
klog.Fatalf("%v: %v", failedToBuildClientConfigErr, err)
}
}

kubeConfig.ContentType = opts.APIContentType

return kubeConfig
}
Loading