-
Notifications
You must be signed in to change notification settings - Fork 11
/
util.go
62 lines (49 loc) · 1.74 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package safe
import (
"context"
"github.com/cosi-project/runtime/pkg/controller"
"github.com/cosi-project/runtime/pkg/controller/generic"
"github.com/cosi-project/runtime/pkg/controller/runtime/options"
"github.com/cosi-project/runtime/pkg/resource"
)
// Map applies the given function to each element of the list and returns a new list with the results.
func Map[T any, R any](list List[T], fn func(T) (R, error)) ([]R, error) {
result := make([]R, 0, list.Len())
for _, item := range list.list.Items {
r, err := fn(item.(T))
if err != nil {
return nil, err
}
result = append(result, r)
}
return result, nil
}
// Input returns a controller.Input for the given resource.
func Input[R generic.ResourceWithRD](kind controller.InputKind) controller.Input {
var r R
return controller.Input{
Namespace: r.ResourceDefinition().DefaultNamespace,
Type: r.ResourceDefinition().Type,
Kind: kind,
}
}
// CleanupOutputs wraps the controller.OutputTracker.CleanupOutputs method.
func CleanupOutputs[R generic.ResourceWithRD](ctx context.Context, tracker controller.OutputTracker) error {
var r R
return tracker.CleanupOutputs(ctx,
resource.NewMetadata(
r.ResourceDefinition().DefaultNamespace,
r.ResourceDefinition().Type,
"",
resource.VersionUndefined,
),
)
}
// WithResourceCache returns a controller runtime options.WithResourceCache.
func WithResourceCache[R generic.ResourceWithRD]() options.Option {
var r R
return options.WithCachedResource(r.ResourceDefinition().DefaultNamespace, r.ResourceDefinition().Type)
}