Skip to content

Commit b2783a3

Browse files
authored
Merge pull request #143 from pradipd/user/pradipd/sshkeys
Adding support for multiple ssh keys
2 parents 4b11680 + e4c6130 commit b2783a3

9 files changed

+59
-16
lines changed

api/v1alpha3/azurestackhcimachine_types.go

+2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ type AzureStackHCIMachineSpec struct {
5151
// AllocatePublicIP allows the ability to create dynamic public ips for machines where this value is true.
5252
// +optional
5353
AllocatePublicIP bool `json:"allocatePublicIP,omitempty"`
54+
55+
AdditionalSSHKeys []string `json:"additionalSSHKeys,omitempty"`
5456
}
5557

5658
// AzureStackHCIMachineStatus defines the observed state of AzureStackHCIMachine

api/v1alpha3/azurestackhcivirtualmachine_types.go

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ type AzureStackHCIVirtualMachineSpec struct {
4747
ClusterName string `json:"clusterName"`
4848
SubnetName string `json:"subnetName"`
4949
BackendPoolNames []string `json:"backendPoolNames,omitempty"`
50+
51+
AdditionalSSHKeys []string `json:"additionalSSHKeys,omitempty"`
5052
}
5153

5254
// AzureStackHCIVirtualMachineStatus defines the observed state of AzureStackHCIVirtualMachine

api/v1alpha3/zz_generated.deepcopy.go

+17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cloud/services/virtualmachines/virtualmachines.go

