Build Docker Image From Go Code - go

I'm trying to build a Docker image using the Docker API and Docker Go libraries (https://github.com/docker/engine-api/). Code example:
package main
import (
"fmt"
"github.com/docker/engine-api/client"
"github.com/docker/engine-api/types"
"golang.org/x/net/context"
)
func main() {
defaultHeaders := map[string]string{"User-Agent": "engine-api-cli-1.0"}
cli, err := client.NewClient("unix:///var/run/docker.sock", "v1.22", nil, defaultHeaders)
if err != nil {
panic(err)
}
fmt.Print(cli.ClientVersion())
opt := types.ImageBuildOptions{
CPUSetCPUs: "2",
CPUSetMems: "12",
CPUShares: 20,
CPUQuota: 10,
CPUPeriod: 30,
Memory: 256,
MemorySwap: 512,
ShmSize: 10,
CgroupParent: "cgroup_parent",
Dockerfile: "dockerSrc/docker-debug-container/Dockerfile",
}
_, err = cli.ImageBuild(context.Background(), nil, opt)
if err == nil || err.Error() != "Error response from daemon: Server error" {
fmt.Printf("expected a Server Error, got %v", err)
}
}
The error is always same:
Error response from daemon: Cannot locate specified Dockerfile: dockerSrc/docker-debug-container/Dockerfile
or
Error response from daemon: Cannot locate specified Dockerfile: Dockerfile
Things I've checked:
The folder exists in build path
I tried both relative and absolute path
There are no softlinks in the path
I tried the same folder for binary and Dockerfile
docker build <path> works
and bunch of other stuff
My other option was to use RemoteContext which looks like it works, but only for fully self contained dockerfiles, and not the ones with "local file presence".
Update:
Tried passing tar as buffer, but got the same result with the following:
dockerBuildContext, err := os.Open("<path to>/docker-debug- container/docker-debug-container.tar")
defer dockerBuildContext.Close()
opt := types.ImageBuildOptions{
Context: dockerBuildContext,
CPUSetCPUs: "2",
CPUSetMems: "12",
CPUShares: 20,
CPUQuota: 10,
CPUPeriod: 30,
Memory: 256,
MemorySwap: 512,
ShmSize: 10,
CgroupParent: "cgroup_parent",
// Dockerfile: "Dockerfile",
}
_, err = cli.ImageBuild(context.Background(), nil, opt)

The following works for me;
package main
import (
"archive/tar"
"bytes"
"context"
"io"
"io/ioutil"
"log"
"os"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
)
func main() {
ctx := context.Background()
cli, err := client.NewEnvClient()
if err != nil {
log.Fatal(err, " :unable to init client")
}
buf := new(bytes.Buffer)
tw := tar.NewWriter(buf)
defer tw.Close()
dockerFile := "myDockerfile"
dockerFileReader, err := os.Open("/path/to/dockerfile")
if err != nil {
log.Fatal(err, " :unable to open Dockerfile")
}
readDockerFile, err := ioutil.ReadAll(dockerFileReader)
if err != nil {
log.Fatal(err, " :unable to read dockerfile")
}
tarHeader := &tar.Header{
Name: dockerFile,
Size: int64(len(readDockerFile)),
}
err = tw.WriteHeader(tarHeader)
if err != nil {
log.Fatal(err, " :unable to write tar header")
}
_, err = tw.Write(readDockerFile)
if err != nil {
log.Fatal(err, " :unable to write tar body")
}
dockerFileTarReader := bytes.NewReader(buf.Bytes())
imageBuildResponse, err := cli.ImageBuild(
ctx,
dockerFileTarReader,
types.ImageBuildOptions{
Context: dockerFileTarReader,
Dockerfile: dockerFile,
Remove: true})
if err != nil {
log.Fatal(err, " :unable to build docker image")
}
defer imageBuildResponse.Body.Close()
_, err = io.Copy(os.Stdout, imageBuildResponse.Body)
if err != nil {
log.Fatal(err, " :unable to read image build response")
}
}

