2
  • client-go v0.19.2
  • golang 1.13

I'm building a tool to create k8s resources from json(just like kubectl create -f).

I found that dynamic client can do such things,but when i use it with code bellow,i found it is hard to find schema.GroupVersionResource for given resource's json.Am i missing something or the only way to get resource is through restmapper?

container := &unstructured.Unstructured{}
if err := container.UnmarshalJSON([]byte(jsonstring); err != nil {
    return err
}

_, err := k8sclient.Dynamic.Resource(?).Create(ctx, container, metav1.CreateOptions{})
if err != nil {
    return err
}

I know a work around is to write some code like bellow, but i'm sure it's not the best practice and there are too many of them besides crds.

var kindResourceMap = map[string]schema.GroupVersionResource{
    "Deployment": {
        Group:    "apps",
        Version:  "v1",
        Resource: "deployments",
    },
    "ConfigMap": {
        Group:    "apps",
        Version:  "v1",
        Resource: "configmaps",
    },
    "Job": {
        Group:    "batch",
        Version:  "v1",
        Resource: "jobs",
    },
    "Secret": {
        Group:    "api",
        Version:  "v1",
        Resource: "secrets",
    },
    "Service": {
        Group:    "api",
        Version:  "v1",
        Resource: "services",
    },
    "StatefulSet": {
        Group:    "apps",
        Version:  "v1",
        Resource: "statefulsets",
    },
    "PersistentVolume": {
        Group:    "api",
        Version:  "v1",
        Resource: "persistentvolumes",
    },
    "CustomResourceDefinition": {
        Group:    "apiextensions.k8s.io",
        Version:  "v1beta1",
        Resource: "customresourcedefinitions",
    },
}
chresse
  • 5,486
  • 3
  • 30
  • 47
Jonyhy96
  • 168
  • 1
  • 2
  • 10

1 Answers1

1

You can use restmapper that directly queries the definitions from metav1 using the discovery client.

import (
    "k8s.io/client-go/rest"
    "k8s.io/client-go/discovery"
    "k8s.io/client-go/restmapper"
    "k8s.io/apimachinery/pkg/runtime/schema"
)

 ...

c := discovery.NewDiscoveryClientForConfigOrDie(&rest.Config{})

groupResources, err := restmapper.GetAPIGroupResources(c)
mapper := restmapper.NewDiscoveryRESTMapper(groupResources)

mapping, err := mapper.RESTMapping(schema.ParseGroupKind("apps.Deployment"))
fmt.Println(mapping.Resource)

This is cooked in sigs.k8s.io/controller-runtime/pkg/client

mapping, err := c.RESTMapper().RESTMapping(schema.ParseGroupKind("apps.Deployment"))
fmt.Println(mapping.Resource)

Look here for how it's done: https://github.com/kubernetes-sigs/controller-runtime/blob/master/pkg/client/apiutil/dynamicrestmapper.go#L77

Tamir Daniely
  • 1,659
  • 1
  • 20
  • 24