github.com/oam-dev/kubevela@v1.9.11/pkg/utils/helm/helm.go (about) 1 /* 2 Copyright 2021 The KubeVela Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package helm 18 19 import ( 20 "context" 21 "fmt" 22 "log" 23 "os" 24 25 v1 "k8s.io/api/core/v1" 26 types2 "k8s.io/apimachinery/pkg/types" 27 "sigs.k8s.io/controller-runtime/pkg/client" 28 29 "helm.sh/helm/v3/pkg/action" 30 "helm.sh/helm/v3/pkg/cli" 31 "helm.sh/helm/v3/pkg/release" 32 33 "github.com/oam-dev/kubevela/pkg/utils/common" 34 ) 35 36 const ( 37 userNameSecKey = "username" 38 userPasswordSecKey = "password" 39 caFileSecKey = "caFile" 40 keyFileKey = "keyFile" 41 certFileKey = "certFile" 42 ) 43 44 var ( 45 settings = cli.New() 46 ) 47 48 func debug(format string, v ...interface{}) { 49 if settings.Debug { 50 format = fmt.Sprintf("[debug] %s\n", format) 51 _ = log.Output(2, fmt.Sprintf(format, v...)) 52 } 53 } 54 55 // GetHelmRelease will get helm release 56 func GetHelmRelease(ns string) ([]*release.Release, error) { 57 actionConfig := new(action.Configuration) 58 client := action.NewList(actionConfig) 59 60 if err := actionConfig.Init(settings.RESTClientGetter(), ns, os.Getenv("HELM_DRIVER"), debug); err != nil { 61 return nil, err 62 } 63 results, err := client.Run() 64 if err != nil { 65 return nil, err 66 } 67 68 return results, nil 69 } 70 71 // SetHTTPOption will read username and password from secret return a httpOption that contain these info. 72 func SetHTTPOption(ctx context.Context, k8sClient client.Client, secretRef types2.NamespacedName) (*common.HTTPOption, error) { 73 sec := v1.Secret{} 74 err := k8sClient.Get(ctx, secretRef, &sec) 75 if err != nil { 76 return nil, err 77 } 78 opts := &common.HTTPOption{} 79 if len(sec.Data[userNameSecKey]) != 0 && len(sec.Data[userPasswordSecKey]) != 0 { 80 opts.Username = string(sec.Data[userNameSecKey]) 81 opts.Password = string(sec.Data[userPasswordSecKey]) 82 } 83 if len(sec.Data[caFileSecKey]) != 0 { 84 opts.CaFile = string(sec.Data[caFileSecKey]) 85 } 86 if len(sec.Data[certFileKey]) != 0 { 87 opts.CertFile = string(sec.Data[certFileKey]) 88 } 89 if len(sec.Data[keyFileKey]) != 0 { 90 opts.KeyFile = string(sec.Data[keyFileKey]) 91 } 92 return opts, nil 93 }