#Mangirdas: staring at a screen long enough DOES help - at least in my case. I have been stuck with the same issue for some time now.
You were right to use the tar file (your second example). If you look at the API doc here https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/build-image-from-a-dockerfile you can see that it expects a tar.
What really helped me was looking at other implementations of the client, perl and ruby in my case. Both create a tar on the fly when being asked to build an image from a directory.
Anyway, you only need to put your dockerBuildContext somewhere else (see the cli.ImageBuild())
dockerBuildContext, err := os.Open("/Path/to/your/docker/tarfile.tar")
defer dockerBuildContext.Close()
buildOptions := types.ImageBuildOptions{
Dockerfile: "Dockerfile", // optional, is the default
}
buildResponse, err := cli.ImageBuild(context.Background(), dockerBuildContext, buildOptions)
if err != nil {
log.Fatal(err)
}
defer buildResponse.Body.Close()
I am not there with naming the images properly yet, but at least I can create them...
Hope this helps.
Cheers

The Docker package has a function for creating a TAR from a file path. It's whats used by the CLI. It's not in the client package so it need to be installed separately:
import (
"github.com/mitchellh/go-homedir"
"github.com/docker/docker/pkg/archive"
)
func GetContext(filePath string) io.Reader {
// Use homedir.Expand to resolve paths like '~/repos/myrepo'
filePath, _ := homedir.Expand(filePath)
ctx, _ := archive.TarWithOptions(filePath, &archive.TarOptions{})
return ctx
}
cli.ImageBuild(context.Background(), GetContext("~/repos/myrepo"), types.ImageBuildOptions{...})

I agree with Marcus Havranek's answer, that method has worked for me. Just want to add how to add a name to an image, since that seemed like an open question:
buildOptions := types.ImageBuildOptions{
Tags: []string{"imagename"},
}
Hope this helps!

Combining a few of the answers, and adding how to correctly parse the returned JSON using DisplayJSONMessagesToStream.
package main
import (
"os"
"log"
"github.com/docker/docker/api/types"
"github.com/docker/docker/pkg/archive"
"github.com/docker/docker/pkg/jsonmessage"
"github.com/docker/docker/pkg/term"
"golang.org/x/net/context"
)
// Build a dockerfile if it exists
func Build(dockerFilePath, buildContextPath string, tags []string) {
ctx := context.Background()
cli := getCLI()
buildOpts := types.ImageBuildOptions{
Dockerfile: dockerFilePath,
Tags: tags,
}
buildCtx, _ := archive.TarWithOptions(buildContextPath, &archive.TarOptions{})
resp, err := cli.ImageBuild(ctx, buildCtx, buildOpts)
if err != nil {
log.Fatalf("build error - %s", err)
}
defer resp.Body.Close()
termFd, isTerm := term.GetFdInfo(os.Stderr)
jsonmessage.DisplayJSONMessagesStream(resp.Body, os.Stderr, termFd, isTerm, nil)
}
I've left our a few convenience functions like getCLI but I'm sure you have your own equivalents.

I encounter same problem. Finally find out the tar file should be docker build context even with Dockerfile.
Here is my code,
package main
import (
"log"
"os"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"golang.org/x/net/context"
)
func main() {
dockerBuildContext, err := os.Open("/Users/elsvent/workspace/Go/src/test/test.tar")
defer dockerBuildContext.Close()
buildOptions := types.ImageBuildOptions{
SuppressOutput: true,
PullParent: true,
Tags: []string{"xxx"},
Dockerfile: "test/Dockerfile",
}
defaultHeaders := map[string]string{"Content-Type": "application/tar"}
cli, _ := client.NewClient("unix:///var/run/docker.sock", "v1.24", nil, defaultHeaders)
buildResp, err := cli.ImageBuild(context.Background(), dockerBuildContext, buildOptions)
if err != nil {
log.Fatal(err)
}
defer buildResp.Body.Close()
}

