Skip to content

Commit

Permalink
code review: adjust log message and emit pod event
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyulin0719 committed Aug 30, 2024
1 parent ca8a864 commit c8fa229
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 22 deletions.
10 changes: 8 additions & 2 deletions pkg/cache/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,16 +536,22 @@ func (task *Task) sanityCheckBeforeScheduling() error {
// After version 1.7.0, we should reject the task whose pod is unbound and has conflicting metadata.
if !utils.PodAlreadyBound(task.pod) {
if err := utils.CheckAppIdInPod(task.pod); err != nil {
log.Log(log.ShimCacheTask).Warn("The task has conflicting metadata will be rejected after version 1.7.0.",
log.Log(log.ShimCacheTask).Warn("Pod has inconsistent application metadata and may be rejected in a future YuniKorn release.",
zap.String("appID", task.applicationID),
zap.String("podName", task.pod.Name),
zap.String("error", err.Error()))

Check warning on line 542 in pkg/cache/task.go

View check run for this annotation

Codecov / codecov/patch

pkg/cache/task.go#L538-L542

Added lines #L538 - L542 were not covered by tests

events.GetRecorder().Eventf(task.pod.DeepCopy(),
nil, v1.EventTypeWarning, "Scheduling", "Scheduling", fmt.Sprintf("Pod has inconsistent application metadata and may be rejected in a future YuniKorn release: %s", err.Error()))

Check warning on line 545 in pkg/cache/task.go

View check run for this annotation

Codecov / codecov/patch

pkg/cache/task.go#L544-L545

Added lines #L544 - L545 were not covered by tests
}
if err := utils.CheckQueueNameInPod(task.pod); err != nil {
log.Log(log.ShimCacheTask).Warn("The task has conflicting metadata will be rejected after version 1.7.0.",
log.Log(log.ShimCacheTask).Warn("Pod has inconsistent queue metadata and may be rejected in a future YuniKorn release.",
zap.String("appID", task.applicationID),
zap.String("podName", task.pod.Name),
zap.String("error", err.Error()))

Check warning on line 551 in pkg/cache/task.go

View check run for this annotation

Codecov / codecov/patch

pkg/cache/task.go#L547-L551

Added lines #L547 - L551 were not covered by tests

events.GetRecorder().Eventf(task.pod.DeepCopy(),
nil, v1.EventTypeWarning, "Scheduling", "Scheduling", fmt.Sprintf("Pod has inconsistent queue metadata and may be rejected in a future YuniKorn release: %s", err.Error()))

Check warning on line 554 in pkg/cache/task.go

View check run for this annotation

Codecov / codecov/patch

pkg/cache/task.go#L553-L554

Added lines #L553 - L554 were not covered by tests
}
}
return task.checkPodPVCs()
Expand Down
4 changes: 4 additions & 0 deletions pkg/common/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ import (
const True = "true"
const False = "false"

// Kubernetes
const Label = "label"
const Annotation = "annotation"

// Cluster
const DefaultNodeAttributeHostNameKey = "si.io/hostname"
const DefaultNodeAttributeRackNameKey = "si.io/rackname"
Expand Down
22 changes: 12 additions & 10 deletions pkg/common/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,44 +214,46 @@ func GetApplicationIDFromPod(pod *v1.Pod) string {
}

func CheckAppIdInPod(pod *v1.Pod) error {
if err := ValidatePodLabelAnnotation(pod, constants.AppIdLabelKeys, constants.AppIdAnnotationKeys); err != nil {
return fmt.Errorf("pod has inconsistent application ID in labels and annotations. %w", err)
}
return nil
return ValidatePodLabelAnnotation(pod, constants.AppIdLabelKeys, constants.AppIdAnnotationKeys)
}

func CheckQueueNameInPod(pod *v1.Pod) error {
if err := ValidatePodLabelAnnotation(pod, constants.QueueLabelKeys, constants.QueueAnnotationKeys); err != nil {
return fmt.Errorf("pod has inconsistent queue name in labels and annotations. %w", err)
}
return nil
return ValidatePodLabelAnnotation(pod, constants.QueueLabelKeys, constants.QueueAnnotationKeys)
}

// return true if all non-empty values are same across all provided label/annotation
func ValidatePodLabelAnnotation(pod *v1.Pod, labelKeys []string, annotationKeys []string) error {
var referenceKey string
var referenceValue string
var referenceType string

checkingType := constants.Label
for _, key := range labelKeys {
value := GetPodLabelValue(pod, key)
if value == "" {
continue
}
if referenceValue == "" {
referenceKey = key
referenceValue = value
referenceType = checkingType
} else if referenceValue != value {
return fmt.Errorf("inconsistent values: %s, %s", referenceValue, value)
return fmt.Errorf("%s %s: \"%s\" doesn't match %s %s: \"%s\"", checkingType, key, value, referenceType, referenceKey, referenceValue)
}
}

checkingType = constants.Annotation
for _, key := range annotationKeys {
value := GetPodAnnotationValue(pod, key)
if value == "" {
continue
}
if referenceValue == "" {
referenceKey = key
referenceValue = value
referenceType = checkingType
} else if referenceValue != value {
return fmt.Errorf("inconsistent values: %s, %s", referenceValue, value)
return fmt.Errorf("%s %s: \"%s\" doesn't match %s %s: \"%s\"", checkingType, key, value, referenceType, referenceKey, referenceValue)
}
}

Expand Down
17 changes: 7 additions & 10 deletions pkg/common/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,6 @@ func TestGetApplicationIDFromPod(t *testing.T) {
}

func TestCheckAppIdInPod(t *testing.T) {
mismatchError := errors.New("pod has inconsistent application ID")
testCases := []struct {
name string
pod *v1.Pod
Expand Down Expand Up @@ -757,7 +756,7 @@ func TestCheckAppIdInPod(t *testing.T) {
},
},
},
expected: mismatchError,
expected: errors.New("label spark-app-selector: \"app-456\" doesn't match label yunikorn.apache.org/app-id: \"app-123\""),
},
{
name: "inconsistent app ID between label and annotation",
Expand All @@ -771,7 +770,7 @@ func TestCheckAppIdInPod(t *testing.T) {
},
},
},
expected: mismatchError,
expected: errors.New("annotation yunikorn.apache.org/app-id: \"app-456\" doesn't match label yunikorn.apache.org/app-id: \"app-123\""),
},
}
for _, tc := range testCases {
Expand All @@ -787,7 +786,6 @@ func TestCheckAppIdInPod(t *testing.T) {
}

func TestCheckQueueNameInPod(t *testing.T) {
mismatchError := errors.New("pod has inconsistent queue name")
testCases := []struct {
name string
pod *v1.Pod
Expand Down Expand Up @@ -818,7 +816,7 @@ func TestCheckQueueNameInPod(t *testing.T) {
},
},
},
expected: mismatchError,
expected: errors.New("label queue: \"root.b\" doesn't match label yunikorn.apache.org/queue: \"root.a\""),
},
{
name: "inconsistent app ID between label and annotation",
Expand All @@ -832,7 +830,7 @@ func TestCheckQueueNameInPod(t *testing.T) {
},
},
},
expected: mismatchError,
expected: errors.New("annotation yunikorn.apache.org/queue: \"root.b\" doesn't match label yunikorn.apache.org/queue: \"root.a\""),
},
}
for _, tc := range testCases {
Expand All @@ -850,7 +848,6 @@ func TestCheckQueueNameInPod(t *testing.T) {
func TestValidatePodLabelAnnotation(t *testing.T) {
labelKeys := []string{"labelKey1", "labelKey2"}
annotationKeys := []string{"annotationKey1", "annotationKey2"}
mismatchError := errors.New("inconsistent values: ")

testCases := []struct {
name string
Expand Down Expand Up @@ -888,7 +885,7 @@ func TestValidatePodLabelAnnotation(t *testing.T) {
},
},
},
expected: mismatchError,
expected: errors.New("label labelKey2: \"value2\" doesn't match label labelKey1: \"value1\""),
},
{
name: "pod with inconsistent value between label and annotation",
Expand All @@ -902,7 +899,7 @@ func TestValidatePodLabelAnnotation(t *testing.T) {
},
},
},
expected: mismatchError,
expected: errors.New("annotation annotationKey1: \"value2\" doesn't match label labelKey1: \"value1\""),
},
{
name: "pod with inconsistent value in annotations",
Expand All @@ -914,7 +911,7 @@ func TestValidatePodLabelAnnotation(t *testing.T) {
},
},
},
expected: mismatchError,
expected: errors.New("annotation annotationKey2: \"value2\" doesn't match annotation annotationKey1: \"value1\""),
},
}

Expand Down

0 comments on commit c8fa229

Please sign in to comment.