-
Notifications
You must be signed in to change notification settings - Fork 24
/
workspace_controller_sshkey.go
78 lines (62 loc) · 2.17 KB
/
workspace_controller_sshkey.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package controllers
import (
"context"
"fmt"
tfc "github.com/hashicorp/go-tfe"
)
func (r *WorkspaceReconciler) getSSHKeyIDByName(ctx context.Context, w *workspaceInstance) (string, error) {
SSHKeyName := w.instance.Spec.SSHKey.Name
SSHKeys, err := w.tfClient.Client.SSHKeys.List(ctx, w.instance.Spec.Organization, &tfc.SSHKeyListOptions{})
if err != nil {
return "", err
}
for _, s := range SSHKeys.Items {
if s.Name == SSHKeyName {
return s.ID, nil
}
}
return "", fmt.Errorf("ssh key ID was not found for ssh key name %q", SSHKeyName)
}
func (r *WorkspaceReconciler) getSSHKeyID(ctx context.Context, w *workspaceInstance) (string, error) {
specSSHKey := w.instance.Spec.SSHKey
if specSSHKey.Name != "" {
w.log.Info("Reconcile SSH Key", "msg", "getting ssh key ID by name")
return r.getSSHKeyIDByName(ctx, w)
}
w.log.Info("Reconcile SSH Key", "msg", "getting ssh key ID from the spec.sshKey.ID")
return specSSHKey.ID, nil
}
func (r *WorkspaceReconciler) reconcileSSHKey(ctx context.Context, w *workspaceInstance, workspace *tfc.Workspace) error {
if w.instance.Spec.SSHKey == nil {
// Verify whether a Workspace has an SSH key and unassign it if so
if workspace.SSHKey != nil {
w.log.Info("Reconcile SSH Key", "msg", "unassigning the ssh key")
_, err := w.tfClient.Client.Workspaces.UnassignSSHKey(ctx, workspace.ID)
return err
}
return nil
}
SSHKeyID, err := r.getSSHKeyID(ctx, w)
if err != nil {
return err
}
// Assign an SSH key to a workspace if nothing is assigned
if workspace.SSHKey == nil {
w.log.Info("Reconcile SSH Key", "msg", "assigning the ssh key")
_, err := w.tfClient.Client.Workspaces.AssignSSHKey(ctx, workspace.ID, tfc.WorkspaceAssignSSHKeyOptions{
SSHKeyID: tfc.String(SSHKeyID),
})
return err
}
// Assign an SSH key to a workspace if it is different from the one in spec
if workspace.SSHKey.ID != SSHKeyID {
w.log.Info("Reconcile SSH Key", "msg", "assigning the ssh key")
_, err := w.tfClient.Client.Workspaces.AssignSSHKey(ctx, workspace.ID, tfc.WorkspaceAssignSSHKeyOptions{
SSHKeyID: tfc.String(SSHKeyID),
})
return err
}
return nil
}