github.com/verrazzano/verrazzano@v1.7.0/tools/oam-converter/pkg/workloads/extractWorkload.go (about) 1 // Copyright (c) 2023, Oracle and/or its affiliates. 2 // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. 3 4 package workloads 5 6 import ( 7 "encoding/json" 8 "errors" 9 "fmt" 10 vzapi "github.com/verrazzano/verrazzano/application-operator/apis/oam/v1alpha1" 11 "github.com/verrazzano/verrazzano/tools/oam-converter/pkg/types" 12 corev1 "k8s.io/api/core/v1" 13 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 14 "log" 15 ) 16 17 // ExtractWorkload - Extract workload from comp map 18 func ExtractWorkload(components []map[string]interface{}, conversionComponents []*types.ConversionComponents) ([]*types.ConversionComponents, error) { 19 weblogicMap := &unstructured.Unstructured{} 20 for _, comp := range components { 21 22 spec, found, err := unstructured.NestedMap(comp, "spec") 23 if !found || err != nil { 24 return nil, errors.New("spec key in a component doesn't exist or not found in the specified type") 25 } 26 workload, found, err := unstructured.NestedMap(spec, "workload") 27 if !found || err != nil { 28 return nil, errors.New("workload in a component doesn't exist or not found in the specified type") 29 } 30 kind, found, err := unstructured.NestedString(workload, "kind") 31 if !found || err != nil { 32 return nil, errors.New("workload kind in a component doesn't exist or not found in the specified type") 33 } 34 35 compMetadata, found, err := unstructured.NestedMap(comp, "metadata") 36 if !found || err != nil { 37 return nil, errors.New("component metadata doesn't exist or not found in the specified type") 38 } 39 name, found, err := unstructured.NestedString(compMetadata, "name") 40 if !found || err != nil { 41 return nil, errors.New("component name doesn't exist or not found in the specified type") 42 } 43 44 //Checking if the specific component name is present in the component names array 45 //where component names array is the array of component names 46 //which has ingress traits applied on it 47 48 for i := range conversionComponents { 49 if conversionComponents[i].ComponentName == name { 50 51 switch kind { 52 case "VerrazzanoWebLogicWorkload": 53 54 weblogicWorkload := &vzapi.VerrazzanoWebLogicWorkload{} 55 workloadJSON, err := json.Marshal(workload) 56 57 if err != nil { 58 log.Fatalf("Failed to marshal trait: %v", err) 59 60 } 61 62 err = json.Unmarshal(workloadJSON, &weblogicWorkload) 63 if err != nil { 64 fmt.Printf("Failed to unmarshal: %v\n", err) 65 66 } 67 68 conversionComponents[i].Weblogicworkload = weblogicMap 69 70 case "VerrazzanoHelidonWorkload": 71 //Appending the helidon workloads in the helidon workload array 72 helidonWorkload := &unstructured.Unstructured{} 73 workloadJSON, err := json.Marshal(workload) 74 75 if err != nil { 76 log.Fatalf("Failed to marshal trait: %v", err) 77 } 78 79 err = json.Unmarshal(workloadJSON, &helidonWorkload) 80 if err != nil { 81 fmt.Printf("Failed to unmarshal: %v\n", err) 82 83 } 84 conversionComponents[i].Helidonworkload = helidonWorkload 85 case "VerrazzanoCoherenceWorkload": 86 87 //Appending the coherence workloads in the coherence workload array 88 coherenceWorkload := &unstructured.Unstructured{} 89 workloadJSON, err := json.Marshal(workload) 90 91 if err != nil { 92 log.Fatalf("Failed to marshal trait: %v", err) 93 } 94 95 err = json.Unmarshal(workloadJSON, &coherenceWorkload) 96 if err != nil { 97 fmt.Printf("Failed to unmarshal: %v\n", err) 98 99 } 100 conversionComponents[i].Coherenceworkload = coherenceWorkload 101 case "Service": 102 service := &corev1.Service{} 103 workloadJSON, err := json.Marshal(workload) 104 if err != nil { 105 log.Fatalf("Failed to marshal trait: %v", err) 106 } 107 108 err = json.Unmarshal(workloadJSON, &service) 109 if err != nil { 110 fmt.Printf("Failed to unmarshal: %v\n", err) 111 112 } 113 conversionComponents[i].Service = service 114 } 115 116 } 117 } 118 } 119 return conversionComponents, nil 120 121 }