github.com/oam-dev/kubevela@v1.9.11/e2e/addon/mock/utils/utils.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 utils 18 19 import ( 20 "context" 21 "fmt" 22 "strings" 23 24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 25 26 v1 "k8s.io/api/core/v1" 27 "k8s.io/apimachinery/pkg/types" 28 "sigs.k8s.io/yaml" 29 30 apierrors "k8s.io/apimachinery/pkg/api/errors" 31 32 "github.com/oam-dev/kubevela/pkg/utils/common" 33 ) 34 35 var ( 36 // Port is mock server's exposed port 37 Port = 9098 38 velaRegistry = ` 39 apiVersion: v1 40 data: 41 registries: '{ "KubeVela":{ "name": "KubeVela", "oss": { "end_point": "http://REGISTRY_ADDR", 42 "bucket": "" } }, "Test-Helm":{ "name": "Test-Helm", "helm": { "name":"", "password":"", "url": "http://HELM_ADDR"} } }' 43 kind: ConfigMap 44 metadata: 45 name: vela-addon-registry 46 namespace: vela-system 47 ` 48 ) 49 50 // ApplyMockServerConfig config mock server as addon registry 51 func ApplyMockServerConfig() error { 52 args := common.Args{Schema: common.Scheme} 53 k8sClient, err := args.GetClient() 54 if err != nil { 55 return err 56 } 57 ctx := context.Background() 58 originCm := v1.ConfigMap{} 59 cm := v1.ConfigMap{} 60 61 registryCmStr := strings.ReplaceAll(velaRegistry, "REGISTRY_ADDR", fmt.Sprintf("127.0.0.1:%d", Port)) 62 registryCmStr = strings.ReplaceAll(registryCmStr, "HELM_ADDR", fmt.Sprintf("127.0.0.1:%d/helm", Port)) 63 64 err = yaml.Unmarshal([]byte(registryCmStr), &cm) 65 if err != nil { 66 return err 67 } 68 69 otherRegistry := cm.DeepCopy() 70 71 err = k8sClient.Get(ctx, types.NamespacedName{Name: cm.Name, Namespace: cm.Namespace}, &originCm) 72 if err != nil && apierrors.IsNotFound(err) { 73 if err = k8sClient.Create(ctx, &cm); err != nil { 74 return err 75 } 76 } else { 77 cm.ResourceVersion = originCm.ResourceVersion 78 if err = k8sClient.Update(ctx, &cm); err != nil { 79 return err 80 } 81 } 82 if err := k8sClient.Create(ctx, &v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "test-vela"}}); err != nil { 83 if !apierrors.IsAlreadyExists(err) { 84 return err 85 } 86 } 87 otherRegistry.SetNamespace("test-vela") 88 if err := k8sClient.Create(ctx, otherRegistry); err != nil { 89 if !apierrors.IsAlreadyExists(err) { 90 return err 91 } 92 } 93 return nil 94 }