forked from devfile/devworkspace-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigmap.go
179 lines (159 loc) · 5.8 KB
/
configmap.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
//
// Copyright (c) 2019-2021 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 restapis
import (
"context"
"encoding/json"
devworkspace "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/devworkspace-operator/apis/controller/v1alpha1"
"github.com/devfile/devworkspace-operator/controllers/workspace/provision"
"github.com/devfile/devworkspace-operator/pkg/common"
"github.com/devfile/devworkspace-operator/pkg/config"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sRuntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
runtimeClient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/yaml"
)
var configmapDiffOpts = cmp.Options{
cmpopts.IgnoreFields(corev1.ConfigMap{}, "TypeMeta", "ObjectMeta"),
}
func SyncRestAPIsConfigMap(workspace *devworkspace.DevWorkspace, components []v1alpha1.ComponentDescription, endpoints map[string]v1alpha1.ExposedEndpointList, clusterAPI provision.ClusterAPI) provision.ProvisioningStatus {
specCM, err := getSpecConfigMap(workspace, components, endpoints, clusterAPI.Scheme)
if err != nil {
return provision.ProvisioningStatus{Err: err}
}
clusterCM, err := getClusterConfigMap(specCM.Name, workspace.Namespace, clusterAPI.Client)
if err != nil {
return provision.ProvisioningStatus{Err: err}
}
if clusterCM == nil {
clusterAPI.Logger.Info("Creating che-rest-apis configmap")
err := clusterAPI.Client.Create(context.TODO(), specCM)
return provision.ProvisioningStatus{Requeue: true, Err: err}
}
if !cmp.Equal(specCM, clusterCM, configmapDiffOpts) {
clusterAPI.Logger.Info("Updating che-rest-apis configmap")
clusterCM.Data = specCM.Data
err := clusterAPI.Client.Update(context.TODO(), clusterCM)
if err != nil && !errors.IsConflict(err) {
return provision.ProvisioningStatus{Err: err}
}
return provision.ProvisioningStatus{Requeue: true}
}
return provision.ProvisioningStatus{Continue: true}
}
func getSpecConfigMap(
workspace *devworkspace.DevWorkspace,
components []v1alpha1.ComponentDescription,
endpoints map[string]v1alpha1.ExposedEndpointList,
scheme *k8sRuntime.Scheme) (*corev1.ConfigMap, error) {
runtimeJSON, err := constructRuntimeAnnotation(components, endpoints)
if err != nil {
return nil, err
}
devfileYAML, err := getDevfileV1Yaml(workspace.Spec.Template)
if err != nil {
return nil, err
}
configmap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: common.CheRestAPIsConfigmapName(workspace.Status.WorkspaceId),
Namespace: workspace.Namespace,
Labels: map[string]string{
config.WorkspaceIDLabel: workspace.Status.WorkspaceId,
},
},
Data: map[string]string{
config.RestAPIsDevfileYamlFilename: devfileYAML,
config.RestAPIsRuntimeJSONFilename: runtimeJSON,
},
}
err = controllerutil.SetControllerReference(workspace, configmap, scheme)
return configmap, err
}
func getClusterConfigMap(name, namespace string, client runtimeClient.Client) (*corev1.ConfigMap, error) {
cm := &corev1.ConfigMap{}
namespacedName := types.NamespacedName{
Name: name,
Namespace: namespace,
}
err := client.Get(context.TODO(), namespacedName, cm)
if err != nil {
if errors.IsNotFound(err) {
return nil, nil
}
return nil, err
}
return cm, err
}
func getDevfileV1Yaml(template devworkspace.DevWorkspaceTemplateSpec) (string, error) {
devfile, err := devworkspaceTemplateToDevfileV1(&template)
if err != nil {
return "", err
}
devfileYaml, err := yaml.Marshal(devfile)
if err != nil {
return "", err
}
return string(devfileYaml), err
}
func constructRuntimeAnnotation(components []v1alpha1.ComponentDescription, endpoints map[string]v1alpha1.ExposedEndpointList) (string, error) {
defaultEnv := "default"
machines := getMachinesAnnotation(components, endpoints)
commands := getWorkspaceCommands(components)
runtime := v1alpha1.CheWorkspaceRuntime{
ActiveEnv: defaultEnv,
Commands: commands,
Machines: machines,
}
runtimeJSON, err := json.Marshal(runtime)
if err != nil {
return "", err
}
return string(runtimeJSON), nil
}
func getMachinesAnnotation(components []v1alpha1.ComponentDescription, endpoints map[string]v1alpha1.ExposedEndpointList) map[string]v1alpha1.CheWorkspaceMachine {
machines := map[string]v1alpha1.CheWorkspaceMachine{}
machineRunningString := v1alpha1.RunningMachineEventType
for _, component := range components {
for containerName, container := range component.ComponentMetadata.Containers {
servers := map[string]v1alpha1.CheWorkspaceServer{}
// TODO: This is likely not a good choice for matching, since it'll fail if container name does not match an endpoint key
for _, endpoint := range endpoints[containerName] {
servers[endpoint.Name] = v1alpha1.CheWorkspaceServer{
Attributes: endpoint.Attributes,
Status: v1alpha1.RunningServerStatus, // TODO: This is just set so the circles are green -- should check readiness
URL: endpoint.Url,
}
}
machines[containerName] = v1alpha1.CheWorkspaceMachine{
Attributes: container.Attributes,
Servers: servers,
Status: &machineRunningString,
}
}
}
return machines
}
func getWorkspaceCommands(components []v1alpha1.ComponentDescription) []v1alpha1.CheWorkspaceCommand {
var commands []v1alpha1.CheWorkspaceCommand
for _, component := range components {
commands = append(commands, component.ComponentMetadata.ContributedRuntimeCommands...)
}
return commands
}