forked from apache/solr-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgateway_util.go
More file actions
216 lines (191 loc) · 6.86 KB
/
Copy pathgateway_util.go
File metadata and controls
216 lines (191 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package util
import (
solr "github.com/apache/solr-operator/api/v1beta1"
"github.com/go-logr/logr"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
)
// GenerateCommonHTTPRoute creates an HTTPRoute for the common Solr service
func GenerateCommonHTTPRoute(solrCloud *solr.SolrCloud, nodeNames []string) *gatewayv1.HTTPRoute {
labels := solrCloud.SharedLabelsWith(solrCloud.GetLabels())
var annotations map[string]string
gatewayOpts := solrCloud.Spec.SolrAddressability.External.Gateway
if gatewayOpts != nil {
labels = MergeLabelsOrAnnotations(labels, gatewayOpts.Labels)
annotations = MergeLabelsOrAnnotations(annotations, gatewayOpts.Annotations)
}
extOpts := solrCloud.Spec.SolrAddressability.External
// Create advertised domain name and possible additional domain names
allDomains := append([]string{extOpts.DomainName}, extOpts.AdditionalDomainNames...)
hostnames := make([]gatewayv1.Hostname, 0, len(allDomains)+len(gatewayOpts.AdditionalHostnames))
for _, domain := range allDomains {
hostname := gatewayv1.Hostname(solrCloud.ExternalCommonUrl(domain, false))
hostnames = append(hostnames, hostname)
}
// Append user-specified additional hostnames for the common route
for _, h := range gatewayOpts.AdditionalHostnames {
hostnames = append(hostnames, gatewayv1.Hostname(h))
}
// Convert parentRefs from our type to Gateway API type
parentRefs := make([]gatewayv1.ParentReference, len(gatewayOpts.ParentRefs))
for i, ref := range gatewayOpts.ParentRefs {
parentRef := gatewayv1.ParentReference{
Name: gatewayv1.ObjectName(ref.Name),
}
if ref.Namespace != nil {
namespace := gatewayv1.Namespace(*ref.Namespace)
parentRef.Namespace = &namespace
}
if ref.SectionName != nil {
sectionName := gatewayv1.SectionName(*ref.SectionName)
parentRef.SectionName = §ionName
}
parentRefs[i] = parentRef
}
// Determine backend port
backendPort := gatewayv1.PortNumber(solrCloud.Spec.SolrAddressability.CommonServicePort)
httpRoute := &gatewayv1.HTTPRoute{
ObjectMeta: metav1.ObjectMeta{
Name: solrCloud.CommonHTTPRouteName(),
Namespace: solrCloud.GetNamespace(),
Labels: labels,
Annotations: annotations,
},
Spec: gatewayv1.HTTPRouteSpec{
CommonRouteSpec: gatewayv1.CommonRouteSpec{
ParentRefs: parentRefs,
},
Hostnames: hostnames,
Rules: []gatewayv1.HTTPRouteRule{
{
BackendRefs: []gatewayv1.HTTPBackendRef{
{
BackendRef: gatewayv1.BackendRef{
BackendObjectReference: gatewayv1.BackendObjectReference{
Name: gatewayv1.ObjectName(solrCloud.CommonServiceName()),
Port: &backendPort,
},
},
},
},
},
},
},
}
return httpRoute
}
// GenerateNodeHTTPRoute creates an HTTPRoute for individual Solr nodes
func GenerateNodeHTTPRoute(solrCloud *solr.SolrCloud, nodeName string) *gatewayv1.HTTPRoute {
labels := solrCloud.SharedLabelsWith(solrCloud.GetLabels())
var annotations map[string]string
gatewayOpts := solrCloud.Spec.SolrAddressability.External.Gateway
if gatewayOpts != nil {
labels = MergeLabelsOrAnnotations(labels, gatewayOpts.Labels)
annotations = MergeLabelsOrAnnotations(annotations, gatewayOpts.Annotations)
}
extOpts := solrCloud.Spec.SolrAddressability.External
// Create hostnames for all domains
allDomains := append([]string{extOpts.DomainName}, extOpts.AdditionalDomainNames...)
hostnames := make([]gatewayv1.Hostname, 0, len(allDomains))
for _, domain := range allDomains {
hostname := gatewayv1.Hostname(solrCloud.ExternalNodeUrl(nodeName, domain, false))
hostnames = append(hostnames, hostname)
}
// Convert parentRefs
parentRefs := make([]gatewayv1.ParentReference, len(gatewayOpts.ParentRefs))
for i, ref := range gatewayOpts.ParentRefs {
parentRef := gatewayv1.ParentReference{
Name: gatewayv1.ObjectName(ref.Name),
}
if ref.Namespace != nil {
namespace := gatewayv1.Namespace(*ref.Namespace)
parentRef.Namespace = &namespace
}
if ref.SectionName != nil {
sectionName := gatewayv1.SectionName(*ref.SectionName)
parentRef.SectionName = §ionName
}
parentRefs[i] = parentRef
}
// Determine backend port (uses NodePort which may be overridden)
backendPort := gatewayv1.PortNumber(solrCloud.NodePort())
httpRoute := &gatewayv1.HTTPRoute{
ObjectMeta: metav1.ObjectMeta{
Name: solrCloud.NodeHTTPRouteName(nodeName),
Namespace: solrCloud.GetNamespace(),
Labels: labels,
Annotations: annotations,
},
Spec: gatewayv1.HTTPRouteSpec{
CommonRouteSpec: gatewayv1.CommonRouteSpec{
ParentRefs: parentRefs,
},
Hostnames: hostnames,
Rules: []gatewayv1.HTTPRouteRule{
{
BackendRefs: []gatewayv1.HTTPBackendRef{
{
BackendRef: gatewayv1.BackendRef{
BackendObjectReference: gatewayv1.BackendObjectReference{
Name: gatewayv1.ObjectName(nodeName),
Port: &backendPort,
},
},
},
},
},
},
},
}
return httpRoute
}
// CopyHTTPRouteFields copies the fields from one HTTPRoute to another
// Returns true if there are differences between the two objects
func CopyHTTPRouteFields(from, to *gatewayv1.HTTPRoute, logger logr.Logger) bool {
requireUpdate := false
// Copy labels
if !DeepEqualWithNils(to.Labels, from.Labels) {
logger.Info("HTTPRoute labels have changed")
requireUpdate = true
to.Labels = from.Labels
}
// Copy annotations
if !DeepEqualWithNils(to.Annotations, from.Annotations) {
logger.Info("HTTPRoute annotations have changed")
requireUpdate = true
to.Annotations = from.Annotations
}
// Copy spec
if !DeepEqualWithNils(to.Spec.ParentRefs, from.Spec.ParentRefs) {
logger.Info("HTTPRoute parentRefs have changed")
requireUpdate = true
to.Spec.ParentRefs = from.Spec.ParentRefs
}
if !DeepEqualWithNils(to.Spec.Hostnames, from.Spec.Hostnames) {
logger.Info("HTTPRoute hostnames have changed")
requireUpdate = true
to.Spec.Hostnames = from.Spec.Hostnames
}
if !DeepEqualWithNils(to.Spec.Rules, from.Spec.Rules) {
logger.Info("HTTPRoute rules have changed")
requireUpdate = true
to.Spec.Rules = from.Spec.Rules
}
return requireUpdate
}