-
Notifications
You must be signed in to change notification settings - Fork 55
/
common.go
251 lines (230 loc) · 8.44 KB
/
common.go
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
//
// Copyright (c) 2019-2024 Red Hat, Inc.
// Licensed 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 solvers
import (
controllerv1alpha1 "github.com/devfile/devworkspace-operator/apis/controller/v1alpha1"
"github.com/devfile/devworkspace-operator/pkg/common"
"github.com/devfile/devworkspace-operator/pkg/constants"
routeV1 "github.com/openshift/api/route/v1"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/utils/pointer"
)
type DevWorkspaceMetadata struct {
DevWorkspaceId string
Namespace string
PodSelector map[string]string
}
// GetDiscoverableServicesForEndpoints converts the endpoint list into a set of services, each corresponding to a single discoverable
// endpoint from the list. Endpoints with the NoneEndpointExposure are ignored.
func GetDiscoverableServicesForEndpoints(endpoints map[string]controllerv1alpha1.EndpointList, meta DevWorkspaceMetadata) []corev1.Service {
var services []corev1.Service
for _, machineEndpoints := range endpoints {
for _, endpoint := range machineEndpoints {
if endpoint.Exposure == controllerv1alpha1.NoneEndpointExposure {
continue
}
if endpoint.Attributes.GetBoolean(string(controllerv1alpha1.DiscoverableAttribute), nil) {
// Create service with name matching endpoint
// TODO: This could cause a reconcile conflict if multiple workspaces define the same discoverable endpoint
// Also endpoint names may not be valid as service names
servicePort := corev1.ServicePort{
Name: common.EndpointName(endpoint.Name),
Protocol: corev1.ProtocolTCP,
Port: int32(endpoint.TargetPort),
TargetPort: intstr.FromInt(endpoint.TargetPort),
}
services = append(services, corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: common.EndpointName(endpoint.Name),
Namespace: meta.Namespace,
Labels: map[string]string{
constants.DevWorkspaceIDLabel: meta.DevWorkspaceId,
},
Annotations: map[string]string{
constants.DevWorkspaceDiscoverableServiceAnnotation: "true",
},
},
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{servicePort},
Selector: meta.PodSelector,
Type: corev1.ServiceTypeClusterIP,
},
})
}
}
}
return services
}
// GetServiceForEndpoints returns a single service that exposes all endpoints of given exposure types, possibly also including the discoverable types.
// `nil` is returned if the service would expose no ports satisfying the provided criteria.
func GetServiceForEndpoints(endpoints map[string]controllerv1alpha1.EndpointList, meta DevWorkspaceMetadata, includeDiscoverable bool, exposureType ...controllerv1alpha1.EndpointExposure) *corev1.Service {
// "set" of ports that are still left for exposure
ports := map[int]bool{}
for _, es := range endpoints {
for _, endpoint := range es {
ports[endpoint.TargetPort] = true
}
}
// "set" of exposure types that are allowed
validExposures := map[controllerv1alpha1.EndpointExposure]bool{}
for _, exp := range exposureType {
validExposures[exp] = true
}
var exposedPorts []corev1.ServicePort
for _, es := range endpoints {
for _, endpoint := range es {
if !validExposures[endpoint.Exposure] {
continue
}
if !includeDiscoverable && endpoint.Attributes.GetBoolean(string(controllerv1alpha1.DiscoverableAttribute), nil) {
continue
}
if ports[endpoint.TargetPort] {
// make sure we don't mention the same port twice
ports[endpoint.TargetPort] = false
exposedPorts = append(exposedPorts, corev1.ServicePort{
Name: common.EndpointName(endpoint.Name),
Protocol: corev1.ProtocolTCP,
Port: int32(endpoint.TargetPort),
TargetPort: intstr.FromInt(endpoint.TargetPort),
})
}
}
}
if len(exposedPorts) == 0 {
return nil
}
return &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: common.ServiceName(meta.DevWorkspaceId),
Namespace: meta.Namespace,
Labels: map[string]string{
constants.DevWorkspaceIDLabel: meta.DevWorkspaceId,
},
},
Spec: corev1.ServiceSpec{
Selector: meta.PodSelector,
Type: corev1.ServiceTypeClusterIP,
Ports: exposedPorts,
},
}
}
func getServicesForEndpoints(endpoints map[string]controllerv1alpha1.EndpointList, meta DevWorkspaceMetadata) []corev1.Service {
if len(endpoints) == 0 {
return nil
}
service := GetServiceForEndpoints(endpoints, meta, true, controllerv1alpha1.PublicEndpointExposure, controllerv1alpha1.InternalEndpointExposure)
if service == nil {
return nil
}
return []corev1.Service{
*service,
}
}
func getRoutesForSpec(routingSuffix string, endpoints map[string]controllerv1alpha1.EndpointList, meta DevWorkspaceMetadata) []routeV1.Route {
var routes []routeV1.Route
for _, machineEndpoints := range endpoints {
for _, endpoint := range machineEndpoints {
if endpoint.Exposure != controllerv1alpha1.PublicEndpointExposure {
continue
}
routes = append(routes, getRouteForEndpoint(routingSuffix, endpoint, meta))
}
}
return routes
}
func getIngressesForSpec(routingSuffix string, endpoints map[string]controllerv1alpha1.EndpointList, meta DevWorkspaceMetadata) []networkingv1.Ingress {
var ingresses []networkingv1.Ingress
for _, machineEndpoints := range endpoints {
for _, endpoint := range machineEndpoints {
if endpoint.Exposure != controllerv1alpha1.PublicEndpointExposure {
continue
}
ingresses = append(ingresses, getIngressForEndpoint(routingSuffix, endpoint, meta))
}
}
return ingresses
}
func getRouteForEndpoint(routingSuffix string, endpoint controllerv1alpha1.Endpoint, meta DevWorkspaceMetadata) routeV1.Route {
targetEndpoint := intstr.FromInt(endpoint.TargetPort)
endpointName := common.EndpointName(endpoint.Name)
return routeV1.Route{
ObjectMeta: metav1.ObjectMeta{
Name: common.RouteName(meta.DevWorkspaceId, endpointName),
Namespace: meta.Namespace,
Labels: map[string]string{
constants.DevWorkspaceIDLabel: meta.DevWorkspaceId,
},
Annotations: routeAnnotations(endpointName, endpoint.Annotations),
},
Spec: routeV1.RouteSpec{
Host: common.WorkspaceHostname(routingSuffix, meta.DevWorkspaceId),
Path: common.EndpointPath(endpointName),
TLS: &routeV1.TLSConfig{
InsecureEdgeTerminationPolicy: routeV1.InsecureEdgeTerminationPolicyRedirect,
Termination: routeV1.TLSTerminationEdge,
},
To: routeV1.RouteTargetReference{
Kind: "Service",
Name: common.ServiceName(meta.DevWorkspaceId),
},
Port: &routeV1.RoutePort{
TargetPort: targetEndpoint,
},
},
}
}
func getIngressForEndpoint(routingSuffix string, endpoint controllerv1alpha1.Endpoint, meta DevWorkspaceMetadata) networkingv1.Ingress {
endpointName := common.EndpointName(endpoint.Name)
hostname := common.EndpointHostname(routingSuffix, meta.DevWorkspaceId, endpointName, endpoint.TargetPort)
ingressPathType := networkingv1.PathTypeImplementationSpecific
return networkingv1.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: common.RouteName(meta.DevWorkspaceId, endpointName),
Namespace: meta.Namespace,
Labels: map[string]string{
constants.DevWorkspaceIDLabel: meta.DevWorkspaceId,
},
Annotations: nginxIngressAnnotations(endpoint.Name, endpoint.Annotations),
},
Spec: networkingv1.IngressSpec{
IngressClassName: pointer.String("nginx"),
Rules: []networkingv1.IngressRule{
{
Host: hostname,
IngressRuleValue: networkingv1.IngressRuleValue{
HTTP: &networkingv1.HTTPIngressRuleValue{
Paths: []networkingv1.HTTPIngressPath{
{
Backend: networkingv1.IngressBackend{
Service: &networkingv1.IngressServiceBackend{
Name: common.ServiceName(meta.DevWorkspaceId),
Port: networkingv1.ServiceBackendPort{Number: int32(endpoint.TargetPort)},
},
},
PathType: &ingressPathType,
Path: "/",
},
},
},
},
},
},
},
}
}