github.com/oam-dev/kubevela@v1.9.11/references/appfile/api/service.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 api 18 19 import ( 20 "encoding/json" 21 22 "k8s.io/apimachinery/pkg/runtime" 23 24 "github.com/oam-dev/kubevela/apis/core.oam.dev/common" 25 "github.com/oam-dev/kubevela/references/appfile/template" 26 ) 27 28 // Service defines the service spec for AppFile, it will contain all related information including OAM component, traits, source to image, etc... 29 type Service map[string]interface{} 30 31 // DefaultWorkloadType defines the default service type if no type specified in Appfile 32 const DefaultWorkloadType = "webservice" 33 34 // GetType get type from AppFile 35 func (s Service) GetType() string { 36 t, ok := s["type"] 37 if !ok { 38 return DefaultWorkloadType 39 } 40 return t.(string) 41 } 42 43 // GetUserConfigName get user config from AppFile, it will contain config file in it. 44 func (s Service) GetUserConfigName() string { 45 t, ok := s["config"] 46 if !ok { 47 return "" 48 } 49 return t.(string) 50 } 51 52 // GetApplicationConfig will get OAM workload and trait information exclude inner section('build','type' and 'config') 53 func (s Service) GetApplicationConfig() map[string]interface{} { 54 config := make(map[string]interface{}) 55 outerLoop: 56 for k, v := range s { 57 switch k { 58 case "build", "type", "config": // skip 59 continue outerLoop 60 } 61 config[k] = v 62 } 63 return config 64 } 65 66 // RenderServiceToApplicationComponent render all capabilities of a service to CUE values to KubeVela Application. 67 func (s Service) RenderServiceToApplicationComponent(tm template.Manager, serviceName string) (common.ApplicationComponent, error) { 68 69 // sort out configs by workload/trait 70 workloadKeys := map[string]interface{}{} 71 var traits []common.ApplicationTrait 72 73 wtype := s.GetType() 74 75 comp := common.ApplicationComponent{ 76 Name: serviceName, 77 Type: wtype, 78 } 79 80 for k, v := range s.GetApplicationConfig() { 81 if tm.IsTrait(k) { 82 trait := common.ApplicationTrait{ 83 Type: k, 84 } 85 pts := &runtime.RawExtension{} 86 jt, err := json.Marshal(v) 87 if err != nil { 88 return comp, err 89 } 90 if err := pts.UnmarshalJSON(jt); err != nil { 91 return comp, err 92 } 93 trait.Properties = pts 94 traits = append(traits, trait) 95 continue 96 } 97 workloadKeys[k] = v 98 } 99 100 // Handle workloadKeys to settings 101 settings := &runtime.RawExtension{} 102 pt, err := json.Marshal(workloadKeys) 103 if err != nil { 104 return comp, err 105 } 106 if err := settings.UnmarshalJSON(pt); err != nil { 107 return comp, err 108 } 109 comp.Properties = settings 110 111 if len(traits) > 0 { 112 comp.Traits = traits 113 } 114 115 return comp, nil 116 } 117 118 // GetServices will get all services defined in AppFile 119 func (af *AppFile) GetServices() map[string]Service { 120 return af.Services 121 }