-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathaks.go
More file actions
241 lines (226 loc) · 7.96 KB
/
aks.go
File metadata and controls
241 lines (226 loc) · 7.96 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package aks
import (
"encoding/base64"
"fmt"
"github.com/pulumi/pulumi-azure-native-sdk/authorization/v3"
containerservice "github.com/pulumi/pulumi-azure-native-sdk/containerservice/v2/v20240801"
"github.com/pulumi/pulumi-azure-native-sdk/managedidentity/v3"
"github.com/pulumi/pulumi-azure-native-sdk/resources/v3"
"github.com/pulumi/pulumi-tls/sdk/v5/go/tls"
"github.com/pulumi/pulumi/sdk/v3/go/auto"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
"github.com/redhat-developer/mapt/pkg/manager"
maptContext "github.com/redhat-developer/mapt/pkg/manager/context"
"github.com/redhat-developer/mapt/pkg/provider/azure"
"github.com/redhat-developer/mapt/pkg/provider/util/output"
spotAzure "github.com/redhat-developer/mapt/pkg/spot/azure"
"github.com/redhat-developer/mapt/pkg/util/logging"
resourcesUtil "github.com/redhat-developer/mapt/pkg/util/resources"
)
type AKSRequest struct {
Prefix string
Location string
VMSize string
// "1.26.3"
KubernetesVersion string
OnlySystemPool bool
EnableAppRouting bool
Spot bool
SpotTolerance spotAzure.EvictionRate
SpotExcludedRegions []string
}
func Create(ctx *maptContext.ContextArgs, r *AKSRequest) (err error) {
// Create mapt Context
logging.Debug("Creating AKS")
if err := maptContext.Init(ctx, azure.Provider()); err != nil {
return err
}
cs := manager.Stack{
StackName: maptContext.StackNameByProject(stackAKS),
ProjectName: maptContext.ProjectName(),
BackedURL: maptContext.BackedURL(),
ProviderCredentials: azure.DefaultCredentials,
DeployFunc: r.deployer,
}
sr, _ := manager.UpStack(cs)
return r.manageResults(sr)
}
func Destroy(ctx *maptContext.ContextArgs) error {
// Create mapt Context
logging.Debug("Destroy AKS")
if err := maptContext.Init(ctx, azure.Provider()); err != nil {
return err
}
return azure.Destroy(
maptContext.ProjectName(),
maptContext.BackedURL(),
maptContext.StackNameByProject(stackAKS))
}
// Main function to deploy all requried resources to azure
func (r *AKSRequest) deployer(ctx *pulumi.Context) error {
// Get values for spot machine
location, spotPrice, err := r.valuesCheckingSpot()
if err != nil {
return err
}
// Get location for creating Resouce Group
rgLocation := azure.GetSuitableLocationForResourceGroup(*location)
rg, err := resources.NewResourceGroup(ctx,
resourcesUtil.GetResourceName(r.Prefix, azureAKSID, "rg"),
&resources.ResourceGroupArgs{
Location: pulumi.String(rgLocation),
ResourceGroupName: pulumi.String(maptContext.RunID()),
Tags: maptContext.ResourceTags(),
})
if err != nil {
return err
}
// Networking
// We will control networking in the future but we need to extend the network module to accept
// count on SN and types as all NodePools should be on dif SN from the same VN
// nr := network.NetworkRequest{
// Prefix: r.Prefix,
// ComponentID: azureAKSID,
// ResourceGroup: rg,
// }
// _, err = nr.Create(ctx)
// if err != nil {
// return err
// }
privateKey, err := tls.NewPrivateKey(
ctx,
resourcesUtil.GetResourceName(r.Prefix, azureAKSID, "privatekey"),
&tls.PrivateKeyArgs{
Algorithm: pulumi.String("RSA"),
RsaBits: pulumi.Int(4096),
})
if err != nil {
return err
}
// create a user assigned identity to use for the cluster
identity, err := managedidentity.NewUserAssignedIdentity(
ctx,
resourcesUtil.GetResourceName(r.Prefix, azureAKSID, "identity"),
&managedidentity.UserAssignedIdentityArgs{
Location: rg.Location,
ResourceGroupName: rg.Name,
Tags: maptContext.ResourceTags(),
})
if err != nil {
return err
}
// create the cluster
agentPoolProfiles := containerservice.ManagedClusterAgentPoolProfileArray{
&containerservice.ManagedClusterAgentPoolProfileArgs{
Name: pulumi.String("systempool"),
Mode: containerservice.AgentPoolModeSystem,
Count: pulumi.Int(1),
VmSize: pulumi.String(systemPoolVMSize),
OsType: pulumi.String("Linux"),
OsDiskSizeGB: pulumi.Int(30),
Type: pulumi.String("VirtualMachineScaleSets"),
},
}
if !r.OnlySystemPool {
agentPoolProfiles = append(agentPoolProfiles,
&containerservice.ManagedClusterAgentPoolProfileArgs{
Name: pulumi.String("userpool"),
Mode: containerservice.AgentPoolModeUser,
Count: pulumi.Int(1),
VmSize: pulumi.String(r.VMSize),
OsType: pulumi.String("Linux"),
OsDiskSizeGB: pulumi.Int(100),
Type: pulumi.String("VirtualMachineScaleSets"),
// VnetSubnetID: n.PublicSubnet.ID(),
ScaleSetPriority: containerservice.ScaleSetPrioritySpot,
SpotMaxPrice: pulumi.Float64(*spotPrice)},
)
}
managedClusterArgs := &containerservice.ManagedClusterArgs{
ResourceGroupName: rg.Name,
Location: rg.Location,
Identity: &containerservice.ManagedClusterIdentityArgs{
Type: containerservice.ResourceIdentityTypeUserAssigned,
UserAssignedIdentities: pulumi.StringArray{
identity.ID(),
},
},
KubernetesVersion: pulumi.String(r.KubernetesVersion),
DnsPrefix: pulumi.String("mapt"),
EnableRBAC: pulumi.Bool(true),
AgentPoolProfiles: agentPoolProfiles,
LinuxProfile: &containerservice.ContainerServiceLinuxProfileArgs{
AdminUsername: pulumi.String("aksuser"),
Ssh: &containerservice.ContainerServiceSshConfigurationArgs{
PublicKeys: containerservice.ContainerServiceSshPublicKeyArray{
&containerservice.ContainerServiceSshPublicKeyArgs{
KeyData: privateKey.PublicKeyOpenssh,
},
},
},
},
Tags: maptContext.ResourceTags(),
}
// Enable app routing if required
if r.EnableAppRouting {
managedClusterArgs.IngressProfile = containerservice.ManagedClusterIngressProfileArgs{
WebAppRouting: containerservice.ManagedClusterIngressProfileWebAppRoutingArgs{
Enabled: pulumi.Bool(true),
},
}
}
cluster, err := containerservice.NewManagedCluster(
ctx,
resourcesUtil.GetResourceName(r.Prefix, azureAKSID, "cluster"),
managedClusterArgs)
if err != nil {
return err
}
// retrieve the admin credentials which contain the kubeconfig
adminCredentials := containerservice.ListManagedClusterAdminCredentialsOutput(
ctx,
containerservice.ListManagedClusterAdminCredentialsOutputArgs{
ResourceGroupName: rg.Name,
ResourceName: cluster.Name,
}, nil)
// grant the 'contributor' role to the identity on the resource group
_, err = authorization.NewRoleAssignment(ctx, "roleAssignment", &authorization.RoleAssignmentArgs{
PrincipalId: identity.PrincipalId,
PrincipalType: pulumi.String(authorization.PrincipalTypeServicePrincipal),
RoleDefinitionId: pulumi.String("/providers/Microsoft.Authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c"),
Scope: rg.ID(),
})
if err != nil {
return err
}
kubeconfig := adminCredentials.ApplyT(
func(adminCredentials containerservice.ListManagedClusterAdminCredentialsResult) (pulumi.String, error) {
value, _ := base64.StdEncoding.DecodeString(adminCredentials.Kubeconfigs[0].Value)
return pulumi.String(value), nil
}).(pulumi.StringOutput)
ctx.Export(fmt.Sprintf("%s-%s", r.Prefix, outputKubeconfig), kubeconfig)
return nil
}
func (r *AKSRequest) valuesCheckingSpot() (*string, *float64, error) {
if r.Spot {
bsc, err :=
spotAzure.GetBestSpotChoice(spotAzure.BestSpotChoiceRequest{
VMTypes: []string{r.VMSize},
OSType: "linux",
EvictionRateTolerance: r.SpotTolerance,
ExcludedRegions: r.SpotExcludedRegions,
})
logging.Debugf("Best spot price option found: %v", bsc)
if err != nil {
return nil, nil, err
}
return &bsc.Location, &bsc.Price, nil
}
return &r.Location, nil, nil
}
// Write exported values in context to files o a selected target folder
func (r *AKSRequest) manageResults(stackResult auto.UpResult) error {
return output.Write(stackResult, maptContext.GetResultsOutputPath(), map[string]string{
fmt.Sprintf("%s-%s", r.Prefix, outputKubeconfig): "kubeconfig",
})
}