-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathnode_regex.go
54 lines (45 loc) · 1.39 KB
/
node_regex.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
package regex
import (
"context"
"fmt"
"regexp"
"github.com/k8s-proxmox/proxmox-go/api"
"github.com/k8s-proxmox/cluster-api-provider-proxmox/cloud/scheduler/framework"
"github.com/k8s-proxmox/cluster-api-provider-proxmox/cloud/scheduler/plugins/names"
)
type NodeRegex struct{}
var _ framework.NodeFilterPlugin = &NodeRegex{}
const (
NodeRegexName = names.NodeRegex
NodeRegexKey = "node.qemu-scheduler/regex"
)
func (pl *NodeRegex) Name() string {
return NodeRegexName
}
// regex is specified in ctx value (key=node.qemu-scheduler/regex)
func (pl *NodeRegex) Filter(ctx context.Context, state *framework.CycleState, config api.VirtualMachineCreateOptions, nodeInfo *framework.NodeInfo) *framework.Status {
reg, err := findNodeRegex(ctx)
if err != nil {
state.SetMessage(pl.Name(), "no valid regex is specified, skip")
return &framework.Status{}
}
if !reg.MatchString(nodeInfo.Node().Node) {
status := framework.NewStatus()
status.SetCode(1)
return status
}
return &framework.Status{}
}
// specify available node name as regex
// example: node.qemu-scheduler/regex=node[0-9]+
func findNodeRegex(ctx context.Context) (*regexp.Regexp, error) {
value := ctx.Value(framework.CtxKey(NodeRegexKey))
if value == nil {
return nil, fmt.Errorf("no node name regex is specified")
}
reg, err := regexp.Compile(fmt.Sprintf("%s", value))
if err != nil {
return nil, err
}
return reg, nil
}