Skip to content

Commit 74efcc9

Browse files
authored
Fix deployment spec annotation (#170)
1 parent cd06933 commit 74efcc9

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

internal/controller/generic_tunnel_reconciler.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package controller
22

33
import (
4+
"crypto/md5"
5+
"encoding/hex"
46
"errors"
57
"fmt"
68
"time"
@@ -250,12 +252,13 @@ func createManagedResources(r GenericTunnelReconciler) (ctrl.Result, error) {
250252
}
251253

252254
// Check if ConfigMap already exists, else create it
253-
if err := k8s.MergeOrApply(r, configMapForTunnel(r)); err != nil {
255+
cm := configMapForTunnel(r)
256+
if err := k8s.MergeOrApply(r, cm); err != nil {
254257
return ctrl.Result{}, err
255258
}
256259

257260
// Apply patch to deployment
258-
dep := deploymentForTunnel(r)
261+
dep := deploymentForTunnel(r, cm.Data[configmapKey])
259262
if err := k8s.StrategicPatch(dep, r.GetTunnel().GetSpec().DeployPatch, dep); err != nil {
260263
r.GetLog().Error(err, "unable to patch deployment, check patch")
261264
r.GetRecorder().Event(r.GetTunnel().GetObject(), corev1.EventTypeWarning, "FailedPatch", "Failed to patch deployment, check patch")
@@ -329,9 +332,10 @@ func secretForTunnel(r GenericTunnelReconciler) *corev1.Secret {
329332
}
330333

331334
// deploymentForTunnel returns a tunnel Deployment object
332-
func deploymentForTunnel(r GenericTunnelReconciler) *appsv1.Deployment {
335+
func deploymentForTunnel(r GenericTunnelReconciler, configStr string) *appsv1.Deployment {
333336
ls := labelsForTunnel(r.GetTunnel())
334337
protocol := r.GetTunnel().GetSpec().Protocol
338+
hash := md5.Sum([]byte(configStr))
335339

336340
args := []string{"tunnel", "--protocol", protocol, "--config", "/etc/cloudflared/config/config.yaml", "--metrics", "0.0.0.0:2000", "run"}
337341
volumes := []corev1.Volume{{
@@ -398,6 +402,9 @@ func deploymentForTunnel(r GenericTunnelReconciler) *appsv1.Deployment {
398402
Template: corev1.PodTemplateSpec{
399403
ObjectMeta: metav1.ObjectMeta{
400404
Labels: ls,
405+
Annotations: map[string]string{
406+
tunnelConfigChecksum: hex.EncodeToString(hash[:]),
407+
},
401408
},
402409
Spec: corev1.PodSpec{
403410
SecurityContext: &corev1.PodSecurityContext{

internal/controller/tunnelbinding_controller.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"fmt"
2424
"sort"
2525
"strings"
26+
"time"
2627

2728
"github.com/adyanth/cloudflare-operator/internal/clients/cf"
2829

@@ -156,7 +157,11 @@ func (r *TunnelBindingReconciler) Reconcile(ctx context.Context, req ctrl.Reques
156157
// TunnelBinding object not found, could have been deleted after reconcile request.
157158
// Owned objects are automatically garbage collected. For additional cleanup logic use finalizers.
158159
// Return and don't requeue
159-
r.log.Info("TunnelBinding deleted, nothing to do")
160+
r.log.Info("TunnelBinding deleted, updating config")
161+
if err = r.configureCloudflareDaemon(); err != nil {
162+
r.log.Error(err, "unable to update config")
163+
return ctrl.Result{}, err
164+
}
160165
return ctrl.Result{}, nil
161166
}
162167
r.log.Error(err, "unable to fetch TunnelBinding")
@@ -170,7 +175,8 @@ func (r *TunnelBindingReconciler) Reconcile(ctx context.Context, req ctrl.Reques
170175

171176
// Check if TunnelBinding is marked for deletion
172177
if r.binding.GetDeletionTimestamp() != nil {
173-
return ctrl.Result{}, r.deletionLogic()
178+
// Requeue to update configmap above
179+
return ctrl.Result{RequeueAfter: time.Second}, r.deletionLogic()
174180
}
175181

176182
if err := r.setStatus(); err != nil {
@@ -535,10 +541,10 @@ func (r *TunnelBindingReconciler) setConfigMapConfiguration(config *cf.Configura
535541
// Restart pods
536542
r.Recorder.Event(r.binding, corev1.EventTypeNormal, "ApplyingConfig", "Applying ConfigMap to Deployment")
537543
r.Recorder.Event(cfDeployment, corev1.EventTypeNormal, "ApplyingConfig", "Applying ConfigMap to Deployment")
538-
if cfDeployment.Annotations == nil {
539-
cfDeployment.Annotations = map[string]string{}
544+
if cfDeployment.Spec.Template.Annotations == nil {
545+
cfDeployment.Spec.Template.Annotations = map[string]string{}
540546
}
541-
cfDeployment.Annotations[tunnelConfigChecksum] = hex.EncodeToString(hash[:])
547+
cfDeployment.Spec.Template.Annotations[tunnelConfigChecksum] = hex.EncodeToString(hash[:])
542548
if err := r.Update(r.ctx, cfDeployment); err != nil {
543549
r.log.Error(err, "Failed to update Deployment for restart")
544550
r.Recorder.Event(r.binding, corev1.EventTypeWarning, "FailedApplyingConfig", "Failed to apply ConfigMap to Deployment")

0 commit comments

Comments
 (0)