@@ -2,16 +2,13 @@ package controller
2
2
3
3
import (
4
4
"context"
5
- "fmt"
6
- "sync"
7
5
"time"
8
6
9
7
"github.com/sirupsen/logrus"
10
8
11
9
apierrors "k8s.io/apimachinery/pkg/api/errors"
12
10
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
13
11
"k8s.io/apimachinery/pkg/runtime/schema"
14
- "k8s.io/apimachinery/pkg/types"
15
12
"sigs.k8s.io/controller-runtime/pkg/client"
16
13
"sigs.k8s.io/controller-runtime/pkg/reconcile"
17
14
@@ -23,40 +20,40 @@ type helmOperatorReconciler struct {
23
20
GVK schema.GroupVersionKind
24
21
Installer helm.Installer
25
22
ResyncPeriod time.Duration
26
-
27
- lastResourceVersions map [types.NamespacedName ]string
28
- mutex sync.RWMutex
29
23
}
30
24
31
25
const (
32
26
finalizer = "uninstall-helm-release"
33
27
)
34
28
35
29
func (r * helmOperatorReconciler ) Reconcile (request reconcile.Request ) (reconcile.Result , error ) {
36
- logrus .Infof ("processing %s" , request .NamespacedName )
37
-
38
30
o := & unstructured.Unstructured {}
39
31
o .SetGroupVersionKind (r .GVK )
32
+ o .SetNamespace (request .Namespace )
33
+ o .SetName (request .Name )
34
+ logrus .Debugf ("Processing %s" , helm .ResourceString (o ))
35
+
40
36
err := r .Client .Get (context .TODO (), request .NamespacedName , o )
41
37
if apierrors .IsNotFound (err ) {
42
38
return reconcile.Result {}, nil
43
39
}
44
40
if err != nil {
41
+ logrus .Errorf ("Failed to lookup %s: %s" , helm .ResourceString (o ), err )
45
42
return reconcile.Result {}, err
46
43
}
47
44
48
45
deleted := o .GetDeletionTimestamp () != nil
49
46
pendingFinalizers := o .GetFinalizers ()
50
47
if ! deleted && ! contains (pendingFinalizers , finalizer ) {
51
- logrus .Debugf ("adding finalizer %s to resource " , finalizer )
48
+ logrus .Debugf ("Adding finalizer \" %s \" to %s " , finalizer , helm . ResourceString ( o ) )
52
49
finalizers := append (pendingFinalizers , finalizer )
53
50
o .SetFinalizers (finalizers )
54
51
err := r .Client .Update (context .TODO (), o )
55
52
return reconcile.Result {}, err
56
53
}
57
54
if deleted {
58
55
if ! contains (pendingFinalizers , finalizer ) {
59
- logrus .Info ( "resouce is terminated, skipping reconciliation" )
56
+ logrus .Infof ( "Resource %s is terminated, skipping reconciliation", helm . ResourceString ( o ) )
60
57
return reconcile.Result {}, nil
61
58
}
62
59
@@ -76,24 +73,19 @@ func (r *helmOperatorReconciler) Reconcile(request reconcile.Request) (reconcile
76
73
return reconcile.Result {}, err
77
74
}
78
75
79
- lastResourceVersion , ok := r .getLastResourceVersion (request .NamespacedName )
80
- if ok && o .GetResourceVersion () == lastResourceVersion {
81
- logrus .Infof ("skipping %s because resource version has not changed" , request .NamespacedName )
82
- return reconcile.Result {RequeueAfter : r .ResyncPeriod }, nil
83
- }
84
-
85
- updatedResource , err := r .Installer .InstallRelease (o )
76
+ updatedResource , needsUpdate , err := r .Installer .ReconcileRelease (o )
86
77
if err != nil {
87
- logrus .Errorf (err . Error () )
78
+ logrus .Errorf ("Failed to reconcile release for %s: %s" , helm . ResourceString ( o ), err )
88
79
return reconcile.Result {}, err
89
80
}
90
81
91
- err = r .Client .Update (context .TODO (), updatedResource )
92
- if err != nil {
93
- logrus .Errorf (err .Error ())
94
- return reconcile.Result {}, fmt .Errorf ("failed to update custom resource status: %v" , err )
82
+ if needsUpdate {
83
+ err = r .Client .Update (context .TODO (), updatedResource )
84
+ if err != nil {
85
+ logrus .Errorf ("Failed to update resource status for %s: %s" , helm .ResourceString (o ), err )
86
+ return reconcile.Result {}, err
87
+ }
95
88
}
96
- r .setLastResourceVersion (request .NamespacedName , o .GetResourceVersion ())
97
89
98
90
return reconcile.Result {RequeueAfter : r .ResyncPeriod }, nil
99
91
}
@@ -106,16 +98,3 @@ func contains(l []string, s string) bool {
106
98
}
107
99
return false
108
100
}
109
-
110
- func (r * helmOperatorReconciler ) getLastResourceVersion (n types.NamespacedName ) (string , bool ) {
111
- r .mutex .RLock ()
112
- defer r .mutex .RUnlock ()
113
- v , ok := r .lastResourceVersions [n ]
114
- return v , ok
115
- }
116
-
117
- func (r * helmOperatorReconciler ) setLastResourceVersion (n types.NamespacedName , v string ) {
118
- r .mutex .Lock ()
119
- defer r .mutex .Unlock ()
120
- r .lastResourceVersions [n ] = v
121
- }
0 commit comments