Skip to content

Commit

Permalink
chore: rename vars
Browse files Browse the repository at this point in the history
  • Loading branch information
binbin-li committed Apr 17, 2024
1 parent f3f2d13 commit 1d3450e
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 50 deletions.
4 changes: 2 additions & 2 deletions pkg/controllers/certificatestore_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (r *CertificateStoreReconciler) Reconcile(ctx context.Context, req ctrl.Req
if apierrors.IsNotFound(err) {
logger.Infof("deletion detected, removing certificate store %v", resource)
// TODO: pass the actual namespace once multi-tenancy is supported.
CertificatesMap.DeleteStore(constants.EmptyNamespace, resource)
NamespacedCertStores.DeleteStore(constants.EmptyNamespace, resource)
} else {
logger.Error(err, "unable to fetch certificate store")
}
Expand Down Expand Up @@ -96,7 +96,7 @@ func (r *CertificateStoreReconciler) Reconcile(ctx context.Context, req ctrl.Req
}

// TODO: pass the actual namespace once multi-tenancy is supported.
CertificatesMap.AddStore(constants.EmptyNamespace, resource, certificates)
NamespacedCertStores.AddStore(constants.EmptyNamespace, resource, certificates)
isFetchSuccessful = true
emptyErrorString := ""
writeCertStoreStatus(ctx, r, certStore, logger, isFetchSuccessful, emptyErrorString, lastFetchedTime, certAttributes)
Expand Down
4 changes: 2 additions & 2 deletions pkg/controllers/policy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (r *PolicyReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
if apierrors.IsNotFound(err) {
policyLogger.Infof("delete event detected, removing policy %s", resource)
// TODO: pass the actual namespace once multi-tenancy is supported.
ActivePolicies.DeletePolicy(constants.EmptyNamespace, resource)
NamespacedPolicies.DeletePolicy(constants.EmptyNamespace, resource)
} else {
policyLogger.Error("failed to get Policy: ", err)
}
Expand Down Expand Up @@ -96,7 +96,7 @@ func policyAddOrReplace(spec configv1beta1.PolicySpec) error {
}

// TODO: pass the actual namespace once multi-tenancy is supported.
ActivePolicies.AddPolicy(constants.EmptyNamespace, constants.RatifyPolicy, policyEnforcer)
NamespacedPolicies.AddPolicy(constants.EmptyNamespace, constants.RatifyPolicy, policyEnforcer)
return nil
}

Expand Down
15 changes: 8 additions & 7 deletions pkg/controllers/resource_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ import (
)

var (
VerifierMap = verifiers.NewActiveVerifiers()
// NamespacedVerifiers is a map between namespace and verifiers.
NamespacedVerifiers = verifiers.NewActiveVerifiers()

// ActivePolicy is the active policy generated from CRD. There would be exactly
// NamespacedPolicies is the active policy generated from CRD. There would be exactly
// one active policy belonging to a namespace at any given time.
ActivePolicies = policies.NewActivePolicies()
NamespacedPolicies = policies.NewActivePolicies()

// a map to track active stores
StoreMap = rs.NewActiveStores()
// NamespacedStores is a map to track active stores across namespaces.
NamespacedStores = rs.NewActiveStores()

// a map between CertificateStore name to array of x509 certificates
CertificatesMap = cs.NewActiveCertStores()
// NamespacedCertStores is a map between namespace and CertificateStores.
NamespacedCertStores = cs.NewActiveCertStores()
)
4 changes: 2 additions & 2 deletions pkg/controllers/store_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (r *StoreReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
if apierrors.IsNotFound(err) {
storeLogger.Infof("deletion detected, removing store %v", req.Name)
// TODO: pass the actual namespace once multi-tenancy is supported.
StoreMap.DeleteStore(constants.EmptyNamespace, resource)
NamespacedStores.DeleteStore(constants.EmptyNamespace, resource)
} else {
storeLogger.Error(err, "unable to fetch store")
}
Expand Down Expand Up @@ -112,7 +112,7 @@ func storeAddOrReplace(spec configv1beta1.StoreSpec, fullname string) error {
}

// TODO: pass the actual namespace once multi-tenancy is supported.
StoreMap.AddStore(constants.EmptyNamespace, fullname, storeReference)
NamespacedStores.AddStore(constants.EmptyNamespace, fullname, storeReference)
logrus.Infof("store '%v' added to store map", storeReference.Name())

return nil
Expand Down
28 changes: 14 additions & 14 deletions pkg/controllers/store_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ func TestStoreAdd_EmptyParameter(t *testing.T) {
if err := storeAddOrReplace(testStoreSpec, "oras"); err != nil {
t.Fatalf("storeAddOrReplace() expected no error, actual %v", err)
}
if StoreMap.GetStoreCount() != 1 {
t.Fatalf("Store map expected size 1, actual %v", StoreMap.GetStoreCount())
if NamespacedStores.GetStoreCount() != 1 {
t.Fatalf("Store map expected size 1, actual %v", NamespacedStores.GetStoreCount())
}
}

func TestStoreAdd_WithParameters(t *testing.T) {
resetStoreMap()
if StoreMap.GetStoreCount() != 0 {
t.Fatalf("Store map expected size 0, actual %v", StoreMap.GetStoreCount())
if NamespacedStores.GetStoreCount() != 0 {
t.Fatalf("Store map expected size 0, actual %v", NamespacedStores.GetStoreCount())
}
dirPath, err := utils.CreatePlugin(sampleName)
if err != nil {
Expand All @@ -69,8 +69,8 @@ func TestStoreAdd_WithParameters(t *testing.T) {
if err := storeAddOrReplace(testStoreSpec, "testObject"); err != nil {
t.Fatalf("storeAddOrReplace() expected no error, actual %v", err)
}
if StoreMap.GetStoreCount() != 1 {
t.Fatalf("Store map expected size 1, actual %v", StoreMap.GetStoreCount())
if NamespacedStores.GetStoreCount() != 1 {
t.Fatalf("Store map expected size 1, actual %v", NamespacedStores.GetStoreCount())
}
}

Expand Down Expand Up @@ -138,8 +138,8 @@ func TestStore_UpdateAndDelete(t *testing.T) {
if err := storeAddOrReplace(testStoreSpec, sampleName); err != nil {
t.Fatalf("storeAddOrReplace() expected no error, actual %v", err)
}
if StoreMap.GetStoreCount() != 1 {
t.Fatalf("Store map expected size 1, actual %v", StoreMap.GetStoreCount())
if NamespacedStores.GetStoreCount() != 1 {
t.Fatalf("Store map expected size 1, actual %v", NamespacedStores.GetStoreCount())
}

// modify the Store
Expand All @@ -153,19 +153,19 @@ func TestStore_UpdateAndDelete(t *testing.T) {
}

// validate no Store has been added
if StoreMap.GetStoreCount() != 1 {
t.Fatalf("Store map should be 1 after replacement, actual %v", StoreMap.GetStoreCount())
if NamespacedStores.GetStoreCount() != 1 {
t.Fatalf("Store map should be 1 after replacement, actual %v", NamespacedStores.GetStoreCount())
}

StoreMap.DeleteStore(constants.EmptyNamespace, sampleName)
NamespacedStores.DeleteStore(constants.EmptyNamespace, sampleName)

if StoreMap.GetStoreCount() != 0 {
t.Fatalf("Store map should be 0 after deletion, actual %v", StoreMap.GetStoreCount())
if NamespacedStores.GetStoreCount() != 0 {
t.Fatalf("Store map should be 0 after deletion, actual %v", NamespacedStores.GetStoreCount())
}
}

func resetStoreMap() {
StoreMap = rs.NewActiveStores()
NamespacedStores = rs.NewActiveStores()
}

func getOrasStoreSpec(pluginName, pluginPath string) configv1beta1.StoreSpec {
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/utils/cert_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ import (
// returns the internal certificate map
// TODO: returns certificates from both cluster-wide and given namespace as namespaced verifier could access both.
func GetCertificatesMap(ctx context.Context) map[string][]*x509.Certificate {
return controllers.CertificatesMap.GetCertStores(ctxUtils.GetNamespace(ctx))
return controllers.NamespacedCertStores.GetCertStores(ctxUtils.GetNamespace(ctx))
}
4 changes: 2 additions & 2 deletions pkg/controllers/utils/cert_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
)

func TestGetCertificatesMap(t *testing.T) {
controllers.CertificatesMap = cs.NewActiveCertStores()
controllers.CertificatesMap.AddStore("default", "default/certStore", []*x509.Certificate{})
controllers.NamespacedCertStores = cs.NewActiveCertStores()
controllers.NamespacedCertStores.AddStore("default", "default/certStore", []*x509.Certificate{})
ctx := ctxUtils.SetContextWithNamespace(context.Background(), "default")

if certs := GetCertificatesMap(ctx); len(certs) != 1 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/controllers/verifier_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (r *VerifierReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
if apierrors.IsNotFound(err) {
verifierLogger.Infof("delete event detected, removing verifier %v", resource)
// TODO: pass the actual namespace once multi-tenancy is supported.
VerifierMap.DeleteVerifier(constants.EmptyNamespace, resource)
NamespacedVerifiers.DeleteVerifier(constants.EmptyNamespace, resource)
} else {
verifierLogger.Error(err, "unable to fetch verifier")
}
Expand Down Expand Up @@ -119,7 +119,7 @@ func verifierAddOrReplace(spec configv1beta1.VerifierSpec, objectName string, na
return err
}
// TODO: pass the actual namespace once multi-tenancy is supported.
VerifierMap.AddVerifier(constants.EmptyNamespace, objectName, referenceVerifier)
NamespacedVerifiers.AddVerifier(constants.EmptyNamespace, objectName, referenceVerifier)
logrus.Infof("verifier '%v' added to verifier map", referenceVerifier.Name())

return nil
Expand Down
30 changes: 15 additions & 15 deletions pkg/controllers/verifier_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const licenseChecker = "licensechecker"

func TestMain(m *testing.M) {
// make sure to reset verifierMap before each test run
VerifierMap = verifiers.NewActiveVerifiers()
NamespacedVerifiers = verifiers.NewActiveVerifiers()
code := m.Run()
os.Exit(code)
}
Expand All @@ -56,15 +56,15 @@ func TestVerifierAdd_EmptyParameter(t *testing.T) {
if err := verifierAddOrReplace(testVerifierSpec, sampleName, constants.EmptyNamespace); err != nil {
t.Fatalf("verifierAddOrReplace() expected no error, actual %v", err)
}
if VerifierMap.GetVerifierCount() != 1 {
t.Fatalf("Verifier map expected size 1, actual %v", VerifierMap.GetVerifierCount())
if NamespacedVerifiers.GetVerifierCount() != 1 {
t.Fatalf("Verifier map expected size 1, actual %v", NamespacedVerifiers.GetVerifierCount())
}
}

func TestVerifierAdd_WithParameters(t *testing.T) {
resetVerifierMap()
if VerifierMap.GetVerifierCount() != 0 {
t.Fatalf("Verifier map expected size 0, actual %v", VerifierMap.GetVerifierCount())
if NamespacedVerifiers.GetVerifierCount() != 0 {
t.Fatalf("Verifier map expected size 0, actual %v", NamespacedVerifiers.GetVerifierCount())
}

dirPath, err := utils.CreatePlugin(licenseChecker)
Expand All @@ -78,8 +78,8 @@ func TestVerifierAdd_WithParameters(t *testing.T) {
if err := verifierAddOrReplace(testVerifierSpec, "testObject", constants.EmptyNamespace); err != nil {
t.Fatalf("verifierAddOrReplace() expected no error, actual %v", err)
}
if VerifierMap.GetVerifierCount() != 1 {
t.Fatalf("Verifier map expected size 1, actual %v", VerifierMap.GetVerifierCount())
if NamespacedVerifiers.GetVerifierCount() != 1 {
t.Fatalf("Verifier map expected size 1, actual %v", NamespacedVerifiers.GetVerifierCount())
}
}

Expand Down Expand Up @@ -109,8 +109,8 @@ func TestVerifier_UpdateAndDelete(t *testing.T) {
if err := verifierAddOrReplace(testVerifierSpec, licenseChecker, constants.EmptyNamespace); err != nil {
t.Fatalf("verifierAddOrReplace() expected no error, actual %v", err)
}
if VerifierMap.GetVerifierCount() != 1 {
t.Fatalf("Verifier map expected size 1, actual %v", VerifierMap.GetVerifierCount())
if NamespacedVerifiers.GetVerifierCount() != 1 {
t.Fatalf("Verifier map expected size 1, actual %v", NamespacedVerifiers.GetVerifierCount())
}

// modify the verifier
Expand All @@ -121,14 +121,14 @@ func TestVerifier_UpdateAndDelete(t *testing.T) {
}

// validate no verifier has been added
if VerifierMap.GetVerifierCount() != 1 {
t.Fatalf("Verifier map should be 1 after replacement, actual %v", VerifierMap.GetVerifierCount())
if NamespacedVerifiers.GetVerifierCount() != 1 {
t.Fatalf("Verifier map should be 1 after replacement, actual %v", NamespacedVerifiers.GetVerifierCount())
}

VerifierMap.DeleteVerifier(constants.EmptyNamespace, licenseChecker)
NamespacedVerifiers.DeleteVerifier(constants.EmptyNamespace, licenseChecker)

if VerifierMap.GetVerifierCount() != 0 {
t.Fatalf("Verifier map should be 0 after deletion, actual %v", VerifierMap.GetVerifierCount())
if NamespacedVerifiers.GetVerifierCount() != 0 {
t.Fatalf("Verifier map should be 0 after deletion, actual %v", NamespacedVerifiers.GetVerifierCount())
}
}

Expand Down Expand Up @@ -206,7 +206,7 @@ func TestGetCertStoreNamespace(t *testing.T) {
}

func resetVerifierMap() {
VerifierMap = verifiers.NewActiveVerifiers()
NamespacedVerifiers = verifiers.NewActiveVerifiers()
}

func getLicenseCheckerFromParam(parametersString, pluginPath string) configv1beta1.VerifierSpec {
Expand Down
6 changes: 3 additions & 3 deletions pkg/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ func StartServer(httpServerAddress, configFilePath, certDirectory, caCertFile st
server, err := httpserver.NewServer(context.Background(), httpServerAddress, func(ctx context.Context) *ef.Executor {
namespace := ctxUtils.GetNamespace(ctx)

activeVerifiers := controllers.VerifierMap.GetVerifiers(namespace)
activePolicyEnforcer := controllers.ActivePolicies.GetPolicy(namespace)
activeStores := controllers.StoreMap.GetStores(namespace)
activeVerifiers := controllers.NamespacedVerifiers.GetVerifiers(namespace)
activePolicyEnforcer := controllers.NamespacedPolicies.GetPolicy(namespace)
activeStores := controllers.NamespacedStores.GetStores(namespace)

// return executor with latest configuration
executor := ef.Executor{
Expand Down

0 comments on commit 1d3450e

Please sign in to comment.