|
5 | 5 | "fmt" |
6 | 6 | corev1 "k8s.io/api/core/v1" |
7 | 7 | "k8s.io/apimachinery/pkg/types" |
8 | | - "net" |
9 | 8 | "sigs.k8s.io/controller-runtime/pkg/client" |
10 | | - "strings" |
11 | 9 | ) |
12 | 10 |
|
13 | 11 | // ListL4Routes retrieves all Layer 4 routes (TCP, UDP, TLS) from the cluster. |
@@ -89,74 +87,3 @@ func isServiceReferredByRoute(route preLoadRouteDescriptor, svcID types.Namespac |
89 | 87 | } |
90 | 88 | return false |
91 | 89 | } |
92 | | - |
93 | | -// IsHostNameInValidFormat follows RFC1123 requirement except |
94 | | -// 1. no IP allowed |
95 | | -// 2. wildcard is only allowed as leftmost character |
96 | | -// Allowed Characters: Hostname labels must only contain lowercase ASCII letters (a-z), digits (0-9), and hyphens (-). |
97 | | -// Starting with a Digit: RFC 1123 allows labels to begin with a digit, which is a departure from the previous RFC 952 restriction. |
98 | | -// Length: Each label in a hostname can be between 1 and 63 characters long. |
99 | | -// Overall Hostname Length: The entire hostname, including the periods separating labels, cannot exceed 253 characters. |
100 | | -// Case: Hostnames are case-insensitive. |
101 | | -// Underscore: Underscores are not permitted in hostnames. |
102 | | -// Other Symbols: No other symbols, punctuation, or whitespace is allowed in hostnames |
103 | | -// Most of the requirements above is already checked by CRD pattern: Pattern=`^(\*\.)?[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$` |
104 | | -// Thus this function only checks for 1. if it is IP 2. label length is between 1 and 63 |
105 | | -func IsHostNameInValidFormat(hostName string) (bool, error) { |
106 | | - if net.ParseIP(hostName) != nil { |
107 | | - |
108 | | - return false, fmt.Errorf("hostname can not be IP address") |
109 | | - } |
110 | | - labels := strings.Split(hostName, ".") |
111 | | - if strings.HasPrefix(hostName, "*.") { |
112 | | - labels = labels[1:] |
113 | | - } |
114 | | - for _, label := range labels { |
115 | | - if len(label) < 1 || len(label) > 63 { |
116 | | - return false, fmt.Errorf("invalid hostname label length, length must between 1 and 63") |
117 | | - } |
118 | | - } |
119 | | - return true, nil |
120 | | -} |
121 | | - |
122 | | -// isHostnameCompatible checks if given two hostnames are compatible with each other |
123 | | -// this function is used to check if listener hostname and Route hostname match |
124 | | -func isHostnameCompatible(hostnameOne, hostnameTwo string) bool { |
125 | | - // exact match |
126 | | - if hostnameOne == hostnameTwo { |
127 | | - return true |
128 | | - } |
129 | | - |
130 | | - // suffix match - hostnameOne is a wildcard |
131 | | - if strings.HasPrefix(hostnameOne, "*.") && strings.HasSuffix(hostnameTwo, hostnameOne[1:]) { |
132 | | - return true |
133 | | - } |
134 | | - // suffix match - hostnameTwo is a wildcard |
135 | | - if strings.HasPrefix(hostnameTwo, "*.") && strings.HasSuffix(hostnameOne, hostnameTwo[1:]) { |
136 | | - return true |
137 | | - } |
138 | | - return false |
139 | | -} |
140 | | - |
141 | | -// GetHostnamePrecedenceOrder Hostname precedence ordering rule: |
142 | | -// 1. non-wildcard has higher precedence than wildcard |
143 | | -// 2. hostname with longer characters have higher precedence than those with shorter ones |
144 | | -// -1 means hostnameOne has higher precedence, 1 means hostnameTwo has higher precedence, 0 means equal |
145 | | -func GetHostnamePrecedenceOrder(hostnameOne, hostnameTwo string) int { |
146 | | - isHostnameOneWildcard := strings.HasPrefix(hostnameOne, "*.") |
147 | | - isHostnameTwoWildcard := strings.HasPrefix(hostnameTwo, "*.") |
148 | | - |
149 | | - if !isHostnameOneWildcard && isHostnameTwoWildcard { |
150 | | - return -1 |
151 | | - } else if isHostnameOneWildcard && !isHostnameTwoWildcard { |
152 | | - return 1 |
153 | | - } else { |
154 | | - if len(hostnameOne) > len(hostnameTwo) { |
155 | | - return -1 |
156 | | - } else if len(hostnameOne) < len(hostnameTwo) { |
157 | | - return 1 |
158 | | - } else { |
159 | | - return 0 |
160 | | - } |
161 | | - } |
162 | | -} |
0 commit comments