Skip to content

Commit 52aface

Browse files
authored
Merge pull request #1 from warm-metal/once
make sure each unit is executed only once
2 parents 1a5288f + 3edaf2c commit 52aface

File tree

6 files changed

+25
-12
lines changed

6 files changed

+25
-12
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Build the manager binary
2-
FROM golang:1.15 as builder
2+
FROM golang:1.16 as builder
33

44
WORKDIR /workspace
55
# Copy the Go Modules manifests

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
# Image URL to use all building/pushing image targets
3-
IMG ?= docker.io/warmmetal/kube-systemd-controller:v0.1.0
3+
IMG ?= docker.io/warmmetal/kube-systemd-controller:v0.1.1
44
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
55
CRD_OPTIONS ?= "crd:trivialVersions=true,preserveUnknownFields=false"
66

config/manager/kustomization.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ kind: Kustomization
1313
images:
1414
- name: controller
1515
newName: docker.io/warmmetal/kube-systemd-controller
16-
newTag: v0.1.0
16+
newTag: v0.1.1

config/samples/install.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ spec:
267267
- --metrics-bind-address=127.0.0.1:8080
268268
command:
269269
- /manager
270-
image: docker.io/warmmetal/kube-systemd-controller:v0.1.0
270+
image: docker.io/warmmetal/kube-systemd-controller:v0.1.1
271271
imagePullPolicy: IfNotPresent
272272
livenessProbe:
273273
httpGet:

controllers/unit_controller.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"path/filepath"
2727
"sort"
2828
"strings"
29+
"time"
2930

3031
"github.com/go-logr/logr"
3132
"k8s.io/apimachinery/pkg/runtime"
@@ -41,7 +42,7 @@ type UnitReconciler struct {
4142
Log logr.Logger
4243
Scheme *runtime.Scheme
4344

44-
Executed map[string]bool
45+
SysUpTime time.Time
4546
}
4647

4748
const (
@@ -73,7 +74,8 @@ func (r *UnitReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
7374

7475
nextUnits := make([]*corev1.Unit, 0, len(list.Items))
7576
for i := range list.Items {
76-
if !r.Executed[list.Items[i].Name] {
77+
unit := list.Items[i]
78+
if len(unit.Status.Error) > 0 || !unit.Status.ExecTimestamp.After(r.SysUpTime) {
7779
nextUnits = append(nextUnits, &list.Items[i])
7880
}
7981
}
@@ -86,6 +88,11 @@ func (r *UnitReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
8688
for i := range nextUnits {
8789
unit := nextUnits[i]
8890
unit.Status.ExecTimestamp = now
91+
// It may lead to the container exit to restart some unit
92+
if err := r.Status().Update(ctx, unit); err != nil {
93+
return ctrl.Result{}, err
94+
}
95+
8996
err := startUnit(ctx, unit)
9097
if err != nil {
9198
unit.Status.Error = err.Error()
@@ -98,8 +105,6 @@ func (r *UnitReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
98105
if err != nil {
99106
return ctrl.Result{}, err
100107
}
101-
102-
r.Executed[unit.Name] = true
103108
}
104109

105110
return ctrl.Result{}, nil

main.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package main
1919
import (
2020
"flag"
2121
"os"
22+
"syscall"
23+
"time"
2224

2325
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
2426
// to ensure that exec-entrypoint and run can make use of them.
@@ -72,11 +74,17 @@ func main() {
7274
os.Exit(1)
7375
}
7476

77+
var sys syscall.Sysinfo_t
78+
if err := syscall.Sysinfo(&sys); err != nil {
79+
setupLog.Error(err, "unable to fetch the system uptime")
80+
os.Exit(1)
81+
}
82+
7583
if err = (&controllers.UnitReconciler{
76-
Client: mgr.GetClient(),
77-
Log: ctrl.Log.WithName("controllers").WithName("Unit"),
78-
Scheme: mgr.GetScheme(),
79-
Executed: make(map[string]bool),
84+
Client: mgr.GetClient(),
85+
Log: ctrl.Log.WithName("controllers").WithName("Unit"),
86+
Scheme: mgr.GetScheme(),
87+
SysUpTime: time.Now().Add(-1 * time.Duration(sys.Uptime) * time.Second),
8088
}).SetupWithManager(mgr); err != nil {
8189
setupLog.Error(err, "unable to create controller", "controller", "Unit")
8290
os.Exit(1)

0 commit comments

Comments
 (0)