add cl-k8s

Signed-off-by: Jess Frazelle <acidburn@microsoft.com>

add attributes

Signed-off-by: Jess Frazelle <acidburn@microsoft.com>
This commit is contained in:
Jess Frazelle 2018-03-20 02:02:00 -04:00
parent 026845b5d4
commit 1d95b0a02a
6 changed files with 202 additions and 9 deletions

1
.gitattributes vendored Normal file
View file

@ -0,0 +1 @@
examples/*/bindata.go filter=lfs diff=lfs merge=lfs -text

View file

@ -9,19 +9,30 @@ GO_LDFLAGS_STATIC=-ldflags "-w -extldflags -static"
all: clean build fmt lint test staticcheck vet ## Runs a clean, build, fmt, lint, test, staticcheck, and vet all: clean build fmt lint test staticcheck vet ## Runs a clean, build, fmt, lint, test, staticcheck, and vet
.PHONY: build .PHONY: build
build: alpine busybox ## Builds a static executable or package build: alpine busybox cl-k8s ## Builds a static executable or package
.PHONY: alpine .PHONY: alpine
alpine: generate alpine:
@echo "+ $@" @echo "+ $@"
go generate ./examples/$@/...
CGO_ENABLED=1 go build \ CGO_ENABLED=1 go build \
-tags "$(BUILDTAGS) static_build" \ -tags "$(BUILDTAGS) static_build" \
${GO_LDFLAGS_STATIC} -o $@ ./examples/$@/... ${GO_LDFLAGS_STATIC} -o $@ ./examples/$@/...
@echo "Static container for $@ created at: ./$@" @echo "Static container for $@ created at: ./$@"
.PHONY: busybox .PHONY: busybox
busybox: generate busybox:
@echo "+ $@" @echo "+ $@"
go generate ./examples/$@/...
CGO_ENABLED=1 go build \
-tags "$(BUILDTAGS) static_build" \
${GO_LDFLAGS_STATIC} -o $@ ./examples/$@/...
@echo "Static container for $@ created at: ./$@"
.PHONY: cl-k8s
cl-k8s:
@echo "+ $@"
go generate ./examples/$@/...
CGO_ENABLED=1 go build \ CGO_ENABLED=1 go build \
-tags "$(BUILDTAGS) static_build" \ -tags "$(BUILDTAGS) static_build" \
${GO_LDFLAGS_STATIC} -o $@ ./examples/$@/... ${GO_LDFLAGS_STATIC} -o $@ ./examples/$@/...
@ -81,14 +92,10 @@ tag: ## Create a new git tag to prepare to build a release
git tag -sa $(VERSION) -m "$(VERSION)" git tag -sa $(VERSION) -m "$(VERSION)"
@echo "Run git push origin $(VERSION) to push your new tag to GitHub and trigger a travis build." @echo "Run git push origin $(VERSION) to push your new tag to GitHub and trigger a travis build."
.PHONY: generate
generate:
GOMAXPROCS=1 go generate ./...
.PHONY: clean .PHONY: clean
clean: ## Cleanup any build binaries or packages clean: ## Cleanup any build binaries or packages
@echo "+ $@" @echo "+ $@"
$(RM) alpine busybox $(RM) alpine busybox cl-k8s
@sudo $(RM) -r rootfs @sudo $(RM) -r rootfs
.PHONY: help .PHONY: help

View file

@ -30,8 +30,12 @@ $ make alpine
Static container created at: ./alpine Static container created at: ./alpine
# building the busybox example # building the busybox example
$ make alpine $ make busybox
Static container created at: ./busybox Static container created at: ./busybox
# building the cl-k8s example
$ make cl-k8s
Static container created at: ./cl-k8s
``` ```
### Running ### Running
@ -39,6 +43,7 @@ Static container created at: ./busybox
```console ```console
$ ./alpine $ ./alpine
$ ./busybox $ ./busybox
$ ./cl-k8s
``` ```
## Cool things ## Cool things

View file

@ -18,6 +18,7 @@ type SpecOpts struct {
Readonly bool Readonly bool
Terminal bool Terminal bool
Args []string Args []string
Mounts []specs.Mount
Hooks *specs.Hooks Hooks *specs.Hooks
} }
@ -55,5 +56,9 @@ func Spec(opts SpecOpts) *specs.Spec {
spec.Process.Args = opts.Args spec.Process.Args = opts.Args
} }
if opts.Mounts != nil {
spec.Mounts = append(spec.Mounts, opts.Mounts...)
}
return spec return spec
} }

View file

@ -0,0 +1,18 @@
// +build ignore
package main
import (
"fmt"
"os"
"github.com/genuinetools/binctr/container"
)
// Pulls an image and saves the binary data in the container package bindata.go.
func main() {
if err := container.EmbedImage("r.j3ss.co/clisp"); err != nil {
fmt.Fprintf(os.Stderr, "embed image failed: %v\n", err)
os.Exit(1)
}
}

157
examples/cl-k8s/main.go Normal file
View file

@ -0,0 +1,157 @@
package main
import (
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"github.com/genuinetools/binctr/container"
"github.com/opencontainers/runc/libcontainer"
_ "github.com/opencontainers/runc/libcontainer/nsenter"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/sirupsen/logrus"
)
const (
defaultRoot = "/tmp/binctr-cl-k8s"
defaultRootfsDir = "rootfs"
)
var (
containerID string
root string
file string
)
func init() {
// Parse flags
flag.StringVar(&containerID, "id", "cl-k8s", "container ID")
flag.StringVar(&root, "root", defaultRoot, "root directory of container state, should be tmpfs")
flag.Usage = func() {
flag.PrintDefaults()
}
flag.Parse()
if flag.NArg() < 1 {
logrus.Fatal("pass a file to run with cl-k8s")
}
// Get the file args passed.
file := flag.Arg(0)
// Get the absolute path.
var err error
file, err = filepath.Abs(file)
if err != nil {
logrus.Fatal(err)
}
// Check if its directory.
fi, err := os.Stat(file)
if err != nil {
logrus.Fatal(err)
}
if !fi.Mode().IsDir() {
// Copy the file to a temporary directory.
file, err = copyFile(file)
if err != nil {
logrus.Fatal(err)
}
fmt.Printf("new file is %s\n", file)
}
}
//go:generate go run generate.go
func main() {
if len(os.Args) > 1 && os.Args[1] == "init" {
runInit()
return
}
// Create a new container spec with the following options.
opts := container.SpecOpts{
Rootless: true,
Terminal: true,
Args: []string{"clisp", filepath.Join("/home/user/scripts", filepath.Base(file))},
Mounts: []specs.Mount{
{
Destination: "/home/user/scripts/",
Type: "bind",
Source: filepath.Dir(file),
Options: []string{"bind", "ro"},
},
},
}
fmt.Printf("opts %#v\n", opts)
spec := container.Spec(opts)
// Initialize the container object.
c := &container.Container{
ID: containerID,
Spec: spec,
Root: root,
Rootless: true,
}
// Unpack the rootfs.
if err := c.UnpackRootfs(defaultRootfsDir, Asset); err != nil {
logrus.Fatal(err)
}
// Run the container.
status, err := c.Run()
if err != nil {
logrus.Fatal(err)
}
// Remove the rootfs after the container has exited.
if err := os.RemoveAll(defaultRootfsDir); err != nil {
logrus.Warnf("removing rootfs failed: %v", err)
}
// Exit with the container's exit status.
os.Exit(status)
}
func runInit() {
runtime.GOMAXPROCS(1)
runtime.LockOSThread()
factory, _ := libcontainer.New("")
if err := factory.StartInitialization(); err != nil {
// as the error is sent back to the parent there is no need to log
// or write it to stderr because the parent process will handle this
os.Exit(1)
}
panic("libcontainer: container init failed to exec")
}
// copyFile copies the src file to a temporary directory.
func copyFile(src string) (string, error) {
in, err := os.Open(src)
if err != nil {
return "", err
}
defer in.Close()
tmpd, err := ioutil.TempDir("", "")
if err != nil {
return "", err
}
out, err := os.Create(filepath.Join(tmpd, filepath.Base(src)))
if err != nil {
return "", err
}
defer out.Close()
_, err = io.Copy(out, in)
if err != nil {
return "", err
}
return out.Name(), out.Close()
}