Skip to content

Commit 2b93775

Browse files
committed
library improvements (from implementing Landscape controller of Gardener ClusterProvider)
1 parent 4942d9b commit 2b93775

File tree

6 files changed

+66
-16
lines changed

6 files changed

+66
-16
lines changed

pkg/clusters/cluster.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ func (c *Cluster) WithConfigPath(cfgPath string) *Cluster {
4040
return c
4141
}
4242

43+
// WithRestConfig allows to set the REST config manually.
44+
// Returns the cluster for chaining.
45+
func (c *Cluster) WithRESTConfig(cfg *rest.Config) *Cluster {
46+
c.restCfg = cfg
47+
return c
48+
}
49+
4350
// RegisterConfigPathFlag adds a flag '--<id>-cluster' for the cluster's config path to the given flag set.
4451
// Panics if the cluster's id is not set.
4552
func (c *Cluster) RegisterConfigPathFlag(flags *flag.FlagSet) {
@@ -117,7 +124,7 @@ func (c *Cluster) InitializeClient(scheme *runtime.Scheme) error {
117124
if err != nil {
118125
return fmt.Errorf("failed to create '%s' cluster client: %w", c.ID(), err)
119126
}
120-
clu, err := cluster.New(c.restCfg, func(o *cluster.Options) { o.Scheme = scheme })
127+
clu, err := cluster.New(c.restCfg, func(o *cluster.Options) { o.Scheme = scheme; o.Cache.Scheme = scheme })
121128
if err != nil {
122129
return fmt.Errorf("failed to create '%s' cluster Cluster representation: %w", c.ID(), err)
123130
}

pkg/conditions/updater_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
. "github.com/onsi/ginkgo/v2"
99
. "github.com/onsi/gomega"
10+
1011
. "github.com/openmcp-project/controller-utils/pkg/testing/matchers"
1112

1213
"github.com/openmcp-project/controller-utils/pkg/conditions"

pkg/controller/annotation_label.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ func EnsureLabel(ctx context.Context, c client.Client, obj client.Object, labelK
103103
}
104104

105105
// ensureMetadataEntry is the common base method for EnsureAnnotation and EnsureLabel.
106-
// This is mainly exposed for testing purposes, usually it is statically known whether an annotation or label is to be modified and the respective method should be used.
107106
func ensureMetadataEntry(mType metadataEntryType, ctx context.Context, c client.Client, obj client.Object, key, value string, patch bool, mode ...ModifyMetadataEntryMode) error {
108107
modeDelete := false
109108
modeOverwrite := false

pkg/controller/predicates.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,47 @@ import (
1010
"sigs.k8s.io/controller-runtime/pkg/predicate"
1111
)
1212

13+
//////////////////
14+
/// CONVERSION ///
15+
//////////////////
16+
17+
// ToTypedPredicate wraps a predicate.Predicate (which is an alias for predicate.TypedPredicate[client.Object]) into a predicate.TypedPredicate[Obj].
18+
func ToTypedPredicate[Obj client.Object](p predicate.Predicate) predicate.TypedPredicate[Obj] {
19+
return &typedPredicateConverter[Obj]{internal: p}
20+
}
21+
22+
type typedPredicateConverter[Obj client.Object] struct {
23+
internal predicate.Predicate
24+
}
25+
26+
var _ predicate.TypedPredicate[client.Object] = &typedPredicateConverter[client.Object]{}
27+
28+
func (t *typedPredicateConverter[Obj]) Create(e event.TypedCreateEvent[Obj]) bool {
29+
return t.internal.Create(event.TypedCreateEvent[client.Object]{
30+
Object: e.Object,
31+
})
32+
}
33+
34+
func (t *typedPredicateConverter[Obj]) Delete(e event.TypedDeleteEvent[Obj]) bool {
35+
return t.internal.Delete(event.TypedDeleteEvent[client.Object]{
36+
Object: e.Object,
37+
DeleteStateUnknown: e.DeleteStateUnknown,
38+
})
39+
}
40+
41+
func (t *typedPredicateConverter[Obj]) Generic(e event.TypedGenericEvent[Obj]) bool {
42+
return t.internal.Generic(event.TypedGenericEvent[client.Object]{
43+
Object: e.Object,
44+
})
45+
}
46+
47+
func (t *typedPredicateConverter[Obj]) Update(e event.TypedUpdateEvent[Obj]) bool {
48+
return t.internal.Update(event.TypedUpdateEvent[client.Object]{
49+
ObjectOld: e.ObjectOld,
50+
ObjectNew: e.ObjectNew,
51+
})
52+
}
53+
1354
/////////////////////////////////////
1455
/// DELETION TIMESTAMP PREDICATES ///
1556
/////////////////////////////////////

pkg/controller/utils.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"encoding/base32"
66
"reflect"
77
"strings"
8+
9+
"sigs.k8s.io/controller-runtime/pkg/client"
810
)
911

1012
const (
@@ -35,3 +37,17 @@ func IsNil(i any) bool {
3537
}
3638
return false
3739
}
40+
41+
// ObjectKey returns a client.ObjectKey for the given name and optionally namespace.
42+
// The first argument is the name of the object.
43+
// An optional second argument contains the namespace. All further arguments are ignored.
44+
func ObjectKey(name string, maybeNamespace ...string) client.ObjectKey {
45+
namespace := ""
46+
if len(maybeNamespace) > 0 {
47+
namespace = maybeNamespace[0]
48+
}
49+
return client.ObjectKey{
50+
Namespace: namespace,
51+
Name: name,
52+
}
53+
}

pkg/testing/utils.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,6 @@ func RequestFromStrings(name string, maybeNamespace ...string) reconcile.Request
7171
}
7272
}
7373

74-
// ObjectKey returns a client.ObjectKey for the given name and optionally namespace.
75-
// The first argument is the name of the object.
76-
// An optional second argument contains the namespace. All further arguments are ignored.
77-
func ObjectKey(name string, maybeNamespace ...string) client.ObjectKey {
78-
namespace := ""
79-
if len(maybeNamespace) > 0 {
80-
namespace = maybeNamespace[0]
81-
}
82-
return client.ObjectKey{
83-
Namespace: namespace,
84-
Name: name,
85-
}
86-
}
87-
8874
// LoadObject reads a file and unmarshals it into the given object.
8975
// obj must be a non-nil pointer.
9076
func LoadObject(obj any, paths ...string) error {

0 commit comments

Comments
 (0)