@@ -25,6 +25,7 @@ import (
2525 "github.com/Azure/go-autorest/autorest/to"
2626 "github.com/go-logr/logr"
2727 "github.com/pkg/errors"
28+ "sigs.k8s.io/cluster-api-provider-azure/util/generators"
2829
2930 infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1alpha3"
3031 azure "sigs.k8s.io/cluster-api-provider-azure/cloud"
@@ -142,13 +143,9 @@ func (s *Service) Reconcile(ctx context.Context) error {
142143 }
143144 }
144145
145- sshKey , err := base64 .StdEncoding .DecodeString (vmssSpec .SSHKeyData )
146- if err != nil {
147- return errors .Wrapf (err , "failed to decode ssh public key" )
148- }
149- bootstrapData , err := s .Scope .GetBootstrapData (ctx )
146+ osProfile , err := s .generateOSProfile (ctx , vmssSpec )
150147 if err != nil {
151- return errors . Wrap ( err , "failed to retrieve bootstrap data" )
148+ return err
152149 }
153150
154151 vmss := compute.VirtualMachineScaleSet {
@@ -170,22 +167,7 @@ func (s *Service) Reconcile(ctx context.Context) error {
170167 Mode : compute .UpgradeModeManual ,
171168 },
172169 VirtualMachineProfile : & compute.VirtualMachineScaleSetVMProfile {
173- OsProfile : & compute.VirtualMachineScaleSetOSProfile {
174- ComputerNamePrefix : to .StringPtr (vmssSpec .Name ),
175- AdminUsername : to .StringPtr (azure .DefaultUserName ),
176- CustomData : to .StringPtr (bootstrapData ),
177- LinuxConfiguration : & compute.LinuxConfiguration {
178- SSH : & compute.SSHConfiguration {
179- PublicKeys : & []compute.SSHPublicKey {
180- {
181- Path : to .StringPtr (fmt .Sprintf ("/home/%s/.ssh/authorized_keys" , azure .DefaultUserName )),
182- KeyData : to .StringPtr (string (sshKey )),
183- },
184- },
185- },
186- DisablePasswordAuthentication : to .BoolPtr (true ),
187- },
188- },
170+ OsProfile : osProfile ,
189171 StorageProfile : storageProfile ,
190172 SecurityProfile : securityProfile ,
191173 DiagnosticsProfile : & compute.DiagnosticsProfile {
@@ -399,6 +381,52 @@ func (s *Service) generateStorageProfile(vmssSpec azure.ScaleSetSpec, sku resour
399381 return storageProfile , nil
400382}
401383
384+ func (s * Service ) generateOSProfile (ctx context.Context , vmssSpec azure.ScaleSetSpec ) (* compute.VirtualMachineScaleSetOSProfile , error ) {
385+ sshKey , err := base64 .StdEncoding .DecodeString (vmssSpec .SSHKeyData )
386+ if err != nil {
387+ return nil , errors .Wrapf (err , "failed to decode ssh public key" )
388+ }
389+ bootstrapData , err := s .Scope .GetBootstrapData (ctx )
390+ if err != nil {
391+ return nil , errors .Wrap (err , "failed to retrieve bootstrap data" )
392+ }
393+
394+ osProfile := & compute.VirtualMachineScaleSetOSProfile {
395+ ComputerNamePrefix : to .StringPtr (vmssSpec .Name ),
396+ AdminUsername : to .StringPtr (azure .DefaultUserName ),
397+ CustomData : to .StringPtr (bootstrapData ),
398+ }
399+
400+ switch vmssSpec .OSDisk .OSType {
401+ case string (compute .Windows ):
402+ // Cloudbase-init is used to generate a password.
403+ // https://cloudbase-init.readthedocs.io/en/latest/plugins.html#setting-password-main
404+ //
405+ // We generate a random password here in case of failure
406+ // but the password on the VM will NOT be the same as created here.
407+ // Access is provided via SSH public key that is set during deployment
408+ // Azure also provides a way to reset user passwords in the case of need.
409+ osProfile .AdminPassword = to .StringPtr (generators .SudoRandomPassword (123 ))
410+ osProfile .WindowsConfiguration = & compute.WindowsConfiguration {
411+ EnableAutomaticUpdates : to .BoolPtr (false ),
412+ }
413+ default :
414+ osProfile .LinuxConfiguration = & compute.LinuxConfiguration {
415+ DisablePasswordAuthentication : to .BoolPtr (true ),
416+ SSH : & compute.SSHConfiguration {
417+ PublicKeys : & []compute.SSHPublicKey {
418+ {
419+ Path : to .StringPtr (fmt .Sprintf ("/home/%s/.ssh/authorized_keys" , azure .DefaultUserName )),
420+ KeyData : to .StringPtr (string (sshKey )),
421+ },
422+ },
423+ },
424+ }
425+ }
426+
427+ return osProfile , nil
428+ }
429+
402430func getVMSSUpdateFromVMSS (vmss compute.VirtualMachineScaleSet ) (compute.VirtualMachineScaleSetUpdate , error ) {
403431 json , err := vmss .MarshalJSON ()
404432 if err != nil {
0 commit comments