Skip to content

Instantly share code, notes, and snippets.

@indradhanush
Created April 6, 2018 11:19
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 indradhanush/a582828dbda4c2dca710d51bd58635f6 to your computer and use it in GitHub Desktop.
Save indradhanush/a582828dbda4c2dca710d51bd58635f6 to your computer and use it in GitHub Desktop.
// -*- mode:go;mode:go-playground -*-
// snippet of code @ 2018-04-06 15:35:29
// === Go Playground ===
// Execute the snippet with Ctl-Return
// Remove the snippet completely with its dir and all files M-x `go-playground-rm`
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
func main() {
secretName := "habitat-osb-redis"
dataKey := "user.toml"
dataString := `requirepass = "helloworldfoo"`
s := &v1.Secret{
TypeMeta: metav1.TypeMeta{
Kind: "Secret",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: secretName,
},
Data: map[string][]byte{
dataKey: []byte(dataString),
},
Type: v1.SecretTypeOpaque,
}
kubeClient, err := setupKubeClient()
if err != nil {
fmt.Printf("%q", err)
os.Exit(1)
}
secret, err := kubeClient.CoreV1().Secrets("default").Create(s)
if err != nil {
switch err.(type) {
case *errors.UnexpectedObjectError:
fmt.Printf("Unexpected: %q", err)
case *errors.StatusError:
fmt.Printf("StatusError: %+v", err.ErrStatus)
default:
fmt.Printf("Generic: %q", err)
}
os.Exit(1)
}
fmt.Printf("%+v\n", secret)
}
func setupKubeClient() (*kubernetes.Clientset, error) {
var kubeconfig *string
home := os.Getenv("HOME")
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
flag.Parse()
// The broker will always run inside the Kubernetes cluster.
c, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
return nil, err
}
apiclientset, err := kubernetes.NewForConfig(c)
if err != nil {
return nil, err
}
return apiclientset, nil
}
func homeDir() string {
if h := os.Getenv("HOME"); h != "" {
return h
}
return os.Getenv("USERPROFILE") // windows
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment