Created
February 22, 2019 14:55
-
-
Save chrigl/5e7396ee785a03bceef09ce25927a9a3 to your computer and use it in GitHub Desktop.
Use Kubernetes client-go with unstructured and dynamic client
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 ( | |
"flag" | |
"fmt" | |
"log" | |
"os" | |
"os/user" | |
"path/filepath" | |
"sigs.k8s.io/yaml" | |
"k8s.io/apimachinery/pkg/api/meta" | |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | |
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | |
"k8s.io/client-go/discovery" | |
"k8s.io/client-go/dynamic" | |
"k8s.io/client-go/rest" | |
"k8s.io/client-go/restmapper" | |
"k8s.io/client-go/tools/clientcmd" | |
) | |
var ( | |
kubeconfig, masterURL string | |
testSecret = `apiVersion: v1 | |
kind: Secret | |
type: Opaque | |
data: | |
some: aGVsbG8gd29ybGQK | |
metadata: | |
name: this-is-a-test | |
namespace: default | |
` | |
) | |
func main() { | |
flag.StringVar(&kubeconfig, "kubernetes config", "", "Path to kubeconfig. Only required if out-of-cluster.") | |
flag.Parse() | |
cfg, err := getConfig() | |
if err != nil { | |
log.Fatal(err) | |
} | |
u := &unstructured.Unstructured{} | |
err = yaml.Unmarshal([]byte(testSecret), u) | |
if err != nil { | |
log.Fatal(err) | |
} | |
dc := discovery.NewDiscoveryClientForConfigOrDie(cfg) | |
gr, err := restmapper.GetAPIGroupResources(dc) | |
if err != nil { | |
log.Fatal(err) | |
} | |
gvk := u.GetObjectKind().GroupVersionKind() | |
rm := restmapper.NewDiscoveryRESTMapper(gr) | |
mapping, err := rm.RESTMapping(gvk.GroupKind(), gvk.Version) | |
if err != nil { | |
log.Fatal(err) | |
} | |
dyn, err := dynamic.NewForConfig(cfg) | |
var ri dynamic.ResourceInterface | |
if mapping.Scope.Name() == meta.RESTScopeNameRoot { | |
ri = dyn.Resource(mapping.Resource) | |
} else { | |
ri = dyn.Resource(mapping.Resource).Namespace(u.GetNamespace()) | |
} | |
annotations := make(map[string]string) | |
annotations["testing.imke.cloud"] = "definitely" | |
metadata := u.Object["metadata"].(map[string]interface{}) | |
metadata["annotations"] = annotations | |
uS, err := ri.Create(u, metav1.CreateOptions{}) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Printf("%#v\n", uS) | |
} | |
func getConfig() (*rest.Config, error) { | |
// If a flag is specified with the config location, use that | |
if len(kubeconfig) > 0 { | |
return clientcmd.BuildConfigFromFlags(masterURL, kubeconfig) | |
} | |
// If an env variable is specified with the config locaiton, use that | |
if len(os.Getenv("KUBECONFIG")) > 0 { | |
return clientcmd.BuildConfigFromFlags(masterURL, os.Getenv("KUBECONFIG")) | |
} | |
// If no explicit location, try the in-cluster config | |
if c, err := rest.InClusterConfig(); err == nil { | |
return c, nil | |
} | |
// If no in-cluster config, try the default location in the user's home directory | |
if usr, err := user.Current(); err == nil { | |
if c, err := clientcmd.BuildConfigFromFlags( | |
"", filepath.Join(usr.HomeDir, ".kube", "config")); err == nil { | |
return c, nil | |
} | |
} | |
return nil, fmt.Errorf("could not locate a kubeconfig") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment