-
Notifications
You must be signed in to change notification settings - Fork 26
/
cmd.go
131 lines (103 loc) · 3.52 KB
/
cmd.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package operator
import (
"time"
"github.com/go-logr/logr"
"github.com/go-logr/zapr"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/troian/pubsub"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"golang.org/x/net/context"
"golang.org/x/sync/errgroup"
"k8s.io/client-go/kubernetes"
"github.com/akash-network/provider/cluster/kube/clientcommon"
providerflags "github.com/akash-network/provider/cmd/provider-services/cmd/flags"
"github.com/akash-network/provider/operator/common"
"github.com/akash-network/provider/operator/hostname"
"github.com/akash-network/provider/operator/inventory"
"github.com/akash-network/provider/operator/ip"
akashclientset "github.com/akash-network/provider/pkg/client/clientset/versioned"
"github.com/akash-network/provider/tools/fromctx"
)
func OperatorsCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "operator",
Short: "kubernetes operators control",
SilenceUsage: true,
PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
zconf := zap.NewDevelopmentConfig()
zconf.DisableCaller = true
zconf.DisableStacktrace = true
zconf.EncoderConfig.EncodeTime = func(time.Time, zapcore.PrimitiveArrayEncoder) {}
zapLog, _ := zconf.Build()
group, ctx := errgroup.WithContext(cmd.Context())
cmd.SetContext(logr.NewContext(ctx, zapr.NewLogger(zapLog)))
kubecfg, err := fromctx.KubeConfigFromCtx(cmd.Context())
if err != nil {
if err := clientcommon.SetKubeConfigToCmd(cmd); err != nil {
return err
}
kubecfg = fromctx.MustKubeConfigFromCtx(cmd.Context())
}
if val := ctx.Value(fromctx.CtxKeyKubeClientSet); val == nil {
kc, err := kubernetes.NewForConfig(kubecfg)
if err != nil {
return err
}
fromctx.CmdSetContextValue(cmd, fromctx.CtxKeyKubeClientSet, kubernetes.Interface(kc))
}
if val := ctx.Value(fromctx.CtxKeyAkashClientSet); val == nil {
ac, err := akashclientset.NewForConfig(kubecfg)
if err != nil {
return err
}
fromctx.CmdSetContextValue(cmd, fromctx.CtxKeyAkashClientSet, akashclientset.Interface(ac))
}
startupch := make(chan struct{}, 1)
fromctx.CmdSetContextValue(cmd, fromctx.CtxKeyStartupCh, (chan<- struct{})(startupch))
pctx, pcancel := context.WithCancel(context.Background())
fromctx.CmdSetContextValue(cmd, fromctx.CtxKeyErrGroup, group)
fromctx.CmdSetContextValue(cmd, fromctx.CtxKeyPubSub, pubsub.New(pctx, 1000))
go func() {
defer pcancel()
select {
case <-ctx.Done():
return
case <-startupch:
}
_ = group.Wait()
}()
return nil
},
}
cmd.PersistentFlags().Uint16(common.FlagRESTPort, 8080, "REST port")
if err := viper.BindPFlag(common.FlagRESTPort, cmd.PersistentFlags().Lookup(common.FlagRESTPort)); err != nil {
panic(err)
}
cmd.PersistentFlags().Uint16(common.FlagGRPCPort, 8081, "REST port")
if err := viper.BindPFlag(common.FlagGRPCPort, cmd.PersistentFlags().Lookup(common.FlagGRPCPort)); err != nil {
panic(err)
}
cmd.Flags().String(common.FlagRESTAddress, "0.0.0.0", "listen address for REST server")
if err := viper.BindPFlag(common.FlagRESTAddress, cmd.Flags().Lookup(common.FlagRESTAddress)); err != nil {
panic(err)
}
err := providerflags.AddKubeConfigPathFlag(cmd)
if err != nil {
panic(err)
}
cmd.AddCommand(inventory.Cmd())
cmd.AddCommand(ip.Cmd())
cmd.AddCommand(hostname.Cmd())
return cmd
}
func ToolsCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "tools",
Short: "debug tools",
SilenceUsage: true,
}
cmd.AddCommand(cmdPsutil())
return cmd
}