Skip to content

Commit

Permalink
Update controller-runtime to v0.14.4
Browse files Browse the repository at this point in the history
controller-runtime deprecated the envtest/printer package in v0.14.0. They don't
needed anymore since they upgraded to ginko v2. In order to keep the
scope contained, I just copied the code we needed, it's quite simple.
This allow us to maintain the same version of ginko while bumping
controller-runtime.

The fake client provided by controller-runtime was updated to allow
for field selectors in List operations. This requires to define the
index prior to its use. Since the Job controller uses
".metadata.controller" for a List query, we now set this index in the
tests client.
kubernetes-sigs/controller-runtime#2025

Signed-off-by: Guillermo Gaston <gaslor@amazon.com>
  • Loading branch information
g-gaston committed Feb 9, 2023
1 parent dee5b26 commit 4f25b88
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 287 deletions.
11 changes: 11 additions & 0 deletions controllers/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package controllers_test
import (
"github.com/go-logr/logr"
"github.com/go-logr/zapr"
bmcv1alpha1 "github.com/tinkerbell/rufio/api/v1alpha1"
rufiov1alpha1 "github.com/tinkerbell/rufio/api/v1alpha1"
"github.com/tinkerbell/rufio/controllers"
"go.uber.org/zap"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -36,6 +38,15 @@ func createKubeClientWithObjects(objects ...client.Object) client.WithWatch {
Build()
}

// createKubeClientWithObjectsForJobController creates a kubernetes client with the given objects
// and the indexes required by the Job controller.
func createKubeClientWithObjectsForJobController(objects ...client.Object) client.WithWatch {
return createKubeClientBuilder().
WithObjects(objects...).
WithIndex(&bmcv1alpha1.Task{}, ".metadata.controller", controllers.TaskOwnerIndexFunc).
Build()
}

// mustCreateLogr creates a logr.Logger implementation backed by a debug style sink. It panics
// if the logger fails to create.
func mustCreateLogr(name ...string) logr.Logger {
Expand Down
8 changes: 4 additions & 4 deletions controllers/job_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (r *JobReconciler) reconcile(ctx context.Context, job *bmcv1alpha1.Job, job
tasks := &bmcv1alpha1.TaskList{}
err = r.client.List(ctx, tasks, client.MatchingFields{jobOwnerKey: job.Name})
if err != nil {
return ctrl.Result{}, fmt.Errorf("failed to list owned Tasks for Job %s/%s", job.Namespace, job.Name)
return ctrl.Result{}, fmt.Errorf("failed to list owned Tasks for Job %s/%s: %v", job.Namespace, job.Name, err)
}

completedTasksCount := 0
Expand Down Expand Up @@ -230,7 +230,7 @@ func (r *JobReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager)
ctx,
&bmcv1alpha1.Task{},
jobOwnerKey,
taskOwnerIndexFunc,
TaskOwnerIndexFunc,
); err != nil {
return err
}
Expand All @@ -246,8 +246,8 @@ func (r *JobReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager)
Complete(r)
}

// taskOwnerIndexFunc is Indexer func which returns the owner name for obj.
func taskOwnerIndexFunc(obj client.Object) []string {
// TaskOwnerIndexFunc is Indexer func which returns the owner name for obj.
func TaskOwnerIndexFunc(obj client.Object) []string {
task, ok := obj.(*bmcv1alpha1.Task)
if !ok {
return nil
Expand Down
7 changes: 2 additions & 5 deletions controllers/job_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ func TestJobReconciler_TasklessJob(t *testing.T) {
secret := createSecret()
job := createJob("test", machine)

builder := createKubeClientBuilder()
builder.WithObjects(machine, secret, job)
kubeClient := builder.Build()
kubeClient := createKubeClientWithObjectsForJobController(machine, secret, job)

reconciler := controllers.NewJobReconciler(kubeClient, logger)

Expand Down Expand Up @@ -77,7 +75,6 @@ func TestJobReconciler_UnknownMachine(t *testing.T) {
}

func TestJobReconciler_Reconcile(t *testing.T) {

for name, action := range map[string]bmcv1alpha1.Action{
"PowerAction": {PowerAction: bmcv1alpha1.PowerOn.Ptr()},
"OneTimeBootDeviceAction": {
Expand All @@ -95,7 +92,7 @@ func TestJobReconciler_Reconcile(t *testing.T) {
job := createJob(name, machine)
job.Spec.Tasks = append(job.Spec.Tasks, action)

cluster := createKubeClientWithObjects(machine, secret, job)
cluster := createKubeClientWithObjectsForJobController(machine, secret, job)

request := reconcile.Request{
NamespacedName: types.NamespacedName{
Expand Down
33 changes: 30 additions & 3 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@ limitations under the License.
package controllers

import (
"fmt"
"path/filepath"
"testing"

. "github.com/onsi/ginkgo"
"github.com/onsi/ginkgo/config"
"github.com/onsi/ginkgo/types"
. "github.com/onsi/gomega"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

Expand All @@ -48,7 +50,7 @@ func TestAPIs(t *testing.T) {

RunSpecsWithDefaultAndCustomReporters(t,
"Controller Suite",
[]Reporter{printer.NewlineReporter{}})
[]Reporter{newlineReporter{}})
}

var _ = BeforeSuite(func() {
Expand All @@ -73,11 +75,36 @@ var _ = BeforeSuite(func() {
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())

}, 60)

var _ = AfterSuite(func() {
By("tearing down the test environment")
err := testEnv.Stop()
Expect(err).NotTo(HaveOccurred())
})

// newlineReporter is Reporter that Prints a newline after the default Reporter output so that the results
// are correctly parsed by test automation.
// See issue https://github.com/jstemmer/go-junit-report/issues/31
// This is copied from sigs.k8s.io/controller-runtime/pkg/envtest/printer since controller-runtime
// doesn't include this starting with version v0.14.0
type newlineReporter struct{}

// SpecSuiteWillBegin implements ginkgo.Reporter.
func (newlineReporter) SpecSuiteWillBegin(config config.GinkgoConfigType, summary *types.SuiteSummary) {
}

// BeforeSuiteDidRun implements ginkgo.Reporter.
func (newlineReporter) BeforeSuiteDidRun(setupSummary *types.SetupSummary) {}

// AfterSuiteDidRun implements ginkgo.Reporter.
func (newlineReporter) AfterSuiteDidRun(setupSummary *types.SetupSummary) {}

// SpecWillRun implements ginkgo.Reporter.
func (newlineReporter) SpecWillRun(specSummary *types.SpecSummary) {}

// SpecDidComplete implements ginkgo.Reporter.
func (newlineReporter) SpecDidComplete(specSummary *types.SpecSummary) {}

// SpecSuiteDidEnd Prints a newline between "35 Passed | 0 Failed | 0 Pending | 0 Skipped" and "--- PASS:".
func (newlineReporter) SpecSuiteDidEnd(summary *types.SuiteSummary) { fmt.Printf("\n") }
63 changes: 25 additions & 38 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,29 @@ require (
github.com/onsi/ginkgo v1.16.5
github.com/onsi/gomega v1.24.1
github.com/spf13/pflag v1.0.5
go.uber.org/zap v1.23.0
go.uber.org/zap v1.24.0
golang.org/x/tools v0.3.0
k8s.io/api v0.25.4
k8s.io/apimachinery v0.25.4
k8s.io/client-go v0.25.4
sigs.k8s.io/controller-runtime v0.13.1
k8s.io/api v0.26.1
k8s.io/apimachinery v0.26.1
k8s.io/client-go v0.26.1
sigs.k8s.io/controller-runtime v0.14.4
)

require (
cloud.google.com/go v0.97.0 // indirect
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
github.com/Azure/go-autorest/autorest v0.11.27 // indirect
github.com/Azure/go-autorest/autorest/adal v0.9.20 // indirect
github.com/Azure/go-autorest/autorest/date v0.3.0 // indirect
github.com/Azure/go-autorest/logger v0.2.1 // indirect
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/VictorLowther/simplexml v0.0.0-20180716164440-0bff93621230 // indirect
github.com/VictorLowther/soap v0.0.0-20150314151524-8e36fca84b22 // indirect
github.com/ammmze/go-amt v0.0.4 // indirect
github.com/ammmze/wsman v0.0.2 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bmc-toolbox/common v0.0.0-20221115135648-0b584f504396 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.8.0 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.20.0 // indirect
github.com/go-openapi/swag v0.19.14 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt/v4 v4.2.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
Expand All @@ -60,41 +48,40 @@ require (
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.12.2 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/prometheus/client_golang v1.14.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/satori/go.uuid v1.2.0 // indirect
github.com/stmcginnis/gofish v0.13.1-0.20221107140645-5cc43fad050f // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // indirect
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect
golang.org/x/mod v0.7.0 // indirect
golang.org/x/net v0.2.0 // indirect
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
golang.org/x/sys v0.2.0 // indirect
golang.org/x/term v0.2.0 // indirect
golang.org/x/text v0.4.0 // indirect
golang.org/x/time v0.0.0-20220609170525-579cf78fd858 // indirect
golang.org/x/net v0.3.1-0.20221206200815-1e63c2f08a10 // indirect
golang.org/x/oauth2 v0.0.0-20220223155221-ee480838109b // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/term v0.3.0 // indirect
golang.org/x/text v0.5.0 // indirect
golang.org/x/time v0.3.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apiextensions-apiserver v0.25.0 // indirect
k8s.io/component-base v0.25.0 // indirect
k8s.io/klog/v2 v2.70.1 // indirect
k8s.io/kube-openapi v0.0.0-20220803162953-67bda5d908f1 // indirect
k8s.io/utils v0.0.0-20220728103510-ee6ede2d64ed // indirect
k8s.io/apiextensions-apiserver v0.26.1 // indirect
k8s.io/component-base v0.26.1 // indirect
k8s.io/klog/v2 v2.80.1 // indirect
k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280 // indirect
k8s.io/utils v0.0.0-20221128185143-99ec85e7a448 // indirect
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
Expand Down
Loading

0 comments on commit 4f25b88

Please sign in to comment.