-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmd.go
149 lines (133 loc) · 3.26 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"encoding/json"
"fmt"
pb "github.com/hemanthmalla/k8s_resourceful/rpc"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"golang.org/x/net/context"
"google.golang.org/grpc"
"log"
"os"
"os/exec"
"sort"
"strings"
"time"
)
const (
GRPCPort = "50051"
)
func makeReq(request *pb.UpdateRequest, ip string) {
var addr string
if ip != ""{
addr = ip + ":" + GRPCPort
}else {
addr = "localhost:" + GRPCPort
}
conn, err := grpc.Dial(addr, grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
c := pb.NewUpdaterClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
status, err := c.UpdateContainerResource(ctx, request)
if err != nil {
fmt.Errorf(err.Error())
} else {
fmt.Println(status.Msg)
}
}
func getWorkerIP(context *cli.Context) string{
singleNode := context.String("singlenode")
if singleNode != ""{
return singleNode
}
if context.Bool("minikube"){
output, err := exec.Command("minikube", "ip").CombinedOutput()
if err != nil {
fmt.Println("Minikube IP couldn't be determined")
}else{
return strings.Trim(string(output), "\n")
}
}
ns := context.String("namespace")
pod := context.String("pod")
type PodResponse struct {
Status struct {
HostIP string `json:"hostIP"`
} `json:"status"`
}
cmd := fmt.Sprintf("get pod %s -o json -n %s", pod, ns)
output, err := exec.Command("kubectl", strings.Split(cmd, " ")...).CombinedOutput()
if err != nil {
fmt.Errorf(err.Error())
return ""
}
var data PodResponse
json.Unmarshal(output, &data)
return data.Status.HostIP
}
var updateContainerCommand = cli.Command{
Name: "update",
Usage: "Update resource limits of a running container",
ArgsUsage: "CONTAINER-NAME",
Flags: []cli.Flag{
cli.Int64Flag{
Name: "cpu",
Usage: "Impose a CPU CFS quota on the container. The number of microseconds per cpu-period that the container is limited to before throttled",
},
cli.Int64Flag{
Name: "memory",
Usage: "Memory limit (in bytes)",
},
cli.StringFlag{
Name: "namespace, n",
Usage: "Namespace of the container",
},
cli.StringFlag{
Name: "pod, p",
Usage: "Name of the Pod",
},
cli.BoolFlag{
Name: "minikube",
Usage: "Set this flag if you're running k8s in minikube",
},
cli.StringFlag{
Name: "singlenode, s",
Usage: "IP address of single node K8S cluster on which resourceful is running",
},
},
Action: func(context *cli.Context) error {
if context.NArg() == 0 {
return cli.ShowSubcommandHelp(context)
}
ip := getWorkerIP(context)
makeReq(&pb.UpdateRequest{
Namespace: context.String("namespace"),
PodName: context.String("pod"),
ContainerName: context.Args().Get(0),
Memory: context.Int64("memory"),
Cpu: context.Int64("cpu"),
}, ip)
return nil
},
}
func main() {
app := cli.NewApp()
app.Name = "resourceful"
app.Usage = "Kubectl plugin to update container resource limits"
app.Version = "0.1"
app.Commands = []cli.Command{
updateContainerCommand,
}
// sort all flags
for _, cmd := range app.Commands {
sort.Sort(cli.FlagsByName(cmd.Flags))
}
sort.Sort(cli.FlagsByName(app.Flags))
if err := app.Run(os.Args); err != nil {
logrus.Fatal(err)
}
}