Skip to content

Commit d16679d

Browse files
committed
Add pausing deployments for upgrades
1 parent 574129d commit d16679d

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

internal/pkg/handler/upgrade.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package handler
22

33
import (
44
"bytes"
5+
"context"
56
"encoding/json"
67
"errors"
78
"fmt"
@@ -10,6 +11,7 @@ import (
1011
"regexp"
1112
"strconv"
1213
"strings"
14+
"time"
1315

1416
"github.com/parnurzeal/gorequest"
1517
"github.com/prometheus/client_golang/prometheus"
@@ -21,8 +23,10 @@ import (
2123
"github.com/stakater/Reloader/internal/pkg/options"
2224
"github.com/stakater/Reloader/internal/pkg/util"
2325
"github.com/stakater/Reloader/pkg/kube"
26+
app "k8s.io/api/apps/v1"
2427
v1 "k8s.io/api/core/v1"
2528
"k8s.io/apimachinery/pkg/api/meta"
29+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2630
"k8s.io/apimachinery/pkg/runtime"
2731
"k8s.io/client-go/tools/record"
2832
)
@@ -211,6 +215,7 @@ func PerformAction(clients kube.Clients, config util.Config, upgradeFuncs callba
211215
items := upgradeFuncs.ItemsFunc(clients, config.Namespace)
212216

213217
for _, i := range items {
218+
214219
// find correct annotation and update the resource
215220
annotations := upgradeFuncs.AnnotationsFunc(i)
216221
annotationValue, found := annotations[config.Annotation]
@@ -274,6 +279,51 @@ func PerformAction(clients kube.Clients, config util.Config, upgradeFuncs callba
274279
}
275280

276281
if result == constants.Updated {
282+
283+
if _, ok := i.(*app.Deployment); ok {
284+
logrus.Infof("Resource '%s' is of type 'Deployment'", config.ResourceName)
285+
annotations := upgradeFuncs.AnnotationsFunc(i)
286+
pauseValue := annotations["pause"]
287+
if pauseValue != "" {
288+
logrus.Infof("Pause value is %s", pauseValue)
289+
pauseDuration, err := time.ParseDuration(pauseValue)
290+
if err != nil {
291+
logrus.Errorf("Failed to parse pause value '%s' for resource '%s': %v", pauseValue, config.ResourceName, err)
292+
} else {
293+
logrus.Infof("Parsed pause value for resource '%s' is %d seconds", config.ResourceName, int(pauseDuration.Seconds()))
294+
deployment, ok := i.(*app.Deployment)
295+
if !ok {
296+
logrus.Errorf("Failed to cast resource '%s' to Deployment", config.ResourceName)
297+
return errors.New("failed to cast resource to Deployment")
298+
}
299+
300+
if !deployment.Spec.Paused {
301+
deployment.Spec.Paused = true
302+
logrus.Infof("Pausing Deployment '%s' in namespace '%s'", config.ResourceName, config.Namespace)
303+
deployment.Annotations["paused-by-reloader-at"] = time.Now().Format(time.RFC3339)
304+
305+
time.AfterFunc(pauseDuration, func() {
306+
deployment, err := clients.KubernetesClient.AppsV1().Deployments(config.Namespace).Get(context.TODO(), config.ResourceName, metav1.GetOptions{})
307+
if err != nil {
308+
logrus.Errorf("Failed to get Deployment '%s' in namespace '%s': %v", config.ResourceName, config.Namespace, err)
309+
return
310+
}
311+
312+
deployment.Spec.Paused = false
313+
deployment.Annotations["paused-by-reloader-at"] = ""
314+
315+
_, err = clients.KubernetesClient.AppsV1().Deployments(config.Namespace).Update(context.TODO(), deployment, metav1.UpdateOptions{})
316+
if err != nil {
317+
logrus.Errorf("Failed to update Deployment '%s' in namespace '%s': %v", config.ResourceName, config.Namespace, err)
318+
}
319+
})
320+
} else {
321+
logrus.Infof("Deployment '%s' in namespace '%s' is already paused", config.ResourceName, config.Namespace)
322+
}
323+
}
324+
}
325+
}
326+
277327
accessor, err := meta.Accessor(i)
278328
if err != nil {
279329
return err

test.env

Whitespace-only changes.

0 commit comments

Comments
 (0)