Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save aaron-prindle/8dd89c5fddb9cbb189d71312c1e135bd to your computer and use it in GitHub Desktop.
Save aaron-prindle/8dd89c5fddb9cbb189d71312c1e135bd to your computer and use it in GitHub Desktop.
package main
import (
"context"
"fmt"
v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/fake"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func getTestCRD() *v1.CustomResourceDefinition {
return &v1.CustomResourceDefinition{
ObjectMeta: metav1.ObjectMeta{
Name: "testcrds.example.com",
},
Spec: v1.CustomResourceDefinitionSpec{
Group: "example.com",
Names: v1.CustomResourceDefinitionNames{
Plural: "testcrds",
Singular: "testcrd",
Kind: "TestCRD",
},
Scope: v1.ClusterScoped,
Versions: []v1.CustomResourceDefinitionVersion{
{
Name: "v1",
Served: true,
Storage: true,
Schema: &v1.CustomResourceValidation{
OpenAPIV3Schema: &v1.JSONSchemaProps{
Type: "object",
Properties: map[string]v1.JSONSchemaProps{
"spec": {
Type: "object",
Properties: map[string]v1.JSONSchemaProps{
"foo": {
Type: "string",
},
},
},
},
},
},
},
},
},
}
}
func main() {
ctx := context.TODO()
crd := getTestCRD()
client := fake.NewClientset() // fails with CRD
// client := fake.NewSimpleClientset() // works with CRD
_, err := client.ApiextensionsV1().CustomResourceDefinitions().Create(ctx, crd, metav1.CreateOptions{})
if err != nil {
fmt.Printf("Failed to create CRD: %v\n", err)
} else {
fmt.Println("CRD created successfully")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment