-
Notifications
You must be signed in to change notification settings - Fork 25
/
configmaps.go
122 lines (103 loc) · 4.35 KB
/
configmaps.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
/*
Copyright 2022.
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 controller
import (
"bytes"
"context"
_ "embed"
"fmt"
"github.com/fluxninja/aperture/v2/operator/controllers"
"github.com/clarketm/json"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/yaml"
controllerv1alpha1 "github.com/fluxninja/aperture/v2/operator/api/controller/v1alpha1"
)
// configMapForAgentConfig prepares the ConfigMap object for the Controller.
func configMapForControllerConfig(instance *controllerv1alpha1.Controller, scheme *runtime.Scheme) (*corev1.ConfigMap, error) {
jsonConfig, err := json.Marshal(instance.Spec.ConfigSpec)
if err != nil {
return nil, fmt.Errorf("failed to marshal Controller config to JSON. Error: '%s'", err.Error())
}
config, err := yaml.JSONToYAML(jsonConfig)
if err != nil {
return nil, fmt.Errorf("failed to marshal Controller config to YAML. Error: '%s'", err.Error())
}
cm := &corev1.ConfigMap{
ObjectMeta: v1.ObjectMeta{
Name: controllers.ControllerResourcesName(instance),
Namespace: instance.GetNamespace(),
Labels: controllers.CommonLabels(instance.Spec.Labels, instance.GetName(), controllers.ControllerServiceName),
Annotations: instance.Spec.Annotations,
},
Data: map[string]string{
"aperture-controller.yaml": string(config),
},
}
if err := ctrl.SetControllerReference(instance, cm, scheme); err != nil {
return nil, err
}
return cm, nil
}
// configMapForControllerClientCert prepares the ConfigMap object for the Aperture Controller client Certificate.
func configMapForControllerClientCert(instance *controllerv1alpha1.Controller, scheme *runtime.Scheme, clientCert *bytes.Buffer) (*corev1.ConfigMap, error) {
cm := &corev1.ConfigMap{
ObjectMeta: v1.ObjectMeta{
Name: fmt.Sprintf("%s-controller-client-cert", instance.GetName()),
Namespace: instance.GetNamespace(),
Labels: controllers.CommonLabels(instance.Spec.Labels, instance.GetName(), controllers.AppName),
Annotations: instance.Spec.Annotations,
},
Data: map[string]string{
controllers.ControllerClientCertKey: clientCert.String(),
},
}
return cm, nil
}
// createConfigMap calls the Kubernetes API to create the provided Controller ConfigMap resource.
func createConfigMapForController(
client client.Client, recorder record.EventRecorder, configMap *corev1.ConfigMap, ctx context.Context, instance *controllerv1alpha1.Controller) (
controllerutil.OperationResult, error,
) {
res, err := controllerutil.CreateOrUpdate(ctx, client, configMap, controllers.ConfigMapMutate(configMap, configMap.Data))
if err != nil {
if errors.IsConflict(err) {
return createConfigMapForController(client, recorder, configMap, ctx, instance)
}
msg := fmt.Sprintf("failed to create ConfigMap '%s' for Instance '%s' in Namespace '%s'. Response='%v', Error='%s'",
configMap.GetName(), instance.GetName(), instance.GetNamespace(), res, err.Error())
if recorder != nil {
recorder.Event(instance, corev1.EventTypeNormal, "ConfigMapCreationFailed", msg)
}
return controllerutil.OperationResultNone, fmt.Errorf(msg)
}
if recorder != nil {
switch res {
case controllerutil.OperationResultCreated:
recorder.Eventf(instance, corev1.EventTypeNormal, "ConfigMapCreationSuccessful",
"Created ConfigMap '%s' in Namespace '%s'", configMap.GetName(), configMap.GetNamespace())
case controllerutil.OperationResultUpdated:
recorder.Eventf(instance, corev1.EventTypeNormal, "ConfigMapUpdationSuccessful",
"Updated ConfigMap '%s' in Namespace '%s'", configMap.GetName(), configMap.GetNamespace())
case controllerutil.OperationResultNone:
default:
}
}
return res, nil
}