-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Azure PowerShell Deployment Script (#92)
* Azure PowerShell Deployment Script * Tests and readme * typo * ManagedIdentity configuration static * fixes
- Loading branch information
Showing
31 changed files
with
1,550 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
166 changes: 166 additions & 0 deletions
166
helm/crds/provisioning.totalsoft.ro_azurepowershellscripts.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
--- | ||
apiVersion: apiextensions.k8s.io/v1 | ||
kind: CustomResourceDefinition | ||
metadata: | ||
annotations: | ||
controller-gen.kubebuilder.io/version: v0.14.0 | ||
name: azurepowershellscripts.provisioning.totalsoft.ro | ||
spec: | ||
group: provisioning.totalsoft.ro | ||
names: | ||
kind: AzurePowerShellScript | ||
listKind: AzurePowerShellScriptList | ||
plural: azurepowershellscripts | ||
singular: azurepowershellscript | ||
scope: Namespaced | ||
versions: | ||
- additionalPrinterColumns: | ||
- jsonPath: .spec.platformRef | ||
name: Platform | ||
type: string | ||
- jsonPath: .spec.domainRef | ||
name: Domain | ||
type: string | ||
name: v1alpha1 | ||
schema: | ||
openAPIV3Schema: | ||
properties: | ||
apiVersion: | ||
description: |- | ||
APIVersion defines the versioned schema of this representation of an object. | ||
Servers should convert recognized schemas to the latest internal value, and | ||
may reject unrecognized values. | ||
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources | ||
type: string | ||
kind: | ||
description: |- | ||
Kind is a string value representing the REST resource this object represents. | ||
Servers may infer this from the endpoint the client submits requests to. | ||
Cannot be updated. | ||
In CamelCase. | ||
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds | ||
type: string | ||
metadata: | ||
type: object | ||
spec: | ||
properties: | ||
dependsOn: | ||
description: List of dependencies | ||
items: | ||
properties: | ||
kind: | ||
description: Kind is a string value representing the REST resource | ||
this dependency represents. | ||
type: string | ||
name: | ||
description: ' The name of the dependency.' | ||
type: string | ||
required: | ||
- kind | ||
- name | ||
type: object | ||
type: array | ||
domainRef: | ||
description: Business Domain that this resource is provision for. | ||
type: string | ||
exports: | ||
description: Export provisioning values spec. | ||
items: | ||
properties: | ||
domain: | ||
description: The domain or bounded-context in which this script | ||
will be used. | ||
type: string | ||
scriptOutputs: | ||
description: Represents the outputs of the Azure PowerShell | ||
script. | ||
properties: | ||
toConfigMap: | ||
properties: | ||
keyTemplate: | ||
type: string | ||
required: | ||
- keyTemplate | ||
type: object | ||
toVault: | ||
properties: | ||
keyTemplate: | ||
type: string | ||
required: | ||
- keyTemplate | ||
type: object | ||
type: object | ||
type: object | ||
type: array | ||
forceUpdateTag: | ||
description: Change value to force the script to execute even if it | ||
has not changed. | ||
type: string | ||
platformRef: | ||
description: Target platform (custom resource name). | ||
type: string | ||
scriptArguments: | ||
description: |- | ||
Represents the arguments to be passed to the PowerShell script. | ||
eg: "-name JohnDoe" | ||
type: string | ||
scriptContent: | ||
description: ScriptContent represents the content of an Azure PowerShell | ||
script. | ||
type: string | ||
target: | ||
default: | ||
category: Tenant | ||
description: The provisioning target. | ||
properties: | ||
category: | ||
default: Tenant | ||
description: 'Provisioning target type. Possible values: Tenant, | ||
Platform' | ||
enum: | ||
- Tenant | ||
- Platform | ||
type: string | ||
filter: | ||
description: |- | ||
Filter targets (applies for category "Tenant"). | ||
If ommited all targets are selected. | ||
properties: | ||
kind: | ||
default: Blacklist | ||
description: 'Includes or excludes the speciffied targets. | ||
Possibile values: Blacklist, Whitelist' | ||
enum: | ||
- Blacklist | ||
- Whitelist | ||
type: string | ||
values: | ||
description: A list of targets to include or exculde | ||
items: | ||
type: string | ||
type: array | ||
required: | ||
- kind | ||
type: object | ||
required: | ||
- category | ||
type: object | ||
tenantOverrides: | ||
additionalProperties: | ||
x-kubernetes-preserve-unknown-fields: true | ||
description: |- | ||
Overrides for tenants. Dictionary with tenant name as key, spec override as value. | ||
The spec override has the same structure as Spec | ||
type: object | ||
required: | ||
- domainRef | ||
- platformRef | ||
- scriptContent | ||
- target | ||
type: object | ||
required: | ||
- spec | ||
type: object | ||
served: true | ||
storage: true | ||
subresources: {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
internal/controllers/provisioning/provisioners/pulumi/azure_powershell_script.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package pulumi | ||
|
||
import ( | ||
"encoding/json" | ||
"os" | ||
|
||
"github.com/pulumi/pulumi-azure-native-sdk/managedidentity/v2" | ||
"github.com/pulumi/pulumi-azure-native-sdk/resources/v2" | ||
"github.com/pulumi/pulumi/sdk/v3/go/pulumi" | ||
"totalsoft.ro/platform-controllers/internal/controllers/provisioning" | ||
"totalsoft.ro/platform-controllers/internal/template" | ||
provisioningv1 "totalsoft.ro/platform-controllers/pkg/apis/provisioning/v1alpha1" | ||
) | ||
|
||
func deployAzurePowerShellScript(target provisioning.ProvisioningTarget, | ||
resourceGroupName pulumi.StringOutput, | ||
azurePowerShellScript *provisioningv1.AzurePowerShellScript, | ||
dependencies []pulumi.Resource, | ||
ctx *pulumi.Context) (*resources.AzurePowerShellScript, error) { | ||
|
||
valueExporter := handleValueExport(target) | ||
gvk := provisioningv1.SchemeGroupVersion.WithKind("AzurePowerShellScript") | ||
|
||
tc := provisioning.GetTemplateContext(target) | ||
|
||
parsedArgs, err := template.ParseTemplate(azurePowerShellScript.Spec.ScriptArguments, tc) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
managedIdentity, err := managedidentity.LookupUserAssignedIdentity(ctx, &managedidentity.LookupUserAssignedIdentityArgs{ | ||
ResourceGroupName: os.Getenv("AZURE_MANAGED_IDENTITY_RG"), | ||
ResourceName: os.Getenv("AZURE_MANAGED_IDENTITY_NAME"), | ||
}) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
script, err := resources.NewAzurePowerShellScript(ctx, azurePowerShellScript.Name, &resources.AzurePowerShellScriptArgs{ | ||
Kind: pulumi.String("AzurePowerShell"), | ||
ForceUpdateTag: pulumi.String(azurePowerShellScript.Spec.ForceUpdateTag), // Change to force redeploying the script if desired | ||
ResourceGroupName: resourceGroupName, | ||
Arguments: pulumi.String(parsedArgs), // Set the arguments for the script'"), | ||
ScriptContent: pulumi.String(azurePowerShellScript.Spec.ScriptContent), | ||
CleanupPreference: pulumi.String("OnSuccess"), // Set the cleanup preference for the script | ||
Timeout: pulumi.String("PT1H"), // Set an appropriate timeout for the script | ||
Identity: &resources.ManagedServiceIdentityArgs{ | ||
Type: pulumi.String(resources.ManagedServiceIdentityTypeUserAssigned), | ||
UserAssignedIdentities: pulumi.StringArray{ | ||
pulumi.String(managedIdentity.Id), | ||
}, | ||
}, | ||
AzPowerShellVersion: pulumi.String("11.0"), // Specify the desired version of Az PowerShell module | ||
RetentionInterval: pulumi.String("P1D"), // Set the retention time for the script's logs | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, exp := range azurePowerShellScript.Spec.Exports { | ||
domain := exp.Domain | ||
if domain == "" { | ||
domain = azurePowerShellScript.Spec.DomainRef | ||
} | ||
|
||
err = valueExporter(newExportContext(ctx, domain, azurePowerShellScript.Name, azurePowerShellScript.ObjectMeta, gvk), | ||
|
||
map[string]exportTemplateWithValue{"scriptOutputs": {exp.ScriptOutputs, script.Outputs.ApplyT(func(outputs map[string]interface{}) (string, error) { | ||
outputsJson, err := json.Marshal(outputs) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return string(outputsJson), err | ||
}).(pulumi.StringOutput)}}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
return script, nil | ||
} |
Oops, something went wrong.