2022-08-08 09:50:37 -04:00
|
|
|
//go:build integration
|
2022-06-27 04:58:09 -04:00
|
|
|
|
2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-06-27 04:58:09 -04:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
2022-06-29 08:47:03 -04:00
|
|
|
"context"
|
2022-06-27 04:58:09 -04:00
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2022-06-30 07:12:05 -04:00
|
|
|
"time"
|
2022-06-27 04:58:09 -04:00
|
|
|
|
2023-11-02 06:16:17 -04:00
|
|
|
//revive:disable:dot-imports
|
2023-06-06 12:15:48 -04:00
|
|
|
. "github.com/onsi/ginkgo/v2"
|
2022-06-27 04:58:09 -04:00
|
|
|
. "github.com/onsi/gomega"
|
2023-11-02 06:16:17 -04:00
|
|
|
|
|
|
|
//revive:enable:dot-imports
|
|
|
|
|
|
|
|
nodemaintenancev1beta1 "github.com/edgelesssys/constellation/v2/3rdparty/node-maintenance-operator/api/v1beta1"
|
2023-04-18 08:20:41 -04:00
|
|
|
appsv1 "k8s.io/api/apps/v1"
|
|
|
|
corev1 "k8s.io/api/core/v1"
|
2022-06-27 04:58:09 -04:00
|
|
|
"k8s.io/client-go/kubernetes/scheme"
|
|
|
|
"k8s.io/client-go/rest"
|
2022-06-30 07:12:05 -04:00
|
|
|
testclock "k8s.io/utils/clock/testing"
|
2023-11-02 06:16:17 -04:00
|
|
|
ctrl "sigs.k8s.io/controller-runtime"
|
2022-06-27 04:58:09 -04:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/envtest"
|
|
|
|
logf "sigs.k8s.io/controller-runtime/pkg/log"
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/log/zap"
|
|
|
|
|
2023-01-04 13:04:28 -05:00
|
|
|
updatev1alpha1 "github.com/edgelesssys/constellation/v2/operators/constellation-node-operator/v2/api/v1alpha1"
|
2022-06-27 04:58:09 -04:00
|
|
|
//+kubebuilder:scaffold:imports
|
|
|
|
)
|
|
|
|
|
|
|
|
// These tests use Ginkgo (BDD-style Go testing framework). Refer to
|
|
|
|
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
|
|
|
|
|
2022-06-27 06:05:42 -04:00
|
|
|
var (
|
|
|
|
cfg *rest.Config
|
|
|
|
k8sClient client.Client
|
|
|
|
testEnv *envtest.Environment
|
2022-06-29 08:47:03 -04:00
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
2022-06-29 10:47:13 -04:00
|
|
|
fakes = newFakes()
|
2022-06-27 06:05:42 -04:00
|
|
|
)
|
2022-06-27 04:58:09 -04:00
|
|
|
|
|
|
|
func TestAPIs(t *testing.T) {
|
|
|
|
RegisterFailHandler(Fail)
|
2023-06-06 12:15:48 -04:00
|
|
|
suiteConfig, reporterConfig := GinkgoConfiguration()
|
|
|
|
// If you want to debug a specific seed, set it here.
|
|
|
|
// suiteConfig.RandomSeed = 1679587116
|
|
|
|
reporterConfig.VeryVerbose = true
|
|
|
|
RunSpecs(t, "Controller Suite", suiteConfig, reporterConfig)
|
2022-06-27 04:58:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ = BeforeSuite(func() {
|
|
|
|
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))
|
2022-06-29 08:47:03 -04:00
|
|
|
ctx, cancel = context.WithCancel(context.Background())
|
2022-06-27 04:58:09 -04:00
|
|
|
|
|
|
|
By("bootstrapping test environment")
|
|
|
|
testEnv = &envtest.Environment{
|
2022-07-01 10:42:08 -04:00
|
|
|
CRDDirectoryPaths: []string{
|
|
|
|
filepath.Join("..", "config", "crd", "bases"),
|
|
|
|
filepath.Join("..", "external", "github.com", "medik8s", "node-maintenance-operator", "config", "crd", "bases"),
|
|
|
|
},
|
2022-06-27 04:58:09 -04:00
|
|
|
ErrorIfCRDPathMissing: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
// cfg is defined in this file globally.
|
|
|
|
cfg, err = testEnv.Start()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(cfg).NotTo(BeNil())
|
|
|
|
|
|
|
|
err = updatev1alpha1.AddToScheme(scheme.Scheme)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2022-07-01 10:42:08 -04:00
|
|
|
err = nodemaintenancev1beta1.AddToScheme(scheme.Scheme)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2022-06-27 04:58:09 -04:00
|
|
|
|
|
|
|
//+kubebuilder:scaffold:scheme
|
|
|
|
|
|
|
|
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(k8sClient).NotTo(BeNil())
|
2022-06-29 08:47:03 -04:00
|
|
|
|
|
|
|
k8sManager, err := ctrl.NewManager(cfg, ctrl.Options{
|
|
|
|
Scheme: scheme.Scheme,
|
|
|
|
})
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
2022-06-29 08:47:34 -04:00
|
|
|
err = (&AutoscalingStrategyReconciler{
|
|
|
|
Client: k8sManager.GetClient(),
|
|
|
|
Scheme: k8sManager.GetScheme(),
|
|
|
|
}).SetupWithManager(k8sManager)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
2022-12-06 12:48:01 -05:00
|
|
|
err = (&JoiningNodesReconciler{
|
|
|
|
Client: k8sManager.GetClient(),
|
|
|
|
Scheme: k8sManager.GetScheme(),
|
2022-12-09 12:30:20 -05:00
|
|
|
Clock: fakes.clock,
|
2022-12-06 12:48:01 -05:00
|
|
|
}).SetupWithManager(k8sManager)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
2022-06-29 10:47:13 -04:00
|
|
|
err = (&ScalingGroupReconciler{
|
|
|
|
scalingGroupUpdater: fakes.scalingGroupUpdater,
|
|
|
|
Client: k8sManager.GetClient(),
|
|
|
|
Scheme: k8sManager.GetScheme(),
|
|
|
|
}).SetupWithManager(k8sManager)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
2022-06-30 07:12:05 -04:00
|
|
|
err = (&PendingNodeReconciler{
|
|
|
|
nodeStateGetter: fakes.nodeStateGetter,
|
|
|
|
Client: k8sManager.GetClient(),
|
|
|
|
Scheme: k8sManager.GetScheme(),
|
|
|
|
Clock: fakes.clock,
|
|
|
|
}).SetupWithManager(k8sManager)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
2023-01-03 06:09:53 -05:00
|
|
|
err = (&NodeVersionReconciler{
|
2023-01-06 06:08:25 -05:00
|
|
|
kubernetesServerVersionGetter: fakes.k8sVerGetter,
|
|
|
|
nodeReplacer: fakes.nodeReplacer,
|
|
|
|
Client: k8sManager.GetClient(),
|
|
|
|
Scheme: k8sManager.GetScheme(),
|
2022-07-01 10:44:38 -04:00
|
|
|
}).SetupWithManager(k8sManager)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
2022-06-29 08:47:03 -04:00
|
|
|
go func() {
|
|
|
|
defer GinkgoRecover()
|
|
|
|
err = k8sManager.Start(ctx)
|
|
|
|
Expect(err).ToNot(HaveOccurred(), "failed to run manager")
|
|
|
|
}()
|
2023-06-06 12:15:48 -04:00
|
|
|
})
|
2022-06-27 04:58:09 -04:00
|
|
|
|
|
|
|
var _ = AfterSuite(func() {
|
2022-06-29 08:47:03 -04:00
|
|
|
cancel()
|
2022-06-27 04:58:09 -04:00
|
|
|
By("tearing down the test environment")
|
|
|
|
err := testEnv.Stop()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
})
|
2022-06-29 10:47:13 -04:00
|
|
|
|
|
|
|
type fakeCollection struct {
|
|
|
|
scalingGroupUpdater *fakeScalingGroupUpdater
|
2022-06-30 07:12:05 -04:00
|
|
|
nodeStateGetter *stubNodeStateGetter
|
2022-07-01 10:44:38 -04:00
|
|
|
nodeReplacer *stubNodeReplacer
|
2023-01-06 06:08:25 -05:00
|
|
|
k8sVerGetter *stubKubernetesServerVersionGetter
|
2022-06-30 07:12:05 -04:00
|
|
|
clock *testclock.FakeClock
|
2022-06-29 10:47:13 -04:00
|
|
|
}
|
|
|
|
|
2023-04-18 08:20:41 -04:00
|
|
|
func (c *fakeCollection) reset() {
|
|
|
|
c.scalingGroupUpdater.reset()
|
|
|
|
c.nodeStateGetter.setNodeState("")
|
|
|
|
c.nodeReplacer.reset()
|
|
|
|
}
|
|
|
|
|
2022-06-29 10:47:13 -04:00
|
|
|
func newFakes() fakeCollection {
|
|
|
|
return fakeCollection{
|
|
|
|
scalingGroupUpdater: newFakeScalingGroupUpdater(),
|
2022-06-30 07:12:05 -04:00
|
|
|
nodeStateGetter: &stubNodeStateGetter{},
|
2022-07-01 10:44:38 -04:00
|
|
|
nodeReplacer: &stubNodeReplacer{},
|
2023-01-06 06:08:25 -05:00
|
|
|
k8sVerGetter: &stubKubernetesServerVersionGetter{},
|
2022-06-30 07:12:05 -04:00
|
|
|
clock: testclock.NewFakeClock(time.Now()),
|
2022-06-29 10:47:13 -04:00
|
|
|
}
|
|
|
|
}
|
2023-04-18 08:20:41 -04:00
|
|
|
|
|
|
|
func resetEnv() error {
|
|
|
|
// cleanup all nodes
|
|
|
|
nodeList := &corev1.NodeList{}
|
|
|
|
if err := k8sClient.List(context.Background(), nodeList); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, node := range nodeList.Items {
|
|
|
|
if err := k8sClient.Delete(context.Background(), &node); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// cleanup all node versions
|
|
|
|
nodeVersionList := &updatev1alpha1.NodeVersionList{}
|
|
|
|
if err := k8sClient.List(context.Background(), nodeVersionList); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, nodeVersion := range nodeVersionList.Items {
|
|
|
|
if err := k8sClient.Delete(context.Background(), &nodeVersion); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// cleanup all scaling groups
|
|
|
|
scalingGroupList := &updatev1alpha1.ScalingGroupList{}
|
|
|
|
if err := k8sClient.List(context.Background(), scalingGroupList); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, scalingGroup := range scalingGroupList.Items {
|
|
|
|
if err := k8sClient.Delete(context.Background(), &scalingGroup); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// cleanup all pending nodes
|
|
|
|
pendingNodeList := &updatev1alpha1.PendingNodeList{}
|
|
|
|
if err := k8sClient.List(context.Background(), pendingNodeList); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, pendingNode := range pendingNodeList.Items {
|
|
|
|
if err := k8sClient.Delete(context.Background(), &pendingNode); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// cleanup all joining nodes
|
|
|
|
joiningNodeList := &updatev1alpha1.JoiningNodeList{}
|
|
|
|
if err := k8sClient.List(context.Background(), joiningNodeList); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, joiningNode := range joiningNodeList.Items {
|
|
|
|
if err := k8sClient.Delete(context.Background(), &joiningNode); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// cleanup all autoscaling strategies
|
|
|
|
autoscalingStrategyList := &updatev1alpha1.AutoscalingStrategyList{}
|
|
|
|
if err := k8sClient.List(context.Background(), autoscalingStrategyList); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, autoscalingStrategy := range autoscalingStrategyList.Items {
|
|
|
|
if err := k8sClient.Delete(context.Background(), &autoscalingStrategy); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// cleanup all deployments
|
|
|
|
deploymentList := &appsv1.DeploymentList{}
|
|
|
|
if err := k8sClient.List(context.Background(), deploymentList); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, deployment := range deploymentList.Items {
|
|
|
|
if err := k8sClient.Delete(context.Background(), &deployment); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fakes.reset()
|
|
|
|
return nil
|
|
|
|
}
|