/
node_cluster_permissions.go
70 lines (56 loc) · 2.02 KB
/
node_cluster_permissions.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
package check
import (
"fmt"
"github.com/vmware/govmomi/vim25/mo"
v1 "k8s.io/api/core/v1"
"k8s.io/klog/v2"
)
// CheckComputeClusterPermissions confirms that resources associated with the node maintain required privileges.
type CheckComputeClusterPermissions struct {
computeClusters map[string]*mo.ClusterComputeResource
}
var _ NodeCheck = &CheckComputeClusterPermissions{}
func (c *CheckComputeClusterPermissions) Name() string {
return "CheckComputeClusterPermissions"
}
func (c *CheckComputeClusterPermissions) StartCheck() error {
c.computeClusters = make(map[string]*mo.ClusterComputeResource)
return nil
}
func (c *CheckComputeClusterPermissions) checkComputeClusterPrivileges(ctx *CheckContext, vm *mo.VirtualMachine, readOnly bool) error {
cluster, err := getComputeCluster(ctx, vm.Runtime.Host.Reference())
if err != nil {
klog.Infof("compute cluster resource could not be obtained for %v", vm.Reference())
return nil
}
if readOnly {
// Having read only privilege is implied if we don't trigger the error above.
return nil
}
if _, ok := c.computeClusters[cluster.Name]; ok {
klog.Infof("privileges for compute cluster %v have already been checked", cluster.Name)
return nil
}
c.computeClusters[cluster.Name] = cluster
if err := comparePrivileges(ctx.Context, ctx.Username, cluster.Reference(), ctx.AuthManager, permissions[permissionCluster]); err != nil {
return fmt.Errorf("missing privileges for compute cluster %s: %s", cluster.Name, err.Error())
}
return nil
}
func (c *CheckComputeClusterPermissions) CheckNode(ctx *CheckContext, node *v1.Node, vm *mo.VirtualMachine) error {
var errs []error
readOnly := false
// If pre-existing resource pool was defined, only check cluster for read privilege
if ctx.VMConfig.Workspace.ResourcePoolPath != "" {
readOnly = true
}
err := c.checkComputeClusterPrivileges(ctx, vm, readOnly)
if err != nil {
errs = append(errs, err)
}
if len(errs) > 0 {
return join(errs)
}
return nil
}
func (c *CheckComputeClusterPermissions) FinishCheck(ctx *CheckContext) {}