Skip to content

Automatically generate a dynamic client or a fargateprofiles client with a runtime scheme #998

@jicowan

Description

@jicowan

Is your feature request related to a problem?
I want to be able to create a CRD object programmatically.

Describe the solution you'd like
I had to create a dynamic client in Go to create an instance of the CRD. I could have used runtime scheme too. I'd like for these clients to be included in the package so I don't have to create them.

Example 1:

client, err := dynamic.NewForConfig(config)
j, err := json.Marshal(fargateInstance)
if err != nil {
    panic(err)
}
u := &unstructured.Unstructured{}
u.UnmarshalJSON(j)

// create GVR. This should exist somewhere already
gvr := v1alpha1.GroupVersion.WithResource("fargateprofiles")

n, err := client.Resource(gvr).Namespace("default").Create(context.TODO(), u, metav1.CreateOptions{})
if err != nil {
    panic(err)
}
fmt.Println(n)

Example 2:

v1alpha1.AddToScheme(scheme.Scheme)

config.ContentConfig.GroupVersion = &v1alpha1.GroupVersion
config.APIPath = "/apis"
config.NegotiatedSerializer = serializer.NewCodecFactory(scheme.Scheme)
fargateClient, err := rest.UnversionedRESTClientFor(config)
if err != nil {
    panic(err)
}
...
result := v1alpha1.FargateProfile{}
body, err := json.Marshal(fargateInstance)
res := fargateClient.Post().Resource("fargateprofiles").Namespace("default").Body(body).Do(context.TODO())
fmt.Println(res.Error())
fmt.Println(result)

Example 3:

obj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(fargateInstance)
if err != nil {
    panic(err)
}
u := &unstructured.Unstructured{Object: obj}

// create GVR. This should exist somewhere already
gvr := v1alpha1.GroupVersion.WithResource("fargateprofiles")
result := &v1alpha1.FargateProfile{}
n, err := client.Resource(gvr).Namespace("default").Create(context.TODO(), u, metav1.CreateOptions{})
if err != nil {
    panic(err)
}
runtime.DefaultUnstructuredConverter.FromUnstructured(n.Object, result)
fmt.Println(result)

Describe alternatives you've considered
Using a different language like Python, https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/CustomObjectsApi.md

Metadata

Metadata

Labels

kind/enhancementCategorizes issue or PR as related to existing feature enhancements.

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions