Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ dist: jammy

# Go version for Travis (fvt)
go:
- "1.18.5"
- "1.19.11"

git:
depth: 9999
Expand Down
14 changes: 7 additions & 7 deletions Makefile.env
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
REGISTRY_REPO?=quay.io/example/static-route-operator
KUBECONFIG?=$$HOME/.kube/config
OP_SDK_RELEASE_VERSION?=1.23.0
GOLANGCI_LINT_VERSION?=1.49.0
GOSEC_VERSION?=2.13.1
KIND_VERSION?=0.11.1
KUBECTL_VERSION?=1.22.2
OP_SDK_RELEASE_VERSION?=1.31.0
GOLANGCI_LINT_VERSION?=1.54.2
GOSEC_VERSION?=2.17.0
KIND_VERSION?=0.20.0
KUBECTL_VERSION?=1.28.1
INSTALL_LOCATION?=/usr/local/bin
GO_BUILDER_IMAGE?=registry.ci.openshift.org/openshift/release:golang-1.18
CONTROLLER_GEN_VERSION=0.9.2
GO_BUILDER_IMAGE?=registry.ci.openshift.org/openshift/release:golang-1.19
CONTROLLER_GEN_VERSION=0.13.0
8 changes: 6 additions & 2 deletions Makefile.sdk
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,13 @@ build: generate fmt vet ## Build manager binary.
run: manifests generate fmt vet ## Run a controller from your host.
go run ./main.go

docker-build: test ## Build docker image with the manager.
docker buildx build --platform linux/amd64 --progress=plain -t ${IMG}-amd64 --build-arg BUILDER_IMAGE=$(GO_BUILDER_IMAGE) --build-arg ARCH=amd64 --load .
docker-build-s390x:
ifdef TRAVIS
docker buildx build --platform linux/s390x --progress=plain -t ${IMG}-s390x --build-arg BUILDER_IMAGE=$(GO_BUILDER_IMAGE) --build-arg ARCH=s390x --load .
endif

docker-build: test docker-build-s390x ## Build docker image with the manager.
docker buildx build --platform linux/amd64 --progress=plain -t ${IMG}-amd64 --build-arg BUILDER_IMAGE=$(GO_BUILDER_IMAGE) --build-arg ARCH=amd64 --load .
docker images

docker-push: ## Push docker image with the manager.
Expand Down
9 changes: 7 additions & 2 deletions controllers/node/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,20 @@ func (m reconcileImplClientMock) Status() client.StatusWriter {
}

type statusWriterMock struct {
createErr error
updateErr error
patchErr error
}

func (m statusWriterMock) Update(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error {
func (m statusWriterMock) Create(ctx context.Context, obj, subResource client.Object, opts ...client.SubResourceCreateOption) error {
return m.createErr
}

func (m statusWriterMock) Update(ctx context.Context, obj client.Object, opts ...client.SubResourceUpdateOption) error {
return m.updateErr
}

func (m statusWriterMock) Patch(context.Context, client.Object, client.Patch, ...client.PatchOption) error {
func (m statusWriterMock) Patch(context.Context, client.Object, client.Patch, ...client.SubResourcePatchOption) error {
return m.patchErr
}

Expand Down
3 changes: 1 addition & 2 deletions controllers/node/node_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
)

var log = logf.Log.WithName("controller_node")
Expand All @@ -51,7 +50,7 @@ func (r *NodeReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
Named("node-controller").
For(&corev1.Node{}).
Watches(&source.Kind{Type: &corev1.Node{}}, &handler.EnqueueRequestForObject{}).
Watches(&corev1.Node{}, &handler.EnqueueRequestForObject{}).
WithEventFilter(
&predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool {
Expand Down
29 changes: 22 additions & 7 deletions controllers/staticroute/mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (
type reconcileImplClientMock struct {
client reconcileImplClient
statusWriteMock client.StatusWriter
postfixGet func(runtime.Object)
getErr error
updateErr error
listErr error
Expand All @@ -43,9 +42,7 @@ func (m reconcileImplClientMock) Get(ctx context.Context, key client.ObjectKey,
if m.getErr != nil {
return m.getErr
}
if m.postfixGet != nil {
defer m.postfixGet(obj)
}

return m.client.Get(ctx, key, obj, options...)
}

Expand All @@ -72,21 +69,31 @@ func (m reconcileImplClientMock) Status() client.StatusWriter {

type statusWriterMock struct {
client client.Client
createCounter int
updateCounter int
patchCounter int
createErr error
updateErr error
patchErr error
}

func (m *statusWriterMock) Update(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error {
func (m *statusWriterMock) Create(ctx context.Context, obj client.Object, subResource client.Object, createOption ...client.SubResourceCreateOption) error {
m.createCounter = m.createCounter + 1
if m.client != nil {
return m.client.Status().Create(ctx, obj, subResource, createOption...)
}
return m.createErr
}

func (m *statusWriterMock) Update(ctx context.Context, obj client.Object, opts ...client.SubResourceUpdateOption) error {
m.updateCounter = m.updateCounter + 1
if m.client != nil {
return m.client.Status().Update(ctx, obj, opts...)
}
return m.updateErr
}

func (m *statusWriterMock) Patch(ctx context.Context, obj client.Object, patch client.Patch, patchOption ...client.PatchOption) error {
func (m *statusWriterMock) Patch(ctx context.Context, obj client.Object, patch client.Patch, patchOption ...client.SubResourcePatchOption) error {
m.patchCounter = m.patchCounter + 1
if m.client != nil {
return m.client.Status().Patch(ctx, obj, patch, patchOption...)
Expand Down Expand Up @@ -131,7 +138,11 @@ func newFakeClient(route *staticroutev1.StaticRoute) client.Client {
s.AddKnownTypes(staticroutev1.GroupVersion, route)
nodes := &corev1.NodeList{}
s.AddKnownTypes(corev1.SchemeGroupVersion, nodes)
return fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects([]runtime.Object{route}...).Build()
return fake.NewClientBuilder().
WithScheme(s).
WithStatusSubresource(route).
WithRuntimeObjects([]runtime.Object{route}...).
Build()
}

func newReconcileImplParams(client reconcileImplClient) *reconcileImplParams {
Expand All @@ -149,6 +160,10 @@ func newReconcileImplParams(client reconcileImplClient) *reconcileImplParams {

func newStaticRouteWithValues(withSpec, withStatus bool) *staticroutev1.StaticRoute {
route := staticroutev1.StaticRoute{
TypeMeta: metav1.TypeMeta{
Kind: "StaticRoute",
APIVersion: "static-route.ibm.com/v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "CR",
Namespace: "default",
Expand Down
18 changes: 7 additions & 11 deletions controllers/staticroute/staticroute_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,12 @@ import (
k8stypes "k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
)

var (
Expand Down Expand Up @@ -241,25 +239,23 @@ func reconcileImpl(params reconcileImplParams) (res *reconcile.Result, err error
// SetupWithManager sets up the controller with the Manager.
func (r *StaticRouteReconciler) SetupWithManager(mgr ctrl.Manager) error {
// Watch for changes to primary resource StaticRoute
staticrouteWatcherBuilder := ctrl.NewControllerManagedBy(mgr).Named("staticroute-controller").WithOptions(controller.Options{Reconciler: r})
err := staticrouteWatcherBuilder.
err := ctrl.NewControllerManagedBy(mgr).Named("staticroute-controller").
For(&staticroutev1.StaticRoute{}).
Watches(&source.Kind{Type: &staticroutev1.StaticRoute{}}, &handler.EnqueueRequestForObject{}).
Watches(&staticroutev1.StaticRoute{}, &handler.EnqueueRequestForObject{}).
Complete(r)
if err != nil {
return err
}

// Watch if the self node labels are changed, so reconcile every route
nodeWatcherBuilder := ctrl.NewControllerManagedBy(mgr).Named("staticroute-controller").WithOptions(controller.Options{Reconciler: r})
err = nodeWatcherBuilder.
err = ctrl.NewControllerManagedBy(mgr).Named("staticroute-controller").
For(&corev1.Node{ObjectMeta: metav1.ObjectMeta{Name: r.options.Hostname}}).
Watches(
&source.Kind{Type: &corev1.Node{ObjectMeta: metav1.ObjectMeta{Name: r.options.Hostname}}},
&corev1.Node{ObjectMeta: metav1.ObjectMeta{Name: r.options.Hostname}},
handler.EnqueueRequestsFromMapFunc(
func(a client.Object) []reconcile.Request {
func(ctx context.Context, a client.Object) []reconcile.Request {
routes := &staticroutev1.StaticRouteList{}
if err := r.client.List(context.Background(), routes); err != nil {
if err := r.client.List(ctx, routes); err != nil {
log.Error(err, "Failed to List StaticRoute CRs")
return nil
}
Expand All @@ -269,7 +265,7 @@ func (r *StaticRouteReconciler) SetupWithManager(mgr ctrl.Manager) error {
result = append(result, reconcile.Request{
NamespacedName: k8stypes.NamespacedName{
Name: route.GetName(),
Namespace: "",
Namespace: route.GetNamespace(),
},
})
}
Expand Down
Loading