Skip to content

Commit

Permalink
fix build errors caused by upgrading controller-runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
Abdiramen committed Jan 8, 2024
1 parent e2f76dd commit 5bcbad9
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 34 deletions.
17 changes: 13 additions & 4 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ import (
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/webhook"

"github.com/ngrok/ngrok-api-go/v5"

Expand Down Expand Up @@ -150,16 +153,22 @@ func runController(ctx context.Context, opts managerOpts) error {

ngrokClientset := ngrokapi.NewClientSet(ngrokClientConfig)
options := ctrl.Options{
Scheme: scheme,
MetricsBindAddress: opts.metricsAddr,
Port: 9443,
Scheme: scheme,
Metrics: server.Options{
BindAddress: opts.metricsAddr,
},
WebhookServer: webhook.NewServer(webhook.Options{Port: 9443}),
HealthProbeBindAddress: opts.probeAddr,
LeaderElection: opts.electionID != "",
LeaderElectionID: opts.electionID,
}

if opts.watchNamespace != "" {
options.Namespace = opts.watchNamespace
options.Cache = cache.Options{
DefaultNamespaces: map[string]cache.Config{
opts.watchNamespace: {},
},
}
}

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), options)
Expand Down
3 changes: 1 addition & 2 deletions internal/controller/ingress/ingress_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/source"
)

