Skip to content

Commit

Permalink
Make operator namespace customisable (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
rallyos authored Apr 5, 2023
1 parent f87de75 commit 2640edb
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 12 deletions.
7 changes: 6 additions & 1 deletion clients/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ func GetClient() (ClientCache, error) {
var err error
var config *rest.Config

namespace := os.Getenv("NAMESPACE")
if namespace == "" {
namespace = "default"
}

config, err = rest.InClusterConfig()
if err != nil {
config, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
Expand All @@ -132,7 +137,7 @@ func GetClient() (ClientCache, error) {
return clientCache, err
}

clientCache.Cache, err = cache.New(config, cache.Options{Namespace: "default", Scheme: scheme})
clientCache.Cache, err = cache.New(config, cache.Options{Namespace: namespace, Scheme: scheme})
if err != nil {
return clientCache, err
}
Expand Down
9 changes: 7 additions & 2 deletions controllers/booking_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controllers
import (
"context"
"fmt"
"os"
"time"

. "github.com/onsi/ginkgo/v2"
Expand All @@ -15,10 +16,10 @@ import (
)

var _ = Describe("Booking controller", func() {
ctx := context.Background()

const (
BookingName = "test-booking"
BookingNamespace = "default"
BookingResourceName = "analytics"
)

Expand All @@ -32,9 +33,13 @@ var _ = Describe("Booking controller", func() {

FinishedBookingStart = fmt.Sprintf("%d-01-01T00:00:00Z", time.Now().AddDate(-1, 0, 0).Year())
FinishedBookingEnd = fmt.Sprintf("%d-01-02T00:00:00Z", time.Now().AddDate(-1, 0, 0).Year())

BookingNamespace = os.Getenv("NAMESPACE")
)

ctx := context.Background()
if BookingNamespace == "" {
BookingNamespace = "default"
}

var bookingSpec managerv1.BookingSpec
var booking *managerv1.Booking
Expand Down
14 changes: 9 additions & 5 deletions controllers/resource_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controllers

import (
"context"
"os"
"time"

. "github.com/onsi/ginkgo/v2"
Expand All @@ -14,18 +15,21 @@ import (
)

var _ = Describe("Resource controller", func() {
ctx := context.Background()

const (
ResourceName = "test-resource"
ResourceTag = "analytics"
ResourceType = "ec2"
ResourceNamespace = "default"
ResourceName = "test-resource"
ResourceTag = "analytics"
ResourceType = "ec2"

timeout = time.Second * 10
interval = time.Millisecond * 250
)

ctx := context.Background()
ResourceNamespace := os.Getenv("NAMESPACE")
if ResourceNamespace == "" {
ResourceNamespace = "default"
}

Context("Resource basics", func() {
BeforeEach(func() {
Expand Down
12 changes: 8 additions & 4 deletions controllers/resourcemonitor_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controllers

import (
"context"
"os"
"time"

. "github.com/onsi/ginkgo/v2"
Expand All @@ -13,17 +14,20 @@ import (
)

var _ = Describe("Resource Monitor controller", func() {
ctx := context.Background()

const (
ResourceName = "test-resource-monitor"
ResourceType = "ec2"
ResourceNamespace = "default"
ResourceName = "test-resource-monitor"
ResourceType = "ec2"

timeout = time.Second * 3
interval = time.Millisecond * 250
)

ctx := context.Background()
ResourceNamespace := os.Getenv("NAMESPACE")
if ResourceNamespace == "" {
ResourceNamespace = "default"
}

Context("Resource monitor management", func() {
BeforeEach(func() {
Expand Down
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ func main() {
var metricsAddr string
var enableLeaderElection bool
var probeAddr string

namespace := os.Getenv("NAMESPACE")
if namespace == "" {
namespace = "default"
}

flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
Expand All @@ -67,6 +73,7 @@ func main() {

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
Namespace: namespace,
MetricsBindAddress: metricsAddr,
Port: 9443,
HealthProbeBindAddress: probeAddr,
Expand Down

0 comments on commit 2640edb

Please sign in to comment.