-
Notifications
You must be signed in to change notification settings - Fork 0
/
AKSCluster.cs
200 lines (178 loc) · 7.26 KB
/
AKSCluster.cs
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
using System;
using System.Collections.Immutable;
using System.Linq;
using System.Text;
using Azure.Core;
using Pulumi;
using Pulumi.AzureNative.Authorization;
using Pulumi.AzureNative.Compute;
using Pulumi.AzureNative.ContainerService.V20230502Preview;
using Pulumi.AzureNative.ContainerService.V20230502Preview.Inputs;
using Pulumi.AzureNative.ContainerService.V20230502Preview.Outputs;
using Pulumi.AzureNative.Network;
using Pulumi.AzureNative.Resources;
using Pulumi.Tls;
using K8s = Pulumi.Kubernetes;
using ResourceIdentityType = Pulumi.AzureNative.ContainerService.V20230502Preview.ResourceIdentityType;
public class AKSCluster : ComponentResource
{
private const string DnsZoneContributorRoleDefinitionId = "/providers/Microsoft.Authorization/roleDefinitions/befefa01-2a29-4197-83a8-272ff33ce314";
public AKSCluster(string name, AKSClusterArgs args, ComponentResourceOptions? options = null)
: base("aks-otel-demo:aks:cluster", name, args, options)
{
var config = new Pulumi.Config();
var location = config.Get("location") ?? "uksouth";
// Create an Azure Resource Group
var resourceGroup = new ResourceGroup(name, new ResourceGroupArgs
{
Location = location
});
// Generate an SSH key
var sshKey = new PrivateKey("ssh-key", new PrivateKeyArgs
{
Algorithm = "RSA",
RsaBits = 4096
});
var addonProfiles = new InputMap<ManagedClusterAddonProfileArgs>();
if (args.CreateApplicationGateway)
addonProfiles.Add("IngressApplicationGateway", new ManagedClusterAddonProfileArgs {
Enabled = true,
Config = {
["subnetCIDR"] = "10.225.0.0/16"
}
});
var cluster = new ManagedCluster(name, new ManagedClusterArgs
{
ResourceGroupName = resourceGroup.Name,
NodeResourceGroup = resourceGroup.Name.Apply(rg => $"{rg}-nodes"),
AddonProfiles = addonProfiles,
Location = resourceGroup.Location,
Identity = new ManagedClusterIdentityArgs
{
Type = ResourceIdentityType.SystemAssigned
},
DnsPrefix = "otel-demo",
EnableRBAC = true,
KubernetesVersion = "1.27.1",
LinuxProfile = new ContainerServiceLinuxProfileArgs
{
AdminUsername = "testuser",
Ssh = new ContainerServiceSshConfigurationArgs
{
PublicKeys =
{
new ContainerServiceSshPublicKeyArgs
{
KeyData = sshKey.PublicKeyOpenssh,
}
}
}
},
IngressProfile = new ManagedClusterIngressProfileArgs
{
WebAppRouting = new ManagedClusterIngressProfileWebAppRoutingArgs
{
Enabled = true,
DnsZoneResourceId = args.DnsZoneId
}
},
AgentPoolProfiles = new[]
{
new ManagedClusterAgentPoolProfileArgs
{
Name = "basepool",
Count = 1,
MaxPods = 110,
Mode = AgentPoolMode.System,
OsType = OSType.Linux,
Type = AgentPoolType.VirtualMachineScaleSets,
VmSize = VirtualMachineSizeTypes.Standard_A2_v2.ToString(),
}
}
});
var agentPool = new AgentPool("agents", new AgentPoolArgs {
ResourceGroupName = resourceGroup.Name,
Count = 3,
MaxPods = 110,
Mode = AgentPoolMode.User,
OsType = OSType.Linux,
Type = AgentPoolType.VirtualMachineScaleSets,
VmSize = VirtualMachineSizeTypes.Standard_DS2_v2.ToString(),
ResourceName = cluster.Name,
}, new CustomResourceOptions {
DeletedWith = cluster,
ReplaceOnChanges = { "vmSize" },
DeleteBeforeReplace = true });
var roleAssignment = new RoleAssignment("cluster-dns-contributor", new()
{
PrincipalId = cluster.IngressProfile.Apply(ip => ip?.WebAppRouting!.Identity.ObjectId!),
PrincipalType = PrincipalType.ServicePrincipal,
RoleDefinitionId = DnsZoneContributorRoleDefinitionId,
Scope = args.DnsZoneId
});
// Export the KubeConfig
this.KubeConfig = ListManagedClusterUserCredentials.Invoke(
new ListManagedClusterUserCredentialsInvokeArgs
{
ResourceGroupName = resourceGroup.Name,
ResourceName = cluster.Name
})
.Apply(x => x.Kubeconfigs[0].Value)
.Apply(Convert.FromBase64String)
.Apply(Encoding.UTF8.GetString);
this.Provider = new K8s.Provider("k8s-provider", new K8s.ProviderArgs
{
KubeConfig = KubeConfig,
EnableServerSideApply = true
});
this.ClusterName = cluster.Name;
this.ClusterResourceGroup = resourceGroup.Name;
if (args.CreateApplicationGateway)
this.GatewayIp = cluster.AddonProfiles.GetApplicationGatewayIp();
}
[Output("clusterName")]
public Output<string> ClusterName { get; set; }
[Output("clusterResourceGroup")]
public Output<string> ClusterResourceGroup { get; set; }
[Output("kubeconfig")]
public Output<string> KubeConfig { get; set; }
[Output("GatewayIp")]
public Output<string?> GatewayIp { get; set; } = null!;
public K8s.Provider Provider { get; set; }
}
public class AKSClusterArgs : Pulumi.ResourceArgs
{
public bool CreateApplicationGateway { get; set; } = false;
public Input<string> DnsZoneId { get; set; } = null!;
}
internal static class ExtensionForCluster
{
public static Output<string?> GetApplicationGatewayIp(this Output<ImmutableDictionary<string, ManagedClusterAddonProfileResponse>?> addonProfiles)
{
return addonProfiles.Apply(a =>
{
var appGatewayResourceId = new Azure.Core.ResourceIdentifier(
a!["IngressApplicationGateway"]!
.Config!["effectiveApplicationGatewayId"]);
var appGatewayDetails = GetApplicationGateway.Invoke(new GetApplicationGatewayInvokeArgs
{
ApplicationGatewayName = appGatewayResourceId.Name,
ResourceGroupName = appGatewayResourceId.ResourceGroupName!
});
return appGatewayDetails.Apply<string?>(a =>
{
var publicIpId = a?.FrontendIPConfigurations.First()?
.PublicIPAddress?.Id;
if (publicIpId == null)
return "";
var publicIpResourceId = new ResourceIdentifier(publicIpId);
var publicIp = GetPublicIPAddress.Invoke(new GetPublicIPAddressInvokeArgs
{
PublicIpAddressName = publicIpResourceId.Name,
ResourceGroupName = publicIpResourceId.ResourceGroupName!
});
return publicIp.Apply(a => a.IpAddress);
});
});
}
}