github.com/oam-dev/kubevela@v1.9.11/pkg/controller/core.oam.dev/v1beta1/application/assemble/assemble.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 assemble 18 19 import ( 20 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 21 "k8s.io/klog/v2" 22 23 "github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1" 24 "github.com/oam-dev/kubevela/apis/types" 25 "github.com/oam-dev/kubevela/pkg/oam" 26 "github.com/oam-dev/kubevela/pkg/oam/util" 27 ) 28 29 // checkAutoDetectComponent will check if the standardWorkload is empty, 30 // currently only Helm-based component is possible to be auto-detected 31 // TODO implement auto-detect mechanism 32 func checkAutoDetectComponent(wl *unstructured.Unstructured) bool { 33 return wl == nil || (len(wl.GetAPIVersion()) == 0 && len(wl.GetKind()) == 0) 34 } 35 36 // PrepareBeforeApply will prepare for some necessary info before apply 37 func PrepareBeforeApply(comp *types.ComponentManifest, appRev *v1beta1.ApplicationRevision) (*unstructured.Unstructured, []*unstructured.Unstructured, error) { 38 if checkAutoDetectComponent(comp.ComponentOutput) { 39 return nil, nil, nil 40 } 41 compRevisionName := comp.RevisionName 42 compName := comp.Name 43 additionalLabel := map[string]string{ 44 oam.LabelAppComponentRevision: compRevisionName, 45 oam.LabelAppRevisionHash: appRev.Labels[oam.LabelAppRevisionHash], 46 } 47 wl := assembleWorkload(compName, comp.ComponentOutput, additionalLabel) 48 49 assembledTraits := make([]*unstructured.Unstructured, len(comp.ComponentOutputsAndTraits)) 50 51 HandleCheckManageWorkloadTrait(*appRev, []*types.ComponentManifest{comp}) 52 53 for i, trait := range comp.ComponentOutputsAndTraits { 54 setTraitLabels(trait, additionalLabel) 55 assembledTraits[i] = trait 56 } 57 58 return wl, assembledTraits, nil 59 } 60 61 func assembleWorkload(compName string, wl *unstructured.Unstructured, labels map[string]string) *unstructured.Unstructured { 62 // use component name as workload name if workload name is not specified 63 // don't override the name set in render phase if exist 64 if len(wl.GetName()) == 0 { 65 wl.SetName(compName) 66 } 67 setWorkloadLabels(wl, labels) 68 69 klog.InfoS("Successfully assemble a workload", "workload", klog.KObj(wl), "APIVersion", wl.GetAPIVersion(), "Kind", wl.GetKind()) 70 return wl 71 } 72 73 // component revision label added here 74 // label key: app.oam.dev/revision 75 func setWorkloadLabels(wl *unstructured.Unstructured, additionalLabels map[string]string) { 76 // add more workload-specific labels here 77 util.AddLabels(wl, additionalLabels) 78 } 79 80 // component revision label added here 81 // label key: app.oam.dev/revision 82 func setTraitLabels(trait *unstructured.Unstructured, additionalLabels map[string]string) { 83 // add more trait-specific labels here 84 util.AddLabels(trait, additionalLabels) 85 } 86 87 // HandleCheckManageWorkloadTrait will checkout every trait whether a manage-workload trait, if yes set label and annotation in trait 88 func HandleCheckManageWorkloadTrait(appRev v1beta1.ApplicationRevision, comps []*types.ComponentManifest) { 89 traitDefs := appRev.Spec.TraitDefinitions 90 manageWorkloadTrait := map[string]bool{} 91 for traitName, definition := range traitDefs { 92 if definition.Spec.ManageWorkload { 93 manageWorkloadTrait[traitName] = true 94 } 95 } 96 if len(manageWorkloadTrait) == 0 { 97 return 98 } 99 for _, comp := range comps { 100 for _, trait := range comp.ComponentOutputsAndTraits { 101 traitType := trait.GetLabels()[oam.TraitTypeLabel] 102 if manageWorkloadTrait[traitType] { 103 trait.SetLabels(util.MergeMapOverrideWithDst(trait.GetLabels(), map[string]string{oam.LabelManageWorkloadTrait: "true"})) 104 } 105 } 106 } 107 }