-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathserver_configmap.go
More file actions
288 lines (244 loc) · 9.06 KB
/
server_configmap.go
File metadata and controls
288 lines (244 loc) · 9.06 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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
//
// Copyright (c) 2019-2026 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 server
import (
"context"
"encoding/json"
"fmt"
"maps"
"slices"
"sort"
"strconv"
"strings"
"github.com/devfile/devworkspace-operator/pkg/infrastructure"
"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/diffs"
k8sclient "github.com/eclipse-che/che-operator/pkg/common/k8s-client"
defaults "github.com/eclipse-che/che-operator/pkg/common/operator-defaults"
"github.com/eclipse-che/che-operator/pkg/common/utils"
"github.com/eclipse-che/che-operator/pkg/deploy"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/pointer"
)
type CheConfigMap struct {
JavaOpts string `json:"JAVA_OPTS"`
CheHost string `json:"CHE_HOST"`
ChePort string `json:"CHE_PORT"`
CheDebugServer string `json:"CHE_DEBUG_SERVER"`
CheLogLevel string `json:"CHE_LOG_LEVEL"`
CheMetricsEnabled string `json:"CHE_METRICS_ENABLED"`
CheInfrastructure string `json:"CHE_INFRASTRUCTURE_ACTIVE"`
UserClusterRoles string `json:"CHE_INFRA_KUBERNETES_USER__CLUSTER__ROLES"`
NamespaceDefault string `json:"CHE_INFRA_KUBERNETES_NAMESPACE_DEFAULT"`
NamespaceCreationAllowed string `json:"CHE_INFRA_KUBERNETES_NAMESPACE_CREATION__ALLOWED"`
Http2Disable string `json:"HTTP2_DISABLE"`
KubernetesLabels string `json:"KUBERNETES_LABELS"`
OpenShiftCreateNamespaceDirectly string `json:"CHE_INFRA_OPENSHIFT__DIRECT_NAMESPACE_CREATION"`
// TODO remove when keycloak codebase is removed from che-server component
CheOIDCAuthServerUrl string `json:"CHE_OIDC_AUTH__SERVER__URL,omitempty"`
}
func (s *CheServerReconciler) syncConfigMap(ctx *chetypes.DeployContext) (bool, error) {
data, err := s.getConfigMapData(ctx)
if err != nil {
return false, err
}
cm := &corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Kind: "ConfigMap",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: configMapName,
Namespace: ctx.CheCluster.Namespace,
Labels: deploy.GetLabels(getComponentName()),
},
Data: data,
}
err = ctx.ClusterAPI.ClientWrapper.Sync(
context.TODO(),
cm,
&k8sclient.SyncOptions{DiffOpts: diffs.ConfigMapAllLabels},
)
return err == nil, err
}
func (s *CheServerReconciler) getConfigMapRevision(ctx *chetypes.DeployContext) (string, error) {
cm := &corev1.ConfigMap{}
if exist, err := ctx.ClusterAPI.ClientWrapper.GetIgnoreNotFound(
context.TODO(),
types.NamespacedName{
Namespace: ctx.CheCluster.Namespace,
Name: configMapName,
},
cm,
); !exist {
return "", err
}
return cm.ResourceVersion, nil
}
func (s *CheServerReconciler) getConfigMapData(ctx *chetypes.DeployContext) (cheEnv map[string]string, err error) {
var cheInfrastructure string
if infrastructure.IsOpenShift() {
cheInfrastructure = "openshift"
} else {
cheInfrastructure = "kubernetes"
}
javaOpts := constants.DefaultJavaOpts
if ctx.Proxy.HttpProxy != "" {
javaOpts += deploy.GenerateProxyJavaOpts(ctx.Proxy, ctx.Proxy.NoProxy)
}
cheLogLevel := utils.GetValue(
ctx.CheCluster.Spec.Components.CheServer.LogLevel,
constants.DefaultServerLogLevel,
)
cheDebugServer := strconv.FormatBool(
pointer.BoolDeref(
ctx.CheCluster.Spec.Components.CheServer.Debug,
constants.DefaultServerDebug,
))
chePort := strconv.Itoa(int(constants.DefaultServerPort))
cheMetricsEnabled := strconv.FormatBool(ctx.CheCluster.Spec.Components.Metrics.Enable)
namespaceDefault := ctx.CheCluster.GetDefaultNamespace()
namespaceCreationAllowed := strconv.FormatBool(
pointer.BoolDeref(
ctx.CheCluster.Spec.DevEnvironments.DefaultNamespace.AutoProvision,
constants.DefaultAutoProvision,
))
kubernetesLabels := labels.FormatLabels(deploy.GetLabels(defaults.GetCheFlavor()))
openShiftCreateNamespaceDirectly := strconv.FormatBool(
pointer.BoolDeref(
ctx.CheCluster.Spec.DevEnvironments.DefaultNamespace.CreateNamespaceDirectly,
constants.OpenShiftCreateNamespaceDirectly,
),
)
data := &CheConfigMap{
JavaOpts: javaOpts,
CheHost: ctx.CheHost,
ChePort: chePort,
CheDebugServer: cheDebugServer,
CheLogLevel: cheLogLevel,
CheMetricsEnabled: cheMetricsEnabled,
CheInfrastructure: cheInfrastructure,
CheOIDCAuthServerUrl: ctx.CheCluster.Spec.Networking.Auth.IdentityProviderURL,
NamespaceDefault: namespaceDefault,
NamespaceCreationAllowed: namespaceCreationAllowed,
KubernetesLabels: kubernetesLabels,
OpenShiftCreateNamespaceDirectly: openShiftCreateNamespaceDirectly,
// Disable HTTP2 protocol.
// Fix issue with creating config maps on the cluster https://issues.redhat.com/browse/CRW-2677
// The root cause is in the HTTP2 protocol support of the okttp3 library that is used by fabric8.kubernetes-client that is used by che-server
// In the past, when che-server used Java 8, HTTP1 protocol was used. Now che-sever uses Java 11
Http2Disable: strconv.FormatBool(true),
}
out, err := json.Marshal(data)
if err != nil {
return nil, err
}
err = json.Unmarshal(out, &cheEnv)
if err != nil {
return nil, err
}
// override envs by extra properties
maps.Copy(cheEnv, ctx.CheCluster.Spec.Components.CheServer.ExtraProperties)
// Updates `CHE_INFRA_KUBERNETES_ADVANCED__AUTHORIZATION__<...>`
s.updateAdvancedAuthorizationEnv(ctx, cheEnv)
// Updates `CHE_INFRA_KUBERNETES_USER__CLUSTER__ROLES`
s.updateUserClusterRoleEnv(ctx, cheEnv)
// Update `CHE_INTEGRATION_<...>_SERVER__ENDPOINTS`
if err := s.updateServerEndpointsEnv(ctx, cheEnv); err != nil {
return nil, err
}
return cheEnv, nil
}
func (s *CheServerReconciler) updateAdvancedAuthorizationEnv(ctx *chetypes.DeployContext, cheEnv map[string]string) {
if ctx.CheCluster.Spec.Networking.Auth.AdvancedAuthorization != nil {
cheEnv["CHE_INFRA_KUBERNETES_ADVANCED__AUTHORIZATION_ALLOW__USERS"] = strings.Join(
ctx.CheCluster.Spec.Networking.Auth.AdvancedAuthorization.AllowUsers,
",",
)
cheEnv["CHE_INFRA_KUBERNETES_ADVANCED__AUTHORIZATION_DENY__USERS"] = strings.Join(
ctx.CheCluster.Spec.Networking.Auth.AdvancedAuthorization.DenyUsers,
",",
)
cheEnv["CHE_INFRA_KUBERNETES_ADVANCED__AUTHORIZATION_ALLOW__GROUPS"] = strings.Join(
ctx.CheCluster.Spec.Networking.Auth.AdvancedAuthorization.AllowGroups,
",",
)
cheEnv["CHE_INFRA_KUBERNETES_ADVANCED__AUTHORIZATION_DENY__GROUPS"] = strings.Join(
ctx.CheCluster.Spec.Networking.Auth.AdvancedAuthorization.DenyGroups,
",",
)
}
}
func (s *CheServerReconciler) updateUserClusterRoleEnv(ctx *chetypes.DeployContext, cheEnv map[string]string) {
userClusterRolesSet := map[string]bool{}
for _, role := range s.getDefaultUserClusterRoles(ctx) {
userClusterRolesSet[role] = true
}
for _, role := range strings.Split(cheEnv["CHE_INFRA_KUBERNETES_USER__CLUSTER__ROLES"], ",") {
role = strings.TrimSpace(role)
if role != "" {
userClusterRolesSet[role] = true
}
}
if ctx.CheCluster.Spec.DevEnvironments.User != nil {
for _, role := range ctx.CheCluster.Spec.DevEnvironments.User.ClusterRoles {
role = strings.TrimSpace(role)
if role != "" {
userClusterRolesSet[role] = true
}
}
}
userClusterRoles := slices.Collect(maps.Keys(userClusterRolesSet))
sort.Strings(userClusterRoles)
cheEnv["CHE_INFRA_KUBERNETES_USER__CLUSTER__ROLES"] = strings.Join(userClusterRoles, ",")
}
func (s *CheServerReconciler) updateServerEndpointsEnv(ctx *chetypes.DeployContext, cheEnv map[string]string) error {
// https://github.com/eclipse-che/che-operator/pull/1250
oAuthProviders := []string{constants.BitbucketOAuth, constants.AzureDevOpsOAuth}
for _, oauthProvider := range oAuthProviders {
secret, err := getOAuthConfigSecret(ctx, oauthProvider)
if err != nil {
return err
} else if secret == nil {
continue
}
envName := fmt.Sprintf(
"CHE_INTEGRATION_%s_SERVER__ENDPOINTS",
strings.ReplaceAll(
strings.ToUpper(oauthProvider),
"-",
"_",
),
)
// make endpoints uniq and sorted
endpointsSet := map[string]bool{}
for _, endpointsStr := range []string{
secret.Annotations[constants.CheEclipseOrgScmServerEndpoint],
cheEnv[envName],
} {
for _, endpoint := range strings.Split(endpointsStr, ",") {
endpoint = strings.TrimSpace(endpoint)
if endpoint != "" {
endpointsSet[endpoint] = true
}
}
}
endpoints := slices.Collect(maps.Keys(endpointsSet))
sort.Strings(endpoints)
cheEnv[envName] = strings.Join(endpoints, ",")
}
return nil
}