Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

Commit

Permalink
Merge pull request #35 from cloudfoundry-incubator/165900520-multinam…
Browse files Browse the repository at this point in the history
…espace

Add testing helper to create labeled namespaces
  • Loading branch information
viovanov authored Apr 26, 2020
2 parents 4b32724 + 67ba786 commit 0571fa2
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 38 deletions.
24 changes: 24 additions & 0 deletions pkg/cmd/monitored_id.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cmd

import (
flag "github.com/spf13/pflag"
"github.com/spf13/viper"

"code.cloudfoundry.org/quarks-utils/pkg/config"
)

// MonitoredID sets the moitored id from viper
func MonitoredID(cfg *config.Config) {
id := viper.GetString("monitored-id")
if id == "" {
id = "default"
}
cfg.MonitoredID = id
}

// MonitoredIDFlags adds to viper flags
func MonitoredIDFlags(pf *flag.FlagSet, argToEnv map[string]string) {
pf.String("monitored-id", "default", "only monitor namespaces with this id in their namespace label")
viper.BindPFlag("monitored-id", pf.Lookup("monitored-id"))
argToEnv["monitored-id"] = "MONITORED_ID"
}
16 changes: 0 additions & 16 deletions pkg/cmd/namespaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ func OperatorNamespace(cfg *config.Config, log *zap.SugaredLogger, name string)
return operatorNamespace
}

// WatchNamespace sets the namespace which is watched and where most resources are created
func WatchNamespace(cfg *config.Config, log *zap.SugaredLogger) string {
watchNamespace := viper.GetString("watch-namespace")
cfg.Namespace = watchNamespace
return watchNamespace
}

// OperatorNamespaceFlags adds to viper flags
func OperatorNamespaceFlags(pf *flag.FlagSet, argToEnv map[string]string, name string) {
pf.StringP(name, "n", "default", "The operator namespace, for the webhook service")
Expand All @@ -35,15 +28,6 @@ func OperatorNamespaceFlags(pf *flag.FlagSet, argToEnv map[string]string, name s
argToEnv[name] = envName(name)
}

// WatchNamespaceFlags adds to viper flags
func WatchNamespaceFlags(pf *flag.FlagSet, argToEnv map[string]string) {
pf.StringP("watch-namespace", "a", "staging", "Act on this namespace, watch for BOSH deployments and create resources")

viper.BindPFlag("watch-namespace", pf.Lookup("watch-namespace"))

argToEnv["watch-namespace"] = "WATCH_NAMESPACE"
}

func envName(name string) string {
n := strings.ToUpper(name)
return strings.Replace(n, "-", "_", -1)
Expand Down
23 changes: 2 additions & 21 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ type Config struct {
CtxTimeOut time.Duration
MeltdownDuration time.Duration
MeltdownRequeueAfter time.Duration
// Namespace that is being watched by controllers
Namespace string
// MonitoredID we look for in namespace labels, before acting
MonitoredID string
// OperatorNamespace is where the webhook services of the operator are placed
OperatorNamespace string
WebhookUseServiceRef bool
Expand All @@ -41,22 +41,3 @@ func NewDefaultConfig(fs afero.Fs) *Config {
Fs: fs,
}
}

// NewConfig returns a new Config for a manager of controllers
func NewConfig(namespace string, operatorNamespace string, ctxTimeOut int, useServiceRef bool, host string, port int32, fs afero.Fs, maxBoshDeploymentWorkers, maxQuarksJobWorkers, maxQuarksSecretWorkers, maxQuarksStatefulSetWorkers int) *Config {
return &Config{
CtxTimeOut: time.Duration(ctxTimeOut) * time.Second,
MeltdownDuration: MeltdownDuration,
MeltdownRequeueAfter: MeltdownRequeueAfter,
Namespace: namespace,
OperatorNamespace: operatorNamespace,
WebhookUseServiceRef: useServiceRef,
WebhookServerHost: host,
WebhookServerPort: port,
Fs: fs,
MaxBoshDeploymentWorkers: maxBoshDeploymentWorkers,
MaxQuarksJobWorkers: maxQuarksJobWorkers,
MaxQuarksSecretWorkers: maxQuarksSecretWorkers,
MaxQuarksStatefulSetWorkers: maxQuarksStatefulSetWorkers,
}
}
17 changes: 17 additions & 0 deletions testing/e2ehelper/e2e_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@ var (
// TearDownFunc tears down the resource
type TearDownFunc func() error

// TearDownAll calls all passed in tear down functions in order
func TearDownAll(funcs []TearDownFunc) error {
var messages string
for _, f := range funcs {
if f != nil {
err := f()
if err != nil {
messages = fmt.Sprintf("%v%v\n", messages, err.Error())
}
}
}
if messages != "" {
return errors.New(messages)
}
return nil
}

// SetUpEnvironment ensures helm binary can run
// being able to reach tiller, and eventually it
// will install the cf-operator chart.
Expand Down
8 changes: 7 additions & 1 deletion testing/machine/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,16 @@ func NewMachine() Machine {

// CreateNamespace creates a namespace, it doesn't return an error if the namespace exists
func (m *Machine) CreateNamespace(namespace string) (TearDownFunc, error) {
return m.CreateLabeledNamespace(namespace, map[string]string{})
}

// CreateLabeledNamespace creates a namespace, it doesn't return an error if the namespace exists
func (m *Machine) CreateLabeledNamespace(namespace string, labels map[string]string) (TearDownFunc, error) {
client := m.Clientset.CoreV1().Namespaces()
ns := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespace,
Name: namespace,
Labels: labels,
},
}
_, err := client.Create(ns)
Expand Down
19 changes: 19 additions & 0 deletions testing/testhelper/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package testhelper

import (
"time"

"github.com/spf13/afero"

"code.cloudfoundry.org/quarks-utils/pkg/config"
)

// NewConfigWithTimeout returns a default config, with a context timeout
func NewConfigWithTimeout(timeout time.Duration) *config.Config {
return &config.Config{
Fs: afero.NewMemMapFs(),
CtxTimeOut: timeout,
MeltdownDuration: config.MeltdownDuration,
MeltdownRequeueAfter: config.MeltdownRequeueAfter,
}
}

0 comments on commit 0571fa2

Please sign in to comment.