forked from pulumi/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
117 lines (106 loc) · 3.35 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"fmt"
"github.com/pulumi/pulumi-azure/sdk/v3/go/azure/containerservice"
"github.com/pulumi/pulumi-azure/sdk/v3/go/azure/core"
"github.com/pulumi/pulumi-azuread/sdk/v2/go/azuread"
"github.com/pulumi/pulumi/sdk/v2/go/pulumi"
"github.com/pulumi/pulumi/sdk/v2/go/pulumi/config"
)
type aksClusterConfig struct {
name string
location string
nodeCount int
nodeSize string
}
func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
// Set up configuration variables for this stack.
c := config.New(ctx, "")
password := c.Require("password")
location := c.Get("location")
if location == "" {
location = "eastus"
}
sshPublicKey := c.Require("sshPublicKey")
resourceGroup, err := core.NewResourceGroup(ctx, "aks", &core.ResourceGroupArgs{
Location: pulumi.String(location),
})
if err != nil {
return err
}
// Create the AD service principal for the K8s cluster.
adApp, err := azuread.NewApplication(ctx, "aks", nil)
if err != nil {
return err
}
adSpArgs := azuread.ServicePrincipalArgs{
ApplicationId: adApp.ApplicationId,
}
adSp, err := azuread.NewServicePrincipal(ctx, "aksSp", &adSpArgs)
if err != nil {
return err
}
adSpPasswordArgs := azuread.ServicePrincipalPasswordArgs{
ServicePrincipalId: adSp.ID(),
Value: pulumi.String(password),
EndDate: pulumi.String("2099-01-01T00:00:00Z"),
}
adSpPassword, err := azuread.NewServicePrincipalPassword(ctx, "aksSpPassword", &adSpPasswordArgs)
if err != nil {
return err
}
// Per-cluster configs
aksClusterConfigs := [2]*aksClusterConfig{
&aksClusterConfig{
name: "east",
location: "eastus",
nodeCount: 2,
nodeSize: "Standard_D2_v2",
},
&aksClusterConfig{
name: "west",
location: "westus",
nodeCount: 5,
nodeSize: "Standard_D2_v2",
},
}
// Create the individual clusters
k8sClusters := make([]*containerservice.KubernetesCluster, len(aksClusterConfigs))
for i, perClusterConfig := range aksClusterConfigs {
clusterArgs := containerservice.KubernetesClusterArgs{
// Global config arguments
ResourceGroupName: resourceGroup.Name,
LinuxProfile: containerservice.KubernetesClusterLinuxProfileArgs{
AdminUsername: pulumi.String("aksuser"),
SshKey: containerservice.KubernetesClusterLinuxProfileSshKeyArgs{
KeyData: pulumi.String(sshPublicKey),
},
},
ServicePrincipal: containerservice.KubernetesClusterServicePrincipalArgs{
ClientId: adApp.ApplicationId,
ClientSecret: adSpPassword.Value,
},
// Per-cluster config arguments
Location: pulumi.String(perClusterConfig.location),
DefaultNodePool: containerservice.KubernetesClusterDefaultNodePoolArgs{
Name: pulumi.String("aksagentpool"),
NodeCount: pulumi.Int(perClusterConfig.nodeCount),
VmSize: pulumi.String(perClusterConfig.nodeSize),
},
DnsPrefix: pulumi.String(fmt.Sprintf("%s-kube", ctx.Stack())),
}
cluster, err := containerservice.NewKubernetesCluster(ctx, fmt.Sprintf("aksCluster-%s", perClusterConfig.name), &clusterArgs)
if err != nil {
return err
}
k8sClusters[i] = cluster
}
var aksClusterNames pulumi.StringArray
for _, cluster := range k8sClusters {
aksClusterNames = append(aksClusterNames, cluster.Name)
}
ctx.Export("aksClusterNames", aksClusterNames)
return nil
})
}