opt := types.ImageBuildOptions{
Dockerfile: "Dockerfile",
}
filePath, _ = homedir.Expand(".")
buildCtx, _ := archive.TarWithOptions(filePath, &archive.TarOptions{})
x, err := cli.ImageBuild(context.Background(), buildCtx, opt)
io.Copy(os.Stdout, x.Body)

Related

Docker client build error

This defeats me. I am not sure what I am doing wrong. There is so less documentation that searching did not produce good result. I will be happy to see what is the reason behind this strange behaviour.
I am on a MAC (10.11.6) and I am running docker for MAC (beta)
Here is the code I am trying to run
package main
import (
"fmt"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"golang.org/x/net/context"
)
func main() {
defaultHeaders := map[string]string{"User-Agent": "ego-v-0.0.1"}
cli, _ := client.NewClient("unix:///var/run/docker.sock", "v1.24", nil, defaultHeaders)
options := types.ImageBuildOptions{
Dockerfile: "/path/to/my/Dockerfile",
SuppressOutput: false,
Remove: true,
ForceRemove: true,
PullParent: true}
buildResponse, err := cli.ImageBuild(context.Background(), nil, options)
if err != nil {
fmt.Printf("%s", err.Error())
}
fmt.Printf("%s", buildResponse.OSType)
}
This gives me this error -
Error response from daemon: {"message":"Cannot locate specified Dockerfile: /path/to/my/Dockerfile"}
Whereas when I run this command (from the same directory where my Go code is)
docker build /path/to/my
It works absolutely fine.
What am I doing wrong? I feel like banging my head against a wall now. Please help.
------------ EDIT / ADD ------------
I ended up doing this -
package main
import (
"archive/tar"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"golang.org/x/net/context"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
)
func tarit(source, target string) error {
filename := filepath.Base(source)
target = filepath.Join(target, fmt.Sprintf("%s.tar", filename))
fmt.Println(target)
tarfile, err := os.Create(target)
if err != nil {
return err
}
defer tarfile.Close()
tarball := tar.NewWriter(tarfile)
defer tarball.Close()
info, err := os.Stat(source)
if err != nil {
return nil
}
var baseDir string
if info.IsDir() {
baseDir = filepath.Base(source)
}
return filepath.Walk(source,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
header, err := tar.FileInfoHeader(info, info.Name())
if err != nil {
return err
}
if baseDir != "" {
header.Name = filepath.Join(baseDir, strings.TrimPrefix(path, source))
}
if err := tarball.WriteHeader(header); err != nil {
return err
}
if info.IsDir() {
return nil
}
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(tarball, file)
return err
})
}
func main() {
tarit("/dir/with/files/and/dockerfile", "repo")
dockerBuildContext, err := os.Open("./repo.tar")
defer dockerBuildContext.Close()
defaultHeaders := map[string]string{"User-Agent": "ego-v-0.0.1"}
cli, _ := client.NewClient("unix:///var/run/docker.sock", "v1.24", nil, defaultHeaders)
options := types.ImageBuildOptions{
Dockerfile: "repo/Dockerfile",
SuppressOutput: false,
Remove: true,
ForceRemove: true,
PullParent: true}
buildResponse, err := cli.ImageBuild(context.Background(), dockerBuildContext, options)
if err != nil {
fmt.Printf("%s", err.Error())
}
fmt.Printf("********* %s **********", buildResponse.OSType)
}
Now it is not complaining about anything and I can see that the tar is getting made properly and the last println is printing
********* linux **********
Which is a reply from the server. But it does not build anything. I understand that reply is almost immediate as under the hood it is just a POST request. But not sure why it is not building anything although.
The Dockerfile has to be part of the build context that gets TARd and sent to the engine. You shouldn't use an absolute path to the Dockerfile, it must be relative to the context, so you just pass the name.
I don't think the CLI command you demonstrate is right - unless Dockerfile is actually a directory name in your case:
> ls /tmp/path/to/context
Dockerfile
> docker build /tmp/path/to/context/Dockerfile
unable to prepare context: context must be a directory: /tmp/path/to/context/Dockerfile
When you send just the path (which contains the Dockerfile file), that works:
> docker build /tmp/path/to/context
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM alpine
---> ee4603260daa
Successfully built ee4603260daa
The working code to this problem. For anyone who will probably be stuck like me -
package main
import (
"fmt"
"io/ioutil"
"os"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/jhoonb/archivex"
"golang.org/x/net/context"
)
func main() {
tar := new(archivex.TarFile)
tar.Create("/path/to/tar/archieve")
tar.AddAll("/path/to/base/folder", false)
tar.Close()
dockerBuildContext, err := os.Open("/path/to/tar/archieve.tar")
defer dockerBuildContext.Close()
defaultHeaders := map[string]string{"Content-Type": "application/tar"}
cli, _ := client.NewClient("unix:///var/run/docker.sock", "v1.24", nil, defaultHeaders)
options := types.ImageBuildOptions{
SuppressOutput: true,
Remove: true,
ForceRemove: true,
PullParent: true,
Tags: []string{"xxx"}}
buildResponse, err := cli.ImageBuild(context.Background(), dockerBuildContext, options)
defer buildResponse.Body.Close()
if err != nil {
fmt.Printf("%s", err.Error())
}
//time.Sleep(5000 * time.Millisecond)
fmt.Printf("********* %s **********", buildResponse.OSType)
response, err := ioutil.ReadAll(buildResponse.Body)
if err != nil {
fmt.Printf("%s", err.Error())
}
fmt.Println(string(response))
}
Be careful that a lot of error checks are not done in this code. This is just a working code where I am using docker client lib successfully.
Let me know what you think.
Thanks to -
"github.com/jhoonb/archivex"

