Skip to content

Commit

Permalink
Feature/skip tenant resource provisioning (#7)
Browse files Browse the repository at this point in the history
* SkipTenant and SkipProvisioning labels

* renamed provisioning controller

* skip tenant tests

* updated readme

* fix merge

Co-authored-by: Radu Popovici <[email protected]>
  • Loading branch information
oncicaradupopovici and Radu Popovici authored May 17, 2022
1 parent 7b49acf commit 3dd2573
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 8 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ spec:
platformRef: charismaonline.qa
```
## provisioning-controller
## provisioning.totalsoft.ro
monitors infrastructure manifests and provisions the desired infrastructure for every platform tenant.
## Configuration
> *Note* You can skip provisioning for one tenant by adding the label `provisioning.totalsoft.ro/skip-provisioning`="true"

### Env
| Variable | example value | details |
|----------------|-----------------------|----------------------------------------------------|
Expand All @@ -42,8 +43,7 @@ monitors infrastructure manifests and provisions the desired infrastructure for
| VAULT_TOKEN | {token} | vault token |


### Kubernetes CRD
#### AzureDatabase
### AzureDatabase
Definition can be found [here](./helm/crds/provisioning.totalsoft.ro_azuredatabases.yaml)

Example:
Expand All @@ -55,6 +55,8 @@ spec:
platformRef: charismaonline.qa
```

> *Note* You can skip provisioning for some tenant by adding the label `provisioning.totalsoft.ro/skip-tenant-SOME_TENANT_CODE`="true"


#### AzureManagedDatabase
Definition can be found [here](./helm/crds/provisioning.totalsoft.ro_azuremanageddatabases.yaml)
Expand All @@ -80,3 +82,7 @@ spec:
sasToken: my-saas-token
platformRef: charismaonline.qa
```
> *Note* You can skip provisioning for some tenant by adding the label `provisioning.totalsoft.ro/skip-tenant-SOME_TENANT_CODE`="true"


## configuration.totalsoft.ro
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ import (
const (
controllerAgentName = "provisioning-controller"
// SuccessSynced is used as part of the Event 'reason' when a Resource is synced
SuccessSynced = "Synced successfully"
ErrorSynced = "Error"
SuccessSynced = "Synced successfully"
ErrorSynced = "Error"
SkipTenantLabelFormat = "provisioning.totalsoft.ro/skip-tenant-%s"
SkipProvisioningLabel = "provisioning.totalsoft.ro/skip-provisioning"
)

// ProvisioningController is the controller implementation for Tenant resources
Expand Down Expand Up @@ -205,7 +207,13 @@ func (c *ProvisioningController) syncHandler(key string) error {
return err
}

azureDbs, err := c.azureDbInformer.Lister().List(labels.Everything())
skipTenantLabel := fmt.Sprintf(SkipTenantLabelFormat, tenant.Spec.Code)
skipTenantLabelSelector, err := labels.Parse(fmt.Sprintf("%s!=true", skipTenantLabel))
if err != nil {
return err
}

azureDbs, err := c.azureDbInformer.Lister().List(skipTenantLabelSelector)
if err != nil {
return err
}
Expand All @@ -219,7 +227,7 @@ func (c *ProvisioningController) syncHandler(key string) error {
}
azureDbs = azureDbs[:n]

azureManagedDbs, err := c.azureManagedDbInformer.Lister().List(labels.Everything())
azureManagedDbs, err := c.azureManagedDbInformer.Lister().List(skipTenantLabelSelector)
if err != nil {
return err
}
Expand Down Expand Up @@ -294,6 +302,10 @@ func (c *ProvisioningController) enqueueTenant(tenant *platformv1.Tenant) {
var tenantKey string
var err error

if v, ok := tenant.Labels[SkipProvisioningLabel]; ok && v == "true" {
return
}

if tenantKey, err = cache.MetaNamespaceKeyFunc(tenant); err != nil {
utilruntime.HandleError(err)
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,30 @@ func TestProvisioningController_processNextWorkItem(t *testing.T) {

})

t.Run("skip tenant provisioning", func(t *testing.T) {
var outputs []result
aSkipProvisioningTenant := newTenant("dev1", "dev")
aSkipProvisioningTenant.ObjectMeta.Labels = map[string]string{
SkipProvisioningLabel: "true",
}
objects := []runtime.Object{
aSkipProvisioningTenant,
}
clientset := fakeClientset.NewSimpleClientset(objects...)
infraCreator := func(platform string, tenant *platformv1.Tenant, infra *provisioners.InfrastructureManifests) error {
outputs = append(outputs, result{platform, tenant, infra})
return nil
}
c := NewProvisioningController(clientset, infraCreator, nil, nil)
c.factory.Start(nil)
c.factory.WaitForCacheSync(nil)

if c.workqueue.Len() != 0 {
t.Error("queue should have only 0 items, but it has", c.workqueue.Len())
}

})

t.Run("add same tenant multiple times", func(t *testing.T) {
var outputs []result
tenant := newTenant("dev1", "dev")
Expand Down Expand Up @@ -149,6 +173,7 @@ func TestProvisioningController_processNextWorkItem(t *testing.T) {
t.Error("expected one managed db, got", len(outputs[0].infra.AzureManagedDbs))
}
})

t.Run("add one tenant and one database different platforms", func(t *testing.T) {
var outputs []result
objects := []runtime.Object{
Expand Down Expand Up @@ -191,6 +216,49 @@ func TestProvisioningController_processNextWorkItem(t *testing.T) {
t.Error("expected no managed db, got", len(outputs[0].infra.AzureManagedDbs))
}
})

t.Run("skip tenant resource provisioning", func(t *testing.T) {
var outputs []result

tenant := newTenant("dev1", "dev")
azureDb := newAzureDb("db1", "dev")
azureDb.ObjectMeta.Labels = map[string]string{
"provisioning.totalsoft.ro/skip-tenant-dev1": "true",
}
objects := []runtime.Object{
tenant,
azureDb,
}
clientset := fakeClientset.NewSimpleClientset(objects...)
infraCreator := func(platform string, tenant *platformv1.Tenant, infra *provisioners.InfrastructureManifests) error {
outputs = append(outputs, result{platform, tenant, infra})
return nil
}
c := NewProvisioningController(clientset, infraCreator, nil, nil)
c.factory.Start(nil)
c.factory.WaitForCacheSync(nil)

if c.workqueue.Len() != 1 {
t.Error("queue should have only 1 item, but it has", c.workqueue.Len())
}

if result := c.processNextWorkItem(1); !result {
t.Error("processing failed")
}

if c.workqueue.Len() != 0 {
item, _ := c.workqueue.Get()
t.Error("queue should be empty, but contains ", item)
}

if len(outputs) != 1 {
t.Error("expected 1 output, got", len(outputs))
return
}
if len(outputs[0].infra.AzureDbs) != 0 {
t.Error("expected zero dbs, got", len(outputs[0].infra.AzureDbs))
}
})
}

func newTenant(name, platform string) *platformv1.Tenant {
Expand Down

0 comments on commit 3dd2573

Please sign in to comment.