Created
September 25, 2018 19:34
-
-
Save grepory/2d156e691a02d3ad1ebc950b9230eb74 to your computer and use it in GitHub Desktop.
Pseudocode for decoding/encoding and api clients.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package v2 | |
import ( | |
somepackage | |
) | |
type Scheme struct { | |
Group string | |
Version string | |
} | |
var ( | |
groupVersion = &GroupVersion{ | |
Group: "checks.sensu.io", | |
Version: "v2" | |
} | |
) | |
func RegisterTypes(GroupVersion, obj interface{}...) | |
type Object interface { | |
func Kind() string | |
} | |
func (c *Check) Default() { | |
c.Kind = "Check" | |
} | |
func NewCheck() Check { | |
c := &Check{} | |
c.Default() | |
return *c | |
} | |
RegisterTypes( | |
groupVersion, | |
NewCheck(), | |
NewEvent(), | |
NewEntity(), | |
) | |
var registry map[Scheme][string]reflect.Type | |
func RegisterTypes(scheme Scheme, objs Object...) { | |
for _, obj := range objs { | |
registry[scheme][obj.Kind()] = reflect.TypeOf(obj) | |
} | |
} | |
func ObjectFor(gvk GroupVersionKind) | |
check := &Check{} | |
sensuctl create --format=yaml -f mycheck.yaml | |
func ParseCheck(contentType, bytes []byte) error {} | |
// # This binds the events-read-only ClusterRole to the events-read-only | |
// # group. | |
// kind: Group | |
// apiVersion: rbac.authorization.sensu.io/v2 | |
//metadata: | |
// name: events-read-only | |
// roleRefs: | |
// kind: ClusterRole | |
// name: events-read-only | |
// apiGroup: rbac.authorization.sensu.io | |
// This is happening only in sensuctl | |
func ParseWhatever(contentType string, bytes []byte) (metav1.GroupVersionKind, err) { | |
// contentType = application/yaml | |
decoder, err := codecs.DecoderFor(contentType) | |
if err != nil { | |
return fmt.Errorf("unrecognized content type: %s", contentType) | |
} | |
gvk := metav1.GroupVersionKind{} | |
if err := decoder.Decode(bytes, &gvk); err != nil { | |
return err | |
} | |
} | |
// this is sensuctl create -f | |
func HandleCreate(contentType, body []byte) { | |
gvk, _ := ParseWhatever(contentType, body) | |
client, _ := ClientFor(gvk) | |
client.Create(context.TODO(), body) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment