Files
ngrok-operator/internal/controller/util.go
T
Jonathan Stacks 92b135c9c2 fix: Load Balancer service status not working when using default mapping strategy (#693)
* refactor(service-controller): Move to own directory

Signed-off-by: Jonathan Stacks <jonstacks@users.noreply.github.com>

* feat: Add TCP Addresses mock client

Signed-off-by: Jonathan Stacks <jonstacks@users.noreply.github.com>

* chore: Update boilerplate.go.txt

Signed-off-by: Jonathan Stacks <jonstacks@users.noreply.github.com>

* chore: Add helper function in common types

Signed-off-by: Jonathan Stacks <jonstacks@users.noreply.github.com>

* test: Add kginkgo helpers

Signed-off-by: Jonathan Stacks <jonstacks@users.noreply.github.com>

* test: Add more tests for LB services

In doing so, migrate to envtest to test the controller. Also fix a bug while we are at it

Signed-off-by: Jonathan Stacks <jonstacks@users.noreply.github.com>

* docs: Add a spec for the service controller

Signed-off-by: Jonathan Stacks <jonstacks@users.noreply.github.com>

---------

Signed-off-by: Jonathan Stacks <jonstacks@users.noreply.github.com>
2025-10-24 18:38:36 +00:00

63 lines
1.3 KiB
Go

package controller
import (
"context"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
const (
FinalizerName = "k8s.ngrok.com/finalizer"
)
func IsUpsert(o client.Object) bool {
return o.GetDeletionTimestamp().IsZero()
}
func IsDelete(o client.Object) bool {
return !o.GetDeletionTimestamp().IsZero()
}
func HasFinalizer(o client.Object) bool {
return controllerutil.ContainsFinalizer(o, FinalizerName)
}
func AddFinalizer(o client.Object) bool {
return controllerutil.AddFinalizer(o, FinalizerName)
}
func RemoveFinalizer(o client.Object) bool {
return controllerutil.RemoveFinalizer(o, FinalizerName)
}
func RegisterAndSyncFinalizer(ctx context.Context, c client.Writer, o client.Object) error {
if !HasFinalizer(o) {
AddFinalizer(o)
return c.Update(ctx, o)
}
return nil
}
func RemoveAndSyncFinalizer(ctx context.Context, c client.Writer, o client.Object) error {
RemoveFinalizer(o)
return c.Update(ctx, o)
}
func AddAnnotations(o client.Object, annotations map[string]string) {
if o == nil || annotations == nil {
return
}
existing := o.GetAnnotations()
if existing == nil {
existing = make(map[string]string)
}
for k, v := range annotations {
existing[k] = v
}
o.SetAnnotations(existing)
}