-
Notifications
You must be signed in to change notification settings - Fork 83
/
ingress.go
201 lines (179 loc) · 5.9 KB
/
ingress.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
//
// Copyright (c) 2019-2023 Red Hat, Inc.
// This program and the accompanying materials are made
// available under the terms of the Eclipse Public License 2.0
// which is available at https://www.eclipse.org/legal/epl-2.0/
//
// SPDX-License-Identifier: EPL-2.0
//
// Contributors:
// Red Hat, Inc. - initial API and implementation
//
package deploy
import (
"reflect"
"sort"
"k8s.io/utils/pointer"
"github.com/eclipse-che/che-operator/pkg/common/chetypes"
"github.com/eclipse-che/che-operator/pkg/common/constants"
"github.com/eclipse-che/che-operator/pkg/common/test"
"github.com/eclipse-che/che-operator/pkg/common/utils"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
networking "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var ingressDiffOpts = cmp.Options{
cmpopts.IgnoreFields(networking.Ingress{}, "TypeMeta", "Status"),
cmpopts.IgnoreFields(networking.HTTPIngressPath{}, "PathType"),
cmp.Comparer(func(x, y metav1.ObjectMeta) bool {
return reflect.DeepEqual(x.Labels, y.Labels) &&
x.Annotations[constants.CheEclipseOrgManagedAnnotationsDigest] == y.Annotations[constants.CheEclipseOrgManagedAnnotationsDigest]
}),
}
var (
DefaultIngressAnnotations = map[string]string{
"kubernetes.io/ingress.class": "nginx",
"nginx.ingress.kubernetes.io/proxy-read-timeout": "3600",
"nginx.ingress.kubernetes.io/proxy-connect-timeout": "3600",
"nginx.ingress.kubernetes.io/ssl-redirect": "true",
}
)
// SyncIngressToCluster creates ingress to expose service with the set settings
// host and path are evaluated if they are empty
func SyncIngressToCluster(
deployContext *chetypes.DeployContext,
name string,
path string,
serviceName string,
servicePort int,
component string) (endpointUrl string, done bool, err error) {
ingressUrl, ingressSpec := GetIngressSpec(deployContext, name, path, serviceName, servicePort, component)
sync, err := Sync(deployContext, ingressSpec, ingressDiffOpts)
return ingressUrl, sync, err
}
// GetIngressSpec returns expected ingress config for given parameters
// host and path are evaluated if they are empty
func GetIngressSpec(
deployContext *chetypes.DeployContext,
name string,
path string,
serviceName string,
servicePort int,
component string) (ingressUrl string, i *networking.Ingress) {
ingressDomain := deployContext.CheCluster.Spec.Networking.Domain
tlsSecretName := deployContext.CheCluster.Spec.Networking.TlsSecretName
labels := GetLabels(component)
for k, v := range deployContext.CheCluster.Spec.Networking.Labels {
labels[k] = v
}
pathType := networking.PathTypeImplementationSpecific
host := deployContext.CheCluster.Spec.Networking.Hostname
if host == "" {
host = ingressDomain
}
var endpointPath, ingressPath string
if path == "" {
endpointPath, ingressPath = evaluatePath(component)
} else {
ingressPath = path
endpointPath = path
}
annotations := map[string]string{}
if len(deployContext.CheCluster.Spec.Networking.Annotations) > 0 {
for k, v := range deployContext.CheCluster.Spec.Networking.Annotations {
annotations[k] = v
}
} else {
for k, v := range DefaultIngressAnnotations {
annotations[k] = v
}
// Set bigger proxy buffer size to prevent 502 auth error.
annotations["nginx.ingress.kubernetes.io/proxy-buffer-size"] = "16k"
annotations["nginx.org/websocket-services"] = serviceName
}
// add 'che.eclipse.org/managed-annotations-digest' annotation
// to store and compare annotations managed by operator only
annotationsKeys := make([]string, 0, len(annotations))
for k := range annotations {
annotationsKeys = append(annotationsKeys, k)
}
if len(annotationsKeys) > 0 {
sort.Strings(annotationsKeys)
data := ""
for _, k := range annotationsKeys {
data += k + ":" + annotations[k] + ","
}
if test.IsTestMode() {
annotations[constants.CheEclipseOrgManagedAnnotationsDigest] = "0000"
} else {
annotations[constants.CheEclipseOrgManagedAnnotationsDigest] = utils.ComputeHash256([]byte(data))
}
}
ingressClassName := deployContext.CheCluster.Spec.Networking.IngressClassName
if ingressClassName == "" {
ingressClassName = annotations["kubernetes.io/ingress.class"]
}
// annotations `kubernetes.io/ingress.class` can not be set when the class field is also set
delete(annotations, "kubernetes.io/ingress.class")
ingress := &networking.Ingress{
TypeMeta: metav1.TypeMeta{
Kind: "Ingress",
APIVersion: networking.SchemeGroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: deployContext.CheCluster.Namespace,
Labels: labels,
Annotations: annotations,
},
Spec: networking.IngressSpec{
IngressClassName: pointer.String(ingressClassName),
Rules: []networking.IngressRule{
{
Host: host,
IngressRuleValue: networking.IngressRuleValue{
HTTP: &networking.HTTPIngressRuleValue{
Paths: []networking.HTTPIngressPath{
{
Backend: networking.IngressBackend{
Service: &networking.IngressServiceBackend{
Name: serviceName,
Port: networking.ServiceBackendPort{
Number: int32(servicePort),
},
},
},
Path: ingressPath,
PathType: &pathType,
},
},
},
},
},
},
},
}
ingress.Spec.TLS = []networking.IngressTLS{
{
Hosts: []string{host},
SecretName: tlsSecretName,
},
}
return host + endpointPath, ingress
}
// evaluatePath evaluates ingress path (one which is used for rule)
// and endpoint path (one which client should use during endpoint accessing)
func evaluatePath(component string) (endpointPath, ingressPath string) {
switch component {
case constants.DevfileRegistryName:
fallthrough
case constants.PluginRegistryName:
endpointPath = "/" + component
ingressPath = endpointPath + "/(.*)"
default:
ingressPath = "/"
endpointPath = "/"
}
return endpointPath, ingressPath
}