Skip to content

Commit 230b1a2

Browse files
committed
group libctrl into a set of packages
1 parent 61820cf commit 230b1a2

26 files changed

+652
-436
lines changed

adopt/adopt.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ import (
3030
"k8s.io/apimachinery/pkg/types"
3131
"k8s.io/klog/v2"
3232

33-
"github.com/authzed/controller-idioms"
3433
"github.com/authzed/controller-idioms/handler"
34+
"github.com/authzed/controller-idioms/queue"
3535
"github.com/authzed/controller-idioms/typed"
36+
"github.com/authzed/controller-idioms/typedctx"
3637
)
3738

3839
// TODO: a variant where there can only be one owner (label only, fail if labelled for someone else)
@@ -64,7 +65,7 @@ type IndexKeyFunc func(ctx context.Context) (indexName string, indexValue string
6465
const Owned = "owned"
6566

6667
type AdoptionHandler[K Object, A Adoptable[A]] struct {
67-
libctrl.HandlerControlContext
68+
queue.OperationsContext
6869

6970
// ControllerFieldManager is the value to use when adopting the object
7071
// for visibility by the controller
@@ -76,13 +77,13 @@ type AdoptionHandler[K Object, A Adoptable[A]] struct {
7677
ControllerFieldManager string
7778

7879
// AdopteeCtx tells the handler how to fetch the adoptee from context
79-
AdopteeCtx libctrl.MustValueContext[types.NamespacedName]
80+
AdopteeCtx typedctx.MustValueContext[types.NamespacedName]
8081

8182
// OwnerCtx tells the handler how to fetch the owner from context
82-
OwnerCtx libctrl.MustValueContext[types.NamespacedName]
83+
OwnerCtx typedctx.MustValueContext[types.NamespacedName]
8384

8485
// AdoptedCtx will store the object after it has been adopted
85-
AdoptedCtx libctrl.SettableContext[K]
86+
AdoptedCtx typedctx.SettableContext[K]
8687

8788
// ObjectAdoptedFunc is called when an adoption was performed
8889
ObjectAdoptedFunc func(ctx context.Context, obj K)

adopt/owners.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
// OwnerKeysFromMeta returns a set of namespace/name keys for the owners
1212
// of adopted objects with `annotationPrefix`. The namespace is always set
1313
// to the namespace of the object passed in.
14-
func OwnerKeysFromMeta(annotationPrefix string) func(in interface{}) ([]string, error) {
15-
return func(in interface{}) ([]string, error) {
14+
func OwnerKeysFromMeta(annotationPrefix string) func(in any) ([]string, error) {
15+
return func(in any) ([]string, error) {
1616
obj := in.(runtime.Object)
1717
objMeta, err := meta.Accessor(obj)
1818
if err != nil {

component.go renamed to component/component.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package libctrl
1+
package component
22

33
import (
44
"context"
@@ -9,6 +9,7 @@ import (
99
"k8s.io/apimachinery/pkg/runtime"
1010
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
1111

12+
"github.com/authzed/controller-idioms/hash"
1213
"github.com/authzed/controller-idioms/typed"
1314
)
1415

@@ -61,11 +62,11 @@ func (c *Component[K]) List(ctx context.Context, indexValue fmt.Stringer) (out [
6162
// previous configuration the controller wrote
6263
type HashableComponent[K KubeObject] struct {
6364
*Component[K]
64-
ObjectHasher
65+
hash.ObjectHasher
6566
HashAnnotationKey string
6667
}
6768

68-
func NewHashableComponent[K KubeObject](component *Component[K], hasher ObjectHasher, key string) *HashableComponent[K] {
69+
func NewHashableComponent[K KubeObject](component *Component[K], hasher hash.ObjectHasher, key string) *HashableComponent[K] {
6970
return &HashableComponent[K]{
7071
Component: component,
7172
ObjectHasher: hasher,

component/component_handler.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package component
2+
3+
import (
4+
"context"
5+
6+
"k8s.io/apimachinery/pkg/types"
7+
8+
"github.com/authzed/controller-idioms/handler"
9+
"github.com/authzed/controller-idioms/typedctx"
10+
)
11+
12+
// ContextHandler fills the value for a Key with the result of
13+
// fetching a component.
14+
type ContextHandler[K KubeObject] struct {
15+
owner typedctx.MustValueContext[types.NamespacedName]
16+
ctxKey typedctx.SettableContext[[]K]
17+
component *Component[K]
18+
next handler.ContextHandler
19+
}
20+
21+
func NewComponentContextHandler[K KubeObject](contextKey typedctx.SettableContext[[]K], component *Component[K], owner typedctx.MustValueContext[types.NamespacedName], next handler.ContextHandler) *ContextHandler[K] {
22+
return &ContextHandler[K]{
23+
owner: owner,
24+
ctxKey: contextKey,
25+
component: component,
26+
next: next,
27+
}
28+
}
29+
30+
func (h *ContextHandler[K]) Handle(ctx context.Context) {
31+
ctx = h.ctxKey.WithValue(ctx, h.component.List(ctx, h.owner.MustValue(ctx)))
32+
h.next.Handle(ctx)
33+
}

ensure_component.go renamed to component/ensure_component.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package libctrl
1+
package component
22

33
import (
44
"context"
@@ -8,6 +8,8 @@ import (
88
applycorev1 "k8s.io/client-go/applyconfigurations/core/v1"
99

1010
"github.com/authzed/controller-idioms/handler"
11+
"github.com/authzed/controller-idioms/queue"
12+
"github.com/authzed/controller-idioms/typedctx"
1113
)
1214

1315
type Annotator[T any] interface {
@@ -16,8 +18,8 @@ type Annotator[T any] interface {
1618

1719
type EnsureComponentByHash[K KubeObject, A Annotator[A]] struct {
1820
*HashableComponent[K]
19-
ctrls *ContextKey[ControlAll]
20-
nn MustValueContext[types.NamespacedName]
21+
ctrls *typedctx.Key[queue.Interface]
22+
nn typedctx.MustValueContext[types.NamespacedName]
2123
applyObject func(ctx context.Context, apply A) (K, error)
2224
deleteObject func(ctx context.Context, nn types.NamespacedName) error
2325
newObj func(ctx context.Context) A
@@ -27,8 +29,8 @@ var _ handler.ContextHandler = &EnsureComponentByHash[*corev1.Service, *applycor
2729

2830
func NewEnsureComponentByHash[K KubeObject, A Annotator[A]](
2931
component *HashableComponent[K],
30-
owner MustValueContext[types.NamespacedName],
31-
ctrls *ContextKey[ControlAll],
32+
owner typedctx.MustValueContext[types.NamespacedName],
33+
ctrls *typedctx.Key[queue.Interface],
3234
applyObj func(ctx context.Context, apply A) (K, error),
3335
deleteObject func(ctx context.Context, nn types.NamespacedName) error,
3436
newObj func(ctx context.Context) A,

component_handler.go

-32
This file was deleted.

context.go

-137
This file was deleted.

0 commit comments

Comments
 (0)