Skip to content
This repository has been archived by the owner on Jun 26, 2023. It is now read-only.

Update controller-runtime to v0.7.0 #1367

Merged
merged 1 commit into from
Jan 29, 2021
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
27 changes: 12 additions & 15 deletions incubator/hnc/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,21 @@ go 1.14
require (
contrib.go.opencensus.io/exporter/prometheus v0.2.0
contrib.go.opencensus.io/exporter/stackdriver v0.13.0
github.com/Azure/go-autorest/autorest v0.9.1 // indirect
github.com/Azure/go-autorest/autorest/adal v0.6.0 // indirect
github.com/emicklei/go-restful v2.10.0+incompatible // indirect
github.com/go-logr/logr v0.1.0
github.com/go-logr/logr v0.3.0
github.com/go-openapi/spec v0.19.5 // indirect
github.com/gophercloud/gophercloud v0.4.0 // indirect
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect
github.com/onsi/ginkgo v1.12.1
github.com/onsi/gomega v1.10.1
github.com/open-policy-agent/cert-controller v0.0.0-20200921224206-24b87bbc4b6e
github.com/spf13/cobra v0.0.5
github.com/onsi/ginkgo v1.14.1
github.com/onsi/gomega v1.10.2
github.com/open-policy-agent/cert-controller v0.0.0-20210129015139-6ff9721a1c47
github.com/spf13/cobra v1.0.0
go.opencensus.io v0.22.3
go.uber.org/zap v1.10.0
k8s.io/api v0.18.6
k8s.io/apiextensions-apiserver v0.18.6
k8s.io/apimachinery v0.18.6
k8s.io/cli-runtime v0.18.5
k8s.io/client-go v0.18.6
sigs.k8s.io/controller-runtime v0.6.4
go.uber.org/zap v1.15.0
k8s.io/api v0.19.2
k8s.io/apiextensions-apiserver v0.19.2
k8s.io/apimachinery v0.19.2
k8s.io/cli-runtime v0.19.2
k8s.io/client-go v0.19.2
sigs.k8s.io/controller-runtime v0.7.0
sigs.k8s.io/controller-tools v0.2.8
)
259 changes: 196 additions & 63 deletions incubator/hnc/go.sum

Large diffs are not rendered by default.

