|
| 1 | +/* |
| 2 | +Copyright 2023 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +/* |
| 18 | + This file contains a copy of the functions from https://github.com/kubernetes-sigs/kueue/blob/main/pkg/util/maps/maps.go |
| 19 | + that are used by the AppWrapper controlller. |
| 20 | +
|
| 21 | + We copy the used functions to eliminate our go dependency on Kueue, which simplifies bundling AppWrapper |
| 22 | + in the codeflare-operator in RedHat OpenShift AI. |
| 23 | +*/ |
| 24 | + |
| 25 | +package maps |
| 26 | + |
| 27 | +import ( |
| 28 | + "fmt" |
| 29 | + "maps" |
| 30 | +) |
| 31 | + |
| 32 | +// merge merges a and b while resolving the conflicts by calling commonKeyValue |
| 33 | +func merge[K comparable, V any, S ~map[K]V](a, b S, commonKeyValue func(a, b V) V) S { |
| 34 | + if a == nil { |
| 35 | + return maps.Clone(b) |
| 36 | + } |
| 37 | + |
| 38 | + ret := maps.Clone(a) |
| 39 | + |
| 40 | + for k, v := range b { |
| 41 | + if _, found := a[k]; found { |
| 42 | + ret[k] = commonKeyValue(a[k], v) |
| 43 | + } else { |
| 44 | + ret[k] = v |
| 45 | + } |
| 46 | + } |
| 47 | + return ret |
| 48 | +} |
| 49 | + |
| 50 | +// MergeKeepFirst merges a and b keeping the values in a in case of conflict |
| 51 | +func MergeKeepFirst[K comparable, V any, S ~map[K]V](a, b S) S { |
| 52 | + return merge(a, b, func(v, _ V) V { return v }) |
| 53 | +} |
| 54 | + |
| 55 | +// HaveConflict checks if a and b have the same key, but different value |
| 56 | +func HaveConflict[K comparable, V comparable, S ~map[K]V](a, b S) error { |
| 57 | + for k, av := range a { |
| 58 | + if bv, found := b[k]; found && av != bv { |
| 59 | + return fmt.Errorf("conflict for key=%v, value1=%v, value2=%v", k, av, bv) |
| 60 | + } |
| 61 | + } |
| 62 | + return nil |
| 63 | +} |
0 commit comments