Created
January 18, 2018 20:50
Star
You must be signed in to star a gist
k8s go-client in cluster pod creation
This file contains 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 ( | |
// "encoding/json" | |
"os" | |
log "github.com/sirupsen/logrus" | |
"k8s.io/api/core/v1" | |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | |
"k8s.io/client-go/kubernetes" | |
"k8s.io/client-go/rest" | |
) | |
func main() { | |
config, err := rest.InClusterConfig() | |
if err != nil { | |
log.WithError(err).Fatal("couldn't get kubernetes config") | |
} | |
clientset, err := kubernetes.NewForConfig(config) | |
if err != nil { | |
log.WithError(err).Fatal("K8s error") | |
} | |
// create pod | |
pod, err := clientset.CoreV1().Pods(v1.NamespaceDefault).Create(&v1.Pod{ | |
ObjectMeta: metav1.ObjectMeta{ | |
Name: "my-pod", | |
}, | |
Spec: v1.PodSpec{ | |
Containers: []v1.Container{ | |
{ | |
Name: "go-client-test", | |
Image: "node:alpine", | |
Stdin: true, | |
TTY: true, | |
}, | |
}, | |
}, | |
}) | |
if err != nil { | |
log.WithError(err).Fatal("Oh Noes") | |
} | |
log.Info("Bye Felicia") | |
os.Exit(0) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment