/
azure.go
92 lines (79 loc) · 3.66 KB
/
azure.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
package nodepool
import (
"crypto/rand"
"crypto/rsa"
"encoding/base64"
"fmt"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
hyperv1 "github.com/openshift/hypershift/api/hypershift/v1beta1"
"golang.org/x/crypto/ssh"
utilpointer "k8s.io/utils/pointer"
capiazure "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
)
func azureMachineTemplateSpec(hcluster *hyperv1.HostedCluster, nodePool *hyperv1.NodePool, existing capiazure.AzureMachineTemplateSpec) (*capiazure.AzureMachineTemplateSpec, error) {
// The azure api requires to pass a public key. This key is randomly generated, the private portion is thrown away and the public key
// gets written to the template.
sshKey := existing.Template.Spec.SSHPublicKey
if sshKey == "" {
var err error
sshKey, err = generateSSHPubkey()
if err != nil {
return nil, fmt.Errorf("failed to generate a SSH key: %w", err)
}
}
azureMachineTemplate := &capiazure.AzureMachineTemplateSpec{Template: capiazure.AzureMachineTemplateResource{Spec: capiazure.AzureMachineSpec{
VMSize: nodePool.Spec.Platform.Azure.VMSize,
Image: &capiazure.Image{ID: utilpointer.String(bootImage(hcluster, nodePool))},
OSDisk: capiazure.OSDisk{
DiskSizeGB: utilpointer.Int32(nodePool.Spec.Platform.Azure.DiskSizeGB),
ManagedDisk: &capiazure.ManagedDiskParameters{
StorageAccountType: nodePool.Spec.Platform.Azure.DiskStorageAccountType,
},
},
NetworkInterfaces: []capiazure.NetworkInterface{{
SubnetName: nodePool.Spec.Platform.Azure.SubnetName,
}},
Identity: capiazure.VMIdentityUserAssigned,
UserAssignedIdentities: []capiazure.UserAssignedIdentity{{ProviderID: hcluster.Spec.Platform.Azure.MachineIdentityID}},
SSHPublicKey: sshKey,
FailureDomain: failureDomain(nodePool),
}}}
if nodePool.Spec.Platform.Azure.DiskEncryptionSetID != "" {
azureMachineTemplate.Template.Spec.OSDisk.ManagedDisk.DiskEncryptionSet = &capiazure.DiskEncryptionSetParameters{
ID: nodePool.Spec.Platform.Azure.DiskEncryptionSetID,
}
azureMachineTemplate.Template.Spec.SecurityProfile = &capiazure.SecurityProfile{
EncryptionAtHost: to.Ptr(true),
}
}
if nodePool.Spec.Platform.Azure.EnableEphemeralOSDisk {
// This is set to "None" if not explicitly set - https://github.com/kubernetes-sigs/cluster-api-provider-azure/blob/f44d953844de58e4b6fe8f51d88b0bf75a04e9ec/api/v1beta1/azuremachine_default.go#L54
// "VMs and VM Scale Set Instances using an ephemeral OS disk support only Readonly caching."
azureMachineTemplate.Template.Spec.OSDisk.CachingType = "ReadOnly"
azureMachineTemplate.Template.Spec.OSDisk.DiffDiskSettings = &capiazure.DiffDiskSettings{Option: "Local"}
}
return azureMachineTemplate, nil
}
func generateSSHPubkey() (string, error) {
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return "", fmt.Errorf("failed to generate private key: %w", err)
}
publicRsaKey, err := ssh.NewPublicKey(&privateKey.PublicKey)
if err != nil {
return "", fmt.Errorf("failed to generate public key: %w", err)
}
return base64.StdEncoding.EncodeToString(ssh.MarshalAuthorizedKey(publicRsaKey)), nil
}
func bootImage(hcluster *hyperv1.HostedCluster, nodepool *hyperv1.NodePool) string {
if nodepool.Spec.Platform.Azure.ImageID != "" {
return nodepool.Spec.Platform.Azure.ImageID
}
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/images/rhcos.x86_64.vhd", hcluster.Spec.Platform.Azure.SubscriptionID, hcluster.Spec.Platform.Azure.ResourceGroupName)
}
func failureDomain(nodepool *hyperv1.NodePool) *string {
if nodepool.Spec.Platform.Azure.AvailabilityZone == "" {
return nil
}
return utilpointer.String(nodepool.Spec.Platform.Azure.AvailabilityZone)
}