// This implements the Reconciler for the controller-runtime
Expand Down Expand Up @@ -42,7 +41,7 @@ func (r *IngressReconciler) SetupWithManager(mgr ctrl.Manager) error {
builder := ctrl.NewControllerManagedBy(mgr).For(&netv1.Ingress{})
for _, obj := range storedResources {
builder = builder.Watches(
&source.Kind{Type: obj},
obj,
store.NewUpdateStoreHandler(obj.GetObjectKind().GroupVersionKind().Kind, r.Driver))
}

Expand Down
15 changes: 7 additions & 8 deletions internal/controller/ingress/tcpedge_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,11 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
"k8s.io/utils/pointer"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"

"github.com/go-logr/logr"
ingressv1alpha1 "github.com/ngrok/kubernetes-ingress-controller/api/ingress/v1alpha1"
Expand Down Expand Up @@ -80,7 +79,7 @@ func (r *TCPEdgeReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&ingressv1alpha1.TCPEdge{}).
Watches(
&source.Kind{Type: &ingressv1alpha1.IPPolicy{}},
&ingressv1alpha1.IPPolicy{},
handler.EnqueueRequestsFromMapFunc(r.listTCPEdgesForIPPolicy),
).
Complete(r)
Expand Down Expand Up @@ -162,8 +161,8 @@ func (r *TCPEdgeReconciler) update(ctx context.Context, edge *ingressv1alpha1.TC
!slices.Equal(resp.Hostports, edge.Status.Hostports) {
resp, err = r.NgrokClientset.TCPEdges().Update(ctx, &ngrok.TCPEdgeUpdate{
ID: resp.ID,
Description: pointer.String(edge.Spec.Description),
Metadata: pointer.String(edge.Spec.Metadata),
Description: ptr.To(edge.Spec.Description),
Metadata: ptr.To(edge.Spec.Metadata),
Hostports: edge.Status.Hostports,
Backend: &ngrok.EndpointBackendMutate{
BackendID: edge.Status.Backend.ID,
Expand Down Expand Up @@ -205,8 +204,8 @@ func (r *TCPEdgeReconciler) reconcileTunnelGroupBackend(ctx context.Context, edg
if !maps.Equal(backend.Labels, specBackend.Labels) {
_, err = r.NgrokClientset.TunnelGroupBackends().Update(ctx, &ngrok.TunnelGroupBackendUpdate{
ID: backend.ID,
Metadata: pointer.String(specBackend.Metadata),
Description: pointer.String(specBackend.Description),
Metadata: ptr.To(specBackend.Metadata),
Description: ptr.To(specBackend.Description),
Labels: specBackend.Labels,
})
if err != nil {
Expand Down Expand Up @@ -345,7 +344,7 @@ func (r *TCPEdgeReconciler) updateIPRestrictionModule(ctx context.Context, edge
return err
}

func (r *TCPEdgeReconciler) listTCPEdgesForIPPolicy(obj client.Object) []reconcile.Request {
func (r *TCPEdgeReconciler) listTCPEdgesForIPPolicy(ctx context.Context, obj client.Object) []reconcile.Request {
r.Log.Info("Listing TCPEdges for ip policy to determine if they need to be reconciled")
policy, ok := obj.(*ingressv1alpha1.IPPolicy)
if !ok {
Expand Down
15 changes: 7 additions & 8 deletions internal/controller/ingress/tlsedge_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,11 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
"k8s.io/utils/pointer"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"

"github.com/go-logr/logr"
ingressv1alpha1 "github.com/ngrok/kubernetes-ingress-controller/api/ingress/v1alpha1"
Expand Down Expand Up @@ -90,7 +89,7 @@ func (r *TLSEdgeReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&ingressv1alpha1.TLSEdge{}).
Watches(
&source.Kind{Type: &ingressv1alpha1.IPPolicy{}},
&ingressv1alpha1.IPPolicy{},
handler.EnqueueRequestsFromMapFunc(r.listTLSEdgesForIPPolicy),
).
Complete(r)
Expand Down Expand Up @@ -165,8 +164,8 @@ func (r *TLSEdgeReconciler) update(ctx context.Context, edge *ingressv1alpha1.TL
!slices.Equal(resp.Hostports, edge.Status.Hostports) {
resp, err = r.NgrokClientset.TLSEdges().Update(ctx, &ngrok.TLSEdgeUpdate{
ID: resp.ID,
Description: pointer.String(edge.Spec.Description),
Metadata: pointer.String(edge.Spec.Metadata),
Description: ptr.To(edge.Spec.Description),
Metadata: ptr.To(edge.Spec.Metadata),
Hostports: edge.Spec.Hostports,
Backend: &ngrok.EndpointBackendMutate{
BackendID: edge.Status.Backend.ID,
Expand Down Expand Up @@ -229,8 +228,8 @@ func (r *TLSEdgeReconciler) reconcileTunnelGroupBackend(ctx context.Context, edg
if !maps.Equal(backend.Labels, specBackend.Labels) {
_, err = r.NgrokClientset.TunnelGroupBackends().Update(ctx, &ngrok.TunnelGroupBackendUpdate{
ID: backend.ID,
Metadata: pointer.String(specBackend.Metadata),
Description: pointer.String(specBackend.Description),
Metadata: ptr.To(specBackend.Metadata),
Description: ptr.To(specBackend.Description),
Labels: specBackend.Labels,
})
if err != nil {
Expand Down Expand Up @@ -356,7 +355,7 @@ func (r *TLSEdgeReconciler) updateIPRestrictionModule(ctx context.Context, edge
return err
}

func (r *TLSEdgeReconciler) listTLSEdgesForIPPolicy(obj client.Object) []reconcile.Request {
func (r *TLSEdgeReconciler) listTLSEdgesForIPPolicy(ctx context.Context, obj client.Object) []reconcile.Request {
r.Log.Info("Listing TLSEdges for ip policy to determine if they need to be reconciled")
policy, ok := obj.(*ingressv1alpha1.IPPolicy)
if !ok {
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/ingress/tunnel_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (r *TunnelReconciler) SetupWithManager(mgr ctrl.Manager) error {
cont = NonLeaderElectedController{cont}

if err := cont.Watch(
&source.Kind{Type: &ingressv1alpha1.Tunnel{}},
source.Kind(mgr.GetCache(), &ingressv1alpha1.Tunnel{}),
&handler.EnqueueRequestForObject{},
commonPredicateFilters,
); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/store/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -711,8 +711,8 @@ func (d *Driver) calculateTunnels() map[tunnelKey]ingressv1alpha1.Tunnel {
Name: ingress.Name,
UID: ingress.UID,
})
slices.SortStableFunc(tunnel.OwnerReferences, func(i, j metav1.OwnerReference) bool {
return i.UID < j.UID
slices.SortStableFunc(tunnel.OwnerReferences, func(i, j metav1.OwnerReference) int {
return strings.Compare(string(i.UID), string(j.UID))
})
}

Expand Down
10 changes: 5 additions & 5 deletions internal/store/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var _ = Describe("Driver", func() {

Describe("Seed", func() {
It("Should not error", func() {
err := driver.Seed(context.Background(), fake.NewFakeClientWithScheme(scheme))
err := driver.Seed(context.Background(), fake.NewClientBuilder().WithScheme(scheme).Build())
Expect(err).ToNot(HaveOccurred())
})
It("Should add all the found items to the store", func() {
Expand All @@ -52,7 +52,7 @@ var _ = Describe("Driver", func() {
e2 := NewHTTPSEdge("test-edge-2", "test-namespace", "test-domain-2.com")
obs := []runtime.Object{&ic1, &ic2, &i1, &i2, &d1, &d2, &e1, &e2}

c := fake.NewFakeClientWithScheme(scheme, obs...)
c := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(obs...).Build()
err := driver.Seed(context.Background(), c)
Expect(err).ToNot(HaveOccurred())

Expand All @@ -69,7 +69,7 @@ var _ = Describe("Driver", func() {
Describe("DeleteIngress", func() {
It("Should remove the ingress from the store", func() {
i1 := NewTestIngressV1("test-ingress", "test-namespace")
c := fake.NewFakeClientWithScheme(scheme, &i1)
c := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(&i1).Build()
err := driver.Seed(context.Background(), c)
Expect(err).ToNot(HaveOccurred())

Expand All @@ -89,7 +89,7 @@ var _ = Describe("Driver", func() {
Describe("Sync", func() {
Context("When there are no ingresses in the store", func() {
It("Should not create anything or error", func() {
c := fake.NewFakeClientWithScheme(scheme)
c := fake.NewClientBuilder().WithScheme(scheme).Build()
err := driver.Sync(context.Background(), c)
Expect(err).ToNot(HaveOccurred())

Expand Down Expand Up @@ -117,7 +117,7 @@ var _ = Describe("Driver", func() {
ic2 := NewTestIngressClass("test-ingress-class-2", true, true)
s := NewTestServiceV1("example", "test-namespace")
obs := []runtime.Object{&ic1, &ic2, &i1, &i2, &s}
c := fake.NewFakeClientWithScheme(scheme, obs...)
c := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(obs...).Build()

for _, obj := range obs {
err := driver.store.Update(obj)
Expand Down
10 changes: 6 additions & 4 deletions internal/store/updatestorehandler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package store

import (
"context"

"github.com/go-logr/logr"
"k8s.io/client-go/util/workqueue"
"sigs.k8s.io/controller-runtime/pkg/event"
Expand Down Expand Up @@ -29,23 +31,23 @@ func NewUpdateStoreHandler(resourceName string, d *Driver) *UpdateStoreHandler {
}

// Create is called in response to an create event - e.g. Edge Creation.
func (e *UpdateStoreHandler) Create(evt event.CreateEvent, q workqueue.RateLimitingInterface) {
func (e *UpdateStoreHandler) Create(ctx context.Context, evt event.CreateEvent, q workqueue.RateLimitingInterface) {
if err := e.store.Update(evt.Object); err != nil {
e.log.Error(err, "error updating object in create", "object", evt.Object)
return
}
}

// Update is called in response to an update event - e.g. Edge Updated.
func (e *UpdateStoreHandler) Update(evt event.UpdateEvent, q workqueue.RateLimitingInterface) {
func (e *UpdateStoreHandler) Update(ctx context.Context, evt event.UpdateEvent, q workqueue.RateLimitingInterface) {
if err := e.store.Update(evt.ObjectNew); err != nil {
e.log.Error(err, "error updating object in update", "object", evt.ObjectNew)
return
}
}

// Delete is called in response to a delete event - e.g. Edge Deleted.
func (e *UpdateStoreHandler) Delete(evt event.DeleteEvent, q workqueue.RateLimitingInterface) {
func (e *UpdateStoreHandler) Delete(ctx context.Context, evt event.DeleteEvent, q workqueue.RateLimitingInterface) {
if err := e.store.Delete(evt.Object); err != nil {
e.log.Error(err, "error deleting object", "object", evt.Object)
return
Expand All @@ -54,7 +56,7 @@ func (e *UpdateStoreHandler) Delete(evt event.DeleteEvent, q workqueue.RateLimit

// Generic is called in response to an event of an unknown type or a synthetic event triggered as a cron or
// external trigger request
func (e *UpdateStoreHandler) Generic(evt event.GenericEvent, q workqueue.RateLimitingInterface) {
func (e *UpdateStoreHandler) Generic(ctx context.Context, evt event.GenericEvent, q workqueue.RateLimitingInterface) {
if err := e.store.Update(evt.Object); err != nil {
e.log.Error(err, "error updating object in generic", "object", evt.Object)
return
Expand Down

0 comments on commit 5bcbad9

Please sign in to comment.