github.com/oam-dev/kubevela@v1.9.11/pkg/cue/cuex/providers/config/config.go (about) 1 /* 2 Copyright 2023 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 config 18 19 import ( 20 "context" 21 _ "embed" 22 23 "github.com/oam-dev/kubevela/pkg/utils/helm" 24 "github.com/oam-dev/kubevela/pkg/utils/registries" 25 26 "github.com/kubevela/pkg/cue/cuex/providers" 27 cuexruntime "github.com/kubevela/pkg/cue/cuex/runtime" 28 "github.com/kubevela/pkg/util/runtime" 29 "k8s.io/klog/v2" 30 ) 31 32 // ImageRegistryVars is the vars for image registry validation 33 type ImageRegistryVars struct { 34 Registry string `json:"registry"` 35 Auth struct { 36 Username string `json:"username"` 37 Password string `json:"password"` 38 Email string `json:"email"` 39 } `json:"auth"` 40 Insecure bool `json:"insecure"` 41 UseHTTP bool `json:"useHTTP"` 42 } 43 44 // HelmRepositoryVars is the vars for helm repository validation 45 type HelmRepositoryVars struct { 46 URL string `json:"url"` 47 Username string `json:"username"` 48 Password string `json:"password"` 49 CAFile string `json:"caFile"` 50 } 51 52 // ResponseVars is the returns for resource 53 type ResponseVars struct { 54 Result bool `json:"result"` 55 Message string `json:"message"` 56 } 57 58 // ImageRegistryParams is the params for image registry 59 type ImageRegistryParams providers.Params[ImageRegistryVars] 60 61 // HelmRepositoryParams is the params for helm repository 62 type HelmRepositoryParams providers.Params[HelmRepositoryVars] 63 64 // ValidationReturns returned struct for http response 65 type ValidationReturns providers.Returns[ResponseVars] 66 67 // ImageRegistry . 68 func ImageRegistry(ctx context.Context, validationParams *ImageRegistryParams) (*ValidationReturns, error) { 69 params := validationParams.Params 70 imageRegistry := ®istries.ImageRegistry{ 71 Registry: params.Registry, 72 Auth: params.Auth, 73 Insecure: params.Insecure, 74 UseHTTP: params.Insecure, 75 } 76 registryHelper := registries.NewRegistryHelper() 77 var message string 78 ok, err := registryHelper.Auth(ctx, imageRegistry) 79 if err != nil { 80 message = err.Error() 81 klog.Warningf("validate image-registry %s failed, err: %v", imageRegistry, err) 82 } 83 return &ValidationReturns{ 84 Returns: ResponseVars{ 85 Result: ok, 86 Message: message, 87 }, 88 }, nil 89 } 90 91 // HelmRepository . 92 func HelmRepository(ctx context.Context, validationParams *HelmRepositoryParams) (*ValidationReturns, error) { 93 params := validationParams.Params 94 helmRepository := &helm.Repository{ 95 URL: params.URL, 96 Username: params.Username, 97 Password: params.Password, 98 CAFile: params.CAFile, 99 } 100 helmHelper := helm.NewHelper() 101 var message string 102 ok, err := helmHelper.ValidateRepo(ctx, helmRepository) 103 if err != nil { 104 message = err.Error() 105 klog.Warningf("validate helm-repository %s failed, err: %v", helmRepository, err) 106 } 107 return &ValidationReturns{ 108 Returns: ResponseVars{ 109 Result: ok, 110 Message: message, 111 }, 112 }, nil 113 } 114 115 // ProviderName . 116 const ProviderName = "config" 117 118 //go:embed config.cue 119 var template string 120 121 // Package . 122 var Package = runtime.Must(cuexruntime.NewInternalPackage(ProviderName, template, map[string]cuexruntime.ProviderFn{ 123 "image-registry": cuexruntime.GenericProviderFn[ImageRegistryParams, ValidationReturns](ImageRegistry), 124 "helm-repository": cuexruntime.GenericProviderFn[HelmRepositoryParams, ValidationReturns](HelmRepository), 125 }))