+14-15
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const (
4646
type Spec struct {
4747
Name string
4848
NICName string
49-
SSHKeyData string
49+
SSHKeyData []string
5050
Size string
5151
Zone string
5252
Image infrav1.Image
@@ -99,7 +99,7 @@ func (s *Service) Reconcile(ctx context.Context, spec interface{}) error {
9999
klog.V(2).Infof("creating vm %s : %v", vmSpec.Name, vmSpec)
100100

101101
sshKeyData := vmSpec.SSHKeyData
102-
if sshKeyData == "" {
102+
if len(sshKeyData) == 0 {
103103
privateKey, perr := rsa.GenerateKey(rand.Reader, 2048)
104104
if perr != nil {
105105
return errors.Wrap(perr, "Failed to generate private key")
@@ -109,7 +109,16 @@ func (s *Service) Reconcile(ctx context.Context, spec interface{}) error {
109109
if perr != nil {
110110
return errors.Wrap(perr, "Failed to generate public key")
111111
}
112-
sshKeyData = string(ssh.MarshalAuthorizedKey(publicRsaKey))
112+
sshKeyData = []string{string(ssh.MarshalAuthorizedKey(publicRsaKey))}
113+
}
114+
115+
sshPublicKeys := []compute.SSHPublicKey{}
116+
sshKeyPath := fmt.Sprintf("/home/%s/.ssh/authorized_keys", azurestackhci.DefaultUserName)
117+
for i := 0; i < len(sshKeyData); i++ {
118+
sshPublicKeys = append(sshPublicKeys, compute.SSHPublicKey{
119+
Path: &sshKeyPath,
120+
KeyData: &sshKeyData[i],
121+
})
113122
}
114123

115124
randomPassword, err := GenerateRandomString(32)
@@ -131,12 +140,7 @@ func (s *Service) Reconcile(ctx context.Context, spec interface{}) error {
131140
OsType: compute.OperatingSystemTypes(vmSpec.OSDisk.OSType),
132141
LinuxConfiguration: &compute.LinuxConfiguration{
133142
SSH: &compute.SSHConfiguration{
134-
PublicKeys: &[]compute.SSHPublicKey{
135-
{
136-
Path: to.StringPtr(fmt.Sprintf("/home/%s/.ssh/authorized_keys", azurestackhci.DefaultUserName)),
137-
KeyData: to.StringPtr(sshKeyData),
138-
},
139-
},
143+
PublicKeys: &sshPublicKeys,
140144
},
141145
DisablePasswordAuthentication: to.BoolPtr(false),
142146
},
@@ -164,12 +168,7 @@ func (s *Service) Reconcile(ctx context.Context, spec interface{}) error {
164168

165169
virtualMachine.OsProfile.WindowsConfiguration = &compute.WindowsConfiguration{
166170
SSH: &compute.SSHConfiguration{
167-
PublicKeys: &[]compute.SSHPublicKey{
168-
{
169-
Path: to.StringPtr(fmt.Sprintf("/users/%s/.ssh/authorized_keys", azurestackhci.DefaultUserName)),
170-
KeyData: to.StringPtr(sshKeyData),
171-
},
172-
},
171+
PublicKeys: &sshPublicKeys,
173172
},
174173
}
175174
}

config/crd/bases/infrastructure.cluster.x-k8s.io_azurestackhcimachines.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ spec:
3939
spec:
4040
description: AzureStackHCIMachineSpec defines the desired state of AzureStackHCIMachine
4141
properties:
42+
additionalSSHKeys:
43+
items:
44+
type: string
45+
type: array
4246
allocatePublicIP:
4347
description: AllocatePublicIP allows the ability to create dynamic
4448
public ips for machines where this value is true.

config/crd/bases/infrastructure.cluster.x-k8s.io_azurestackhcimachinetemplates.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ spec:
4848
description: Spec is the specification of the desired behavior
4949
of the machine.
5050
properties:
51+
additionalSSHKeys:
52+
items:
53+
type: string
54+
type: array
5155
allocatePublicIP:
5256
description: AllocatePublicIP allows the ability to create
5357
dynamic public ips for machines where this value is true.

config/crd/bases/infrastructure.cluster.x-k8s.io_azurestackhcivirtualmachines.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ spec:
4040
description: AzureStackHCIVirtualMachineSpec defines the desired state
4141
of AzureStackHCIVirtualMachine
4242
properties:
43+
additionalSSHKeys:
44+
items:
45+
type: string
46+
type: array
4347
availabilityZone:
4448
properties:
4549
enabled:

controllers/azurestackhcimachine_controller.go

+1
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ func (r *AzureStackHCIMachineReconciler) reconcileVirtualMachineNormal(machineSc
305305
vm.Spec.Location = machineScope.AzureStackHCIMachine.Spec.Location
306306
vm.Spec.SSHPublicKey = machineScope.AzureStackHCIMachine.Spec.SSHPublicKey
307307
vm.Spec.BootstrapData = &bootstrapData
308+
vm.Spec.AdditionalSSHKeys = machineScope.AzureStackHCIMachine.Spec.AdditionalSSHKeys
308309

309310
return nil
310311
}

controllers/azurestackhcivirtualmachine_reconciler.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,20 @@ func (s *azureStackHCIVirtualMachineService) reconcileNetworkInterface(nicName s
164164

165165
func (s *azureStackHCIVirtualMachineService) createVirtualMachine(nicName string) (*infrav1.VM, error) {
166166
var vm *infrav1.VM
167+
decodedKeys := []string{}
167168
decoded, err := base64.StdEncoding.DecodeString(s.vmScope.AzureStackHCIVirtualMachine.Spec.SSHPublicKey)
168169
if err != nil {
169170
return nil, errors.Wrapf(err, "failed to decode ssh public key")
170171
}
172+
decodedKeys = append(decodedKeys, string(decoded))
173+
174+
for _, key := range s.vmScope.AzureStackHCIVirtualMachine.Spec.AdditionalSSHKeys {
175+
decoded, err = base64.StdEncoding.DecodeString(key)
176+
if err != nil {
177+
return nil, errors.Wrapf(err, "failed to decode an additional ssh public key")
178+
}
179+
decodedKeys = append(decodedKeys, string(decoded))
180+
}
171181

172182
vmSpec := &virtualmachines.Spec{
173183
Name: s.vmScope.Name(),
@@ -205,7 +215,7 @@ func (s *azureStackHCIVirtualMachineService) createVirtualMachine(nicName string
205215
vmSpec = &virtualmachines.Spec{
206216
Name: s.vmScope.Name(),
207217
NICName: nicName,
208-
SSHKeyData: string(decoded),
218+
SSHKeyData: decodedKeys,
209219
Size: s.vmScope.AzureStackHCIVirtualMachine.Spec.VMSize,
210220
OSDisk: s.vmScope.AzureStackHCIVirtualMachine.Spec.OSDisk,
211221
Image: s.vmScope.AzureStackHCIVirtualMachine.Spec.Image,

0 commit comments

Comments
 (0)