Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit 7d0963c

Browse files
committed
add yaml format pod file support and kubernetes compatible
1 parent 12bfefe commit 7d0963c

39 files changed

+11408
-47
lines changed

client/create.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ func (cli *HyperClient) HyperCmdCreate(args ...string) error {
1313
if len(args) == 0 {
1414
return fmt.Errorf("\"create\" requires a minimum of 1 argument, please provide POD spec file.\n")
1515
}
16-
var parser = gflag.NewParser(nil, gflag.Default)
17-
parser.Usage = "create POD_FILE\n\ncreate a pod, but without running it"
16+
var opts struct {
17+
Yaml bool `short:"y" long:"yaml" default:"false" default-mask:"-" description:"create a pod based on Yaml file"`
18+
}
19+
var parser = gflag.NewParser(&opts, gflag.Default)
20+
parser.Usage = "create [OPTIONS] POD_FILE\n\ncreate a pod, but without running it"
1821
args, err := parser.Parse()
1922
if err != nil {
2023
if !strings.Contains(err.Error(), "Usage") {
@@ -28,6 +31,15 @@ func (cli *HyperClient) HyperCmdCreate(args ...string) error {
2831
return err
2932
}
3033
jsonbody, err := ioutil.ReadFile(jsonFile)
34+
if err != nil {
35+
return err
36+
}
37+
if opts.Yaml == true {
38+
jsonbody, err = cli.ConvertYamlToJson(jsonbody)
39+
if err != nil {
40+
return err
41+
}
42+
}
3143
podId, err := cli.CreatePod(string(jsonbody))
3244
if err != nil {
3345
return err

client/replace.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ func (cli *HyperClient) HyperCmdReplace(args ...string) error {
1919
OldPod string `short:"o" long:"oldpod" value-name:"\"\"" description:"The Pod which will be replaced, must be 'running' status"`
2020
NewPod string `short:"n" long:"newpod" value-name:"\"\"" description:"The Pod which will be running, must be 'pending' status"`
2121
PodFile string `short:"f" long:"file" value-name:"\"\"" description:"The Pod file is used to create a new POD and run"`
22+
Yaml bool `short:"y" long:"yaml" default:"false" default-mask:"-" description:"The Pod file is based on Yaml file"`
2223
}
2324
var parser = gflag.NewParser(&opts, gflag.Default)
2425
parser.Usage = "replace --oldpod POD_ID --newpod POD_ID [--file POD_FILE]\n\nreplace the pod in a running VM with a new one"
@@ -67,6 +68,13 @@ func (cli *HyperClient) HyperCmdReplace(args ...string) error {
6768
if err != nil {
6869
return err
6970
}
71+
72+
if opts.Yaml == true {
73+
jsonbody, err = cli.ConvertYamlToJson(jsonbody)
74+
if err != nil {
75+
return err
76+
}
77+
}
7078
newPodId, err := cli.CreatePod(string(jsonbody))
7179
if err != nil {
7280
return err

client/run.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func (cli *HyperClient) HyperCmdRun(args ...string) error {
2424
var opts struct {
2525
PodFile string `short:"p" long:"podfile" value-name:"\"\"" description:"Create and Run a pod based on the pod file"`
2626
K8s string `short:"k" long:"kubernetes" value-name:"\"\"" description:"Create and Run a pod based on the kubernetes pod file"`
27+
Yaml bool `short:"y" long:"yaml" default:"false" default-mask:"-" description:"Create a pod based on Yaml file"`
2728
Name string `long:"name" value-name:"\"\"" description:"Assign a name to the container"`
2829
Attach bool `long:"attach" default:"true" default-mask:"-" description:"Attach the stdin, stdout and stderr to the container"`
2930
Workdir string `long:"workdir" default:"/" value-name:"\"\"" default-mask:"-" description:"Working directory inside the container"`
@@ -32,7 +33,7 @@ func (cli *HyperClient) HyperCmdRun(args ...string) error {
3233
Memory int `long:"memory" default:"128" value-name:"128" default-mask:"-" description:"Memory size (MB) for the VM"`
3334
Env []string `long:"env" value-name:"[]" default-mask:"-" description:"Set environment variables"`
3435
EntryPoint string `long:"entrypoint" value-name:"\"\"" default-mask:"-" description:"Overwrite the default ENTRYPOINT of the image"`
35-
RestartPolicy string `long:"restart" default:"never" value-name:"\"\"" default-mask:"-" description:"Restart policy to apply when a container exits (no, on-failure[:max-retry], always)"`
36+
RestartPolicy string `long:"restart" default:"never" value-name:"\"\"" default-mask:"-" description:"Restart policy to apply when a container exits (never, onFailure, always)"`
3637
}
3738

3839
var parser = gflag.NewParser(&opts, gflag.Default|gflag.IgnoreUnknown)
@@ -55,6 +56,12 @@ func (cli *HyperClient) HyperCmdRun(args ...string) error {
5556
return err
5657
}
5758

59+
if opts.Yaml == true {
60+
jsonbody, err = cli.ConvertYamlToJson(jsonbody)
61+
if err != nil {
62+
return err
63+
}
64+
}
5865
t1 := time.Now()
5966
podId, err := cli.RunPod(string(jsonbody))
6067
if err != nil {
@@ -78,6 +85,12 @@ func (cli *HyperClient) HyperCmdRun(args ...string) error {
7885
if err != nil {
7986
return err
8087
}
88+
if opts.Yaml == true {
89+
jsonbody, err = cli.ConvertYamlToJson(jsonbody)
90+
if err != nil {
91+
return err
92+
}
93+
}
8194
if err := json.Unmarshal(jsonbody, &kpod); err != nil {
8295
return err
8396
}
@@ -170,7 +183,6 @@ func (cli *HyperClient) HyperCmdRun(args ...string) error {
170183
if err != nil {
171184
return err
172185
}
173-
cmd, err := json.Marshal(command)
174186
var (
175187
tag = cli.GetTag()
176188
hijacked = make(chan io.Closer)
@@ -179,7 +191,6 @@ func (cli *HyperClient) HyperCmdRun(args ...string) error {
179191
v := url.Values{}
180192
v.Set("type", "container")
181193
v.Set("value", containerId)
182-
v.Set("command", string(cmd))
183194
v.Set("tag", tag)
184195

185196
// Block the return until the chan gets closed
@@ -191,7 +202,7 @@ func (cli *HyperClient) HyperCmdRun(args ...string) error {
191202
}()
192203

193204
errCh = promise.Go(func() error {
194-
return cli.hijack("POST", "/exec?"+v.Encode(), true, cli.in, cli.out, cli.out, hijacked, nil, "")
205+
return cli.hijack("POST", "/attach?"+v.Encode(), true, cli.in, cli.out, cli.out, hijacked, nil, "")
195206
})
196207

197208
if err := cli.monitorTtySize(podId, tag); err != nil {

client/utils.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ import (
1515
"net/url"
1616
"strconv"
1717

18-
1918
"hyper/utils"
2019
"hyper/lib/term"
2120
"hyper/pod"
21+
"hyper/lib/yaml"
2222
)
2323

2424
var (
@@ -92,10 +92,10 @@ func (cli *HyperClient) clientRequest(method, path string, in io.Reader, headers
9292
if len(body) == 0 {
9393
return nil, "", statusCode, fmt.Errorf("Error: request returned %s for API route and version %s, check if the server supports the requested API version", http.StatusText(statusCode), req.URL)
9494
}
95-
if len(bytes.TrimSpace(body)) > 50 {
95+
if len(bytes.TrimSpace(body)) > 150 {
9696
return nil, "", statusCode, fmt.Errorf("Error from daemon's response")
9797
}
98-
return nil, "", statusCode, fmt.Errorf("Error from daemon's response: %s", bytes.TrimSpace(body))
98+
return nil, "", statusCode, fmt.Errorf("%s", bytes.TrimSpace(body))
9999
}
100100

101101
return resp.Body, resp.Header.Get("Content-Type"), statusCode, nil
@@ -142,7 +142,6 @@ func (cli *HyperClient) streamBody(body io.ReadCloser, contentType string, setRa
142142
}
143143
return nil
144144
}
145-
fmt.Printf("There is something wrong in print the IO Stream!\n")
146145
return nil
147146
}
148147

@@ -208,3 +207,15 @@ func (cli *HyperClient) getTtySize() (int, int) {
208207
func (cli *HyperClient)GetTag() string {
209208
return pod.RandStr(8, "alphanum")
210209
}
210+
211+
func (cli *HyperClient) ConvertYamlToJson(yamlBody []byte) ([]byte, error) {
212+
var userPod pod.UserPod
213+
if err:= yaml.Unmarshal(yamlBody, &userPod); err != nil {
214+
return []byte(""), err
215+
}
216+
jsonBody, err := json.Marshal(&userPod)
217+
if err != nil {
218+
return []byte(""), err
219+
}
220+
return jsonBody, nil
221+
}

examples/ubuntu.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: process-yaml-file-ubuntu
2+
containers:
3+
- name: ubuntu
4+
image: ubuntu:latest
5+
workdir: /
6+
command:
7+
- /bin/bash
8+
resource:
9+
vcpu: 1
10+
files:
11+
volumes:
12+
tty: true

hyperdaemon/daemon.go

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ type Pod struct {
3737
Vm string
3838
Containers []*Container
3939
Status uint
40+
Type string
41+
RestartPolicy string
4042
}
4143

4244
type Container struct {
@@ -370,9 +372,21 @@ func (daemon *Daemon) GetRunningPodNum() int64 {
370372

371373
func (daemon *Daemon) WritePodToDB(podName string, podData []byte) error {
372374
key := fmt.Sprintf("pod-%s", podName)
373-
err := (daemon.db).Put([]byte(key), podData, nil)
375+
_, err := (daemon.db).Get([]byte(key), nil)
374376
if err != nil {
375-
return err
377+
err = (daemon.db).Put([]byte(key), podData, nil)
378+
if err != nil {
379+
return err
380+
}
381+
} else {
382+
err = (daemon.db).Delete([]byte(key), nil)
383+
if err != nil {
384+
return err
385+
}
386+
err = (daemon.db).Put([]byte(key), podData, nil)
387+
if err != nil {
388+
return err
389+
}
376390
}
377391
return nil
378392
}
@@ -649,11 +663,26 @@ func (daemon *Daemon) RemoveVm(vmId string) {
649663
}
650664

651665
func (daemon *Daemon) SetContainerStatus(podId string, status uint) {
652-
for _, v := range daemon.containerList {
653-
if v.PodId == podId {
654-
v.Status = status
666+
for _, c := range daemon.podList[podId].Containers {
667+
c.Status = status
668+
}
669+
}
670+
671+
func (daemon *Daemon) SetPodContainerStatus(podId string, data []uint32) {
672+
failure := 0
673+
for i, c := range daemon.podList[podId].Containers {
674+
if data[i] != 0 {
675+
failure ++
676+
c.Status = types.S_POD_FAILED
677+
} else {
678+
c.Status = types.S_POD_SUCCEEDED
655679
}
656680
}
681+
if failure == 0 {
682+
daemon.podList[podId].Status = types.S_POD_SUCCEEDED
683+
} else {
684+
daemon.podList[podId].Status = types.S_POD_FAILED
685+
}
657686
}
658687

659688
func (daemon *Daemon) UpdateVmData(vmId string, data []byte) error {

hyperdaemon/list.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,18 @@ func (daemon *Daemon) CmdList(job *engine.Job) error {
5858
case types.S_POD_CREATED:
5959
status = "pending"
6060
break
61+
case types.S_POD_FAILED:
62+
status = "failed"
63+
if v.Type == "kubernetes" {
64+
status = "failed(kubernetes)"
65+
}
66+
break
67+
case types.S_POD_SUCCEEDED:
68+
status = "succeeded"
69+
if v.Type == "kubernetes" {
70+
status = "succeeded(kubernetes)"
71+
}
72+
break
6173
default:
6274
status = ""
6375
break
@@ -71,10 +83,16 @@ func (daemon *Daemon) CmdList(job *engine.Job) error {
7183
for _, c := range daemon.containerList {
7284
switch c.Status {
7385
case types.S_POD_RUNNING:
74-
status = "online"
86+
status = "running"
7587
break
7688
case types.S_POD_CREATED:
77-
status = "stop"
89+
status = "pending"
90+
break
91+
case types.S_POD_FAILED:
92+
status = "failed"
93+
break
94+
case types.S_POD_SUCCEEDED:
95+
status = "succeeded"
7896
break
7997
default:
8098
status = ""

0 commit comments

Comments
 (0)