30 changes: 14 additions & 16 deletions incubator/hnc/internal/reconcilers/anchor.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ type AnchorReconciler struct {

// Reconcile sets up some basic variables and then calls the business logic. It currently
// only handles the creation of the namespaces but no deletion or state reporting yet.
func (r *AnchorReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
ctx := context.Background()
func (r *AnchorReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := loggerWithRID(r.Log).WithValues("trigger", req.NamespacedName)
log.V(1).Info("Reconciling anchor")

Expand Down Expand Up @@ -302,7 +301,7 @@ func (r *AnchorReconciler) enqueue(log logr.Logger, nm, pnm, reason string) {
inst.ObjectMeta.Name = nm
inst.ObjectMeta.Namespace = pnm
log.V(1).Info("Enqueuing for reconciliation", "affected", pnm+"/"+nm, "reason", reason)
r.Affected <- event.GenericEvent{Meta: inst}
r.Affected <- event.GenericEvent{Object: inst}
}()
}

Expand Down Expand Up @@ -372,21 +371,20 @@ func (r *AnchorReconciler) deleteNamespace(ctx context.Context, log logr.Logger,

func (r *AnchorReconciler) SetupWithManager(mgr ctrl.Manager) error {
// Maps an subnamespace to its anchor in the parent namespace.
nsMapFn := handler.ToRequestsFunc(
func(a handler.MapObject) []reconcile.Request {
if a.Meta.GetAnnotations()[api.SubnamespaceOf] == "" {
return nil
}
return []reconcile.Request{
{NamespacedName: types.NamespacedName{
Name: a.Meta.GetName(),
Namespace: a.Meta.GetAnnotations()[api.SubnamespaceOf],
}},
}
})
nsMapFn := func(obj client.Object) []reconcile.Request {
if obj.GetAnnotations()[api.SubnamespaceOf] == "" {
return nil
}
return []reconcile.Request{
{NamespacedName: types.NamespacedName{
Name: obj.GetName(),
Namespace: obj.GetAnnotations()[api.SubnamespaceOf],
}},
}
}
return ctrl.NewControllerManagedBy(mgr).
For(&api.SubnamespaceAnchor{}).
Watches(&source.Channel{Source: r.Affected}, &handler.EnqueueRequestForObject{}).
Watches(&source.Kind{Type: &corev1.Namespace{}}, &handler.EnqueueRequestsFromMapFunc{ToRequests: nsMapFn}).
Watches(&source.Kind{Type: &corev1.Namespace{}}, handler.EnqueueRequestsFromMapFunc(nsMapFn)).
Complete(r)
}
43 changes: 20 additions & 23 deletions incubator/hnc/internal/reconcilers/hierarchy_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,14 @@ type HierarchyConfigReconciler struct {
// +kubebuilder:rbac:groups=core,resources=namespaces,verbs=get;list;watch;update;patch

// Reconcile sets up some basic variables and then calls the business logic.
func (r *HierarchyConfigReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
func (r *HierarchyConfigReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
if config.EX[req.Namespace] {
return ctrl.Result{}, nil
}

stats.StartHierConfigReconcile()
defer stats.StopHierConfigReconcile()

ctx := context.Background()
ns := req.NamespacedName.Namespace

log := loggerWithRID(r.Log).WithValues("ns", ns)
Expand Down Expand Up @@ -520,7 +519,7 @@ func (r *HierarchyConfigReconciler) enqueueAffected(log logr.Logger, reason stri
inst := &api.HierarchyConfiguration{}
inst.ObjectMeta.Name = api.Singleton
inst.ObjectMeta.Namespace = nm
r.Affected <- event.GenericEvent{Meta: inst}
r.Affected <- event.GenericEvent{Object: inst}
}
}()
}
Expand Down Expand Up @@ -671,33 +670,31 @@ func (r *HierarchyConfigReconciler) getAnchorNames(ctx context.Context, nm strin

func (r *HierarchyConfigReconciler) SetupWithManager(mgr ctrl.Manager, maxReconciles int) error {
// Maps namespaces to their singletons
nsMapFn := handler.ToRequestsFunc(
func(a handler.MapObject) []reconcile.Request {
return []reconcile.Request{
{NamespacedName: types.NamespacedName{
Name: api.Singleton,
Namespace: a.Meta.GetName(),
}},
}
})
nsMapFn := func(obj client.Object) []reconcile.Request {
return []reconcile.Request{
{NamespacedName: types.NamespacedName{
Name: api.Singleton,
Namespace: obj.GetName(),
}},
}
}
// Maps a subnamespace anchor to the parent singleton.
anchorMapFn := handler.ToRequestsFunc(
func(a handler.MapObject) []reconcile.Request {
return []reconcile.Request{
{NamespacedName: types.NamespacedName{
Name: api.Singleton,
Namespace: a.Meta.GetNamespace(),
}},
}
})
anchorMapFn := func(obj client.Object) []reconcile.Request {
return []reconcile.Request{
{NamespacedName: types.NamespacedName{
Name: api.Singleton,
Namespace: obj.GetNamespace(),
}},
}
}
opts := controller.Options{
MaxConcurrentReconciles: maxReconciles,
}
return ctrl.NewControllerManagedBy(mgr).
For(&api.HierarchyConfiguration{}).
Watches(&source.Channel{Source: r.Affected}, &handler.EnqueueRequestForObject{}).
Watches(&source.Kind{Type: &corev1.Namespace{}}, &handler.EnqueueRequestsFromMapFunc{ToRequests: nsMapFn}).
Watches(&source.Kind{Type: &api.SubnamespaceAnchor{}}, &handler.EnqueueRequestsFromMapFunc{ToRequests: anchorMapFn}).
Watches(&source.Kind{Type: &corev1.Namespace{}}, handler.EnqueueRequestsFromMapFunc(nsMapFn)).
Watches(&source.Kind{Type: &api.SubnamespaceAnchor{}}, handler.EnqueueRequestsFromMapFunc(anchorMapFn)).
WithOptions(opts).
Complete(r)
}
19 changes: 8 additions & 11 deletions incubator/hnc/internal/reconcilers/hnc_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ type gvk2gr map[schema.GroupVersionKind]schema.GroupResource
const checkPeriod = 3 * time.Second

// Reconcile is the entrypoint to the reconciler.
func (r *ConfigReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
ctx := context.Background()

func (r *ConfigReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
// Load the config and clear its conditions so they can be reset.
inst, err := r.getSingleton(ctx)
if err != nil {
Expand Down Expand Up @@ -530,27 +528,26 @@ func (r *ConfigReconciler) triggerReconcileIfNeeded() {
go func() {
inst := &api.HNCConfiguration{}
inst.ObjectMeta.Name = api.HNCConfigSingleton
r.Trigger <- event.GenericEvent{Meta: inst}
r.Trigger <- event.GenericEvent{Object: inst}
}()
}

// SetupWithManager builds a controller with the reconciler.
func (r *ConfigReconciler) SetupWithManager(mgr ctrl.Manager) error {
// Whenever a CRD is created/updated, we will send a request to reconcile the singleton again, in
// case the singleton has configuration for the custom resource type.
crdMapFn := handler.ToRequestsFunc(
func(_ handler.MapObject) []reconcile.Request {
return []reconcile.Request{{NamespacedName: types.NamespacedName{
Name: api.HNCConfigSingleton,
}}}
})
crdMapFn := func(_ client.Object) []reconcile.Request {
return []reconcile.Request{{NamespacedName: types.NamespacedName{
Name: api.HNCConfigSingleton,
}}}
}

// Register the reconciler
err := ctrl.NewControllerManagedBy(mgr).
For(&api.HNCConfiguration{}).
Watches(&source.Channel{Source: r.Trigger}, &handler.EnqueueRequestForObject{}).
Watches(&source.Kind{Type: &apiextensions.CustomResourceDefinition{}},
&handler.EnqueueRequestsFromMapFunc{ToRequests: crdMapFn}).
handler.EnqueueRequestsFromMapFunc(crdMapFn)).
Complete(r)
if err != nil {
return err
Expand Down
9 changes: 4 additions & 5 deletions incubator/hnc/internal/reconcilers/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,8 @@ func (r *ObjectReconciler) enqueueAllObjects(ctx context.Context, log logr.Logge
return nil
}

func (r *ObjectReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
func (r *ObjectReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
resp := ctrl.Result{}
ctx := context.Background()
log := loggerWithRID(r.Log).WithValues("trigger", req.NamespacedName)

if config.EX[req.Namespace] {
Expand Down Expand Up @@ -483,7 +482,7 @@ func (r *ObjectReconciler) enqueueDescendants(ctx context.Context, log logr.Logg
dc := object.Canonical(src)
dc.SetNamespace(ns)
log.V(1).Info("... enqueuing descendant copy", "affected", ns+"/"+src.GetName(), "reason", reason)
r.Affected <- event.GenericEvent{Meta: dc}
r.Affected <- event.GenericEvent{Object: dc}
}
}

Expand All @@ -501,7 +500,7 @@ func (r *ObjectReconciler) enqueueLocalObjects(ctx context.Context, log logr.Log
co := object.Canonical(&inst)
co.SetNamespace(inst.GetNamespace())
log.V(1).Info("Enqueuing existing object for reconciliation", "affected", co.GetName())
r.Affected <- event.GenericEvent{Meta: co}
r.Affected <- event.GenericEvent{Object: co}
}

return nil
Expand All @@ -520,7 +519,7 @@ func (r *ObjectReconciler) enqueuePropagatedObjects(ctx context.Context, log log
lc := object.Canonical(obj)
lc.SetNamespace(ns)
log.V(1).Info("Enqueuing local copy of the ancestor original for reconciliation", "affected", lc.GetName())
r.Affected <- event.GenericEvent{Meta: lc}
r.Affected <- event.GenericEvent{Object: lc}
}
}

Expand Down
7 changes: 3 additions & 4 deletions incubator/hnc/internal/reconcilers/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -21,7 +20,7 @@ import (
type FakeDeleteCRDClient struct{}

// FakeDeleteCRDClient doesn't return any err on Get() because none of the reconciler test performs CRD deletion
func (f FakeDeleteCRDClient) Get(context.Context, types.NamespacedName, runtime.Object) error {
func (f FakeDeleteCRDClient) Get(context.Context, types.NamespacedName, client.Object) error {
return nil
}

Expand All @@ -33,7 +32,7 @@ var crds = map[string]bool{

// deleteCRDClientType could be either a real client or FakeDeleteCRDClient
type deleteCRDClientType interface {
Get(context.Context, types.NamespacedName, runtime.Object) error
Get(context.Context, types.NamespacedName, client.Object) error
}

// deleteCRDClient is an uncached client for checking CRD deletion
Expand Down Expand Up @@ -101,7 +100,7 @@ func Create(mgr ctrl.Manager, f *forest.Forest, maxReconciles int, useFakeClient

// crdClient is any client that can be used in isDeletingCRD (i.e. any reconciler).
type crdClient interface {
Get(context.Context, types.NamespacedName, runtime.Object) error
Get(context.Context, types.NamespacedName, client.Object) error
}

// isDeletingCRD returns true if the specified HNC CRD is being or has been deleted. The argument
Expand Down
2 changes: 1 addition & 1 deletion incubator/hnc/internal/reconcilers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestAPIs(t *testing.T) {
// All tests in the reconcilers_test package are in one suite. As a result, they
// share the same test environment (e.g., same api server).
var _ = BeforeSuite(func(done Done) {
logf.SetLogger(zap.LoggerTo(GinkgoWriter, true))
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))

By("bootstrapping test environment")
testEnv = &envtest.Environment{
Expand Down
10 changes: 5 additions & 5 deletions incubator/hnc/internal/validators/anchor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"

"github.com/go-logr/logr"
"k8s.io/api/admission/v1beta1"
k8sadm "k8s.io/api/admission/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

Expand Down Expand Up @@ -34,7 +34,7 @@ type Anchor struct {
// req defines the aspects of the admission.Request that we care about.
type anchorRequest struct {
anchor *api.SubnamespaceAnchor
op v1beta1.Operation
op k8sadm.Operation
}

// Handle implements the validation webhook.
Expand Down Expand Up @@ -77,7 +77,7 @@ func (v *Anchor) handle(req *anchorRequest) admission.Response {
cns := v.Forest.Get(cnm)

switch req.op {
case v1beta1.Create:
case k8sadm.Create:
// Can't create subnamespaces in excluded namespaces
if config.EX[pnm] {
msg := fmt.Sprintf("The namespace %s is not allowed to create subnamespaces. Please create subnamespaces in a different namespace.", pnm)
Expand All @@ -94,7 +94,7 @@ func (v *Anchor) handle(req *anchorRequest) admission.Response {
}
}

case v1beta1.Delete:
case k8sadm.Delete:
// Don't allow the anchor to be deleted if it's in a good state and has descendants of its own,
// unless allowCascadingDeletion is set.
if req.anchor.Status.State == api.Ok && cns.ChildNames() != nil && !cns.AllowsCascadingDeletion() {
Expand All @@ -116,7 +116,7 @@ func (v *Anchor) decodeRequest(log logr.Logger, in admission.Request) (*anchorRe
var err error
// For DELETE request, use DecodeRaw() from req.OldObject, since Decode() only uses req.Object,
// which will be empty for a DELETE request.
if in.Operation == v1beta1.Delete {
if in.Operation == k8sadm.Delete {
log.V(1).Info("Decoding a delete request.")
if in.OldObject.Raw == nil {
// https://github.com/kubernetes-sigs/multi-tenancy/issues/688
Expand Down
6 changes: 3 additions & 3 deletions incubator/hnc/internal/validators/anchor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"testing"

. "github.com/onsi/gomega"
"k8s.io/api/admission/v1beta1"
k8sadm "k8s.io/api/admission/v1"
api "sigs.k8s.io/multi-tenancy/incubator/hnc/api/v1alpha2"
"sigs.k8s.io/multi-tenancy/incubator/hnc/internal/foresttest"
)
Expand Down Expand Up @@ -36,7 +36,7 @@ func TestCreateSubnamespaces(t *testing.T) {
anchor.ObjectMeta.Name = tc.cnm
req := &anchorRequest{
anchor: anchor,
op: v1beta1.Create,
op: k8sadm.Create,
}

// Test
Expand Down Expand Up @@ -91,7 +91,7 @@ func TestAllowCascadingDeleteSubnamespaces(t *testing.T) {
anchor.Status.State = tc.stt
req := &anchorRequest{
anchor: anchor,
op: v1beta1.Delete,
op: k8sadm.Delete,
}

// Test
Expand Down
6 changes: 3 additions & 3 deletions incubator/hnc/internal/validators/hierarchy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"

"github.com/go-logr/logr"
admissionv1beta1 "k8s.io/api/admission/v1beta1"
k8sadm "k8s.io/api/admission/v1"
authnv1 "k8s.io/api/authentication/v1"
authzv1 "k8s.io/api/authorization/v1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -505,7 +505,7 @@ func (r *realClient) IsAdmin(ctx context.Context, ui *authnv1.UserInfo, nnm stri
// allow is a replacement for controller-runtime's admission.Allowed() that allows you to set the
// message (human-readable) as opposed to the reason (machine-readable).
func allow(msg string) admission.Response {
return admission.Response{AdmissionResponse: admissionv1beta1.AdmissionResponse{
return admission.Response{AdmissionResponse: k8sadm.AdmissionResponse{
Allowed: true,
Result: &metav1.Status{
Code: 0,
Expand All @@ -518,7 +518,7 @@ func allow(msg string) admission.Response {
// human-readable message _and_ a machine-readable reason, and also sets the code correctly instead
// of hardcoding it to 403 Forbidden.
func deny(reason metav1.StatusReason, msg string) admission.Response {
return admission.Response{AdmissionResponse: admissionv1beta1.AdmissionResponse{
return admission.Response{AdmissionResponse: k8sadm.AdmissionResponse{
Allowed: false,
Result: &metav1.Status{
Code: codeFromReason(reason),
Expand Down
Loading