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

Commit

Permalink
Merge pull request #468 from yiqigao217/hc-controller
Browse files Browse the repository at this point in the history
Create a flag to toggle HC and HNS Reconcilers
  • Loading branch information
k8s-ci-robot authored and sophieliu15 committed Feb 28, 2020
2 parents ac8554b + a2bad3f commit 6eca685
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 25 deletions.
4 changes: 3 additions & 1 deletion incubator/hnc/cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func main() {
debugLogs bool
testLog bool
qps int
enableHNSReconciler bool
)
flag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "enable-leader-election", false,
Expand All @@ -72,6 +73,7 @@ func main() {
flag.BoolVar(&testLog, "enable-test-log", false, "Enables test log.")
flag.IntVar(&maxReconciles, "max-reconciles", 1, "Number of concurrent reconciles to perform.")
flag.IntVar(&qps, "apiserver-qps-throttle", 50, "The maximum QPS to the API server.")
flag.BoolVar(&enableHNSReconciler, "enable-hierarchicalnamespace-reconciler", false, "Enables hierarchicalnamespace reconciler.")
flag.Parse()

// Enable OpenCensus exporters to export metrics
Expand Down Expand Up @@ -131,7 +133,7 @@ func main() {
// Create all reconciling controllers
f := forest.NewForest()
setupLog.Info("Creating controllers", "maxReconciles", maxReconciles)
if err := reconcilers.Create(mgr, f, maxReconciles); err != nil {
if err := reconcilers.Create(mgr, f, maxReconciles, enableHNSReconciler); err != nil {
setupLog.Error(err, "cannot create controllers")
os.Exit(1)
}
Expand Down
12 changes: 12 additions & 0 deletions incubator/hnc/pkg/reconcilers/hierarchy_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ type HierarchyConfigReconciler struct {
// were part of the same reconciliation attempt, even if multiple are running parallel (or it's
// simply hard to tell when one ends and another begins).
reconcileID int32

// This is a temporary field to toggle different behaviours of this HierarchyConfigurationReconciler
// depending on if the HierarchicalNamespaceReconciler is enabled or not. It will be removed after
// the GitHub issue "Implement self-service namespace" is resolved
// (https://github.com/kubernetes-sigs/multi-tenancy/issues/457)
HNSReconcilerEnabled bool
}

// +kubebuilder:rbac:groups=hnc.x-k8s.io,resources=hierarchies,verbs=get;list;watch;create;update;patch;delete
Expand All @@ -83,6 +89,12 @@ func (r *HierarchyConfigReconciler) Reconcile(req ctrl.Request) (ctrl.Result, er

rid := (int)(atomic.AddInt32(&r.reconcileID, 1))
log := r.Log.WithValues("ns", ns, "rid", rid)

// TODO remove this log and use the HNSReconcilerEnabled to toggle the behavour of this
// reconciler accordingly. See issue: https://github.com/kubernetes-sigs/multi-tenancy/issues/467
// Output a log for testing.
log.Info("HC will be reconciled with", "HNSReconcilerEnabled", r.HNSReconcilerEnabled)

return ctrl.Result{}, r.reconcile(ctx, log, ns)
}

Expand Down
54 changes: 35 additions & 19 deletions incubator/hnc/pkg/reconcilers/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,43 @@ var ex = map[string]bool{
// Create creates all reconcilers.
//
// This function is called both from main.go as well as from the integ tests.
func Create(mgr ctrl.Manager, f *forest.Forest, maxReconciles int) error {
func Create(mgr ctrl.Manager, f *forest.Forest, maxReconciles int, enableHNSReconciler bool) error {
hcChan := make(chan event.GenericEvent)

// Create the HierarchyConfigReconciler.
hr := &HierarchyConfigReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("reconcilers").WithName("Hierarchy"),
Forest: f,
Affected: hcChan,
}
if err := hr.SetupWithManager(mgr, maxReconciles); err != nil {
return fmt.Errorf("cannot create Hierarchy reconciler: %s", err.Error())
// Create different reconcilers based on if the enableHNSReconciler flag is set or not.
if enableHNSReconciler {
// Create the HierarchyConfigReconciler with HNSReconciler enabled.
hr := &HierarchyConfigReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("reconcilers").WithName("Hierarchy"),
Forest: f,
Affected: hcChan,
HNSReconcilerEnabled: true,
}
if err := hr.SetupWithManager(mgr, maxReconciles); err != nil {
return fmt.Errorf("cannot create Hierarchy reconciler: %s", err.Error())
}

// Create HierarchicalNamespaceReconciler.
hnsr := &HierarchicalNamespaceReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("reconcilers").WithName("HierarchicalNamespace"),
}
if err := hnsr.SetupWithManager(mgr); err != nil {
return fmt.Errorf("cannot create HierarchicalNamespace reconciler: %s", err.Error())
}
} else {
// Create the HierarchyConfigReconciler with HNSReconciler disabled.
hr := &HierarchyConfigReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("reconcilers").WithName("Hierarchy"),
Forest: f,
Affected: hcChan,
HNSReconcilerEnabled: false,
}
if err := hr.SetupWithManager(mgr, maxReconciles); err != nil {
return fmt.Errorf("cannot create Hierarchy reconciler: %s", err.Error())
}
}

// Create the ConfigReconciler.
Expand All @@ -47,14 +72,5 @@ func Create(mgr ctrl.Manager, f *forest.Forest, maxReconciles int) error {
return fmt.Errorf("cannot create Config reconciler: %s", err.Error())
}

// Create HierarchicalNamespaceReconciler.
hnsr := &HierarchicalNamespaceReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("reconcilers").WithName("HierarchicalNamespace"),
}
if err := hnsr.SetupWithManager(mgr); err != nil {
return fmt.Errorf("cannot create HierarchicalNamespace reconciler: %s", err.Error())
}

return nil
}
19 changes: 14 additions & 5 deletions incubator/hnc/pkg/reconcilers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package reconcilers_test

import (
"context"
"flag"
"path/filepath"
"testing"
"time"
Expand Down Expand Up @@ -44,12 +45,20 @@ import (
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.

var (
cfg *rest.Config
k8sClient client.Client
testEnv *envtest.Environment
k8sManager ctrl.Manager
cfg *rest.Config
k8sClient client.Client
testEnv *envtest.Environment
k8sManager ctrl.Manager
enableHNSReconciler bool
)

func init() {
// This is a temporary flag to enable the hierarchicalnamespace reconciler for testing.
// It will be removed after the GitHub issue "Implement self-service namespace" is resolved
// (https://github.com/kubernetes-sigs/multi-tenancy/issues/457)
flag.BoolVar(&enableHNSReconciler, "enable-hierarchicalnamespace-reconciler", false, "Enables hierarchicalnamespace reconciler.")
}

func TestAPIs(t *testing.T) {
RegisterFailHandler(Fail)

Expand Down Expand Up @@ -86,7 +95,7 @@ var _ = BeforeSuite(func(done Done) {
Scheme: scheme.Scheme,
})
Expect(err).ToNot(HaveOccurred())
err = reconcilers.Create(k8sManager, forest.NewForest(), 100)
err = reconcilers.Create(k8sManager, forest.NewForest(), 100, enableHNSReconciler)
Expect(err).ToNot(HaveOccurred())

k8sClient = k8sManager.GetClient()
Expand Down

0 comments on commit 6eca685

Please sign in to comment.