Skip to content
This repository has been archived by the owner on Nov 19, 2020. It is now read-only.

*: refactor generation and add type registration #73

Merged
merged 4 commits into from
Jan 19, 2018
Merged

Conversation

ericchiang
Copy link
Owner

@ericchiang ericchiang commented Jan 15, 2018

This PR is a large refactor of the library to add 1.9 support.

This is a large change because all types are now registered instead of being generated at the root of the repo. There are no more generated clients, just a common one that types are registered onto.

Changing the registration means:

  • You only compile API groups you import.
  • Users can register their own types the same way the core groups are registered.
  • No more "generated generics" style client.CoreV1().CreateFoo just client.Create.

closes #70
closes #52
closes #63
closes #62
closes #61
closes #46
closes #66

Concretely creating a configmap went from:

cm := &v1.ConfigMap{
    Metadata: &metav1.ObjectMeta{
        Name:      &name,
        Namespace: &client.Namespace,
    },
    Data: values,
}
// Will return the created configmap as well.
_, err := client.CoreV1().CreateConfigMap(context.TODO(), cm)

To:

cm := &v1.ConfigMap{
    Metadata: &metav1.ObjectMeta{
        Name:      &name,
        Namespace: &client.Namespace,
    },
    Data: values,
}
err := client.Create(context.TODO(), cm) // Just Create, no typed clients

Get, List, and Watch saw the biggest changes, with responses being passed as arguments instead of being return values. For example, a Get now requires passing the type you want to decode into.

// Get the configmap "cluster-info" in the namespace "kube-public"
cm := new(v1.ConfigMap)
err := client.Get(context.TODO(), "kube-public", "cluster-info", cm)

Users can also register their own types using the Register and RegisterList methods.

import (
    "github.com/ericchiang/k8s"
    metav1 "github.com/ericchiang/k8s/apis/meta/v1"
)

type MyResource struct {
    Metadata *metav1.ObjectMeta `json:"metadata"`
    Foo      string             `json:"foo"`
    Bar      int                `json:"bar"`
}

// Required for MyResource to implement k8s.Resource
func (m *MyResource) GetMetadata() *metav1.ObjectMeta {
    return m.Metadata
}

func init() {
    k8s.Register("resource.example.com", "v1", "myresources", true, &MyResource{})
}

Then Create, Update, Delete, and Watch the custom type the same as other resources.

TODOs before merge:

  • Tests for non-namespaced resources.
  • Tests for label selectors.

@ericchiang ericchiang force-pushed the rewrite branch 3 times, most recently from a8293ee to 2edf7d1 Compare January 16, 2018 01:03
@ericchiang ericchiang merged commit 72a6e54 into master Jan 19, 2018
@ericchiang ericchiang deleted the rewrite branch January 19, 2018 04:58
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
1 participant