Skip to content

Commit d080bb0

Browse files
committed
Patch status instead of updating
Signed-off-by: ccremer <github.account@chrigel.net>
1 parent ea46cdd commit d080bb0

3 files changed

Lines changed: 21 additions & 32 deletions

File tree

cmd/operator/main.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,24 @@ import (
44
"fmt"
55
"strings"
66

7+
k8upv1 "github.com/k8up-io/k8up/v2/api/v1"
8+
"github.com/k8up-io/k8up/v2/cmd"
79
"github.com/k8up-io/k8up/v2/operator/archivecontroller"
810
"github.com/k8up-io/k8up/v2/operator/backupcontroller"
11+
"github.com/k8up-io/k8up/v2/operator/cfg"
912
"github.com/k8up-io/k8up/v2/operator/checkcontroller"
13+
"github.com/k8up-io/k8up/v2/operator/executor"
1014
"github.com/k8up-io/k8up/v2/operator/jobcontroller"
1115
"github.com/k8up-io/k8up/v2/operator/locker"
1216
"github.com/k8up-io/k8up/v2/operator/prunecontroller"
1317
"github.com/k8up-io/k8up/v2/operator/restorecontroller"
1418
"github.com/k8up-io/k8up/v2/operator/schedulecontroller"
1519
"github.com/urfave/cli/v2"
16-
batchv1 "k8s.io/api/batch/v1"
1720
"k8s.io/apimachinery/pkg/api/resource"
1821
"k8s.io/apimachinery/pkg/runtime"
1922
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
2023
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
2124
ctrl "sigs.k8s.io/controller-runtime"
22-
23-
k8upv1 "github.com/k8up-io/k8up/v2/api/v1"
24-
"github.com/k8up-io/k8up/v2/cmd"
25-
"github.com/k8up-io/k8up/v2/operator/cfg"
26-
"github.com/k8up-io/k8up/v2/operator/executor"
2725
)
2826

2927
const (
@@ -144,7 +142,6 @@ func operatorMain(c *cli.Context) error {
144142
func k8upScheme() *runtime.Scheme {
145143
scheme := runtime.NewScheme()
146144
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
147-
utilruntime.Must(batchv1.AddToScheme(scheme))
148145
utilruntime.Must(k8upv1.AddToScheme(scheme))
149146
// +kubebuilder:scaffold:scheme
150147
return scheme

operator/executor/worker.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import (
66
"time"
77

88
"github.com/k8up-io/k8up/v2/operator/locker"
9-
"k8s.io/apimachinery/pkg/api/errors"
10-
119
"github.com/k8up-io/k8up/v2/operator/observer"
1210
"github.com/k8up-io/k8up/v2/operator/queue"
1311
)
@@ -75,9 +73,7 @@ func (qe *QueueWorker) loopRepositoryJobs(repository string) {
7573

7674
err := job.Execute()
7775
if err != nil {
78-
if !errors.IsAlreadyExists(err) {
79-
job.Logger().Error(err, "cannot create job", "repository", repository)
80-
}
76+
job.Logger().Error(err, "failed to execute", "repository", repository)
8177
}
8278

8379
// Skip the rest for this repository if we just started an exclusive job.

operator/job/status.go

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,28 +36,22 @@ func (c *Config) SetConditionFalseWithMessage(condition k8upv1.ConditionType, re
3636
}
3737

3838
// patchConditions patches the Status object on the K8s controller with the given Conditions
39-
func (c *Config) patchConditions(conditionStatus metav1.ConditionStatus, reason k8upv1.ConditionReason, message string, conditions ...k8upv1.ConditionType) {
40-
runtimeObject := c.Obj
41-
patch := client.MergeFrom(runtimeObject.DeepCopyObject().(client.Object))
42-
39+
func (c *Config) patchConditions(conditionStatus metav1.ConditionStatus, reason k8upv1.ConditionReason, message string, condition k8upv1.ConditionType) {
4340
status := c.Obj.GetStatus()
44-
45-
for _, condition := range conditions {
46-
meta.SetStatusCondition(&status.Conditions, metav1.Condition{
47-
Type: condition.String(),
48-
Status: conditionStatus,
49-
Message: message,
50-
Reason: reason.String(),
51-
})
52-
}
53-
41+
meta.SetStatusCondition(&status.Conditions, metav1.Condition{
42+
Type: condition.String(),
43+
Status: conditionStatus,
44+
Message: message,
45+
Reason: reason.String(),
46+
})
5447
c.Obj.SetStatus(status)
55-
err := c.Client.Status().Patch(c.CTX, c.Obj.(client.Object), patch)
48+
49+
err := c.Client.Status().Update(c.CTX, c.Obj)
5650
if err != nil {
5751
if errors.IsNotFound(err) {
5852
return
5953
}
60-
c.Log.Error(err, "could not patch status conditions")
54+
c.Log.Error(err, "could not patch status condition")
6155
}
6256
}
6357

@@ -68,12 +62,13 @@ func (c *Config) SetStarted(message string, args ...interface{}) {
6862
status.SetStarted(fmt.Sprintf(message, args...))
6963
c.Obj.SetStatus(status)
7064

71-
err := c.Client.Status().Update(c.CTX, c.Obj)
65+
patch := client.MergeFrom(c.Obj.DeepCopyObject().(client.Object))
66+
err := c.Client.Status().Patch(c.CTX, c.Obj, patch)
7267
if err != nil {
7368
if errors.IsNotFound(err) {
7469
return
7570
}
76-
c.Log.Error(err, "could not update status")
71+
c.Log.Error(err, "could not patch status")
7772
}
7873
}
7974

@@ -83,12 +78,13 @@ func (c *Config) SetFinished(namespace, name string) {
8378
status.SetFinished(fmt.Sprintf("the Job '%s/%s' ended", namespace, name))
8479
c.Obj.SetStatus(status)
8580

86-
err := c.Client.Status().Update(c.CTX, c.Obj)
81+
patch := client.MergeFrom(c.Obj.DeepCopyObject().(client.Object))
82+
err := c.Client.Status().Patch(c.CTX, c.Obj, patch)
8783
if err != nil {
8884
if errors.IsNotFound(err) {
8985
return
9086
}
91-
c.Log.Error(err, "could not update status")
87+
c.Log.Error(err, "could not patch status")
9288
}
9389
}
9490

0 commit comments

Comments
 (0)