forked from Azure/AgentBaker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aks_model.go
206 lines (184 loc) · 7.3 KB
/
aks_model.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
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
package e2e
import (
"context"
"fmt"
"net"
"strings"
"testing"
"github.com/Azure/agentbakere2e/config"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerservice/armcontainerservice"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork"
)
func getKubenetClusterModel(name string) *armcontainerservice.ManagedCluster {
model := getBaseClusterModel(name)
model.Properties.NetworkProfile.NetworkPlugin = to.Ptr(armcontainerservice.NetworkPluginKubenet)
return model
}
func getAzureNetworkClusterModel(name string) *armcontainerservice.ManagedCluster {
cluster := getBaseClusterModel(name)
cluster.Properties.NetworkProfile.NetworkPlugin = to.Ptr(armcontainerservice.NetworkPluginAzure)
if cluster.Properties.AgentPoolProfiles != nil {
for _, app := range cluster.Properties.AgentPoolProfiles {
app.MaxPods = to.Ptr[int32](30)
}
}
return cluster
}
func getBaseClusterModel(clusterName string) *armcontainerservice.ManagedCluster {
return &armcontainerservice.ManagedCluster{
Name: to.Ptr(clusterName),
Location: to.Ptr(config.Config.Location),
Properties: &armcontainerservice.ManagedClusterProperties{
DNSPrefix: to.Ptr(clusterName),
AgentPoolProfiles: []*armcontainerservice.ManagedClusterAgentPoolProfile{
{
Name: to.Ptr("nodepool1"),
Count: to.Ptr[int32](1),
VMSize: to.Ptr("standard_d2ds_v5"),
MaxPods: to.Ptr[int32](110),
OSType: to.Ptr(armcontainerservice.OSTypeLinux),
Type: to.Ptr(armcontainerservice.AgentPoolTypeVirtualMachineScaleSets),
Mode: to.Ptr(armcontainerservice.AgentPoolModeSystem),
OSDiskSizeGB: to.Ptr[int32](512),
},
},
NetworkProfile: &armcontainerservice.NetworkProfile{
NetworkPlugin: to.Ptr(armcontainerservice.NetworkPluginKubenet),
},
},
Identity: &armcontainerservice.ManagedClusterIdentity{
Type: to.Ptr(armcontainerservice.ResourceIdentityTypeSystemAssigned),
},
}
}
func addAirgapNetworkSettings(ctx context.Context, t *testing.T, cluster *Cluster) error {
t.Logf("Adding network settings for airgap cluster %s in rg %s\n", *cluster.Model.Name, *cluster.Model.Properties.NodeResourceGroup)
vnet, err := getClusterVNet(ctx, *cluster.Model.Properties.NodeResourceGroup)
if err != nil {
return err
}
subnetId := vnet.subnetId
nsgParams, err := airGapSecurityGroup(config.Config.Location, *cluster.Model.Properties.Fqdn)
if err != nil {
return err
}
nsg, err := createAirgapSecurityGroup(ctx, cluster.Model, nsgParams, nil)
if err != nil {
return err
}
subnetParameters := armnetwork.Subnet{
ID: to.Ptr(subnetId),
Properties: &armnetwork.SubnetPropertiesFormat{
AddressPrefix: to.Ptr("10.224.0.0/16"),
NetworkSecurityGroup: &armnetwork.SecurityGroup{
ID: nsg.ID,
},
},
}
if err = updateSubnet(ctx, cluster.Model, subnetParameters, vnet.name); err != nil {
return err
}
t.Logf("updated cluster %s subnet with airggap settings", *cluster.Model.Name)
return nil
}
func airGapSecurityGroup(location, clusterFQDN string) (armnetwork.SecurityGroup, error) {
requiredRules, err := getRequiredSecurityRules(clusterFQDN)
if err != nil {
return armnetwork.SecurityGroup{}, fmt.Errorf("failed to get required security rules for airgap resource group: %w", err)
}
allowVnet := &armnetwork.SecurityRule{
Name: to.Ptr("AllowVnetOutBound"),
Properties: &armnetwork.SecurityRulePropertiesFormat{
Protocol: to.Ptr(armnetwork.SecurityRuleProtocolAsterisk),
Access: to.Ptr(armnetwork.SecurityRuleAccessAllow),
Direction: to.Ptr(armnetwork.SecurityRuleDirectionOutbound),
SourceAddressPrefix: to.Ptr("VirtualNetwork"),
SourcePortRange: to.Ptr("*"),
DestinationAddressPrefix: to.Ptr("VirtualNetwork"),
DestinationPortRange: to.Ptr("*"),
Priority: to.Ptr[int32](2000),
},
}
blockOutbound := &armnetwork.SecurityRule{
Name: to.Ptr("block-all-outbound"),
Properties: &armnetwork.SecurityRulePropertiesFormat{
Protocol: to.Ptr(armnetwork.SecurityRuleProtocolAsterisk),
Access: to.Ptr(armnetwork.SecurityRuleAccessDeny),
Direction: to.Ptr(armnetwork.SecurityRuleDirectionOutbound),
SourceAddressPrefix: to.Ptr("*"),
SourcePortRange: to.Ptr("*"),
DestinationAddressPrefix: to.Ptr("*"),
DestinationPortRange: to.Ptr("*"),
Priority: to.Ptr[int32](2001),
},
}
rules := append([]*armnetwork.SecurityRule{allowVnet, blockOutbound}, requiredRules...)
return armnetwork.SecurityGroup{
Location: &location,
Name: &config.Config.AirgapNSGName,
Properties: &armnetwork.SecurityGroupPropertiesFormat{SecurityRules: rules},
}, nil
}
func getRequiredSecurityRules(clusterFQDN string) ([]*armnetwork.SecurityRule, error) {
// https://learn.microsoft.com/en-us/azure/aks/outbound-rules-control-egress#azure-global-required-fqdn--application-rules
// note that we explicitly exclude packages.microsoft.com
requiredDNSNames := []string{
"mcr.microsoft.com",
"management.azure.com",
"acs-mirror.azureedge.net",
clusterFQDN,
}
var rules []*armnetwork.SecurityRule
var priority int32 = 100
for _, dnsName := range requiredDNSNames {
ips, err := net.LookupIP(dnsName)
if err != nil {
return nil, fmt.Errorf("failed to lookup IP for DNS name %s: %w", dnsName, err)
}
for _, ip := range ips {
if ipv4 := ip.To4(); ipv4 != nil {
rules = append(rules, getSecurityRule(fmt.Sprintf("%s-%d", strings.ReplaceAll(dnsName, ".", "-"), priority), ipv4.String(), priority))
priority++
}
}
}
return rules, nil
}
func getSecurityRule(name, destinationAddressPrefix string, priority int32) *armnetwork.SecurityRule {
return &armnetwork.SecurityRule{
Name: to.Ptr(name),
Properties: &armnetwork.SecurityRulePropertiesFormat{
Protocol: to.Ptr(armnetwork.SecurityRuleProtocolAsterisk),
Access: to.Ptr(armnetwork.SecurityRuleAccessAllow),
Direction: to.Ptr(armnetwork.SecurityRuleDirectionOutbound),
SourceAddressPrefix: to.Ptr("*"),
SourcePortRange: to.Ptr("*"),
DestinationAddressPrefix: to.Ptr(destinationAddressPrefix),
DestinationPortRange: to.Ptr("*"),
Priority: to.Ptr[int32](priority),
},
}
}
func createAirgapSecurityGroup(ctx context.Context, cluster *armcontainerservice.ManagedCluster, nsgParams armnetwork.SecurityGroup, options *armnetwork.SecurityGroupsClientBeginCreateOrUpdateOptions) (*armnetwork.SecurityGroupsClientCreateOrUpdateResponse, error) {
poller, err := config.Azure.SecurityGroup.BeginCreateOrUpdate(ctx, *cluster.Properties.NodeResourceGroup, config.Config.AirgapNSGName, nsgParams, options)
if err != nil {
return nil, err
}
nsg, err := poller.PollUntilDone(ctx, nil)
if err != nil {
return nil, err
}
return &nsg, nil
}
func updateSubnet(ctx context.Context, cluster *armcontainerservice.ManagedCluster, subnetParameters armnetwork.Subnet, vnetName string) error {
poller, err := config.Azure.Subnet.BeginCreateOrUpdate(ctx, *cluster.Properties.NodeResourceGroup, vnetName, config.Config.DefaultSubnetName, subnetParameters, nil)
if err != nil {
return err
}
_, err = poller.PollUntilDone(ctx, nil)
if err != nil {
return err
}
return nil
}