github.com/oam-dev/kubevela@v1.9.11/references/cuegen/generators/provider/testdata/valid.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 test copied and modified from https://github.com/kubevela/pkg/blob/main/cue/cuex/providers/kube/kube.go. 18 package test 19 20 import ( 21 "context" 22 _ "embed" 23 24 "github.com/kubevela/pkg/cue/cuex/providers" 25 cuexruntime "github.com/kubevela/pkg/cue/cuex/runtime" 26 "github.com/kubevela/pkg/util/runtime" 27 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 28 ) 29 30 // ResourceVars . 31 type ResourceVars struct { 32 // +usage=The cluster to use 33 Cluster string `json:"cluster"` 34 // +usage=The resource to get or apply 35 Resource *unstructured.Unstructured `json:"resource"` 36 // +usage=The options to get or apply 37 Options ApplyOptions `json:"options"` 38 } 39 40 // ApplyOptions . 41 type ApplyOptions struct { 42 // +usage=The strategy of the resource 43 ThreeWayMergePatch ThreeWayMergePatchOptions `json:"threeWayMergePatch"` 44 } 45 46 // ThreeWayMergePatchOptions . 47 type ThreeWayMergePatchOptions struct { 48 // +usage=The strategy to get or apply the resource 49 Enabled bool `json:"enabled" cue:"default:true"` 50 // +usage=The annotation prefix to use for the three way merge patch 51 AnnotationPrefix string `json:"annotationPrefix" cue:"default:resource"` 52 } 53 54 // ResourceParams is the params for resource 55 type ResourceParams providers.Params[ResourceVars] 56 57 // ResourceReturns is the returns for resource 58 type ResourceReturns providers.Returns[*unstructured.Unstructured] 59 60 // Apply . 61 func Apply(_ context.Context, _ *ResourceParams) (*ResourceReturns, error) { 62 return nil, nil 63 } 64 65 // Get . 66 func Get(_ context.Context, _ *ResourceParams) (*ResourceReturns, error) { 67 return nil, nil 68 } 69 70 // ListFilter filter for list resources 71 type ListFilter struct { 72 // +usage=The namespace to list the resources 73 Namespace string `json:"namespace,omitempty"` 74 // +usage=The label selector to filter the resources 75 MatchingLabels map[string]string `json:"matchingLabels,omitempty"` 76 } 77 78 // ListVars is the vars for list 79 type ListVars struct { 80 // +usage=The cluster to use 81 Cluster string `json:"cluster"` 82 // +usage=The filter to list the resources 83 Filter *ListFilter `json:"filter,omitempty"` 84 // +usage=The resource to list 85 Resource *unstructured.Unstructured `json:"resource"` 86 } 87 88 // ListParams is the params for list 89 type ListParams providers.Params[ListVars] 90 91 // ListReturns is the returns for list 92 type ListReturns providers.Returns[*unstructured.UnstructuredList] 93 94 // List . 95 func List(_ context.Context, _ *ListParams) (*ListReturns, error) { 96 return nil, nil 97 } 98 99 // PatchVars is the vars for patch 100 type PatchVars struct { 101 // +usage=The cluster to use 102 Cluster string `json:"cluster"` 103 // +usage=The resource to patch 104 Resource *unstructured.Unstructured `json:"resource"` 105 // +usage=The patch to be applied to the resource with kubernetes patch 106 Patch Patcher `json:"patch"` 107 } 108 109 // Patcher is the patcher 110 type Patcher struct { 111 // +usage=The type of patch being provided 112 Type string `json:"type" cue:"enum:merge,json,strategic;default:merge"` 113 Data any `json:"data"` 114 } 115 116 // PatchParams is the params for patch 117 type PatchParams providers.Params[PatchVars] 118 119 // Patch patches a kubernetes resource with patch strategy 120 func Patch(_ context.Context, _ *PatchParams) (*ResourceReturns, error) { 121 return nil, nil 122 } 123 124 // ProviderName . 125 const ProviderName = "kube" 126 127 // Package . 128 var Package = runtime.Must(cuexruntime.NewInternalPackage(ProviderName, "", map[string]cuexruntime.ProviderFn{ 129 "apply": cuexruntime.GenericProviderFn[ResourceParams, ResourceReturns](Apply), 130 "get": cuexruntime.GenericProviderFn[ResourceParams, ResourceReturns](Get), 131 "list": cuexruntime.GenericProviderFn[ListParams, ListReturns](List), 132 "patch": cuexruntime.GenericProviderFn[PatchParams, ResourceReturns](Patch), 133 }))