Skip to content

Commit 20d938a

Browse files
committed
vendor 2 map util functions from Kueue
1 parent f27059e commit 20d938a

File tree

4 files changed

+68
-8
lines changed

4 files changed

+68
-8
lines changed

internal/controller/appwrapper/resource_management.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"time"
2424

2525
awv1beta2 "github.com/project-codeflare/appwrapper/api/v1beta2"
26+
utilmaps "github.com/project-codeflare/appwrapper/internal/util"
2627
"github.com/project-codeflare/appwrapper/pkg/utils"
2728
v1 "k8s.io/api/core/v1"
2829
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -35,7 +36,6 @@ import (
3536
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
3637
"sigs.k8s.io/controller-runtime/pkg/log"
3738
"sigs.k8s.io/kueue/pkg/podset"
38-
utilmaps "sigs.k8s.io/kueue/pkg/util/maps"
3939
)
4040

4141
func parseComponent(raw []byte, expectedNamespace string) (*unstructured.Unstructured, error) {

internal/util/maps.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
}

internal/webhook/appwrapper_webhook.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,16 @@ import (
3232
"k8s.io/client-go/kubernetes"
3333
authClientv1 "k8s.io/client-go/kubernetes/typed/authorization/v1"
3434
"k8s.io/utils/ptr"
35-
utilmaps "sigs.k8s.io/kueue/pkg/util/maps"
36-
3735
ctrl "sigs.k8s.io/controller-runtime"
3836
"sigs.k8s.io/controller-runtime/pkg/client"
3937
"sigs.k8s.io/controller-runtime/pkg/log"
4038
"sigs.k8s.io/controller-runtime/pkg/webhook"
4139
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
42-
4340
"sigs.k8s.io/kueue/pkg/controller/jobframework"
4441

4542
awv1beta2 "github.com/project-codeflare/appwrapper/api/v1beta2"
4643
wlc "github.com/project-codeflare/appwrapper/internal/controller/workload"
44+
utilmaps "github.com/project-codeflare/appwrapper/internal/util"
4745
"github.com/project-codeflare/appwrapper/pkg/config"
4846
"github.com/project-codeflare/appwrapper/pkg/utils"
4947
)

internal/webhook/appwrapper_webhook_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,12 @@ import (
2121

2222
. "github.com/onsi/ginkgo/v2"
2323
. "github.com/onsi/gomega"
24-
25-
awv1beta2 "github.com/project-codeflare/appwrapper/api/v1beta2"
26-
2724
"k8s.io/apimachinery/pkg/runtime"
2825
"k8s.io/apimachinery/pkg/types"
2926
"k8s.io/utils/ptr"
30-
utilmaps "sigs.k8s.io/kueue/pkg/util/maps"
27+
28+
awv1beta2 "github.com/project-codeflare/appwrapper/api/v1beta2"
29+
utilmaps "github.com/project-codeflare/appwrapper/internal/util"
3130
)
3231

3332
var _ = Describe("AppWrapper Webhook Tests", func() {

0 commit comments

Comments
 (0)