Created
November 16, 2017 01:25
-
-
Save kunalkushwaha/f76feec5322751d79f7b285ab590b384 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "bytes" | |
| "context" | |
| "fmt" | |
| "sync" | |
| "github.com/containerd/containerd" | |
| "github.com/containerd/containerd/containers" | |
| "github.com/containerd/containerd/mount" | |
| "github.com/containerd/containerd/namespaces" | |
| ) | |
| var ( | |
| Root = "/var/lib/containerd" | |
| State = "/run/containerd" | |
| RuntimeEndpoint = "/run/containerd/containerd.sock" | |
| DebugEndpoint = "/run/containerd/debug.sock" | |
| imageName = "docker.io/library/alpine:latest" | |
| ) | |
| func main() { | |
| ctxo := context.Background() | |
| ctx := namespaces.WithNamespace(ctxo, "tasktest") | |
| client, err := containerd.New(RuntimeEndpoint, containerd.WithDefaultNamespace("tasktest")) | |
| if err != nil { | |
| fmt.Println("Client Create", err) | |
| return | |
| } | |
| //Create | |
| c := &containers.Container{ | |
| ID: "test-task-ctr2", | |
| } | |
| specs, err := containerd.GenerateSpec(ctx, client, c) | |
| if err != nil { | |
| fmt.Println("Spec Gen", err) | |
| return | |
| } | |
| var wg sync.WaitGroup | |
| wg.Add(2) | |
| rootMount1 := make([]mount.Mount, 1) | |
| rootMount1[0].Options = make([]string, 1) | |
| rootMount1[0].Type = "overlay" | |
| rootMount1[0].Source = "overlay" | |
| rootMount1[0].Options[0] = "workdir=/var/lib/test/work,upperdir=/var/lib/test/upper,lowerdir=/var/lib/test/lower1" | |
| rootMount2 := make([]mount.Mount, 1) | |
| rootMount2[0].Options = make([]string, 1) | |
| rootMount2[0].Type = "overlay" | |
| rootMount2[0].Source = "overlay" | |
| rootMount2[0].Options[0] = "workdir=/var/lib/test/work,upperdir=/var/lib/test/upper,lowerdir=/var/lib/test/lower2" | |
| fmt.Println(rootMount1) | |
| fmt.Println(rootMount2) | |
| ctr, err := client.NewContainer(ctx, "test-task-ctr2", containerd.WithSpec(specs, containerd.WithProcessArgs("sh", "-C", "/start.sh"))) | |
| if err != nil { | |
| fmt.Println("Container Create", err) | |
| return | |
| } | |
| defer ctr.Delete(ctx) | |
| executeTask(ctx, ctr, rootMount1, &wg) | |
| executeTask(ctx, ctr, rootMount2, &wg) | |
| wg.Wait() | |
| return | |
| } | |
| func executeTask(ctx context.Context, ctr containerd.Container, rootMount []mount.Mount, wg *sync.WaitGroup) { | |
| defer wg.Done() | |
| stdout := bytes.NewBuffer(nil) | |
| //NewTask | |
| task, err := ctr.NewTask(ctx, | |
| containerd.NewIO(bytes.NewBuffer(nil), stdout, bytes.NewBuffer(nil)), | |
| containerd.WithRootFS(rootMount)) | |
| if err != nil { | |
| fmt.Println("Error in NewTask: ", err) | |
| return | |
| } | |
| defer task.Delete(ctx) | |
| //Wait | |
| statusCh, err := task.Wait(ctx) | |
| if err != nil { | |
| fmt.Println("Error in TaskWait: ", err) | |
| return | |
| } | |
| //Start | |
| if err := task.Start(ctx); err != nil { | |
| fmt.Println("Error in Start: ", err) | |
| return | |
| } | |
| //event | |
| fmt.Println("waiting for Task") | |
| status := <-statusCh | |
| //exit. | |
| fmt.Println("Event Recieved!!!", status.ExitCode()) | |
| if status.ExitCode() != 0 { | |
| fmt.Println("process returned non-zero exit code: ", status.ExitCode()) | |
| } | |
| fmt.Println("Output: ", stdout.String()) | |
| return | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment