-
Notifications
You must be signed in to change notification settings - Fork 24
/
secrets.go
111 lines (94 loc) · 4.05 KB
/
secrets.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
/*
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"
"fmt"
"github.com/fluxninja/aperture/v2/operator/controllers"
controllerv1alpha1 "github.com/fluxninja/aperture/v2/operator/api/controller/v1alpha1"
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"
)
// secretForControllerAPIKey prepares the Secret object for the ApiKey of Controller.
func secretForControllerAPIKey(instance *controllerv1alpha1.Controller, scheme *runtime.Scheme) (*corev1.Secret, error) {
spec := &instance.Spec.Secrets.FluxNinjaExtension
secret := &corev1.Secret{
ObjectMeta: v1.ObjectMeta{
Name: controllers.SecretName(instance.GetName(), "controller", spec),
Namespace: instance.GetNamespace(),
Labels: controllers.CommonLabels(instance.Spec.Labels, instance.GetName(), controllers.ControllerServiceName),
Annotations: instance.Spec.Annotations,
},
Data: map[string][]byte{
controllers.SecretDataKey(&spec.SecretKeyRef): []byte(spec.Value),
},
}
if err := ctrl.SetControllerReference(instance, secret, scheme); err != nil {
return nil, err
}
return secret, nil
}
// secretForControllerCert prepares the Secret object for the Aperture Controller Certificate.
func secretForControllerCert(instance *controllerv1alpha1.Controller, scheme *runtime.Scheme, serverCert, serverKey *bytes.Buffer) (*corev1.Secret, error) {
secret := &corev1.Secret{
ObjectMeta: v1.ObjectMeta{
Name: fmt.Sprintf("%s-controller-cert", instance.GetName()),
Namespace: instance.GetNamespace(),
Labels: controllers.CommonLabels(instance.Spec.Labels, instance.GetName(), controllers.AppName),
Annotations: instance.Spec.Annotations,
},
Data: map[string][]byte{
controllers.ControllerCertName: serverCert.Bytes(),
controllers.ControllerCertKeyName: serverKey.Bytes(),
},
}
return secret, nil
}
// createSecret calls the Kubernetes API to create the provided Controller Secret resource.
func createSecretForController(
client client.Client, recorder record.EventRecorder, secret *corev1.Secret, ctx context.Context, instance *controllerv1alpha1.Controller) (
controllerutil.OperationResult, error,
) {
res, err := controllerutil.CreateOrUpdate(ctx, client, secret, controllers.SecretMutate(secret, secret.Data, secret.OwnerReferences))
if err != nil {
if errors.IsConflict(err) {
return createSecretForController(client, recorder, secret, ctx, instance)
}
msg := fmt.Sprintf("failed to create Secret '%s' for Instance '%s' in Namespace '%s'. Response='%v', Error='%s'",
secret.GetName(), instance.GetName(), instance.GetNamespace(), res, err.Error())
if recorder != nil {
recorder.Event(instance, corev1.EventTypeNormal, "SecretCreationFailed", msg)
}
return controllerutil.OperationResultNone, fmt.Errorf(msg)
}
if recorder != nil {
switch res {
case controllerutil.OperationResultCreated:
recorder.Eventf(instance, corev1.EventTypeNormal, "SecretCreationSuccessful",
"Created Secret '%s' in Namespace '%s'", secret.GetName(), secret.GetNamespace())
case controllerutil.OperationResultUpdated:
recorder.Eventf(instance, corev1.EventTypeNormal, "SecretUpdationSuccessful",
"Updated Secret '%s' in Namespace '%s'", secret.GetName(), secret.GetNamespace())
case controllerutil.OperationResultNone:
default:
}
}
return res, nil
}