-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.go
82 lines (72 loc) · 1.89 KB
/
run.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
79
80
81
82
package option
import (
"context"
"errors"
"github.com/go-logr/logr"
"github.com/spf13/pflag"
"github.com/uesyn/dotfiles/tools/devk/cmd/util"
cmdutil "github.com/uesyn/dotfiles/tools/devk/cmd/util"
"github.com/uesyn/dotfiles/tools/devk/manager"
"github.com/uesyn/dotfiles/tools/devk/mutator"
"k8s.io/client-go/kubernetes"
)
type RunOptions struct {
name string
selectNodes bool
gpuNum int
namespace string
clientSet kubernetes.Interface
manager manager.Manager
}
func (o *RunOptions) AddFlags(fs *pflag.FlagSet) {
fs.StringVarP(&o.name, "name", "n", "default", "Devk name")
fs.BoolVarP(&o.selectNodes, "select-nodes", "s", false, "Select node to run on with fuzzy finder")
fs.IntVar(&o.gpuNum, "gpu", 0, "Amount of GPU")
}
func (o *RunOptions) Complete(f util.Factory) error {
namespace, _, err := f.Namespace()
if err != nil {
return err
}
o.namespace = namespace
clientset, err := f.KubeClientSet()
if err != nil {
return err
}
o.clientSet = clientset
manager, err := f.Manager()
if err != nil {
return err
}
o.manager = manager
return nil
}
func (o *RunOptions) Validate() error {
if len(o.name) == 0 {
return errors.New("must set --name flag")
}
if len(o.namespace) == 0 {
return errors.New("must set Namespace")
}
return nil
}
func (o *RunOptions) Run(ctx context.Context) error {
logger := logr.FromContextOrDiscard(ctx).WithValues("devkName", o.name, "namespace", o.namespace)
var ms []mutator.Mutator
if o.selectNodes {
nodes, err := cmdutil.SelectNodesWithFuzzyFinder(ctx, o.clientSet)
if err != nil {
logger.Error(err, "failed to get nodes")
return err
}
ms = append(ms, mutator.NewDefaultNodeAffinityMutator(nodes))
}
if o.gpuNum > 0 {
ms = append(ms, mutator.NewGPULimitRequest(o.gpuNum))
}
if err := o.manager.Run(ctx, o.name, o.namespace, ms...); err != nil {
logger.Error(err, "failed to run")
return err
}
return nil
}