@@ -241,14 +241,29 @@ func (d *Driver) Create() error {
241
241
return errors .Wrap (err , "could not read ssh public key" )
242
242
}
243
243
244
- keyopts := hcloud.SSHKeyCreateOpts {
245
- Name : d .GetMachineName (),
246
- PublicKey : string (buf ),
244
+ // Try to find a key with the same Fingerprint
245
+ pubkey , _ , _ , _ , err := ssh .ParseAuthorizedKey (buf )
246
+ if err != nil {
247
+ return errors .Wrap (err , "could not parse ssh public key" )
247
248
}
248
249
249
- key , _ , err := d .getClient ().SSHKey .Create (context .Background (), keyopts )
250
+ fp := ssh .FingerprintLegacyMD5 (pubkey )
251
+
252
+ key , _ , err := d .getClient ().SSHKey .GetByFingerprint (context .Background (), fp )
250
253
if err != nil {
251
- return errors .Wrap (err , "could not create ssh key" )
254
+ log .Debugf ("SSH key not found in Hetzner. Uploading..." )
255
+
256
+ keyopts := hcloud.SSHKeyCreateOpts {
257
+ Name : d .GetMachineName (),
258
+ PublicKey : string (buf ),
259
+ }
260
+
261
+ key , _ , err = d .getClient ().SSHKey .Create (context .Background (), keyopts )
262
+ if err != nil {
263
+ return errors .Wrap (err , "could not create ssh key" )
264
+ }
265
+ } else {
266
+ log .Debugf ("SSH key found in Hetzner. ID: %d" , key .ID )
252
267
}
253
268
254
269
d .KeyID = key .ID
0 commit comments