Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions pkg/webhook/plugins/datasetusageinjector/dataset_usage_injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ func NewPlugin(c client.Client, args string) (api.MutatingHandler, error) {
}, nil
}

// TODO: Support cases where fuse sidecars are injected in multi-round. Currently, only dataset names in the first round will be recorded.
func (injector *DatasetUsageInjector) Mutate(pod *corev1.Pod, runtimeInfos map[string]base.RuntimeInfoInterface) (shouldStop bool, err error) {
if len(runtimeInfos) == 0 {
return false, nil
Expand All @@ -44,13 +43,30 @@ func (injector *DatasetUsageInjector) Mutate(pod *corev1.Pod, runtimeInfos map[s
podName = pod.GenerateName
}

datasetsInUse := make([]string, 0, len(runtimeInfos))
annotationKey := common.LabelAnnotationDatasetsInUse

datasetsInUseMap := make(map[string]struct{})

// 1. Read existing datasets from annotation
if existingVal, exists := pod.Annotations[annotationKey]; exists && len(existingVal) > 0 {
existingDatasets := strings.Split(existingVal, ",")
for _, ds := range existingDatasets {
datasetsInUseMap[strings.TrimSpace(ds)] = struct{}{}
}
Comment thread
Sukuna0007Abhi marked this conversation as resolved.
Comment thread
Sukuna0007Abhi marked this conversation as resolved.
}
Comment thread
Sukuna0007Abhi marked this conversation as resolved.

// 2. Add new datasets from current round
for _, runtimeInfo := range runtimeInfos {
datasetsInUse = append(datasetsInUse, runtimeInfo.GetName())
datasetsInUseMap[runtimeInfo.GetName()] = struct{}{}
}

// 3. Convert map to sorted slice
datasetsInUse := make([]string, 0, len(datasetsInUseMap))
for ds := range datasetsInUseMap {
datasetsInUse = append(datasetsInUse, ds)
}
slices.Sort(datasetsInUse)

annotationKey := common.LabelAnnotationDatasetsInUse
annotationValue := strings.Join(datasetsInUse, ",")

log.Info("Injecting dataset usage annotation to pod",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ var _ = Describe("DatasetUsageInjector", func() {
})

Context("when pod has different annotation value", func() {
It("should update the annotation", func() {
It("should merge the existing annotation with the new one", func() {
pod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "test-update",
Expand All @@ -188,7 +188,7 @@ var _ = Describe("DatasetUsageInjector", func() {
Expect(err).NotTo(HaveOccurred())
Expect(shouldStop).To(BeFalse())

Expect(pod.Annotations[common.LabelAnnotationDatasetsInUse]).To(Equal("demo-dataset-1"))
Expect(pod.Annotations[common.LabelAnnotationDatasetsInUse]).To(Equal("demo-dataset-1,old-dataset"))
})
})
})
Expand Down
Loading