- 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",
},
}