Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

Watch all namespaces #82

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 22 additions & 18 deletions e2e/e2e.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@ import (
)

type E2E struct {
ctx context.Context
cs *kubernetes.Clientset
cfg *rest.Config
kubeconfig string
namespace string
moduleURL string
crds []crd.CRD
}

func NewE2E(namespace, kubeconfig, module string, crdsNames []string) *E2E {
ctx context.Context
cs *kubernetes.Clientset
cfg *rest.Config
kubeconfig string
ctrlNamespace string
namespace string
moduleURL string
name string
crds []crd.CRD
}

func NewE2E(name, namespace, kubeconfig, module string, crdsNames []string) *E2E {
cfg, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
logrus.Fatalf("Error building kubeconfig: %s", err.Error())
Expand All @@ -45,13 +47,15 @@ func NewE2E(namespace, kubeconfig, module string, crdsNames []string) *E2E {
}

return &E2E{
ctx: signals.SetupSignalHandler(context.Background()),
cs: cs,
cfg: cfg,
kubeconfig: kubeconfig,
namespace: namespace,
moduleURL: module,
crds: crds,
ctx: signals.SetupSignalHandler(context.Background()),
cs: cs,
cfg: cfg,
kubeconfig: kubeconfig,
ctrlNamespace: name,
namespace: namespace,
name: name,
moduleURL: module,
crds: crds,
}
}

Expand Down Expand Up @@ -215,7 +219,7 @@ func (e *E2E) getConfigMapEnv() *v1.ConfigMap {
Namespace: e.namespace,
},
Data: map[string]string{
"test_config_map_env": e.namespace,
"TF_VAR_test_config_map_env": e.namespace,
},
}
}
Expand Down
5 changes: 3 additions & 2 deletions e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import (
)

const (
Namespace = "terraform-controller"
Name = "terraform-controller"
Namespace = "app"
ModuleURL = "https://github.com/luthermonson/terraform-controller-test-module"
TestConfigMap = "test-config-map"
)
Expand All @@ -35,7 +36,7 @@ func TestMain(m *testing.M) {
namespace = Namespace
}

