-
Notifications
You must be signed in to change notification settings - Fork 4k
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 #6180
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 { | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Passing all There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. type AutoscalingOptions struct {
...
// 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
KubeClientQPS float64
...
} Yeah I think it's very reasonable. How about moving these fields to a dedicated struct I want to create a kube client in cloudprovider(see #5820 (comment)) but
If we move kube-client releated fields and functions to utils/kubernetes/client.go, I'm wondering how to create kube client in cloudprovider. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the struct should still live next to What I'm proposing is: type AutoscalingOptions struct {
...
KubeClient KubeClientOptions
...
}
type KubeClientOptions struct {
// Kubernetes master location.
Kubernetes string
// Path to kube configuration if available
KubeConfigPath string
// Content type of requests sent to APIServer.
ContentType string
// Burst setting for kubernetes client
BurstLimit int
// QPS setting for kubernetes client
QPSLimit float64
} And then in the new package: package client
...
func New(opts config.KubeClientOptions) kube_client.Interface {
...
} With that, given |
||||
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 | ||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
KubeConfig
abstraction needed at all in main? Can the entire client creation be moved to a dedicated package?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. Releated fields and
getKubeConfig
、createKubeClient
now are only used inmain.go
. I do this refactor because I want to reuse these functions in cloudprovider.I think Yes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ack, in this case let's move both kube config and client creation to the util package, so main will be able to create client directly and kube config you'll be able to reuse in cloudprovider.