-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(*): move getKubeClient to utils/kubernetes
(cherry picked from commit b9f636d) Signed-off-by: qianlei.qianl <qianlei.qianl@bytedance.com> refactor: move logic to create client to utils/kubernetes pkg - 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> fix: CI failing because cloudproviders were not updated to use new autoscaling option fields Signed-off-by: vadasambar <surajrbanakar@gmail.com> refactor: define errors as constants Signed-off-by: vadasambar <surajrbanakar@gmail.com>
- Loading branch information
1 parent
8de60c9
commit fe6f655
Showing
6 changed files
with
96 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |