Skip to content

Instantly share code, notes, and snippets.

@salrashid123
Created August 4, 2023 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save salrashid123/fde22ebfb4a006f95ebdc22e1e2a9799 to your computer and use it in GitHub Desktop.
Save salrashid123/fde22ebfb4a006f95ebdc22e1e2a9799 to your computer and use it in GitHub Desktop.
containerd nginx in golang
package main
import (
"context"
"fmt"
"log"
"syscall"
"time"
// "github.com/containerd/cgroups/v3"
// "github.com/containerd/cgroups/v3/cgroup1"
cgroupsv2 "github.com/containerd/cgroups/v3/cgroup2"
"github.com/containerd/containerd"
"github.com/containerd/containerd/cio"
"github.com/containerd/containerd/namespaces"
"github.com/containerd/containerd/oci"
"github.com/opencontainers/runtime-spec/specs-go"
//sandboxstore "github.com/containerd/cri/pkg/store/sandbox"
//"github.com/containerd/typeurl"
//specs "github.com/opencontainers/runtime-spec/specs-go"
)
func init() {
// typeurl.Register(&sandboxstore.Metadata{},
// "github.com/containerd/cri/pkg/store/sandbox", "Metadata")
}
func main() {
if err := nginxExample(); err != nil {
log.Fatal(err)
}
}
func nginxExample() error {
client, err := containerd.New("/run/containerd/containerd.sock")
if err != nil {
return err
}
defer client.Close()
ctx := namespaces.WithNamespace(context.Background(), "example")
image, err := client.Pull(ctx, "docker.io/library/nginx:latest", containerd.WithPullUnpack)
if err != nil {
return err
}
//client.Containers()
containerId := "mynginx-1"
hostname := "srashid2"
container, err := client.NewContainer(
ctx,
containerId,
containerd.WithImage(image),
containerd.WithNewSnapshot("nginx-1", image),
containerd.WithNewSpec(
oci.WithImageConfig(image),
oci.WithHostHostsFile,
oci.WithHostResolvconf,
oci.WithHostNamespace(specs.NetworkNamespace),
oci.WithEnv([]string{fmt.Sprintf("HOSTNAME=%s", hostname)}),
//oci.WithImageConfigArgs(image, launchSpec.Cmd),
),
)
if err != nil {
return err
}
defer container.Delete(ctx, containerd.WithSnapshotCleanup)
fmt.Println("NewTask")
task, err := container.NewTask(ctx, cio.NewCreator(cio.WithStdio))
if err != nil {
fmt.Printf("NewTask error %v", err)
return err
}
defer task.Delete(ctx)
fmt.Println("Waiting")
exitStatusC, err := task.Wait(ctx)
if err != nil {
fmt.Printf("Wait error %v", err)
fmt.Println(err)
}
if err := task.Start(ctx); err != nil {
fmt.Printf("Start error %v", err)
return err
}
groupPath, err := cgroupsv2.PidGroupPath(int(task.Pid()))
if err != nil {
fmt.Printf("Start error %v", err)
return err
}
cgroup2, err := cgroupsv2.Load(groupPath)
if err != nil {
fmt.Printf("Start error %v", err)
return err
}
stat, err := cgroup2.Stat()
if err != nil {
fmt.Printf("Start error %v", err)
return err
}
fmt.Printf("Stat %s\n", stat.CPU)
time.Sleep(5 * time.Second)
if err := task.Kill(ctx, syscall.SIGTERM); err != nil {
return err
}
status := <-exitStatusC
code, _, err := status.Result()
if err != nil {
return err
}
fmt.Printf("nginx exited with status: %d\n", code)
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment