github.com/verrazzano/verrazzano@v1.7.0/platform-operator/internal/vzconfig/ingress.go (about) 1 // Copyright (c) 2021, 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 package vzconfig 4 5 import ( 6 "fmt" 7 "github.com/verrazzano/verrazzano/pkg/nginxutil" 8 vzapi "github.com/verrazzano/verrazzano/platform-operator/apis/verrazzano/v1alpha1" 9 "github.com/verrazzano/verrazzano/platform-operator/apis/verrazzano/v1beta1" 10 vpoconst "github.com/verrazzano/verrazzano/platform-operator/constants" 11 v1 "k8s.io/api/core/v1" 12 "k8s.io/apimachinery/pkg/runtime" 13 "sigs.k8s.io/controller-runtime/pkg/client" 14 ) 15 16 const defaultWildcardDomain = "nip.io" 17 const defaultIngressClassName = "verrazzano-nginx" 18 19 // GetEnvName Returns the configured environment name, or "default" if not specified in the configuration 20 func GetEnvName(vz *vzapi.Verrazzano) string { 21 envName := vz.Spec.EnvironmentName 22 if len(envName) == 0 { 23 envName = "default" 24 } 25 return envName 26 } 27 28 // FindVolumeTemplate Find a named VolumeClaimTemplate in the list for v1beta1. 29 func FindVolumeTemplate(templateName string, object runtime.Object) (*v1.PersistentVolumeClaimSpec, bool) { 30 templates := getVolumeClaimSpecTemplates(object) 31 for i, template := range templates { 32 if templateName == template.Name { 33 return &templates[i].Spec, true 34 } 35 } 36 return nil, false 37 } 38 39 // GetWildcardDomain Get the wildcard domain from the Verrazzano config 40 func GetWildcardDomain(DNS interface{}) string { 41 wildcardDomain := defaultWildcardDomain 42 if dnsConfigv1alpha1, ok := DNS.(*vzapi.DNSComponent); ok { 43 if dnsConfigv1alpha1 != nil && dnsConfigv1alpha1.Wildcard != nil && len(dnsConfigv1alpha1.Wildcard.Domain) > 0 { 44 wildcardDomain = dnsConfigv1alpha1.Wildcard.Domain 45 } 46 } 47 if dnsConfigv1beta1, ok := DNS.(*v1beta1.DNSComponent); ok { 48 if dnsConfigv1beta1 != nil && dnsConfigv1beta1.Wildcard != nil && len(dnsConfigv1beta1.Wildcard.Domain) > 0 { 49 wildcardDomain = dnsConfigv1beta1.Wildcard.Domain 50 } 51 } 52 return wildcardDomain 53 } 54 55 // GetDNSSuffix Returns the DNS suffix for the Verrazzano installation 56 // - port of install script function get_dns_suffix from config.sh 57 func GetDNSSuffix(client client.Client, vz *vzapi.Verrazzano) (string, error) { 58 var dnsSuffix string 59 dnsConfig := vz.Spec.Components.DNS 60 if dnsConfig == nil || dnsConfig.Wildcard != nil { 61 ingressIP, err := GetIngressIP(client, vz) 62 if err != nil { 63 return "", err 64 } 65 dnsSuffix = fmt.Sprintf("%s.%s", ingressIP, GetWildcardDomain(dnsConfig)) 66 } else if dnsConfig.OCI != nil { 67 dnsSuffix = dnsConfig.OCI.DNSZoneName 68 } else if dnsConfig.External != nil { 69 dnsSuffix = dnsConfig.External.Suffix 70 } 71 if len(dnsSuffix) == 0 { 72 return "", fmt.Errorf("Invalid OCI DNS configuration, no zone name specified") 73 } 74 return dnsSuffix, nil 75 } 76 77 // Identify the service type, LB vs NodePort 78 func GetIngressServiceType(cr *vzapi.Verrazzano) (vzapi.IngressType, error) { 79 ingressConfig := cr.Spec.Components.Ingress 80 if ingressConfig == nil || len(ingressConfig.Type) == 0 { 81 return vzapi.LoadBalancer, nil 82 } 83 switch ingressConfig.Type { 84 case vzapi.NodePort, vzapi.LoadBalancer: 85 return ingressConfig.Type, nil 86 default: 87 return "", fmt.Errorf("Unrecognized ingress type %s", ingressConfig.Type) 88 } 89 } 90 91 // GetIngressIP Returns the ingress IP of the LoadBalancer 92 // - port of install scripts function get_verrazzano_ingress_ip in config.sh 93 func GetIngressIP(client client.Client, vz *vzapi.Verrazzano) (string, error) { 94 serviceType, err := GetIngressServiceType(vz) 95 if err != nil { 96 return "", err 97 } 98 return GetExternalIP(client, serviceType, vpoconst.NGINXControllerServiceName, nginxutil.IngressNGINXNamespace()) 99 } 100 101 // BuildDNSDomain Constructs the full DNS subdomain for the deployment 102 func BuildDNSDomain(client client.Client, vz *vzapi.Verrazzano) (string, error) { 103 dnsSuffix, err := GetDNSSuffix(client, vz) 104 if err != nil { 105 return "", err 106 } 107 envName := GetEnvName(vz) 108 dnsDomain := fmt.Sprintf("%s.%s", envName, dnsSuffix) 109 return dnsDomain, nil 110 } 111 112 // GetIngressClassName gets the ingress class name or default of "nginx" if not specified 113 func GetIngressClassName(vz *vzapi.Verrazzano) string { 114 ingressComponent := vz.Spec.Components.Ingress 115 if ingressComponent != nil && ingressComponent.IngressClassName != nil && *ingressComponent.IngressClassName != "" { 116 return *ingressComponent.IngressClassName 117 } 118 return defaultIngressClassName 119 } 120 121 // getVolumeClaimSpecTemplates returns the volume claim specs in v1beta1. 122 func getVolumeClaimSpecTemplates(object runtime.Object) []v1beta1.VolumeClaimSpecTemplate { 123 if effectiveCR, ok := object.(*vzapi.Verrazzano); ok { 124 return vzapi.ConvertVolumeClaimTemplateTo(effectiveCR.Spec.VolumeClaimSpecTemplates) 125 } else if effectiveCR, ok := object.(*v1beta1.Verrazzano); ok { 126 return effectiveCR.Spec.VolumeClaimSpecTemplates 127 } 128 return nil 129 }