Skip to content

Commit

Permalink
feat: handle CP refs in Consumer, ConsumerGroup and Vault reconcilers
Browse files Browse the repository at this point in the history
  • Loading branch information
czeslavo committed Nov 18, 2024
1 parent 3ebfbe0 commit 47aa9a9
Show file tree
Hide file tree
Showing 7 changed files with 398 additions and 26 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ require (
github.com/jpillora/backoff v1.0.0
github.com/kong/go-database-reconciler v1.15.0
github.com/kong/go-kong v0.59.1
github.com/kong/kubernetes-configuration v0.0.44
github.com/kong/kubernetes-configuration v0.0.46
github.com/kong/kubernetes-telemetry v0.1.7
github.com/kong/kubernetes-testing-framework v0.47.2
github.com/lithammer/dedent v1.1.0
Expand Down Expand Up @@ -209,7 +209,7 @@ require (
golang.org/x/term v0.26.0 // indirect
golang.org/x/text v0.20.0 // indirect
golang.org/x/time v0.8.0 // indirect
golang.org/x/tools v0.24.0 // indirect
golang.org/x/tools v0.26.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20241104194629-dd2ea8efbc28 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20241104194629-dd2ea8efbc28 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ github.com/kong/go-database-reconciler v1.15.0 h1:5F5Zzp2H14aiDmqWUCaU4+LGR/lGnv
github.com/kong/go-database-reconciler v1.15.0/go.mod h1:T5BkBw13PZWub3y2jKAoM7fYD+UmXp2iNqj1YqD0L90=
github.com/kong/go-kong v0.59.1 h1:AJZtyCD+Zyqe/mF/m+x3/qN/GPVxAH7jq9zGJTHRfjc=
github.com/kong/go-kong v0.59.1/go.mod h1:8Vt6HmtgLNgL/7bSwAlz3DIWqBtzG7qEt9+OnMiQOa0=
github.com/kong/kubernetes-configuration v0.0.44 h1:85q8PugPeWQ7AKvEeGXxDoa+XCzTdNMj1qFcHyxHHds=
github.com/kong/kubernetes-configuration v0.0.44/go.mod h1:ym++Oygj/wZjaCanK8a+mjZ1jttPF7gPP1OBV0aqI00=
github.com/kong/kubernetes-configuration v0.0.46 h1:dIxVu9dOtGi9aY2prTlQ1CkiSu8Fk/0oal9m9iiUdSk=
github.com/kong/kubernetes-configuration v0.0.46/go.mod h1:Bk0H032d+aPgVYakc7C9Zo5nLwiXXm9thKUWF8vvisA=
github.com/kong/kubernetes-telemetry v0.1.7 h1:R4NUpvbF5uZ+5kgSQsIcf/oulRBGQCHsffFRDE4wxV4=
github.com/kong/kubernetes-telemetry v0.1.7/go.mod h1:USy5pcD1+Mm9NtKuz3Pb/rSx71VN76gHCFhdbAB4/lg=
github.com/kong/kubernetes-testing-framework v0.47.2 h1:+2Z9anTpbV/hwNeN+NFQz53BMU+g3QJydkweBp3tULo=
Expand Down Expand Up @@ -539,8 +539,8 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.24.0 h1:J1shsA93PJUEVaUSaay7UXAyE8aimq3GW0pjlolpa24=
golang.org/x/tools v0.24.0/go.mod h1:YhNqVBIfWHdzvTLs0d8LCuMhkKUgSUKldakyV7W/WDQ=
golang.org/x/tools v0.26.0 h1:v/60pFQmzmT9ExmjDv2gGIfi3OqfKoEP6I5+umXlbnQ=
golang.org/x/tools v0.26.0/go.mod h1:TPVVj70c7JJ3WCazhD8OdXcZg/og+b9+tH/KxylGwH0=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
25 changes: 23 additions & 2 deletions hack/generators/controllers/networking/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ var inputControllersNeeded = &typesNeeded{
ProgrammedCondition: ProgrammedConditionConfiguration{
UpdatesEnabled: true,
},
HasControlPlaneReference: true,
},
typeNeeded{
Group: "configuration.konghq.com",
Expand All @@ -181,6 +182,7 @@ var inputControllersNeeded = &typesNeeded{
AcceptsIngressClassNameSpec: false,
NeedsUpdateReferences: true,
RBACVerbs: []string{"get", "list", "watch"},
HasControlPlaneReference: true,
},
typeNeeded{
Group: "configuration.konghq.com",
Expand Down Expand Up @@ -263,6 +265,7 @@ var inputControllersNeeded = &typesNeeded{
},
AcceptsIngressClassNameAnnotation: true,
RBACVerbs: []string{"get", "list", "watch"},
HasControlPlaneReference: true,
},
typeNeeded{
Group: "configuration.konghq.com",
Expand Down Expand Up @@ -414,6 +417,11 @@ type typeNeeded struct {
// NeedUpdateReferences is true if we need to update the reference relationships
// between reconciled object and other objects.
NeedsUpdateReferences bool

// HasControlPlaneReference is true if the object's spec has a control plane reference.
// If true, the controller will only reconcile the object if the control plane reference is set to 'kic' or
// is left empty.
HasControlPlaneReference bool
}

type ProgrammedConditionConfiguration struct {
Expand Down Expand Up @@ -560,16 +568,29 @@ func (r *{{.PackageAlias}}{{.Kind}}Reconciler) SetupWithManager(mgr ctrl.Manager
}
{{- end}}
{{- if .AcceptsIngressClassNameAnnotation}}
{{- if .HasControlPlaneReference }}
cpRefPredicate := ctrlutils.GenerateCPReferenceMatchesPredicate[*{{.PackageImportAlias}}.{{.Kind}}]()
{{- end}}
if !r.DisableIngressClassLookups {
blder.Watches(&netv1.IngressClass{},
handler.EnqueueRequestsFromMapFunc(r.listClassless),
builder.WithPredicates(predicate.NewPredicateFuncs(ctrlutils.IsDefaultIngressClass)),
builder.WithPredicates(
predicate.NewPredicateFuncs(ctrlutils.IsDefaultIngressClass),
{{- if .HasControlPlaneReference }}
cpRefPredicate,
{{- end}}
),
)
}
preds := ctrlutils.GeneratePredicateFuncsForIngressClassFilter(r.IngressClassName)
return blder.Watches(&{{.PackageImportAlias}}.{{.Kind}}{},
&handler.EnqueueRequestForObject{},
builder.WithPredicates(preds),
builder.WithPredicates(
preds,
{{- if .HasControlPlaneReference }}
cpRefPredicate,
{{- end}}
),
).
Complete(r)
{{- else}}
Expand Down
81 changes: 63 additions & 18 deletions internal/controllers/configuration/zz_generated_controllers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions internal/controllers/utils/control_plane_reference.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package utils

import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/predicate"

kongv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1"
)

// ObjectWithControlPlaneRef is an interface that represents an object that has a control plane reference.
type ObjectWithControlPlaneRef interface {
GetControlPlaneRef() *kongv1alpha1.ControlPlaneRef
}

// GenerateCPReferenceMatchesPredicate generates a predicate function that filters out objects that have a control plane
// reference set to a value other than 'kic'.
func GenerateCPReferenceMatchesPredicate[T ObjectWithControlPlaneRef]() predicate.Predicate {
return predicate.NewPredicateFuncs(func(o client.Object) bool {
c, ok := o.(T)
if !ok {
return false
}
if cpRef := c.GetControlPlaneRef(); cpRef != nil {
// If the cpRef is set, reconcile the object only if it is set explicitly to 'kic'.
return cpRef.Type == kongv1alpha1.ControlPlaneRefKIC
}
// If there's no cpRef set, we should reconcile it as by default it's 'kic'.
return true
})
}
Loading

0 comments on commit 47aa9a9

Please sign in to comment.