/
values_controller.go
144 lines (115 loc) · 4.73 KB
/
values_controller.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
/*
Copyright 2021.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package helm
import (
"context"
"strings"
"time"
"github.com/go-logr/logr"
helmv1alpha1 "github.com/soer3n/yaho/apis/yaho/v1alpha1"
"k8s.io/apimachinery/pkg/api/errors"
meta "k8s.io/apimachinery/pkg/api/meta"
metav1 "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/predicate"
)
// ValuesReconciler reconciles a Values object
type ValuesReconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
Recorder record.EventRecorder
}
// +kubebuilder:rbac:groups=yaho.soer3n.dev,resources=values,verbs=get;list;watch
// +kubebuilder:rbac:groups=yaho.soer3n.dev,resources=releases,verbs=get;list;watch;update;patch
// +kubebuilder:rbac:groups=yaho.soer3n.dev,resources=releases/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=yaho.soer3n.dev,resources=values/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=yaho.soer3n.dev,resources=values/finalizers,verbs=update
// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
// TODO(user): Modify the Reconcile function to compare the state specified by
// the Chart object against the actual cluster state, and then
// perform operations to make the cluster state reflect the state specified by
// the user.
//
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.7.0/pkg/reconcile
func (r *ValuesReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
reqLogger := r.Log.WithValues("values", req.NamespacedName)
_ = r.Log.WithValues("valuesreq", req)
// fetch app instance
instance := &helmv1alpha1.Values{}
reqLogger.Info("start reconcile loop")
reqLogger.Info("meta", "value", instance.ObjectMeta)
err := r.Get(ctx, req.NamespacedName, instance)
if err != nil {
if errors.IsNotFound(err) {
// Request object not found, could have been deleted after reconcile request.
// Owned objects are automatically garbage collected. For additional cleanup logic use finalizers.
// Return and don't requeue
reqLogger.Info("HelmValues resource not found. Ignoring since object must be deleted")
return ctrl.Result{}, nil
}
// Error reading the object - requeue the request.
reqLogger.Error(err, "Failed to get HelmValues")
return ctrl.Result{}, err
}
reqLogger.Info("spec", "value", instance.Spec)
annotations := instance.GetAnnotations()
if _, ok := annotations["releases"]; ok {
releaseList := strings.Split(annotations["releases"], ",")
for _, release := range releaseList {
current := &helmv1alpha1.Release{}
err := r.Client.Get(ctx, client.ObjectKey{
Namespace: instance.ObjectMeta.Namespace,
Name: release,
}, current)
if err == nil {
if meta.IsStatusConditionTrue(current.Status.Conditions, "synced") {
condition := metav1.Condition{Type: "synced", Status: metav1.ConditionFalse, LastTransitionTime: metav1.Time{Time: time.Now()}, Reason: "valueschange", Message: "valuesupdated"}
meta.SetStatusCondition(¤t.Status.Conditions, condition)
synced := false
current.Status.Synced = &synced
err = r.Status().Update(ctx, current)
reqLogger.Info("Update release resource status.")
if err != nil {
reqLogger.Info(err.Error())
}
if current.ObjectMeta.Labels == nil {
current.ObjectMeta.Labels = make(map[string]string)
}
current.ObjectMeta.Labels["yaho.soer3n.dev/reconcile"] = "true"
err := r.Update(ctx, current)
reqLogger.Info("Trigger release sync.")
if err != nil {
reqLogger.Info(err.Error())
return ctrl.Result{}, err
}
}
}
}
}
return ctrl.Result{}, nil
}
// SetupWithManager sets up the controller with the Manager.
func (r *ValuesReconciler) SetupWithManager(mgr ctrl.Manager) error {
pred := predicate.GenerationChangedPredicate{}
return ctrl.NewControllerManagedBy(mgr).
For(&helmv1alpha1.Values{}).
WithEventFilter(pred).
Complete(r)
}