OpenAPITools / OpenAPITools/openapi-generator

[REQ] Dedicated go generator library for kubernetes

Open
#3,317 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement: Feature
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

the kubernetes community is enhancing its integration w/ the openapi-specs for CRD. there will be more and more users thinking about generating codes for kubernetes-related development usages while the current generic go generator doesn't generate out-of-box models fits into kubernetes SDK. e.g.

model_foo.go

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
type Foo struct {
    // 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/api-conventions.md#resources
    ApiVersion string `json:"apiVersion,omitempty"`
    // 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/api-conventions.md#types-kinds
    Kind     string                             `json:"kind,omitempty"`
    Metadata v1.ObjectMeta                      `json:"metadata,omitempty"`
    Spec     FooSpec `json:"spec,omitempty"`
}

func (o *Foo) GetObjectKind() schema.ObjectKind {
    return &v1.TypeMeta{o.Kind, o.ApiVersion}
}

func (o *Foo) GetObjectMeta() v1.Object {
    return &o.Metadata
}

register.go:

var (
	SchemeBuilder = runtime.NewSchemeBuilder(
		addKnownTypes,
	)
	AddToScheme = SchemeBuilder.AddToScheme
)

func addKnownTypes(scheme *runtime.Scheme) error {
	metav1.AddToGroupVersion(scheme, schema.GroupVersion{"example.io", "v1alpha1"})
	scheme.AddKnownTypeWithName(schema.GroupVersionKind{"example.io", "v1alpha1", "Foo"},
		&Foo{},
	)
	return nil
}

basically, there're following things we need to add to the code template:

  1. comment tags on the model: // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
  2. extension methods: GetObjectKind, GetObjectMeta
  3. additional imports for kubernetes depedencies
  4. rendering register.go for installing models

/cc @wing328

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the existing generic Go generator and compare its model template with the model_foo.go example in the issue; then trace how register.go would be rendered. Done means a dedicated Kubernetes generator produces the requested model tags, extension methods, dependency imports, and registration file.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, kubernetes
Domain
infrastructure, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.