Skip to content

Commit

Permalink
add contextual logging to job-controller
Browse files Browse the repository at this point in the history
Signed-off-by: Ziqi Zhao <zhaoziqi9146@gmail.com>
  • Loading branch information
fatsheep9146 committed Jun 14, 2023
1 parent 9740bc0 commit 7bc449d
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 125 deletions.
1 change: 1 addition & 0 deletions cmd/kube-controller-manager/app/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (

func startJobController(ctx context.Context, controllerContext ControllerContext) (controller.Interface, bool, error) {
go job.NewController(
ctx,
controllerContext.InformerFactory.Core().V1().Pods(),
controllerContext.InformerFactory.Batch().V1().Jobs(),
controllerContext.ClientBuilder.ClientOrDie("job-controller"),
Expand Down
1 change: 0 additions & 1 deletion hack/logcheck.conf
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ contextual k8s.io/kubernetes/test/e2e/dra/.*
-contextual k8s.io/kubernetes/pkg/controller/endpointslice/.*
-contextual k8s.io/kubernetes/pkg/controller/endpointslicemirroring/.*
-contextual k8s.io/kubernetes/pkg/controller/garbagecollector/.*
-contextual k8s.io/kubernetes/pkg/controller/job/.*
-contextual k8s.io/kubernetes/pkg/controller/nodeipam/.*
-contextual k8s.io/kubernetes/pkg/controller/podgc/.*
-contextual k8s.io/kubernetes/pkg/controller/replicaset/.*
Expand Down
10 changes: 5 additions & 5 deletions pkg/controller/job/indexed_job_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ type orderedIntervals []interval
// The old list is solely based off .status.completedIndexes, but returns an
// empty list if this Job is not tracked with finalizers. The new list includes
// the indexes that succeeded since the last sync.
func calculateSucceededIndexes(job *batch.Job, pods []*v1.Pod) (orderedIntervals, orderedIntervals) {
prevIntervals := succeededIndexesFromString(job.Status.CompletedIndexes, int(*job.Spec.Completions))
func calculateSucceededIndexes(logger klog.Logger, job *batch.Job, pods []*v1.Pod) (orderedIntervals, orderedIntervals) {
prevIntervals := succeededIndexesFromString(logger, job.Status.CompletedIndexes, int(*job.Spec.Completions))
newSucceeded := sets.New[int]()
for _, p := range pods {
ix := getCompletionIndex(p.Annotations)
Expand Down Expand Up @@ -148,7 +148,7 @@ func (oi orderedIntervals) has(ix int) bool {
return oi[hi].First <= ix
}

func succeededIndexesFromString(completedIndexes string, completions int) orderedIntervals {
func succeededIndexesFromString(logger klog.Logger, completedIndexes string, completions int) orderedIntervals {
if completedIndexes == "" {
return nil
}
Expand All @@ -160,7 +160,7 @@ func succeededIndexesFromString(completedIndexes string, completions int) ordere
var err error
inter.First, err = strconv.Atoi(limitsStr[0])
if err != nil {
klog.InfoS("Corrupted completed indexes interval, ignoring", "interval", intervalStr, "err", err)
logger.Info("Corrupted completed indexes interval, ignoring", "interval", intervalStr, "err", err)
continue
}
if inter.First >= completions {
Expand All @@ -169,7 +169,7 @@ func succeededIndexesFromString(completedIndexes string, completions int) ordere
if len(limitsStr) > 1 {
inter.Last, err = strconv.Atoi(limitsStr[1])
if err != nil {
klog.InfoS("Corrupted completed indexes interval, ignoring", "interval", intervalStr, "err", err)
logger.Info("Corrupted completed indexes interval, ignoring", "interval", intervalStr, "err", err)
continue
}
if inter.Last >= completions {
Expand Down
4 changes: 3 additions & 1 deletion pkg/controller/job/indexed_job_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ import (
"github.com/google/go-cmp/cmp"
batch "k8s.io/api/batch/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/klog/v2/ktesting"
"k8s.io/utils/pointer"
)

const noIndex = "-"

func TestCalculateSucceededIndexes(t *testing.T) {
logger, _ := ktesting.NewTestContext(t)
cases := map[string]struct {
prevSucceeded string
pods []indexPhase
Expand Down Expand Up @@ -206,7 +208,7 @@ func TestCalculateSucceededIndexes(t *testing.T) {
for _, p := range pods {
p.Finalizers = append(p.Finalizers, batch.JobTrackingFinalizer)
}
gotStatusIntervals, gotIntervals := calculateSucceededIndexes(job, pods)
gotStatusIntervals, gotIntervals := calculateSucceededIndexes(logger, job, pods)
if diff := cmp.Diff(tc.wantStatusIntervals, gotStatusIntervals); diff != "" {
t.Errorf("Unexpected completed indexes from status (-want,+got):\n%s", diff)
}
Expand Down
Loading

0 comments on commit 7bc449d

Please sign in to comment.