Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to controller-runtime v0.7.0 #1073

Merged
merged 13 commits into from
Jan 29, 2021
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion apis/addtoscheme_config_v1alpha1.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ import (

func init() {
// Register the types with the Scheme so the components can map objects to GroupVersionKinds and back
AddToSchemes = append(AddToSchemes, v1alpha1.SchemeBuilder.AddToScheme)
AddToSchemes = append(AddToSchemes, v1alpha1.AddToScheme)
}
2 changes: 1 addition & 1 deletion apis/addtoscheme_constrainttemplate_v1alpha1.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ import (

func init() {
// Register the types with the Scheme so the components can map objects to GroupVersionKinds and back
AddToSchemes = append(AddToSchemes, v1alpha1.SchemeBuilder.AddToScheme)
AddToSchemes = append(AddToSchemes, v1alpha1.AddToScheme)
}
2 changes: 1 addition & 1 deletion apis/addtoscheme_constrainttemplate_v1beta1.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ import (

func init() {
// Register the types with the Scheme so the components can map objects to GroupVersionKinds and back
AddToSchemes = append(AddToSchemes, v1beta1.SchemeBuilder.AddToScheme)
AddToSchemes = append(AddToSchemes, v1beta1.AddToScheme)
}
2 changes: 1 addition & 1 deletion apis/addtoscheme_customresourcedefinition_v1beta1.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ import (

func init() {
// Register the types with the Scheme so the components can map objects to GroupVersionKinds and back
AddToSchemes = append(AddToSchemes, apiextensionsv1beta1.SchemeBuilder.AddToScheme)
AddToSchemes = append(AddToSchemes, apiextensionsv1beta1.AddToScheme)
}
2 changes: 1 addition & 1 deletion apis/addtoscheme_status_v1beta1.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ import (

func init() {
// Register the types with the Scheme so the components can map objects to GroupVersionKinds and back
AddToSchemes = append(AddToSchemes, v1beta1.SchemeBuilder.AddToScheme)
AddToSchemes = append(AddToSchemes, v1beta1.AddToScheme)
}
2 changes: 1 addition & 1 deletion apis/status/v1beta1/constraintpodstatus_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestNewConstraintStatusForPod(t *testing.T) {
defer os.Unsetenv("POD_NAMESPACE")

scheme := runtime.NewScheme()
g.Expect(SchemeBuilder.AddToScheme(scheme)).NotTo(HaveOccurred())
g.Expect(AddToScheme(scheme)).NotTo(HaveOccurred())
g.Expect(corev1.AddToScheme(scheme)).NotTo(HaveOccurred())

pod := &corev1.Pod{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestNewConstraintTemplateStatusForPod(t *testing.T) {
defer os.Unsetenv("POD_NAMESPACE")

scheme := runtime.NewScheme()
g.Expect(SchemeBuilder.AddToScheme(scheme)).NotTo(HaveOccurred())
g.Expect(AddToScheme(scheme)).NotTo(HaveOccurred())
g.Expect(corev1.AddToScheme(scheme)).NotTo(HaveOccurred())

pod := &corev1.Pod{}
Expand Down
28 changes: 15 additions & 13 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,27 @@ require (
contrib.go.opencensus.io/exporter/prometheus v0.1.0
github.com/davecgh/go-spew v1.1.1
github.com/ghodss/yaml v1.0.0
github.com/go-logr/logr v0.1.0
github.com/go-logr/zapr v0.1.0
github.com/google/go-cmp v0.5.0
github.com/onsi/ginkgo v1.12.1
github.com/onsi/gomega v1.10.1
github.com/go-logr/logr v0.3.0
github.com/go-logr/zapr v0.2.0
github.com/google/go-cmp v0.5.2
github.com/onsi/ginkgo v1.14.1
github.com/onsi/gomega v1.10.2
github.com/open-policy-agent/cert-controller v0.0.0-20201118195149-7df0cfffc634
github.com/open-policy-agent/frameworks/constraint v0.0.0-20201020161305-2e11d4556af8
github.com/open-policy-agent/frameworks/constraint v0.0.0-20210121003109-e55b2bb4cf1c
github.com/open-policy-agent/opa v0.24.0
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.1.0
github.com/prometheus/client_golang v1.7.1
go.opencensus.io v0.22.2
go.uber.org/zap v1.13.0
go.uber.org/zap v1.15.0
golang.org/x/net v0.0.0-20201016165138-7b1cca2348c0
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208
gopkg.in/yaml.v2 v2.3.0
k8s.io/api v0.18.6
k8s.io/apiextensions-apiserver v0.18.6
k8s.io/apimachinery v0.18.6
k8s.io/client-go v0.18.6
sigs.k8s.io/controller-runtime v0.6.3
k8s.io/api v0.19.2
k8s.io/apiextensions-apiserver v0.19.2
k8s.io/apimachinery v0.19.2
k8s.io/client-go v0.19.2
sigs.k8s.io/controller-runtime v0.7.0
sigs.k8s.io/yaml v1.2.0
)

replace github.com/open-policy-agent/cert-controller => github.com/stijndehaes/cert-controller v0.0.0-20210114145504-e0beabda7d4c
202 changes: 139 additions & 63 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ func setLoggerForProduction(encoder zapcore.LevelEncoder) {
lvl := zap.NewAtomicLevelAt(zap.WarnLevel)
opts = append(opts, zap.AddStacktrace(zap.ErrorLevel),
zap.WrapCore(func(core zapcore.Core) zapcore.Core {
return zapcore.NewSampler(core, time.Second, 100, 100)
return zapcore.NewSamplerWithOptions(core, time.Second, 100, 100)
}))
opts = append(opts, zap.AddCallerSkip(1), zap.ErrorOutput(sink))
zlog := zap.New(zapcore.NewCore(&crzap.KubeAwareEncoder{Encoder: enc, Verbose: false}, sink, lvl))
Expand Down
6 changes: 2 additions & 4 deletions pkg/audit/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,12 +429,10 @@ func (am *Manager) auditManagerLoop(ctx context.Context) {
}

// Start implements controller.Controller
func (am *Manager) Start(stop <-chan struct{}) error {
func (am *Manager) Start(ctx context.Context) error {
log.Info("Starting Audit Manager")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go am.auditManagerLoop(ctx)
<-stop
<-ctx.Done()
log.Info("Stopping audit manager workers")
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/assign/assign_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ type Reconciler struct {

// Reconcile reads that state of the cluster for a Assign object and makes changes based on the state read
// and what is in the Assign.Spec
func (r *Reconciler) Reconcile(request reconcile.Request) (reconcile.Result, error) {
func (r *Reconciler) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
log.Info("Reconcile", "request", request)
deleted := false
assign := &mutationsv1alpha1.Assign{}
err := r.Get(context.TODO(), request.NamespacedName, assign)
err := r.Get(ctx, request.NamespacedName, assign)
if err != nil {
if !errors.IsNotFound(err) {
return reconcile.Result{}, err
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/assignmetadata/assignmetadata_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ type Reconciler struct {

// Reconcile reads that state of the cluster for a AssignMetadata object and makes changes based on the state read
// and what is in the AssignMetadata.Spec
func (r *Reconciler) Reconcile(request reconcile.Request) (reconcile.Result, error) {
func (r *Reconciler) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
log.Info("Reconcile", "request", request)
deleted := false
assignMetadata := &mutationsv1alpha1.AssignMetadata{}
err := r.Get(context.TODO(), request.NamespacedName, assignMetadata)
err := r.Get(ctx, request.NamespacedName, assignMetadata)
if err != nil {
if !errors.IsNotFound(err) {
return reconcile.Result{}, err
Expand Down
7 changes: 3 additions & 4 deletions pkg/controller/config/config_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ type ReconcileConfig struct {
// and what is in the Config.Spec
// Automatically generate RBAC rules to allow the Controller to read all things (for sync)
// update is needed for finalizers
func (r *ReconcileConfig) Reconcile(request reconcile.Request) (reconcile.Result, error) {
func (r *ReconcileConfig) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
// Short-circuit if shutting down.
if r.cs != nil {
running := r.cs.Enter()
Expand All @@ -202,7 +202,6 @@ func (r *ReconcileConfig) Reconcile(request reconcile.Request) (reconcile.Result
}
exists := true
instance := &configv1alpha1.Config{}
ctx := context.Background()
err := r.reader.Get(ctx, request.NamespacedName, instance)
if err != nil {
// if config is not found, we should remove cached data
Expand Down Expand Up @@ -312,7 +311,7 @@ func (r *ReconcileConfig) wipeCacheIfNeeded(ctx context.Context) error {

// reset sync cache before sending the metric
r.syncMetricsCache.ResetCache()
r.syncMetricsCache.ReportSync(&syncc.Reporter{Ctx: context.TODO()})
r.syncMetricsCache.ReportSync(&syncc.Reporter{Ctx: ctx})

r.needsWipe = false
}
Expand All @@ -337,7 +336,7 @@ func (r *ReconcileConfig) replayData(ctx context.Context) error {
return fmt.Errorf("replaying data for %+v: %w", gvk, err)
}

defer r.syncMetricsCache.ReportSync(&syncc.Reporter{Ctx: context.TODO()})
defer r.syncMetricsCache.ReportSync(&syncc.Reporter{Ctx: ctx})

for i := range u.Items {
syncKey := r.syncMetricsCache.GetSyncKey(u.Items[i].GetNamespace(), u.Items[i].GetName())
Expand Down
11 changes: 5 additions & 6 deletions pkg/controller/config/config_controller_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,23 @@ func TestMain(m *testing.M) {
// writes the request to requests after Reconcile is finished.
func SetupTestReconcile(inner reconcile.Reconciler) (reconcile.Reconciler, chan reconcile.Request) {
requests := make(chan reconcile.Request)
fn := reconcile.Func(func(req reconcile.Request) (reconcile.Result, error) {
result, err := inner.Reconcile(req)
fn := reconcile.Func(func(ctx context.Context, req reconcile.Request) (reconcile.Result, error) {
result, err := inner.Reconcile(ctx, req)
requests <- req
return result, err
})
return fn, requests
}

// StartTestManager adds recFn
func StartTestManager(mgr manager.Manager, g *gomega.GomegaWithT) (chan struct{}, *sync.WaitGroup) {
stop := make(chan struct{})
func StartTestManager(ctx context.Context, mgr manager.Manager, g *gomega.GomegaWithT) *sync.WaitGroup {
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
g.Expect(mgr.Start(stop)).NotTo(gomega.HaveOccurred())
g.Expect(mgr.Start(ctx)).NotTo(gomega.HaveOccurred())
}()
return stop, wg
return wg
}

// Bootstrap the gatekeeper-system namespace for use in tests
Expand Down
32 changes: 17 additions & 15 deletions pkg/controller/config/config_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import (
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/rest"
Expand Down Expand Up @@ -122,7 +121,7 @@ func TestReconcile(t *testing.T) {
// Setup the Manager and Controller. Wrap the Controller Reconcile function so it writes each request to a
// channel when it is finished.
mgr, wm := setupManager(t)
c := &testclient.RetryClient{Client: mgr.GetClient()}
c := testclient.NewRetryClient(mgr.GetClient())

// initialize OPA
driver := local.New(local.Tracing(true))
Expand All @@ -147,11 +146,12 @@ func TestReconcile(t *testing.T) {
recFn, requests := SetupTestReconcile(rec)
g.Expect(add(mgr, recFn)).NotTo(gomega.HaveOccurred())

stopMgr, mgrStopped := StartTestManager(mgr, g)
ctx, cancelFunc := context.WithCancel(context.Background())
mgrStopped := StartTestManager(ctx, mgr, g)
once := gosync.Once{}
testMgrStopped := func() {
once.Do(func() {
close(stopMgr)
cancelFunc()
mgrStopped.Wait()
})
}
Expand Down Expand Up @@ -236,7 +236,7 @@ func TestConfig_DeleteSyncResources(t *testing.T) {
// setup the Manager and Controller. Wrap the Controller Reconcile function so it writes each request to a
// channel when it is finished.
mgr, wm := setupManager(t)
c := &testclient.RetryClient{Client: mgr.GetClient()}
c := testclient.NewRetryClient(mgr.GetClient())

// create the Config object and expect the Reconcile to be created when controller starts
instance := &configv1alpha1.Config{
Expand Down Expand Up @@ -294,11 +294,12 @@ func TestConfig_DeleteSyncResources(t *testing.T) {
g.Expect(err).NotTo(gomega.HaveOccurred())

// start manager that will start tracker and controller
stopMgr, mgrStopped := StartTestManager(mgr, g)
ctx, cancelFunc := context.WithCancel(context.Background())
mgrStopped := StartTestManager(ctx, mgr, g)
once := gosync.Once{}
defer func() {
once.Do(func() {
close(stopMgr)
cancelFunc()
mgrStopped.Wait()
})
}()
Expand Down Expand Up @@ -332,7 +333,6 @@ func TestConfig_DeleteSyncResources(t *testing.T) {
}

events <- event.GenericEvent{
Meta: podObj,
Object: podObj,
}

Expand Down Expand Up @@ -389,7 +389,7 @@ func TestConfig_CacheContents(t *testing.T) {

// Setup the Manager and Controller.
mgr, wm := setupManager(t)
c := &testclient.RetryClient{Client: mgr.GetClient()}
c := testclient.NewRetryClient(mgr.GetClient())

opa := &fakeOpa{}
cs := watch.NewSwitch()
Expand All @@ -402,11 +402,12 @@ func TestConfig_CacheContents(t *testing.T) {
rec, _ := newReconciler(mgr, opa, wm, cs, tracker, processExcluder, events, events)
g.Expect(add(mgr, rec)).NotTo(gomega.HaveOccurred())

stopMgr, mgrStopped := StartTestManager(mgr, g)
ctx, cancelFunc := context.WithCancel(context.Background())
mgrStopped := StartTestManager(ctx, mgr, g)
once := gosync.Once{}
testMgrStopped := func() {
once.Do(func() {
close(stopMgr)
cancelFunc()
mgrStopped.Wait()
})
}
Expand Down Expand Up @@ -516,7 +517,7 @@ func TestConfig_Retries(t *testing.T) {

// Setup the Manager and Controller.
mgr, wm := setupManager(t)
c := &testclient.RetryClient{Client: mgr.GetClient()}
c := testclient.NewRetryClient(mgr.GetClient())

opa := &fakeOpa{}
cs := watch.NewSwitch()
Expand All @@ -533,7 +534,7 @@ func TestConfig_Retries(t *testing.T) {
failPlease := make(chan string, 1)
rec.reader = hookReader{
Reader: mgr.GetCache(),
ListFunc: func(ctx context.Context, list runtime.Object, opts ...client.ListOption) error {
ListFunc: func(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error {
// Return an error the first go-around.
var failKind string
select {
Expand All @@ -547,11 +548,12 @@ func TestConfig_Retries(t *testing.T) {
},
}

stopMgr, mgrStopped := StartTestManager(mgr, g)
ctx, cancelFunc := context.WithCancel(context.Background())
mgrStopped := StartTestManager(ctx, mgr, g)
once := gosync.Once{}
testMgrStopped := func() {
once.Do(func() {
close(stopMgr)
cancelFunc()
mgrStopped.Wait()
})
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/config/fakes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ func (f *fakeOpa) Len() int {
// hookReader is a client.Reader with overrideable methods.
type hookReader struct {
client.Reader
ListFunc func(ctx context.Context, list runtime.Object, opts ...client.ListOption) error
ListFunc func(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error
}

func (r hookReader) List(ctx context.Context, list runtime.Object, opts ...client.ListOption) error {
func (r hookReader) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error {
if r.ListFunc != nil {
return r.ListFunc(ctx, list, opts...)
}
Expand Down
Loading