Skip to content

Instantly share code, notes, and snippets.

@adietish
Created February 8, 2022 17:58
Show Gist options
  • Save adietish/f83d4347e96ce14788b9f739613abdfb to your computer and use it in GitHub Desktop.
Save adietish/f83d4347e96ce14788b9f739613abdfb to your computer and use it in GitHub Desktop.
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: crontabs.stable.example.com
spec:
group: stable.example.com
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
cronSpec:
type: string
image:
type: string
replicas:
type: integer
scope: Namespaced
names:
plural: crontabs
singular: crontab
kind: CronTab
shortNames:
- ct
"com.example.stable.v1.CronTab": {
"type": "object",
"properties": {
"apiVersion": {
"description": "APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources",
"type": "string"
},
"kind": {
"description": "Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds",
"type": "string"
},
"metadata": {
"description": "Standard object's metadata. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata",
"$ref": "#/definitions/io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta_v2"
},
"spec": {
"type": "object",
"properties": {
"cronSpec": {
"type": "string"
},
"image": {
"type": "string"
},
"replicas": {
"type": "integer"
}
}
}
},
"x-kubernetes-group-version-kind": [
{
"group": "stable.example.com",
"kind": "CronTab",
"version": "v1"
}
]
}
@adietish
Copy link
Author

adietish commented Feb 8, 2022

return CustomResourceDefinitionContext.Builder()
    .withGroup(definition.spec.group)
    .withVersion(getHighestPriorityVersion(definition.spec)) // use version with highest priority
    .withScope(definition.spec.scope)
    .withName(definition.metadata.name)
    .withPlural(definition.spec.names.plural)
    .withKind(definition.spec.names.kind)
    .build()
/**
 * Returns the version for given [CustomResourceDefinitionSpec].
 * The version with the highest priority is chosen if there are several available.
 *
 * @param spec the [CustomResourceDefinitionSpec] to get the version from
 *
 * @return the version for the given [CustomResourceDefinitionSpec]
 */
fun getHighestPriorityVersion(spec: CustomResourceDefinitionSpec): String? {
	val versions = spec.versions.map { it.name }
	val version = KubernetesVersionPriority.highestPriority(versions)
	if (version == null) {
		logger<CustomResourceDefinitionSpec>().warn(
			"Could not find version with highest priority in ${spec.group}/${spec.names.kind}.")
	}
	return version
}

@adietish
Copy link
Author

adietish commented Feb 8, 2022

using swagger.json:

  • name should be deducible via <kind>.<group>
  • group, kind & version can be taken from "x-kubernetes-group-version-kind"
  • scope is missing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment