Skip to content

Commit

Permalink
test: Gracefully shutdown operator in integration test (#544)
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Pacak <pacak.daniel@gmail.com>
  • Loading branch information
danielpacak committed May 5, 2021
1 parent 4927727 commit e65f26b
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 14 deletions.
3 changes: 2 additions & 1 deletion cmd/starboard-operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/aquasecurity/starboard/pkg/operator"
"github.com/aquasecurity/starboard/pkg/operator/etc"
"github.com/aquasecurity/starboard/pkg/starboard"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
)
Expand Down Expand Up @@ -44,5 +45,5 @@ func run() error {

setupLog.Info("Starting operator", "buildInfo", buildInfo)

return operator.Run(buildInfo, operatorConfig)
return operator.Start(ctrl.SetupSignalHandler(), buildInfo, operatorConfig)
}
5 changes: 5 additions & 0 deletions itest/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/rand"
"k8s.io/utils/pointer"
)

Expand Down Expand Up @@ -77,6 +78,10 @@ func (b *DeploymentBuilder) WithName(name string) *DeploymentBuilder {
return b
}

func (b *DeploymentBuilder) WithRandomName(prefix string) *DeploymentBuilder {
return b.WithName(prefix + "-" + rand.String(5))
}

func (b *DeploymentBuilder) WithNamespace(namespace string) *DeploymentBuilder {
b.namespace = namespace
return b
Expand Down
7 changes: 3 additions & 4 deletions itest/starboard-operator/starboard_operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/rand"
"k8s.io/apimachinery/pkg/util/wait"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
Expand Down Expand Up @@ -71,7 +70,7 @@ var _ = Describe("Starboard Operator", func() {
BeforeEach(func() {
ctx = context.Background()
deploy = helper.NewDeployment().
WithName(baseDeploymentName+"-"+rand.String(5)).
WithRandomName(baseDeploymentName).
WithNamespace(namespaceName).
WithContainer("wordpress", "wordpress:4.9").
Build()
Expand Down Expand Up @@ -105,7 +104,7 @@ var _ = Describe("Starboard Operator", func() {
By("Creating Deployment wordpress")
ctx = context.Background()
deploy = helper.NewDeployment().
WithName(baseDeploymentName+"-"+rand.String(5)).
WithRandomName(baseDeploymentName).
WithNamespace(namespaceName).
WithContainer("wordpress", "wordpress:4.9").
Build()
Expand Down Expand Up @@ -221,7 +220,7 @@ var _ = Describe("Starboard Operator", func() {
By("Creating Deployment wordpress")
ctx = context.Background()
deploy = helper.NewDeployment().
WithName(baseDeploymentName+"-"+rand.String(5)).
WithRandomName(baseDeploymentName).
WithNamespace(namespaceName).
WithContainer("wordpress", "wordpress:4.9").
Build()
Expand Down
26 changes: 19 additions & 7 deletions itest/starboard-operator/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"context"
"path/filepath"
"testing"

Expand All @@ -21,7 +22,11 @@ import (
)

var (
buildInfo = starboard.BuildInfo{Version: "dev", Commit: "none", Date: "unknown"}
buildInfo = starboard.BuildInfo{
Version: "dev",
Commit: "none",
Date: "unknown",
}
)

var (
Expand All @@ -31,6 +36,8 @@ var (
var (
scheme *runtime.Scheme
kubeClient client.Client
startCtx context.Context
stopFunc context.CancelFunc
)

var (
Expand All @@ -45,11 +52,11 @@ func TestStarboardOperator(t *testing.T) {
RunSpecs(t, "Starboard Operator")
}

var _ = BeforeSuite(func(done Done) {
var _ = BeforeSuite(func() {
operatorConfig, err := etc.GetOperatorConfig()
Expect(err).ToNot(HaveOccurred())

logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(operatorConfig.LogDevMode)))

kubeConfig, err := ctrl.GetConfig()
Expect(err).ToNot(HaveOccurred())
Expand All @@ -68,20 +75,25 @@ var _ = BeforeSuite(func(done Done) {
CRDDirectoryPaths: []string{filepath.Join("..", "..", "deploy", "crd")},
}

By("Starting Kubernetes test environment")
_, err = testEnv.Start()
Expect(err).ToNot(HaveOccurred())

startCtx, stopFunc = context.WithCancel(context.Background())

go func() {
defer GinkgoRecover()

err = operator.Run(buildInfo, operatorConfig)
By("Starting Starboard operator")
err = operator.Start(startCtx, buildInfo, operatorConfig)
Expect(err).ToNot(HaveOccurred())
}()

close(done)
}, 60)
})

var _ = AfterSuite(func() {
By("Stopping Starboard operator")
stopFunc()
By("Stopping Kubernetes test environment")
err := testEnv.Stop()
Expect(err).ToNot(HaveOccurred())
})
2 changes: 2 additions & 0 deletions pkg/operator/etc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/caarlos0/env/v6"
)

// Config defines parameters for running the operator.
type Config struct {
Namespace string `env:"OPERATOR_NAMESPACE"`
TargetNamespaces string `env:"OPERATOR_TARGET_NAMESPACES"`
Expand All @@ -27,6 +28,7 @@ type Config struct {
BatchDeleteDelay time.Duration `env:"OPERATOR_BATCH_DELETE_DELAY" envDefault:"10s"`
}

// GetOperatorConfig loads Config from environment variables.
func GetOperatorConfig() (Config, error) {
var config Config
err := env.Parse(&config)
Expand Down
6 changes: 4 additions & 2 deletions pkg/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ var (
setupLog = log.Log.WithName("operator")
)

func Run(buildInfo starboard.BuildInfo, operatorConfig etc.Config) error {
// Start starts all registered reconcilers and blocks until the context is cancelled.
// Returns an error if there is an error starting any reconciler.
func Start(ctx context.Context, buildInfo starboard.BuildInfo, operatorConfig etc.Config) error {
installMode, operatorNamespace, targetNamespaces, err := operatorConfig.ResolveInstallMode()
if err != nil {
return fmt.Errorf("resolving install mode: %w", err)
Expand Down Expand Up @@ -205,7 +207,7 @@ func Run(buildInfo starboard.BuildInfo, operatorConfig etc.Config) error {
}

setupLog.Info("Starting controllers manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
if err := mgr.Start(ctx); err != nil {
return fmt.Errorf("starting controllers manager: %w", err)
}

Expand Down

0 comments on commit e65f26b

Please sign in to comment.