@@ -25,10 +25,16 @@ import (
25
25
ctrl "sigs.k8s.io/controller-runtime"
26
26
"sigs.k8s.io/controller-runtime/pkg/client"
27
27
28
+ finalizerUtil "github.com/stakater/operator-utils/util/finalizer"
29
+ reconcilerUtil "github.com/stakater/operator-utils/util/reconciler"
28
30
slackv1alpha1 "github.com/stakater/slack-operator/api/v1alpha1"
29
31
slack "github.com/stakater/slack-operator/pkg/slack"
30
32
)
31
33
34
+ var (
35
+ channelFinalizer string = "slack.stakater.com/channel"
36
+ )
37
+
32
38
// ChannelReconciler reconciles a Channel object
33
39
type ChannelReconciler struct {
34
40
client.Client
@@ -53,13 +59,34 @@ func (r *ChannelReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
53
59
// Request object not found, could have been deleted after reconcile request.
54
60
// Owned objects are automatically garbage collected. For additional cleanup logic use finalizers.
55
61
// Return and don't requeue
56
- log .Info ("Channel resource not found. Deleting channel" )
57
62
return ctrl.Result {}, nil
58
63
}
59
64
// Error reading channel, requeue
60
65
return ctrl.Result {}, err
61
66
}
62
67
68
+ isChannelMarkedToBeDeleted := channel .GetDeletionTimestamp () != nil
69
+ if isChannelMarkedToBeDeleted {
70
+ log .Info ("Deletion timestamp found for channel " + req .Name )
71
+ if finalizerUtil .HasFinalizer (channel , channelFinalizer ) {
72
+ return r .finalizeChannel (req , channel )
73
+ }
74
+ // Finalizer doesn't exist so clean up is already done
75
+ return reconcilerUtil .DoNotRequeue ()
76
+ }
77
+
78
+ // Add finalizer if it doesn't exist
79
+ if ! finalizerUtil .HasFinalizer (channel , channelFinalizer ) {
80
+ log .Info ("Adding finalizer for channel " + req .Name )
81
+
82
+ finalizerUtil .AddFinalizer (channel , channelFinalizer )
83
+
84
+ err := r .Client .Update (ctx , channel )
85
+ if err != nil {
86
+ return reconcilerUtil .ManageError (r .Client , channel , err , true )
87
+ }
88
+ }
89
+
63
90
if channel .Status .ID == "" {
64
91
name := channel .Spec .Name
65
92
isPrivate := channel .Spec .Private
@@ -69,10 +96,9 @@ func (r *ChannelReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
69
96
channelID , err := r .SlackService .CreateChannel (name , isPrivate )
70
97
71
98
if err != nil {
72
- // Set error state and don't requeue
73
- channel .Status .Error = err .Error ()
74
- return ctrl.Result {}, nil
99
+ return reconcilerUtil .ManageError (r .Client , channel , err , false )
75
100
}
101
+
76
102
channel .Status .ID = * channelID
77
103
78
104
err = r .Status ().Update (ctx , channel )
@@ -86,7 +112,6 @@ func (r *ChannelReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
86
112
return r .updateSlackChannel (ctx , channel )
87
113
}
88
114
89
- // TODO: too verbose code for error checking
90
115
func (r * ChannelReconciler ) updateSlackChannel (ctx context.Context , channel * slackv1alpha1.Channel ) (ctrl.Result , error ) {
91
116
channelID := channel .Status .ID
92
117
log := r .Log .WithValues ("channelID" , channelID )
@@ -101,58 +126,55 @@ func (r *ChannelReconciler) updateSlackChannel(ctx context.Context, channel *sla
101
126
_ , err := r .SlackService .RenameChannel (channelID , name )
102
127
if err != nil {
103
128
log .Error (err , "Error renaming channel" )
104
- channel .Status .Error = err .Error ()
105
-
106
- err = r .Status ().Update (ctx , channel )
107
- if err != nil {
108
- log .Error (err , "Failed to update Channel status" )
109
- return ctrl.Result {}, err
110
- }
111
- return ctrl.Result {}, nil
129
+ return reconcilerUtil .ManageError (r .Client , channel , err , false )
112
130
}
113
131
114
132
_ , err = r .SlackService .SetTopic (channelID , topic )
115
133
if err != nil {
116
134
log .Error (err , "Error setting channel topic" )
117
- channel .Status .Error = err .Error ()
118
-
119
- err = r .Status ().Update (ctx , channel )
120
- if err != nil {
121
- log .Error (err , "Failed to update Channel status" )
122
- return ctrl.Result {}, err
123
- }
124
- return ctrl.Result {}, nil
135
+ return reconcilerUtil .ManageError (r .Client , channel , err , false )
125
136
}
126
137
127
138
_ , err = r .SlackService .SetDescription (channelID , description )
128
139
if err != nil {
129
140
log .Error (err , "Error setting channel description" )
130
- channel .Status .Error = err .Error ()
131
-
132
- err = r .Status ().Update (ctx , channel )
133
- if err != nil {
134
- log .Error (err , "Failed to update Channel status" )
135
- return ctrl.Result {}, err
136
- }
137
- return ctrl.Result {}, nil
141
+ return reconcilerUtil .ManageError (r .Client , channel , err , false )
138
142
}
139
143
140
144
err = r .SlackService .InviteUsers (channelID , users )
141
145
if err != nil {
142
146
log .Error (err , "Error inviting users to channel" )
143
- channel .Status .Error = err .Error ()
144
-
145
- err = r .Status ().Update (ctx , channel )
146
- if err != nil {
147
- log .Error (err , "Failed to update Channel status" )
148
- return ctrl.Result {}, err
149
- }
150
- return ctrl.Result {}, nil
147
+ return reconcilerUtil .ManageError (r .Client , channel , err , false )
151
148
}
152
149
153
150
return ctrl.Result {}, nil
154
151
}
155
152
153
+ func (r * ChannelReconciler ) finalizeChannel (req ctrl.Request , channel * slackv1alpha1.Channel ) (ctrl.Result , error ) {
154
+ channelID := channel .Status .ID
155
+ log := r .Log .WithValues ("channelID" , channelID )
156
+
157
+ if channel == nil {
158
+ return reconcilerUtil .DoNotRequeue ()
159
+ }
160
+
161
+ err := r .SlackService .ArchiveChannel (channelID )
162
+
163
+ if err != nil && err .Error () != "channel_not_found" {
164
+ return reconcilerUtil .ManageError (r .Client , channel , err , false )
165
+ }
166
+
167
+ finalizerUtil .DeleteFinalizer (channel , channelFinalizer )
168
+ log .V (1 ).Info ("Finalizer removed for channel" )
169
+
170
+ err = r .Client .Update (context .Background (), channel )
171
+ if err != nil {
172
+ return reconcilerUtil .ManageError (r .Client , channel , err , false )
173
+ }
174
+
175
+ return reconcilerUtil .DoNotRequeue ()
176
+ }
177
+
156
178
// SetupWithManager - Controller-Manager binding configuration
157
179
func (r * ChannelReconciler ) SetupWithManager (mgr ctrl.Manager ) error {
158
180
return ctrl .NewControllerManagedBy (mgr ).
0 commit comments