github.com/verrazzano/verrazzano@v1.7.1/tools/oam-converter/pkg/resources/virtualservice/virtualservice.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 virtualservice 5 6 import ( 7 vzapi "github.com/verrazzano/verrazzano/application-operator/apis/oam/v1alpha1" 8 consts "github.com/verrazzano/verrazzano/tools/oam-converter/pkg/constants" 9 10 destination "github.com/verrazzano/verrazzano/tools/oam-converter/pkg/resources/destinationrule" 11 12 istio "istio.io/api/networking/v1beta1" 13 vsapi "istio.io/client-go/pkg/apis/networking/v1beta1" 14 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 15 "strings" 16 ) 17 18 // GetPathsFromRule gets the paths from a trait. 19 // If the trait has no paths a default path is returned. 20 func GetPathsFromRule(rule vzapi.IngressRule) []vzapi.IngressPath { 21 paths := rule.Paths 22 // If there are no paths create a default. 23 if len(paths) == 0 { 24 paths = []vzapi.IngressPath{{Path: "/", PathType: "prefix"}} 25 } 26 return paths 27 } 28 29 // CreateVirtualServiceMatchURIFromIngressTraitPath create the virtual service match uri map from an ingress trait path 30 // This is primarily used to set up defaults when either path or type are not present in the ingress path. 31 // If the provided ingress path doesn't contain a path it is default to / 32 // If the provided ingress path doesn't contain a type it is defaulted to prefix if path is / and exact otherwise. 33 func CreateVirtualServiceMatchURIFromIngressTraitPath(path vzapi.IngressPath) *istio.StringMatch { 34 // Default path to / 35 p := strings.TrimSpace(path.Path) 36 if p == "" { 37 p = "/" 38 } 39 // If path is / default type to prefix 40 // If path is not / default to exact 41 t := strings.ToLower(strings.TrimSpace(path.PathType)) 42 if t == "" { 43 if p == "/" { 44 t = "prefix" 45 } else { 46 t = "exact" 47 } 48 } 49 50 switch t { 51 case "regex": 52 return &istio.StringMatch{MatchType: &istio.StringMatch_Regex{Regex: p}} 53 case "prefix": 54 return &istio.StringMatch{MatchType: &istio.StringMatch_Prefix{Prefix: p}} 55 default: 56 return &istio.StringMatch{MatchType: &istio.StringMatch_Exact{Exact: p}} 57 } 58 } 59 60 // CreateVirtualService creates the VirtualService child resource of the trait. 61 func CreateVirtualService(ingresstrait *vzapi.IngressTrait, rule vzapi.IngressRule, 62 allHostsForTrait []string, name string, gateway *vsapi.Gateway) (*vsapi.VirtualService, error) { 63 virtualService := &vsapi.VirtualService{ 64 TypeMeta: metav1.TypeMeta{ 65 APIVersion: consts.VirtualServiceAPIVersion, 66 Kind: "VirtualService", 67 }, 68 ObjectMeta: metav1.ObjectMeta{ 69 Namespace: ingresstrait.Namespace, 70 Name: name, 71 }, 72 } 73 return mutateVirtualService(virtualService, rule, allHostsForTrait, gateway) 74 } 75 76 // mutateVirtualService mutates the output virtual service resource 77 func mutateVirtualService(virtualService *vsapi.VirtualService, rule vzapi.IngressRule, allHostsForTrait []string, gateway *vsapi.Gateway) (*vsapi.VirtualService, error) { 78 virtualService.Spec.Gateways = []string{gateway.Name} 79 virtualService.Spec.Hosts = allHostsForTrait 80 matches := []*istio.HTTPMatchRequest{} 81 paths := GetPathsFromRule(rule) 82 for _, path := range paths { 83 matches = append(matches, &istio.HTTPMatchRequest{ 84 Uri: CreateVirtualServiceMatchURIFromIngressTraitPath(path)}) 85 } 86 87 dest, err := destination.CreateDestinationFromRule(rule) 88 89 if err != nil { 90 91 return nil, err 92 } 93 route := istio.HTTPRoute{ 94 Match: matches, 95 Route: []*istio.HTTPRouteDestination{dest}} 96 virtualService.Spec.Http = []*istio.HTTPRoute{&route} 97 98 return virtualService, nil 99 }