Skip to content

Commit

Permalink
refactor: move logic to create client to utils/kubernetes pkg
Browse files Browse the repository at this point in the history
- expose `CreateKubeClient` as public function
- make `GetKubeConfig` into a private `getKubeConfig` function (can be exposed as a public function in the future if needed)
Signed-off-by: vadasambar <surajrbanakar@gmail.com>
  • Loading branch information
vadasambar committed Nov 17, 2023
1 parent 5871df6 commit b365cf7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 19 deletions.
16 changes: 12 additions & 4 deletions cluster-autoscaler/config/autoscaling_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,8 @@ 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
// 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 @@ -276,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
}
21 changes: 10 additions & 11 deletions cluster-autoscaler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ 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/leaderelection"
"k8s.io/client-go/tools/leaderelection/resourcelock"
Expand Down Expand Up @@ -351,10 +350,13 @@ func createAutoscalingOptions() config.AutoscalingOptions {
StatusTaints: *statusTaintsFlag,
BalancingExtraIgnoredLabels: *balancingIgnoreLabelsFlag,
BalancingLabels: *balancingLabelsFlag,
Kubernetes: *kubernetes,
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 @@ -390,10 +392,6 @@ func createAutoscalingOptions() config.AutoscalingOptions {
}
}

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 @@ -413,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 @@ -592,7 +590,8 @@ func main() {
klog.Fatalf("Unable to get hostname: %v", err)
}

kubeClient := createKubeClient(kube_util.GetKubeConfig(createAutoscalingOptions()))
kubeClientOpts := createAutoscalingOptions().KubeClientOpts
kubeClient := kube_util.CreateKubeClient(&kubeClientOpts)

// Validate that the client is ok.
_, err = kubeClient.CoreV1().Nodes().List(ctx.TODO(), metav1.ListOptions{})
Expand Down
14 changes: 10 additions & 4 deletions cluster-autoscaler/utils/kubernetes/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,19 @@ import (

"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"
)

// GetKubeConfig returns the rest config from AutoscalingOptions.
func GetKubeConfig(opts config.AutoscalingOptions) *rest.Config {
// 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

Expand All @@ -39,7 +45,7 @@ func GetKubeConfig(opts config.AutoscalingOptions) *rest.Config {
klog.Fatalf("Failed to build config: %v", err)
}
} else {
url, err := url.Parse(opts.Kubernetes)
url, err := url.Parse(opts.Master)
if err != nil {
klog.Fatalf("Failed to parse Kubernetes url: %v", err)
}
Expand All @@ -50,7 +56,7 @@ func GetKubeConfig(opts config.AutoscalingOptions) *rest.Config {
}
}

kubeConfig.ContentType = opts.KubeAPIContentType
kubeConfig.ContentType = opts.APIContentType

return kubeConfig
}

0 comments on commit b365cf7

Please sign in to comment.