Skip to content

Commit

Permalink
Introduce disabling envs for namespace resources
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Saratura committed Jun 20, 2023
1 parent fbd9183 commit 2c6f9b1
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 4 deletions.
4 changes: 4 additions & 0 deletions controllers/stardoginstance_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ func (r *StardogInstanceReconciler) Reconcile(ctx context.Context, req ctrl.Requ
return ctrl.Result{Requeue: true, RequeueAfter: ReconFreqErr}, err
}

if environmentDisabled(stardogInstance) {
return ctrl.Result{Requeue: false}, nil
}

sir := &StardogInstanceReconciliation{
reconciliationContext: &ReconciliationContext{
context: ctx,
Expand Down
4 changes: 4 additions & 0 deletions controllers/stardogrole_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ func (r *StardogRoleReconciler) Reconcile(ctx context.Context, req ctrl.Request)
return ctrl.Result{Requeue: true, RequeueAfter: ReconFreqErr}, err
}

if environmentDisabled(stardogRole) {
return ctrl.Result{Requeue: false}, nil
}

srr := &StardogRoleReconciliation{
reconciliationContext: &ReconciliationContext{
context: ctx,
Expand Down
4 changes: 4 additions & 0 deletions controllers/stardoguser_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ func (r *StardogUserReconciler) Reconcile(ctx context.Context, req ctrl.Request)
return ctrl.Result{Requeue: true, RequeueAfter: ReconFreqErr}, err
}

if environmentDisabled(stardogUser) {
return ctrl.Result{Requeue: false}, nil
}

sur := &StardogUserReconciliation{
reconciliationContext: &ReconciliationContext{
context: ctx,
Expand Down
19 changes: 17 additions & 2 deletions controllers/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/vshn/stardog-userrole-operator/stardogrest/models"
"os"
"reflect"
"sigs.k8s.io/controller-runtime/pkg/client"
"strings"
"time"

Expand All @@ -21,14 +22,16 @@ import (
)

var (
ReconFreqErr = time.Second * 30
ReconFreq = time.Duration(0)
ReconFreqErr = time.Second * 30
ReconFreq = time.Duration(0)
disabledEnvironments = ""
)

// InitEnv initialize env variables
func InitEnv() {
ReconFreqErr, _ = time.ParseDuration(os.Getenv("RECONCILIATION_FREQUENCY_ON_ERROR"))
ReconFreq, _ = time.ParseDuration(os.Getenv("RECONCILIATION_FREQUENCY"))
disabledEnvironments = os.Getenv("DISABLED_ENVIRONMENTS")
if ReconFreq < 0 || ReconFreqErr < 0 {
ReconFreq = 0
ReconFreqErr = 0
Expand Down Expand Up @@ -241,3 +244,15 @@ func NotFound(err error) bool {
errType == reflect.TypeOf(users_permissions.NewRemoveUserPermissionNotFound()).String() ||
errType == reflect.TypeOf(db.NewGetDBSizeNotFound()).String()
}

func environmentDisabled(object client.Object) bool {
if disabledEnvironments == "" {
return false
}
for _, env := range strings.Split(disabledEnvironments, ";") {
if env == object.GetNamespace() {
return true
}
}
return false
}
65 changes: 63 additions & 2 deletions controllers/util_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build exclude

package controllers

import (
Expand All @@ -15,6 +13,69 @@ import (
"time"
)

func Test_disabledEnvironments(t *testing.T) {
tests := []struct {
name string
user StardogUser
setEnv func()
expectBool bool
}{
{
name: "GivenNoEnvVariableAndSecret_ThenReturnFalse",
user: StardogUser{
ObjectMeta: metav1.ObjectMeta{Namespace: "stardog-test"},
},
setEnv: func() {},
expectBool: false,
},
{
name: "GivenWrongEnvVariableAndSecret_ThenReturnFalse",
user: StardogUser{
ObjectMeta: metav1.ObjectMeta{Namespace: "stardog-test"},
},
setEnv: func() {
os.Setenv("DISABLED_ENVIRONMENTS", "stardog-non.exists")
},
expectBool: false,
},
{
name: "GivenOneMatchingEnvVariableAndSecret_ThenReturnTrue",
user: StardogUser{
ObjectMeta: metav1.ObjectMeta{Namespace: "stardog-test"},
},
setEnv: func() {
os.Setenv("DISABLED_ENVIRONMENTS", "stardog-test")
},
expectBool: true,
},
{
name: "GivenMoreEnvsWithOneMatchingEnvVariableAndSecret_ThenReturnTrue",
user: StardogUser{
ObjectMeta: metav1.ObjectMeta{Namespace: "stardog-test"},
},
setEnv: func() {
os.Setenv("DISABLED_ENVIRONMENTS", "stardog-test;stardog-prod")
},
expectBool: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

// GIVEN
tt.setEnv()
InitEnv()

// WHEN
disabled := environmentDisabled(&tt.user)

// THEN
assert.Equal(t, tt.expectBool, disabled)
os.Unsetenv("DISABLED_ENVIRONMENTS")
})
}
}

func Test_getData(t *testing.T) {

tests := []struct {
Expand Down

0 comments on commit 2c6f9b1

Please sign in to comment.