go-dockerclient `UploadToContainer` tar examples

Unlike the python docker client which I've been using there seems to be very few examples for how to use go-dockerclient. I'm trying to work out how to upload a tar archive to a docker container using UploadToContainer which is documented here: https://godoc.org/github.com/fsouza/go-dockerclient#Client.UploadToContainer
I've built a .tar archive in memory which looks like this:
import (
"archive/tar"
"bytes"
"fmt"
"log"
)
func main() {
// Create a buffer to write our archive to.
buf := new(bytes.Buffer)
// Create a new tar archive.
tw := tar.NewWriter(buf)
// Add some files to the archive.
var files = []struct {
Name, Body string
}{
{"file1.txt", "This archive contains some text files."},
{"file2.txt", "Gopher names:\nGeorge\nGeoffrey\nGonzo"},
{"file3.txt", "Get animal handling license."},
}
for _, file := range files {
hdr := &tar.Header{
Name: file.Name,
Mode: 0600,
Size: int64(len(file.Body)),
}
if err := tw.WriteHeader(hdr); err != nil {
log.Fatalln(err)
}
if _, err := tw.Write([]byte(file.Body)); err != nil {
log.Fatalln(err)
}
}
// Make sure to check the error on Close.
if err := tw.Close(); err != nil {
log.Fatalln(err)
}
fmt.Print(buf)
I've also successfully created a docker-client like this:
import (
"fmt"
"github.com/fsouza/go-dockerclient"
"os"
)
func main() {
endpoint := "https://localhost:52376"
path := os.Getenv("DOCKER_CERT_PATH")
ca := fmt.Sprintf("%s/ca.pem", path)
cert := fmt.Sprintf("%s/cert.pem", path)
key := fmt.Sprintf("%s/key.pem", path)
client, _ := docker.NewTLSClient(endpoint, cert, key, ca)
I have a running container called nginx-ssl and I need to upload the tar archive to the container but can't work out whats needed to write the tar to my container.
This what I do using python where data is an in memory tar object thats been pre-created.
cli.put_archive(container='nginx-ssl', path="/keys", data=newtar)
UPDATE
++++++
I just tried this:
input := bytes.NewBufferString("test")
uploadOpts := docker.UploadToContainerOptions{
InputStream: input,
Path: "/test-test",
}
err := client.UploadToContainer("20e0c347cd41", uploadOpts)
if err != nil {
print(err)
}
20e0c347cd41 is the "CONTAINER ID"
which returns this error:
(0xd60088,0xc820278100)
Then I tried this:
//input := bytes.NewBufferString(buf)
uploadOpts := docker.UploadToContainerOptions{
InputStream: buf,
Path: "/test-test.txt",
}
err := client.UploadToContainer("nginx-ssl", uploadOpts)
if err != nil {
print(err)
}
where InputStream: buf is the output from my tar archive shown above. This throws the following error:
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x0 pc=0x2df8]
goroutine 1 [running]:
panic(0x3d3420, 0xc82000a0d0)
but nothing gets uploaded.
ΒΆ
In case you still need it, that is how I have managed to upload a file (named test.txt) onto the container(in the /data dir) using go-dockerclient.
content := "File content"
buf := new(bytes.Buffer)
tw := tar.NewWriter(buf)
hdr := &tar.Header{
Name: "test.txt",
Mode: 0644,
Size: int64(len(content)),
}
err := tw.WriteHeader(hdr)
if err != nil {
fmt.Println(err.Error())
}
_, err = tw.Write([]byte(content))
if err != nil {
fmt.Println(err.Error())
}
in := sT{bytes.NewBufferString(string(buf.Bytes()))}
opts := docker.UploadToContainerOptions{Path: "data", InputStream: in}
cont, err := client.CreateContainer(options)
if err != nil {
log.Printf(err.Error())
return
}
err = client.UploadToContainer(container.ID, opts)
and sT is defined as:
type sT struct {
*bytes.Buffer
}
You might still improve the code (like use a better aproach for the buffer), but for my tests this is ok.
The result is /data/test.txt containing the text "File content"
HTH

List Openshift objects via Go client API

Trying to write a microservice to manage imagestreams on my Openshift cluster. I read the oc client code to work out how to read my kubeconfig and create the Client.
I can make requests with the Kubernetes Client to get the Kubernetes objects, e.g. pods, but any requests I make with the Openshift Client returns back an empty list.
I'm new to Go as well, so I'm sure I'm doing something wrong. Here's what I have so far:
package main
import (
"fmt"
"log"
"github.com/spf13/pflag"
kapi "k8s.io/kubernetes/pkg/api"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
)
func main() {
flags := pflag.FlagSet{}
factory := clientcmd.New(&flags)
osclient, kclient, err := factory.Clients()
if err != nil {
log.Fatalln("Error:", err)
}
config, _ := factory.ClientConfig()
fmt.Println("KClient config", config)
config, _ = factory.OpenShiftClientConfig.ClientConfig()
fmt.Println("OSClient config", config)
// Empty list!
projects, err := osclient.Projects().List(kapi.ListOptions{})
if err != nil {
log.Println("Error:", err)
} else {
fmt.Println("Projects", projects, len(projects.Items))
}
// Also empty list
buildconfigs, err := osclient.BuildConfigs("my-project").List(kapi.ListOptions{})
if err != nil {
log.Println("Error:", err)
} else {
fmt.Println("Buildconfigs", buildconfigs, len(buildconfigs.Items))
}
// Works!
pods, err := kclient.Pods("my-project").List(kapi.ListOptions{})
if err != nil {
log.Println("Error:", err)
} else {
fmt.Println("Pods", len(pods.Items))
for _, pod := range pods.Items {
fmt.Println(pod.ObjectMeta.Name)
}
}
// Permission error, as expected
namespaces, err := kclient.Namespaces().List(kapi.ListOptions{})
if err != nil {
log.Println("Error:", err)
} else {
fmt.Println("Namespaces", namespaces, len(namespaces.Items))
}
}
You were ever so close and the issue was a tiny one: you needed to include the following additional import:
import _ "github.com/openshift/origin/pkg/api/install"
I'm not fully clear what the import actually does, but evidently it causes necessary additional functionality to be linked into the binary, without which the OpenShift client doesn't work (returns empty lists).
All of the OpenShift command line tools include that import, and as of writing many include some/all of the following as well:
import (
_ "github.com/openshift/origin/pkg/api/install"
_ "k8s.io/kubernetes/pkg/api/install"
_ "k8s.io/kubernetes/pkg/apis/autoscaling/install"
_ "k8s.io/kubernetes/pkg/apis/batch/install"
_ "k8s.io/kubernetes/pkg/apis/extensions/install"
)
Finally, here's a full code example which works for me (updated against origin v3.6.0-alpha):
package main
import (
"fmt"
_ "github.com/openshift/origin/pkg/api/install"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
"github.com/spf13/pflag"
"k8s.io/apimachinery/pkg/apis/meta/v1"
)
func main() {
factory := clientcmd.New(pflag.CommandLine)
pflag.Parse()
oc, kc, err := factory.Clients()
if err != nil {
panic(err)
}
namespace, _, err := factory.DefaultNamespace()
if err != nil {
panic(err)
}
pods, err := kc.Core().Pods(namespace).List(v1.ListOptions{})
if err != nil {
panic(err)
}
for _, pod := range pods.Items {
fmt.Printf("Pod: %s\n", pod.Name)
}
buildconfigs, err := oc.BuildConfigs(namespace).List(v1.ListOptions{})
if err != nil {
panic(err)
}
for _, buildconfig := range buildconfigs.Items {
fmt.Printf("BuildConfig: %s\n", buildconfig.Name)
}
}
To run this example, you will currently need to vendor OpenShift and its dependencies. One very hacky way to do this is as follows:
rm -rf vendor
mkdir -p vendor/github.com/openshift/origin
ln -s $GOPATH/src/github.com/openshift/origin/vendor/* vendor
ln -s $GOPATH/src/github.com/openshift/origin/vendor/github.com/* vendor/github.com
ln -s $GOPATH/src/github.com/openshift/origin/vendor/github.com/openshift/* vendor/github.com/openshift
ln -s $GOPATH/src/github.com/openshift/origin/pkg vendor/github.com/openshift/origin
Finally, it is intended to make a proper standalone Go client for OpenShift - the backlog card for this is at https://trello.com/c/PTDrY0GF/794-13-client-provide-go-client-similar-to-kubernetes.

Go build & exec: fork/exec: permission denied

I need to build a program using the Go toolchain and then execute it. For some reasons I get a permission error due the forking. Is there a way to circumvent this error or any best practice? I think my program does something similar with Go test tool, though go test doesn't get this kind of error.
package main
import(
"os"
"os/exec"
"flag"
log "github.com/golang/glog"
)
func main(){
flag.Parse()
tdir := "abc"
if err := os.MkdirAll(tdir, 0777); err !=nil{
log.Error(err)
return
}
f, err := os.Create(tdir + "/main.go")
if err !=nil{
log.Error(err)
return
}
if err = f.Chmod(0777); err !=nil{
log.Error(err)
return
}
defer f.Close()
defer os.Remove(f.Name())
if _, err = f.Write([]byte(tpl)); err !=nil{
log.Error(err)
return
}
cmd := exec.Command("go", "build", "-o", "edoc")
cmd.Path = tdir
b, err := cmd.CombinedOutput()
if err !=nil{
log.Errorf("%s, err %v", b, err)
return
}
}
var tpl = `package main
import(
"fmt"
"flag"
)
func main(){
flag.Parse()
fmt.Printf("Hello World")
}`
Error:
E0202 18:24:42.359008 13600 main.go:36] , err fork/exec abc: permission denied
OS: OSX 10.11
You're changing the command path from the location of your go binary, to abc.
type Cmd struct {
// Path is the path of the command to run.
//
// This is the only field that must be set to a non-zero
// value. If Path is relative, it is evaluated relative
// to Dir.
Path string
If you want to change the working directory, use Cmd.Dir

How can I create a simple client app with the Kubernetes Go library?

I'm struggling with the Kubernetes Go library. The docs--at least the ones I found--appear out-of-date with the library itself. The example provided does not build because of issues with the imports. I'm just trying to do something simple: get a Service object by name and print some attributes (like nodePort). I just need a simple example of library usage to get me going.
I could easily do this using the RESTful API but that feels like re-inventing the wheel.
So after a little experimentation and a hint from the k8s Slack channel, I have this example. Perhaps someone can update the example with a proper import path.
package main
import (
"fmt"
"log"
"github.com/kubernetes/kubernetes/pkg/api"
client "github.com/kubernetes/kubernetes/pkg/client/unversioned"
)
func main() {
config := client.Config{
Host: "http://my-kube-api-server.me:8080",
}
c, err := client.New(&config)
if err != nil {
log.Fatalln("Can't connect to Kubernetes API:", err)
}
s, err := c.Services(api.NamespaceDefault).Get("some-service-name")
if err != nil {
log.Fatalln("Can't get service:", err)
}
fmt.Println("Name:", s.Name)
for p, _ := range s.Spec.Ports {
fmt.Println("Port:", s.Spec.Ports[p].Port)
fmt.Println("NodePort:", s.Spec.Ports[p].NodePort)
}
}
Here's how to do it with the latest Go client.
If you're inside the k8s cluster:
package main
import (
"fmt"
"k8s.io/client-go/1.5/kubernetes"
"k8s.io/client-go/1.5/pkg/api/v1"
"k8s.io/client-go/1.5/rest"
)
func main() {
config, err = rest.InClusterConfig()
if err != nil {
return nil, err
}
c, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
// Get Pod by name
pod, err := c.Pods(v1.NamespaceDefault).Get("my-pod")
if err != nil {
fmt.Println(err)
return
}
// Print its creation time
fmt.Println(pod.GetCreationTimestamp())
}
And if you're outside of the cluster:
package main
import (
"fmt"
"k8s.io/client-go/1.5/kubernetes"
"k8s.io/client-go/1.5/pkg/api/v1"
"k8s.io/client-go/1.5/tools/clientcmd"
)
func main() {
config, err := clientcmd.BuildConfigFromFlags("", <kube-config-path>)
if err != nil {
return nil, err
}
c, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
// Get Pod by name
pod, err := c.Pods(v1.NamespaceDefault).Get("my-pod")
if err != nil {
fmt.Println(err)
return
}
// Print its creation time
fmt.Println(pod.GetCreationTimestamp())
}
I have gone into more detail on this in a blog post.
With kubernetes go client, it could be done this way:
package main
import (
"flag"
"fmt"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/pkg/api/v1"
"k8s.io/client-go/tools/clientcmd"
)
var (
kubeconfig = flag.String("kubeconfig", "./config", "absolute path to the kubeconfig file")
)
func main() {
flag.Parse()
// uses the current context in kubeconfig
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
panic(err.Error())
}
// creates the clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
services, err := clientset.Core().Services("").List(v1.ListOptions{})
if err != nil {
panic(err.Error())
}
fmt.Printf("There are %d pods in the cluster\n", len(services.Items))
for _, s := range services.Items {
for p, _ := range s.Spec.Ports {
fmt.Println("Port:", s.Spec.Ports[p].Port)
fmt.Println("NodePort:", s.Spec.Ports[p].NodePort)
}
}
}

Resources