/
configmap.go
103 lines (92 loc) · 3.24 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
//
// Copyright (c) 2020 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 (
"context"
"fmt"
"github.com/eclipse/che-operator/pkg/util"
"github.com/google/go-cmp/cmp"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
runtimeClient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
// SyncConfigMapToCluster makes sure that given config map spec is actual.
// It compares config map data and labels.
// If returned config map is nil then it means that the config map update is in progress and reconcile loop probably should be restarted.
func SyncConfigMapToCluster(deployContext *DeployContext, specConfigMap *corev1.ConfigMap) (*corev1.ConfigMap, error) {
clusterConfigMap, err := GetClusterConfigMap(specConfigMap.Name, specConfigMap.Namespace, deployContext.ClusterAPI.Client)
if err != nil {
return nil, err
}
if clusterConfigMap == nil {
logrus.Infof("Creating a new object: %s, name %s", specConfigMap.Kind, specConfigMap.Name)
err := deployContext.ClusterAPI.Client.Create(context.TODO(), specConfigMap)
return nil, err
}
dataDiff := cmp.Diff(clusterConfigMap.Data, specConfigMap.Data)
labelsDiff := cmp.Diff(clusterConfigMap.ObjectMeta.Labels, specConfigMap.ObjectMeta.Labels)
if len(dataDiff) > 0 || len(labelsDiff) > 0 {
logrus.Infof("Updating existing object: %s, name: %s", specConfigMap.Kind, specConfigMap.Name)
fmt.Printf("Difference:\n%s\n%s", dataDiff, labelsDiff)
clusterConfigMap.Data = specConfigMap.Data
clusterConfigMap.ObjectMeta.Labels = specConfigMap.ObjectMeta.Labels
err := deployContext.ClusterAPI.Client.Update(context.TODO(), clusterConfigMap)
return nil, err
}
return clusterConfigMap, nil
}
// GetSpecConfigMap returns config map spec template
func GetSpecConfigMap(
deployContext *DeployContext,
name string,
data map[string]string,
component string) (*corev1.ConfigMap, error) {
configMap := &corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Kind: "ConfigMap",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: deployContext.CheCluster.Namespace,
Labels: GetLabels(deployContext.CheCluster, component),
},
Data: data,
}
if !util.IsTestMode() {
err := controllerutil.SetControllerReference(deployContext.CheCluster, configMap, deployContext.ClusterAPI.Scheme)
if err != nil {
return nil, err
}
}
return configMap, nil
}
// GetClusterConfigMap reads config map from cluster
func GetClusterConfigMap(name string, namespace string, client runtimeClient.Client) (*corev1.ConfigMap, error) {
configMap := &corev1.ConfigMap{}
namespacedName := types.NamespacedName{
Namespace: namespace,
Name: name,
}
err := client.Get(context.TODO(), namespacedName, configMap)
if err != nil {
if errors.IsNotFound(err) {
return nil, nil
}
return nil, err
}
return configMap, nil
}