-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathcluster_solver.go
More file actions
146 lines (126 loc) · 4.54 KB
/
cluster_solver.go
File metadata and controls
146 lines (126 loc) · 4.54 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
//
// Copyright (c) 2019-2025 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 (
"fmt"
"github.com/devfile/devworkspace-operator/pkg/common"
"github.com/devfile/devworkspace-operator/pkg/constants"
corev1 "k8s.io/api/core/v1"
controllerv1alpha1 "github.com/devfile/devworkspace-operator/apis/controller/v1alpha1"
"github.com/go-logr/logr"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const (
serviceServingCertAnnot = "service.beta.openshift.io/serving-cert-secret-name"
)
type ClusterSolver struct {
TLS bool
client client.Client
logger logr.Logger
}
var _ RoutingSolver = (*ClusterSolver)(nil)
// NewClusterSolver creates a new ClusterSolver with the provided dependencies
func NewClusterSolver(client client.Client, logger logr.Logger, tls bool) *ClusterSolver {
return &ClusterSolver{
TLS: tls,
client: client,
logger: logger,
}
}
func (s *ClusterSolver) FinalizerRequired(*controllerv1alpha1.DevWorkspaceRouting) bool {
return false
}
func (s *ClusterSolver) Finalize(*controllerv1alpha1.DevWorkspaceRouting) error {
return nil
}
func (s *ClusterSolver) GetSpecObjects(routing *controllerv1alpha1.DevWorkspaceRouting, workspaceMeta DevWorkspaceMetadata) (RoutingObjects, error) {
spec := routing.Spec
services := getServicesForEndpoints(spec.Endpoints, workspaceMeta)
discoverableServices, err := GetDiscoverableServicesForEndpoints(spec.Endpoints, workspaceMeta, s.client, s.logger)
if err != nil {
return RoutingObjects{}, err
}
services = append(services, discoverableServices...)
podAdditions := &controllerv1alpha1.PodAdditions{}
if s.TLS {
readOnlyMode := int32(420)
for idx, service := range services {
if services[idx].Annotations == nil {
services[idx].Annotations = map[string]string{}
}
services[idx].Annotations[serviceServingCertAnnot] = service.Name
podAdditions.Volumes = append(podAdditions.Volumes, corev1.Volume{
Name: common.ServingCertVolumeName(service.Name),
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: service.Name,
DefaultMode: &readOnlyMode,
},
},
})
podAdditions.VolumeMounts = append(podAdditions.VolumeMounts, corev1.VolumeMount{
Name: common.ServingCertVolumeName(service.Name),
ReadOnly: true,
MountPath: "/var/serving-cert/",
})
}
}
return RoutingObjects{
Services: services,
PodAdditions: podAdditions,
}, nil
}
func (s *ClusterSolver) GetExposedEndpoints(
endpoints map[string]controllerv1alpha1.EndpointList,
routingObj RoutingObjects) (exposedEndpoints map[string]controllerv1alpha1.ExposedEndpointList, ready bool, err error) {
exposedEndpoints = map[string]controllerv1alpha1.ExposedEndpointList{}
for machineName, machineEndpoints := range endpoints {
for _, endpoint := range machineEndpoints {
if endpoint.Exposure == controllerv1alpha1.NoneEndpointExposure {
continue
}
url, err := resolveServiceHostnameForEndpoint(endpoint, routingObj.Services)
if err != nil {
return nil, false, err
}
exposedEndpoints[machineName] = append(exposedEndpoints[machineName], controllerv1alpha1.ExposedEndpoint{
Name: endpoint.Name,
Url: url,
Attributes: endpoint.Attributes,
})
}
}
return exposedEndpoints, true, nil
}
func resolveServiceHostnameForEndpoint(endpoint controllerv1alpha1.Endpoint, services []corev1.Service) (string, error) {
for _, service := range services {
if service.Annotations[constants.DevWorkspaceDiscoverableServiceAnnotation] == "true" {
continue
}
for _, servicePort := range service.Spec.Ports {
if servicePort.Port == int32(endpoint.TargetPort) {
return getHostnameFromService(service, servicePort.Port), nil
}
}
}
return "", fmt.Errorf("could not find service for endpoint %s", endpoint.Name)
}
func getHostnameFromService(service corev1.Service, port int32) string {
scheme := "http"
if _, ok := service.Annotations[serviceServingCertAnnot]; ok {
scheme = "https"
}
return fmt.Sprintf("%s://%s.%s.svc:%d", scheme, service.Name, service.Namespace, port)
}