Skip to content

Commit

Permalink
Merge pull request #1 from warm-metal/once
Browse files Browse the repository at this point in the history
make sure each unit is executed only once
  • Loading branch information
kitt1987 authored Apr 24, 2021
2 parents 1a5288f + 3edaf2c commit 52aface
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Build the manager binary
FROM golang:1.15 as builder
FROM golang:1.16 as builder

WORKDIR /workspace
# Copy the Go Modules manifests
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

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

Expand Down
2 changes: 1 addition & 1 deletion config/manager/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ kind: Kustomization
images:
- name: controller
newName: docker.io/warmmetal/kube-systemd-controller
newTag: v0.1.0
newTag: v0.1.1
2 changes: 1 addition & 1 deletion config/samples/install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ spec:
- --metrics-bind-address=127.0.0.1:8080
command:
- /manager
image: docker.io/warmmetal/kube-systemd-controller:v0.1.0
image: docker.io/warmmetal/kube-systemd-controller:v0.1.1
imagePullPolicy: IfNotPresent
livenessProbe:
httpGet:
Expand Down
13 changes: 9 additions & 4 deletions controllers/unit_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"path/filepath"
"sort"
"strings"
"time"

"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -41,7 +42,7 @@ type UnitReconciler struct {
Log logr.Logger
Scheme *runtime.Scheme

Executed map[string]bool
SysUpTime time.Time
}

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

nextUnits := make([]*corev1.Unit, 0, len(list.Items))
for i := range list.Items {
if !r.Executed[list.Items[i].Name] {
unit := list.Items[i]
if len(unit.Status.Error) > 0 || !unit.Status.ExecTimestamp.After(r.SysUpTime) {
nextUnits = append(nextUnits, &list.Items[i])
}
}
Expand All @@ -86,6 +88,11 @@ func (r *UnitReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
for i := range nextUnits {
unit := nextUnits[i]
unit.Status.ExecTimestamp = now
// It may lead to the container exit to restart some unit
if err := r.Status().Update(ctx, unit); err != nil {
return ctrl.Result{}, err
}

err := startUnit(ctx, unit)
if err != nil {
unit.Status.Error = err.Error()
Expand All @@ -98,8 +105,6 @@ func (r *UnitReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
if err != nil {
return ctrl.Result{}, err
}

r.Executed[unit.Name] = true
}

return ctrl.Result{}, nil
Expand Down
16 changes: 12 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package main
import (
"flag"
"os"
"syscall"
"time"

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

var sys syscall.Sysinfo_t
if err := syscall.Sysinfo(&sys); err != nil {
setupLog.Error(err, "unable to fetch the system uptime")
os.Exit(1)
}

if err = (&controllers.UnitReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Unit"),
Scheme: mgr.GetScheme(),
Executed: make(map[string]bool),
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Unit"),
Scheme: mgr.GetScheme(),
SysUpTime: time.Now().Add(-1 * time.Duration(sys.Uptime) * time.Second),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Unit")
os.Exit(1)
Expand Down

0 comments on commit 52aface

Please sign in to comment.