e = NewE2E(namespace, kubeconfig, ModuleURL, []string{
e = NewE2E(Name, namespace, kubeconfig, ModuleURL, []string{
"Module",
"State",
"Execution",
Expand Down
29 changes: 20 additions & 9 deletions e2e/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ func (e *E2E) initialize() error {
return err
}

_, err = e.cs.CoreV1().Namespaces().Create(e.ctx, e.getCtrlNs(), v13.CreateOptions{})
if err != nil {
return err
}
_, err = e.cs.CoreV1().Namespaces().Create(e.ctx, e.getNs(), v13.CreateOptions{})
if err != nil {
return err
Expand All @@ -34,13 +38,13 @@ func (e *E2E) initialize() error {
return err
}

_, err = e.cs.CoreV1().ServiceAccounts(e.namespace).Create(e.ctx, e.getSa(), v13.CreateOptions{})
_, err = e.cs.CoreV1().ServiceAccounts(e.ctrlNamespace).Create(e.ctx, e.getSa(), v13.CreateOptions{})
if err != nil {
return err
}

err = wait.Poll(time.Second, 15*time.Second, func() (bool, error) {
_, err := e.cs.CoreV1().ServiceAccounts(e.namespace).Get(e.ctx, e.namespace, v13.GetOptions{})
_, err := e.cs.CoreV1().ServiceAccounts(e.ctrlNamespace).Get(e.ctx, e.name, v13.GetOptions{})
if err == nil {
return true, nil
}
Expand All @@ -67,15 +71,22 @@ func (e *E2E) getNs() *v12.Namespace {
},
}
}
func (e *E2E) getCtrlNs() *v12.Namespace {
return &v12.Namespace{
ObjectMeta: v13.ObjectMeta{
Name: e.name,
},
}
}

func (e *E2E) getSa() *v12.ServiceAccount {
return &v12.ServiceAccount{
ObjectMeta: v13.ObjectMeta{
Name: e.namespace,
Namespace: e.namespace,
Name: e.name,
Namespace: e.ctrlNamespace,
Labels: map[string]string{
"apps.kubernetes.io/component": "controller",
"apps.kubernetes.io/name": e.namespace,
"apps.kubernetes.io/name": e.name,
},
},
}
Expand All @@ -84,10 +95,10 @@ func (e *E2E) getSa() *v12.ServiceAccount {
func (e *E2E) getCrb() *v1.ClusterRoleBinding {
return &v1.ClusterRoleBinding{
ObjectMeta: v13.ObjectMeta{
Name: e.namespace,
Name: e.name,
Labels: map[string]string{
"apps.kubernetes.io/component": "controller",
"apps.kubernetes.io/name": e.namespace,
"apps.kubernetes.io/name": e.name,
},
},
RoleRef: v1.RoleRef{
Expand All @@ -98,8 +109,8 @@ func (e *E2E) getCrb() *v1.ClusterRoleBinding {
Subjects: []v1.Subject{
{
Kind: "ServiceAccount",
Name: e.namespace,
Namespace: e.namespace,
Name: e.name,
Namespace: e.ctrlNamespace,
},
},
}
Expand Down
14 changes: 7 additions & 7 deletions e2e/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,25 @@ func lowerPlural(s string) string {

func TestGetNs(t *testing.T) {
assert := assert.New(t)
ns := e.getNs()
ns := e.getCtrlNs()
assert.Equal(reflect.TypeOf(ns), reflect.TypeOf(&corev1.Namespace{}))
assert.Equal(ns.ObjectMeta.Name, e.namespace)
assert.Equal(ns.ObjectMeta.Name, e.ctrlNamespace)
}

func TestGetSa(t *testing.T) {
assert := assert.New(t)
sa := e.getSa()
assert.Equal(reflect.TypeOf(sa), reflect.TypeOf(&corev1.ServiceAccount{}))
assert.Equal(sa.ObjectMeta.Name, e.namespace)
assert.Equal(sa.ObjectMeta.Namespace, e.namespace)
assert.Equal(sa.ObjectMeta.Name, e.name)
assert.Equal(sa.ObjectMeta.Namespace, e.ctrlNamespace)
}

func TestGetCrb(t *testing.T) {
assert := assert.New(t)
crb := e.getCrb()
assert.Equal(reflect.TypeOf(crb), reflect.TypeOf(&rbacv1.ClusterRoleBinding{}))
assert.Equal(crb.ObjectMeta.Name, e.namespace)
assert.Equal(crb.ObjectMeta.Name, e.name)
assert.Equal(crb.Subjects[0].Kind, "ServiceAccount")
assert.Equal(crb.Subjects[0].Name, e.namespace)
assert.Equal(crb.Subjects[0].Namespace, e.namespace)
assert.Equal(crb.Subjects[0].Name, e.name)
assert.Equal(crb.Subjects[0].Namespace, e.ctrlNamespace)
}
16 changes: 5 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ func main() {
EnvVar: "KUBECONFIG",
Value: "${HOME}/.kube/config",
},
cli.StringFlag{
Name: "namespace",
EnvVar: "NAMESPACE",
Value: "default",
},
cli.StringFlag{
Name: "masterurl",
EnvVar: "MASTERURL",
Expand Down Expand Up @@ -83,9 +78,8 @@ func run(c *cli.Context) {

threadiness := c.Int("threads")
masterurl := c.String("masterurl")
ns := c.String("namespace")

logrus.Printf("Booting Terraform Controller, namespace: %s", ns)
logrus.Println("Booting Terraform Controller")

ctx := signals.SetupSignalHandler(context.Background())

Expand All @@ -94,22 +88,22 @@ func run(c *cli.Context) {
logrus.Fatalf("Error building kubeconfig: %s", err.Error())
}

tfFactory, err := terraformcontroller.NewFactoryFromConfigWithNamespace(cfg, ns)
tfFactory, err := terraformcontroller.NewFactoryFromConfig(cfg)
if err != nil {
logrus.Fatalf("Error building terraform controllers: %s", err.Error())
}

coreFactory, err := core.NewFactoryFromConfigWithNamespace(cfg, ns)
coreFactory, err := core.NewFactoryFromConfig(cfg)
if err != nil {
logrus.Fatalf("Error building core controllers: %s", err.Error())
}

rbacFactory, err := rbac.NewFactoryFromConfigWithNamespace(cfg, ns)
rbacFactory, err := rbac.NewFactoryFromConfig(cfg)
if err != nil {
logrus.Fatalf("Error building rbac controllers: %s", err.Error())
}

batchFactory, err := batch.NewFactoryFromConfigWithNamespace(cfg, ns)
batchFactory, err := batch.NewFactoryFromConfig(cfg)
if err != nil {
logrus.Fatalf("Error building rbac controllers: %s", err.Error())
}
Expand Down
1 change: 0 additions & 1 deletion scripts/e2e
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ k3s ctr images import --base-name terraform-controller-executor artifacts/images
k3s kubectl rollout status deployment coredns -n kube-system # make sure coredns is actually running

export KUBECONFIG=$kubeconfig
export NAMESPACE=terraform-controller

./bin/terraform-controller --threads 1&
tfc_pid=$!
Expand Down