From 58951c58fb9508f02f4e97a19c53e87c59172ea1 Mon Sep 17 00:00:00 2001 From: LidolLxf <13660354959@163.com> Date: Fri, 23 Feb 2024 16:08:18 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9Arelease=E5=88=97=E8=A1=A8=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bcs-services/bcs-helm-manager/go.mod | 2 +- .../internal/release/bcs/sdk/sdk_test.go | 94 +++++++++++++++++++ 2 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 bcs-services/bcs-helm-manager/internal/release/bcs/sdk/sdk_test.go diff --git a/bcs-services/bcs-helm-manager/go.mod b/bcs-services/bcs-helm-manager/go.mod index 91c885b948..75c4dcf9cf 100644 --- a/bcs-services/bcs-helm-manager/go.mod +++ b/bcs-services/bcs-helm-manager/go.mod @@ -7,7 +7,7 @@ replace ( github.com/micro/go-micro/v2 => github.com/OvertimeDog/go-micro/v2 v2.9.3 go.etcd.io/bbolt v1.3.4 => github.com/coreos/bbolt v1.3.4 google.golang.org/grpc => google.golang.org/grpc v1.26.0 - helm.sh/helm/v3 => github.com/adevjoe/helm/v3 v3.12.1-tx.3 + helm.sh/helm/v3 => github.com/adevjoe/helm/v3 v3.12.1-tx.4 k8s.io/api => k8s.io/api v0.27.2 k8s.io/apiextensions-apiserver => k8s.io/apiextensions-apiserver v0.27.2 k8s.io/apimachinery => k8s.io/apimachinery v0.27.2 diff --git a/bcs-services/bcs-helm-manager/internal/release/bcs/sdk/sdk_test.go b/bcs-services/bcs-helm-manager/internal/release/bcs/sdk/sdk_test.go new file mode 100644 index 0000000000..71aa9dfc12 --- /dev/null +++ b/bcs-services/bcs-helm-manager/internal/release/bcs/sdk/sdk_test.go @@ -0,0 +1,94 @@ +/* + * Tencent is pleased to support the open source community by making Blueking Container Service available. + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * Licensed under the MIT License (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * http://opensource.org/licenses/MIT + * 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 sdk + +import ( + "errors" + "fmt" + "os" + "testing" + + "github.com/Tencent/bk-bcs/bcs-common/common/blog" + "helm.sh/helm/v3/pkg/action" + rspb "helm.sh/helm/v3/pkg/release" + "k8s.io/cli-runtime/pkg/genericclioptions" + "k8s.io/client-go/rest" + + "github.com/Tencent/bk-bcs/bcs-services/bcs-helm-manager/internal/common" +) + +func BenchmarkList(b *testing.B) { + namespace := "bcs-system" + clusterId := "BCS-K8S-00000" + for i := 0; i < b.N; i++ { + _, err := List(namespace, clusterId) + if err != nil { + b.Errorf("List error: %s", err) + return + } + } +} + +// List helm release List的模拟函数 +func List(namespace, clusterId string) ([]*rspb.Release, error) { + conf := new(action.Configuration) + configFlag, err := getConfigFlag(namespace, clusterId) + if err != nil { + return nil, err + } + if err = conf.Init(configFlag, namespace, "", blog.Infof); err != nil { + return nil, err + } + + lister := action.NewList(conf) + lister.All = true + if len(namespace) == 0 { + lister.AllNamespaces = true + } + + releases, err := lister.Run() + if err != nil { + return nil, err + } + for i := range releases { + releases[i].Config = removeValuesTemplate(releases[i].Config) + } + return releases, nil +} + +// getConfigFlag 获取helm-client配置 +func getConfigFlag(namespace, clusterID string) (*genericclioptions.ConfigFlags, error) { + flags := genericclioptions.NewConfigFlags(false) + + apiserver := os.Getenv("APIServer") + if apiserver == "" { + return nil, errors.New("APIServer is null") + } + flagsAPIServer := fmt.Sprintf(bcsAPIGWK8SBaseURI, apiserver, clusterID) + flagsBearerToken := os.Getenv("BearerToken") + if flagsBearerToken == "" { + return nil, errors.New("BearerToken is null") + } + flags.WrapConfigFn = func(config *rest.Config) *rest.Config { + config.TLSClientConfig = rest.TLSClientConfig{ + Insecure: true, + } + config.Host = flagsAPIServer + config.BearerToken = flagsBearerToken + return config + } + + flags.Namespace = common.GetStringP(namespace) + flags.Timeout = common.GetStringP(defaultTimeout) + return flags, nil +}