-
Notifications
You must be signed in to change notification settings - Fork 49
/
vaultengineresourcereconciler.go
129 lines (116 loc) · 4.42 KB
/
vaultengineresourcereconciler.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
/*
Copyright 2021.
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 vaultresourcecontroller
import (
"context"
vaultutils "github.com/redhat-cop/vault-config-operator/api/v1alpha1/utils"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
type VaultEngineResource struct {
vaultEngineEndpoint *vaultutils.VaultEngineEndpoint
reconcilerBase *ReconcilerBase
}
func NewVaultEngineResource(reconcilerBase *ReconcilerBase, obj client.Object) *VaultEngineResource {
return &VaultEngineResource{
reconcilerBase: reconcilerBase,
vaultEngineEndpoint: vaultutils.NewVaultEngineEndpoint(obj),
}
}
func (r *VaultEngineResource) manageCleanUpLogic(context context.Context, instance client.Object) error {
log := log.FromContext(context)
// we delete this only if it has actually been created. We assume that if there was a successful reconcyle cycle the resource was created in Vault
if conditionAware, ok := instance.(vaultutils.ConditionsAware); ok {
for _, condition := range conditionAware.GetConditions() {
if condition.Status == metav1.ConditionTrue && condition.Type == ReconcileSuccessful {
err := r.vaultEngineEndpoint.DeleteIfExists(context)
if err != nil {
log.Error(err, "unable to delete vault resource", "instance", instance)
return err
}
}
}
}
return nil
}
func (r *VaultEngineResource) Reconcile(ctx context.Context, instance client.Object) (ctrl.Result, error) {
log := log.FromContext(ctx)
log.Info("starting reconcile cycle")
log.V(1).Info("reconcile", "instance", instance)
if !instance.GetDeletionTimestamp().IsZero() {
if !controllerutil.ContainsFinalizer(instance, vaultutils.GetFinalizer(instance)) {
return reconcile.Result{}, nil
}
err := r.manageCleanUpLogic(ctx, instance)
if err != nil {
log.Error(err, "unable to delete instance", "instance", instance)
return ManageOutcome(ctx, *r.reconcilerBase, instance, err)
}
controllerutil.RemoveFinalizer(instance, vaultutils.GetFinalizer(instance))
err = r.reconcilerBase.GetClient().Update(ctx, instance)
if err != nil {
log.Error(err, "unable to update instance", "instance", instance)
return ManageOutcome(ctx, *r.reconcilerBase, instance, err)
}
return reconcile.Result{}, nil
}
err := r.manageReconcileLogic(ctx, instance)
if err != nil {
log.Error(err, "unable to complete reconcile logic", "instance", instance)
return ManageOutcome(ctx, *r.reconcilerBase, instance, err)
}
return ManageOutcome(ctx, *r.reconcilerBase, instance, err)
}
func (r *VaultEngineResource) manageReconcileLogic(context context.Context, instance client.Object) error {
log := log.FromContext(context)
// prepare internal values
err := instance.(vaultutils.VaultObject).PrepareInternalValues(context, instance)
if err != nil {
log.Error(err, "unable to prepare internal values", "instance", instance)
return err
}
err = instance.(vaultutils.VaultObject).PrepareTLSConfig(context, instance)
if err != nil {
log.Error(err, "unable to prepare TLS Config values", "instance", instance)
return err
}
found, err := r.vaultEngineEndpoint.Exists(context)
if err != nil {
log.Error(err, "unable to check if exists", "instance", instance)
return err
}
if !found {
err = r.vaultEngineEndpoint.Create(context)
if err != nil {
log.Error(err, "unable to create", "instance", instance)
return err
}
} else {
err := r.vaultEngineEndpoint.CreateOrUpdateTuneConfig(context)
if err != nil {
log.Error(err, "unable to create or update tune config", "instance", instance)
return err
}
}
accessor, err := r.vaultEngineEndpoint.GetAccessor(context)
if err != nil {
log.Error(err, "unable to get accessor", "instance", instance)
return err
}
instance.(vaultutils.VaultEngineObject).SetAccessor(accessor)
return nil
}