Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor provisioning package #93

Merged
merged 1 commit into from
Feb 13, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func deployAzureDb(target provisioning.ProvisioningTarget,
ignoreChanges = []string{"resourceGroupName", "serverName", "elasticPoolId", "createMode", "sourceDatabaseId", "maxSizeBytes", "readScale", "requestedBackupStorageRedundancy", "catalogCollation", "collation", "sku", "zoneRedundant", "maintenanceConfigurationId", "isLedgerOn"}
}

dbNameV1 := provisioning.Match(target,
dbNameV1 := provisioning.MatchTarget(target,
func(tenant *platformv1.Tenant) string {
return fmt.Sprintf("%s_%s_%s", azureDb.Spec.DbName, tenant.Spec.PlatformRef, tenant.GetName())
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func deployAzureManagedDb(
valueExporter := handleValueExport(target)
gvk := provisioningv1.SchemeGroupVersion.WithKind("AzureManagedDatabase")

dbNameV1 := provisioning.Match(target,
dbNameV1 := provisioning.MatchTarget(target,
func(tenant *platformv1.Tenant) string {
return fmt.Sprintf("%s_%s_%s", azureDb.Spec.DbName, tenant.Spec.PlatformRef, tenant.GetName())
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func deployAzureRG(target provisioning.ProvisioningTarget, domain string) func(ctx *pulumi.Context) (pulumi.StringOutput, error) {
return func(ctx *pulumi.Context) (pulumi.StringOutput, error) {
resourceGroupName := provisioning.Match(target,
resourceGroupName := provisioning.MatchTarget(target,
func(tenant *platformv1.Tenant) string {
return fmt.Sprintf("%s-%s-%s", tenant.Spec.PlatformRef, tenant.GetName(), domain)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ func deployAzureVirtualDesktop(target provisioning.ProvisioningTarget, resourceG

hostPoolName := avd.Spec.HostPoolName

globalQalifier := provisioning.Match(target,
globalQalifier := provisioning.MatchTarget(target,
func(tenant *platformv1.Tenant) string {
return fmt.Sprintf("%s-%s", tenant.Spec.PlatformRef, tenant.GetName())
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func deployAzureVirtualMachine(target provisioning.ProvisioningTarget,
valueExporter := handleValueExport(target)
gvk := provisioningv1.SchemeGroupVersion.WithKind("AzureVirtualMachine")

vmName := provisioning.Match(target,
vmName := provisioning.MatchTarget(target,
func(tenant *platformv1.Tenant) string {
return fmt.Sprintf("%s-%s-%s", azureVM.Spec.VmName, tenant.Spec.PlatformRef, tenant.GetName())
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func handleValueExport(target provisioning.ProvisioningTarget) ValueExporterFunc
return func(exportContext ExportContext, values map[string]exportTemplateWithValue, opts ...pulumi.ResourceOption) error {
v := onlyVaultValues(values)
if len(v) > 0 {
path := provisioning.Match(target,
path := provisioning.MatchTarget(target,
func(tenant *platformv1.Tenant) string {
return strings.Join([]string{tenant.Spec.PlatformRef, exportContext.ownerMeta.Namespace, exportContext.domain, tenant.GetName(), exportContext.objectName}, "/")
},
Expand All @@ -71,7 +71,7 @@ func handleValueExport(target provisioning.ProvisioningTarget) ValueExporterFunc

v = onlyConfigMapValues(values)
if len(v) > 0 {
name := provisioning.Match(target,
name := provisioning.MatchTarget(target,
func(tenant *platformv1.Tenant) string {
return strings.Join([]string{exportContext.domain, tenant.GetName(), exportContext.objectName}, "-")
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func deployHelmRelease(target provisioning.ProvisioningTarget,
}

func pulumiFluxHrArgs(target provisioning.ProvisioningTarget, hr *provisioningv1.HelmRelease) (*fluxcd.HelmReleaseArgs, error) {
helmReleaseName, fluxHelmReleaseName := provisioning.Match(target,
helmReleaseName, fluxHelmReleaseName := provisioning.MatchTarget(target,
func(tenant *platformv1.Tenant) tuple.T2[string, string] {
helmReleaseName := fmt.Sprintf("%s-%s", hr.Spec.Release.ReleaseName, tenant.GetName())
fluxHelmReleaseName := fmt.Sprintf("%s-%s", hr.Name, tenant.GetName())
Expand Down
20 changes: 10 additions & 10 deletions internal/controllers/provisioning/provisioners/pulumi/pulumi.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func Create(target provisioning.ProvisioningTarget, domain string, infra *provis
anyResource := anyAzureDb || anyManagedAzureDb || anyHelmRelease || anyVirtualMachine || anyVirtualDesktop || anyEntraUser || anyAzurePowerShellScript
needsResourceGroup := anyVirtualMachine || anyVirtualDesktop || anyAzurePowerShellScript

stackName := provisioning.Match(target,
stackName := provisioning.MatchTarget(target,
func(tenant *platformv1.Tenant) string {
return fmt.Sprintf("%s-%s", tenant.GetName(), domain)
},
Expand Down Expand Up @@ -212,7 +212,7 @@ func createOrSelectStack(ctx context.Context, stackName, projectName string, dep

func deployResource(target provisioning.ProvisioningTarget,
rgName *pulumi.StringOutput,
res provisioning.BaseProvisioningResource,
res provisioning.ProvisioningResource,
dependencies []pulumi.Resource,
ctx *pulumi.Context) (pulumi.Resource, error) {

Expand All @@ -224,26 +224,26 @@ func deployResource(target provisioning.ProvisioningTarget,
}

switch kind {
case string(provisioningv1.ProvisioningResourceKindEntraUser):
case string(provisioning.ProvisioningResourceKindEntraUser):
return deployEntraUser(target, res.(*provisioningv1.EntraUser), dependencies, ctx)
case string(provisioningv1.ProvisioningResourceKindAzureDatabase):
case string(provisioning.ProvisioningResourceKindAzureDatabase):
return deployAzureDb(target, res.(*provisioningv1.AzureDatabase), dependencies, ctx)
case string(provisioningv1.ProvisioningResourceKindAzureManagedDatabase):
case string(provisioning.ProvisioningResourceKindAzureManagedDatabase):
return deployAzureManagedDb(target, res.(*provisioningv1.AzureManagedDatabase), dependencies, ctx)
case string(provisioningv1.ProvisioningResourceKindAzurePowerShellScript):
case string(provisioning.ProvisioningResourceKindAzurePowerShellScript):
return deployAzurePowerShellScript(target, *rgName, res.(*provisioningv1.AzurePowerShellScript), dependencies, ctx)
case string(provisioningv1.ProvisioningResourceKindHelmRelease):
case string(provisioning.ProvisioningResourceKindHelmRelease):
return deployHelmRelease(target, res.(*provisioningv1.HelmRelease), dependencies, ctx)
case string(provisioningv1.ProvisioningResourceKindAzureVirtualMachine):
case string(provisioning.ProvisioningResourceKindAzureVirtualMachine):
return deployAzureVirtualMachine(target, *rgName, res.(*provisioningv1.AzureVirtualMachine), dependencies, ctx)
case string(provisioningv1.ProvisioningResourceKindAzureVirtualDesktop):
case string(provisioning.ProvisioningResourceKindAzureVirtualDesktop):
return deployAzureVirtualDesktop(target, *rgName, res.(*provisioningv1.AzureVirtualDesktop), dependencies, ctx)
default:
return nil, fmt.Errorf("unknown resource kind: %s", kind)
}
}

func deployResourceWithDeps(target provisioning.ProvisioningTarget, rgName *pulumi.StringOutput, res provisioning.BaseProvisioningResource, provisionedRes provisionedResourceMap, infra *provisioning.InfrastructureManifests, ctx *pulumi.Context) (pulumi.Resource, error) {
func deployResourceWithDeps(target provisioning.ProvisioningTarget, rgName *pulumi.StringOutput, res provisioning.ProvisioningResource, provisionedRes provisionedResourceMap, infra *provisioning.InfrastructureManifests, ctx *pulumi.Context) (pulumi.Resource, error) {

id := provisioningv1.ProvisioningResourceIdendtifier{Name: res.GetName(), Kind: provisioningv1.ProvisioningResourceKind(res.GetObjectKind().GroupVersionKind().Kind)}
if pulumiRes, found := provisionedRes[id]; found {
Expand Down
6 changes: 3 additions & 3 deletions internal/controllers/provisioning/provisioning_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ func (c *ProvisioningController) syncTarget(target ProvisioningTarget, domain st
})

if result.Error == nil && result.HasChanges {
result.Error = Match(target,
result.Error = MatchTarget(target,
func(tenant *platformv1.Tenant) error {
if c.tenantMigrator != nil && tenant.Spec.Enabled {
return c.tenantMigrator(tenant.GetNamespace(), tenant, domain)
Expand All @@ -388,7 +388,7 @@ func (c *ProvisioningController) syncTarget(target ProvisioningTarget, domain st
func (c *ProvisioningController) publishSuccessEvents(target ProvisioningTarget, domain string) {
c.recorder.Event(target, corev1.EventTypeNormal, fmt.Sprintf(DomainProvisionedSuccessfullyFormat, domain), fmt.Sprintf(DomainProvisionedSuccessfullyFormat, domain))

topic, ev := Match(target,
topic, ev := MatchTarget(target,
func(tenant *platformv1.Tenant) tuple.T2[string, any] {
var ev any = struct {
TenantId string
Expand Down Expand Up @@ -430,7 +430,7 @@ func (c *ProvisioningController) publishSuccessEvents(target ProvisioningTarget,
func (c *ProvisioningController) publishFailureEvents(target ProvisioningTarget, domain string, err error) {
c.recorder.Event(target, corev1.EventTypeWarning, fmt.Sprintf(DomainProvisionningFailedFormat, domain), err.Error())

topic, ev := Match(target,
topic, ev := MatchTarget(target,
func(tenant *platformv1.Tenant) tuple.T2[string, any] {
var ev any = struct {
TenantId string
Expand Down
123 changes: 123 additions & 0 deletions internal/controllers/provisioning/provisioning_resources.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package provisioning

import (
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/utils/strings/slices"
platformv1 "totalsoft.ro/platform-controllers/pkg/apis/platform/v1alpha1"
provisioningv1 "totalsoft.ro/platform-controllers/pkg/apis/provisioning/v1alpha1"
)

const (
ProvisioningResourceKindEntraUser = provisioningv1.ProvisioningResourceKind("EntraUser")
ProvisioningResourceKindAzureDatabase = provisioningv1.ProvisioningResourceKind("AzureDatabase")
ProvisioningResourceKindAzureManagedDatabase = provisioningv1.ProvisioningResourceKind("AzureManagedDatabase")
ProvisioningResourceKindAzurePowerShellScript = provisioningv1.ProvisioningResourceKind("AzurePowerShellScript")
ProvisioningResourceKindAzureVirtualDesktop = provisioningv1.ProvisioningResourceKind("AzureVirtualDesktop")
ProvisioningResourceKindAzureVirtualMachine = provisioningv1.ProvisioningResourceKind("AzureVirtualMachine")
ProvisioningResourceKindHelmRelease = provisioningv1.ProvisioningResourceKind("HelmRelease")
)

type InfrastructureManifests struct {
EntraUsers []*provisioningv1.EntraUser
AzureDbs []*provisioningv1.AzureDatabase
AzureManagedDbs []*provisioningv1.AzureManagedDatabase
AzurePowerShellScripts []*provisioningv1.AzurePowerShellScript
HelmReleases []*provisioningv1.HelmRelease
AzureVirtualMachines []*provisioningv1.AzureVirtualMachine
AzureVirtualDesktops []*provisioningv1.AzureVirtualDesktop
}

type ProvisioningResource interface {
GetProvisioningMeta() *provisioningv1.ProvisioningMeta
GetName() string
GetNamespace() string
GetObjectKind() schema.ObjectKind
GetSpec() any
}

func (infra *InfrastructureManifests) Get(id provisioningv1.ProvisioningResourceIdendtifier) (ProvisioningResource, bool) {
switch id.Kind {
case ProvisioningResourceKindEntraUser:
return findByName(id.Name, infra.EntraUsers)
case ProvisioningResourceKindAzureDatabase:
return findByName(id.Name, infra.AzureDbs)
case ProvisioningResourceKindAzureManagedDatabase:
return findByName(id.Name, infra.AzureManagedDbs)
case ProvisioningResourceKindAzurePowerShellScript:
return findByName(id.Name, infra.AzurePowerShellScripts)
case ProvisioningResourceKindHelmRelease:
return findByName(id.Name, infra.HelmReleases)
case ProvisioningResourceKindAzureVirtualMachine:
return findByName(id.Name, infra.AzureVirtualMachines)
case ProvisioningResourceKindAzureVirtualDesktop:
return findByName(id.Name, infra.AzureVirtualDesktops)
default:
return nil, false
}
}

func findByName[R ProvisioningResource](name string, slice []R) (ProvisioningResource, bool) {
for _, res := range slice {
if res.GetName() == name {
return res, true
}
}
return nil, false
}

func selectItemsInTarget[R ProvisioningResource](platform string, domain string, source []R, target ProvisioningTarget) []R {
result := []R{}
for _, res := range source {
provisioningMeta := res.GetProvisioningMeta()

if provisioningMeta.Target.Category == provisioningv1.ProvisioningTargetCategoryTenant {
if exludeTenant(provisioningMeta.Target.Filter, target.GetName()) {
continue
}

}

targetCategory := MatchTarget(target,
func(tenant *platformv1.Tenant) provisioningv1.ProvisioningTargetCategory {
return provisioningv1.ProvisioningTargetCategoryTenant
},
func(*platformv1.Platform) provisioningv1.ProvisioningTargetCategory {
return provisioningv1.ProvisioningTargetCategoryPlatform
},
)

if targetCategory != provisioningMeta.Target.Category {
continue
}

if provisioningMeta.PlatformRef == platform && provisioningMeta.DomainRef == domain {
result = append(result, res)
}
}
return result
}

func exludeTenant(filter provisioningv1.ProvisioningTargetFilter, tenant string) bool {
tenantInList := slices.Contains(filter.Values, tenant)
if filter.Kind == provisioningv1.ProvisioningFilterKindBlacklist && tenantInList {
return true
}

if filter.Kind == provisioningv1.ProvisioningFilterKindWhitelist && !tenantInList {
return true
}

return false
}

func getResourceKeys(res ProvisioningResource) (platform, domain string, target provisioningv1.ProvisioningTargetCategory, ok bool) {
platform = res.GetProvisioningMeta().PlatformRef
domain = res.GetProvisioningMeta().DomainRef
target = res.GetProvisioningMeta().Target.Category

if len(platform) == 0 || len(domain) == 0 {
return platform, domain, target, false
}

return platform, domain, target, true
}
Loading
Loading