Skip to content

Commit

Permalink
Move ProviderServiceAccount & ServiceDiscovery controller to supervisor
Browse files Browse the repository at this point in the history
package

Signed-off-by: Stefan Büringer [email protected]
  • Loading branch information
sbueringer committed Sep 2, 2024
1 parent 3a99f4b commit c04707b
Show file tree
Hide file tree
Showing 14 changed files with 154 additions and 38 deletions.
6 changes: 0 additions & 6 deletions controllers/controllers_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,6 @@ func setup() {
if err := AddVSphereDeploymentZoneControllerToManager(ctx, testEnv.GetControllerManagerContext(), testEnv.Manager, controllerOpts); err != nil {
panic(fmt.Sprintf("unable to setup VSphereDeploymentZone controller: %v", err))
}
if err := AddServiceAccountProviderControllerToManager(ctx, testEnv.GetControllerManagerContext(), testEnv.Manager, tracker, controllerOpts); err != nil {
panic(fmt.Sprintf("unable to setup ServiceAccount controller: %v", err))
}
if err := AddServiceDiscoveryControllerToManager(ctx, testEnv.GetControllerManagerContext(), testEnv.Manager, tracker, controllerOpts); err != nil {
panic(fmt.Sprintf("unable to setup SvcDiscovery controller: %v", err))
}

go func() {
fmt.Println("Starting the manager")
Expand Down
137 changes: 137 additions & 0 deletions controllers/vmware/controllers_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package vmware

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

. "github.com/onsi/ginkgo/v2"
"github.com/onsi/ginkgo/v2/types"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/kubernetes/scheme"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/controllers/remote"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"

infrav1 "sigs.k8s.io/cluster-api-provider-vsphere/apis/v1beta1"
vmwarev1 "sigs.k8s.io/cluster-api-provider-vsphere/apis/vmware/v1beta1"
"sigs.k8s.io/cluster-api-provider-vsphere/internal/test/helpers"
"sigs.k8s.io/cluster-api-provider-vsphere/pkg/manager"
)

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

reporterConfig := types.NewDefaultReporterConfig()
if artifactFolder, exists := os.LookupEnv("ARTIFACTS"); exists {
reporterConfig.JUnitReport = filepath.Join(artifactFolder, "junit.ginkgo.controllers_vmware.xml")
}
RunSpecs(t, "VMware Controller Suite", reporterConfig)
}

var (
testEnv *helpers.TestEnvironment
tracker *remote.ClusterCacheTracker
ctx = ctrl.SetupSignalHandler()
)

func TestMain(m *testing.M) {
setup()
code := m.Run()
teardown()
os.Exit(code)
}

func setup() {
utilruntime.Must(infrav1.AddToScheme(scheme.Scheme))
utilruntime.Must(clusterv1.AddToScheme(scheme.Scheme))
utilruntime.Must(vmwarev1.AddToScheme(scheme.Scheme))

testEnv = helpers.NewTestEnvironment(ctx)

secretCachingClient, err := client.New(testEnv.Manager.GetConfig(), client.Options{
HTTPClient: testEnv.Manager.GetHTTPClient(),
Cache: &client.CacheOptions{
Reader: testEnv.Manager.GetCache(),
},
})
if err != nil {
panic("unable to create secret caching client")
}

tracker, err = remote.NewClusterCacheTracker(
testEnv.Manager,
remote.ClusterCacheTrackerOptions{
SecretCachingClient: secretCachingClient,
ControllerName: "testenv-manager",
},
)
if err != nil {
panic(fmt.Sprintf("unable to setup ClusterCacheTracker: %v", err))
}

controllerOpts := controller.Options{MaxConcurrentReconciles: 10}

if err := (&remote.ClusterCacheReconciler{
Client: testEnv.Manager.GetClient(),
Tracker: tracker,
}).SetupWithManager(ctx, testEnv.Manager, controllerOpts); err != nil {
panic(fmt.Sprintf("unable to create ClusterCacheReconciler controller: %v", err))
}

if err := AddServiceAccountProviderControllerToManager(ctx, testEnv.GetControllerManagerContext(), testEnv.Manager, tracker, controllerOpts); err != nil {
panic(fmt.Sprintf("unable to setup ServiceAccount controller: %v", err))
}
if err := AddServiceDiscoveryControllerToManager(ctx, testEnv.GetControllerManagerContext(), testEnv.Manager, tracker, controllerOpts); err != nil {
panic(fmt.Sprintf("unable to setup SvcDiscovery controller: %v", err))
}

go func() {
fmt.Println("Starting the manager")
if err := testEnv.StartManager(ctx); err != nil {
panic(fmt.Sprintf("failed to start the envtest manager: %v", err))
}
}()
<-testEnv.Manager.Elected()

// wait for webhook port to be open prior to running tests
testEnv.WaitForWebhooks()

// create manager pod namespace
ns := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: manager.DefaultPodNamespace,
},
}
if err := testEnv.Create(ctx, ns); err != nil {
panic("unable to create controller namespace")
}
}

func teardown() {
if err := testEnv.Stop(); err != nil {
panic(fmt.Sprintf("Failed to stop envtest: %v", err))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package controllers
package vmware

import (
"context"
Expand Down Expand Up @@ -61,7 +61,6 @@ import (

const (
kindProviderServiceAccount = "ProviderServiceAccount"
systemServiceAccountPrefix = "system.serviceaccount"
)

// AddServiceAccountProviderControllerToManager adds this controller to the provided manager.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package controllers
package vmware

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package controllers
package vmware

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package controllers
package vmware

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package controllers
package vmware

import (
"context"
"fmt"
"net"
"net/url"
"reflect"
"time"

"github.com/pkg/errors"
Expand Down Expand Up @@ -485,3 +486,8 @@ func allClustersRequests(ctx context.Context, c client.Client) []reconcile.Reque
}
return requests
}

func clusterToVMwareInfrastructureMapFunc(ctx context.Context, controllerCtx *capvcontext.ControllerManagerContext) handler.MapFunc {
gvk := vmwarev1.GroupVersion.WithKind(reflect.TypeOf(&vmwarev1.VSphereCluster{}).Elem().Name())
return clusterutilv1.ClusterToInfrastructureMapFunc(ctx, gvk, controllerCtx.Client, &vmwarev1.VSphereCluster{})
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package controllers
package vmware

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package controllers
package vmware

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package controllers
package vmware

import (
. "github.com/onsi/ginkgo/v2"
Expand Down
2 changes: 1 addition & 1 deletion controllers/vmware/test/controllers_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func TestAPIs(t *testing.T) {
if artifactFolder, exists := os.LookupEnv("ARTIFACTS"); exists {
reporterConfig.JUnitReport = filepath.Join(artifactFolder, "junit.ginkgo.controllers_vmware_test.xml")
}
RunSpecs(t, "VMware Controller Suite", reporterConfig)
RunSpecs(t, "VMware Controller Suite test", reporterConfig)
}

func getTestEnv() (*envtest.Environment, *rest.Config) {
Expand Down
15 changes: 0 additions & 15 deletions controllers/vmware/vspherecluster_reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@ package vmware

import (
"context"
"os"
"path/filepath"
"reflect"
"testing"

. "github.com/onsi/ginkgo/v2"
"github.com/onsi/ginkgo/v2/types"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -34,7 +31,6 @@ import (
"k8s.io/utils/ptr"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/util/conditions"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

Expand All @@ -48,16 +44,6 @@ import (
"sigs.k8s.io/cluster-api-provider-vsphere/pkg/util"
)

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

reporterConfig := types.NewDefaultReporterConfig()
if artifactFolder, exists := os.LookupEnv("ARTIFACTS"); exists {
reporterConfig.JUnitReport = filepath.Join(artifactFolder, "junit.ginkgo.controllers_vmware.xml")
}
RunSpecs(t, "VSphereCluster Controller Suite", reporterConfig)
}

var _ = Describe("Cluster Controller Tests", func() {
const (
clusterName = "test-cluster"
Expand All @@ -72,7 +58,6 @@ var _ = Describe("Cluster Controller Tests", func() {
cluster *clusterv1.Cluster
vsphereCluster *vmwarev1.VSphereCluster
vsphereMachine *vmwarev1.VSphereMachine
ctx = ctrl.SetupSignalHandler()
clusterCtx *vmware.ClusterContext
controllerManagerContext *capvcontext.ControllerManagerContext
reconciler *ClusterReconciler
Expand Down
5 changes: 0 additions & 5 deletions controllers/vspherecluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,3 @@ func clusterToInfrastructureMapFunc(ctx context.Context, controllerCtx *capvcont
gvk := infrav1.GroupVersion.WithKind(reflect.TypeOf(&infrav1.VSphereCluster{}).Elem().Name())
return clusterutilv1.ClusterToInfrastructureMapFunc(ctx, gvk, controllerCtx.Client, &infrav1.VSphereCluster{})
}

func clusterToVMwareInfrastructureMapFunc(ctx context.Context, controllerCtx *capvcontext.ControllerManagerContext) handler.MapFunc {
gvk := vmwarev1.GroupVersion.WithKind(reflect.TypeOf(&vmwarev1.VSphereCluster{}).Elem().Name())
return clusterutilv1.ClusterToInfrastructureMapFunc(ctx, gvk, controllerCtx.Client, &vmwarev1.VSphereCluster{})
}
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,11 +408,11 @@ func setupSupervisorControllers(ctx context.Context, controllerCtx *capvcontext.
return err
}

if err := controllers.AddServiceAccountProviderControllerToManager(ctx, controllerCtx, mgr, tracker, concurrency(providerServiceAccountConcurrency)); err != nil {
if err := vmware.AddServiceAccountProviderControllerToManager(ctx, controllerCtx, mgr, tracker, concurrency(providerServiceAccountConcurrency)); err != nil {
return err
}

return controllers.AddServiceDiscoveryControllerToManager(ctx, controllerCtx, mgr, tracker, concurrency(serviceDiscoveryConcurrency))
return vmware.AddServiceDiscoveryControllerToManager(ctx, controllerCtx, mgr, tracker, concurrency(serviceDiscoveryConcurrency))
}

func setupChecks(mgr ctrlmgr.Manager) {
Expand Down

0 comments on commit c04707b

Please sign in to comment.