From 09d51abd409ee361e93884baae24ffc92cde63a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20K=C3=A4ldstr=C3=B6m?= Date: Tue, 2 Jul 2019 17:27:04 +0300 Subject: [PATCH] Rework makefile to support k8s apimachinery. Add Ignite meta package --- Makefile | 47 +++++- pkg/apis/meta/v1alpha1/doc.go | 2 + pkg/apis/meta/v1alpha1/meta.go | 154 ++++++++++++++++++ .../meta/v1alpha1/zz_generated.deepcopy.go | 53 ++++++ .../meta/v1alpha1/zz_generated.defaults.go | 16 ++ 5 files changed, 268 insertions(+), 4 deletions(-) create mode 100644 pkg/apis/meta/v1alpha1/doc.go create mode 100644 pkg/apis/meta/v1alpha1/meta.go create mode 100644 pkg/apis/meta/v1alpha1/zz_generated.deepcopy.go create mode 100644 pkg/apis/meta/v1alpha1/zz_generated.defaults.go diff --git a/Makefile b/Makefile index de751e884..fbf51efec 100644 --- a/Makefile +++ b/Makefile @@ -7,13 +7,14 @@ GIT_VERSION:=$(shell hack/ldflags.sh --version-only) # IS_DIRTY is 1 if the tree state is dirty, otherwise 0 IS_DIRTY:=$(shell echo ${GIT_VERSION} | grep -o dirty | wc -l) WHAT?=ignite - +PROJECT = github.com/weaveworks/ignite +APIS_DIR = ${PROJECT}/pkg/apis +API_DIRS = ${APIS_DIR}/ignite/v1alpha1,${APIS_DIR}/meta/v1alpha1 +CACHE_DIR = /tmp/go-cache all: binary binary: - docker run -it --rm -v $(shell pwd):/build -w /build golang:${GO_VERSION} sh -c "\ - make ${WHAT} && \ - chown ${UID_GID} bin/${WHAT}" + $(MAKE) shell COMMAND="make bin/${WHAT}" install: binary sudo cp bin/ignite /usr/local/bin @@ -44,3 +45,41 @@ tidy: gofmt -s -w pkg cmd goimports -w pkg cmd go run hack/cobra.go + +shell: + mkdir -p $(CACHE_DIR)/bin $(CACHE_DIR)/src $(CACHE_DIR)/cache bin/cache + docker run -it \ + -v $(CACHE_DIR)/bin:/go/bin \ + -v $(CACHE_DIR)/src:/go/src \ + -v $(CACHE_DIR)/cache:/.cache/go-build \ + -v $(shell pwd):/go/src/github.com/weaveworks/ignite \ + -w /go/src/github.com/weaveworks/ignite \ + -u $(shell id -u):$(shell id -g) \ + -e GO111MODULE=on \ + golang:$(GO_VERSION) \ + $(COMMAND) + +autogen: + $(MAKE) shell COMMAND="make dockerized-autogen" + +dockerized-autogen: /go/bin/deepcopy-gen /go/bin/defaulter-gen /go/bin/conversion-gen + # Let the boilerplate be empty + touch /tmp/boilerplate + /go/bin/deepcopy-gen \ + --input-dirs ${API_DIRS} \ + --bounding-dirs ${APIS_DIR} \ + -O zz_generated.deepcopy \ + -h /tmp/boilerplate + + /go/bin/defaulter-gen \ + --input-dirs ${API_DIRS} \ + -O zz_generated.defaults \ + -h /tmp/boilerplate + + /go/bin/conversion-gen \ + --input-dirs ${API_DIRS} \ + -O zz_generated.conversion \ + -h /tmp/boilerplate + +/go/bin/%: vendor + go install k8s.io/code-generator/cmd/$* diff --git a/pkg/apis/meta/v1alpha1/doc.go b/pkg/apis/meta/v1alpha1/doc.go new file mode 100644 index 000000000..30f47fcdd --- /dev/null +++ b/pkg/apis/meta/v1alpha1/doc.go @@ -0,0 +1,2 @@ +// +k8s:deepcopy-gen=package +package v1alpha1 diff --git a/pkg/apis/meta/v1alpha1/meta.go b/pkg/apis/meta/v1alpha1/meta.go new file mode 100644 index 000000000..716501f3b --- /dev/null +++ b/pkg/apis/meta/v1alpha1/meta.go @@ -0,0 +1,154 @@ +package v1alpha1 + +import ( + "encoding/json" + "fmt" + + "github.com/c2h5oh/datasize" + + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/types" +) + +const ( + SectorSize = 512 +) + +type ObjectMeta struct { + Name string `json:"name"` + UID types.UID `json:"uid"` +} + +func (o *ObjectMeta) GetName() string { + return o.Name +} + +func (o *ObjectMeta) GetUID() types.UID { + return o.UID +} + +// All types implementing Object conform to this +// interface, it's mainly used for filtering +type Object interface { + runtime.Object + GetName() string + GetUID() types.UID +} + +// Size specifies a common unit for data sizes +type Size struct { + datasize.ByteSize +} + +var EmptySize = NewSizeFromBytes(0) + +var _ json.Marshaler = &Size{} +var _ json.Unmarshaler = &Size{} + +func NewSizeFromBytes(bytes uint64) Size { + return Size{ + datasize.ByteSize(bytes), + } +} + +func NewSizeFromSectors(sectors uint64) Size { + return Size{ + datasize.ByteSize(sectors * SectorSize), + } +} + +func (s *Size) Sectors() uint64 { + return s.Bytes() / SectorSize +} + +// Override ByteSize's default string implementation which results in something similar to HR() +func (s *Size) String() string { + b, _ := s.MarshalText() + return string(b) +} + +// Int64 returns the byte size as int64 +func (s *Size) Int64() int64 { + return int64(s.Bytes()) +} + +// Add returns a copy, does not modify the receiver +func (s Size) Add(other Size) Size { + s.ByteSize += other.ByteSize + return s +} + +func (s Size) Min(other Size) Size { + if other.ByteSize < s.ByteSize { + return other + } + + return s +} + +func (s Size) Max(other Size) Size { + if other.ByteSize > s.ByteSize { + return other + } + + return s +} + +func (s *Size) MarshalJSON() ([]byte, error) { + return json.Marshal(s.Bytes()) +} + +func (s *Size) UnmarshalJSON(b []byte) error { + var i uint64 + if err := json.Unmarshal(b, &i); err != nil { + return err + } + + *s = NewSizeFromBytes(i) + return nil +} + +// DMID specifies the format for device mapper IDs +type DMID struct { + index int32 +} + +var _ fmt.Stringer = DMID{} + +func NewDMID(i int) DMID { + // device mapper IDs are unsigned 24-bit integers + if i < 0 || i >= 1<<24 { + panic(fmt.Sprintf("device mapper ID out of range: %d", i)) + } + + return DMID{ + index: int32(i), + } +} + +func NewPoolDMID() DMID { + // Internally we keep the pool ID out of range + return DMID{ + index: -1, + } +} + +func (d *DMID) Pool() bool { + return d.index < 0 +} + +func (d *DMID) Index() int { + if !d.Pool() { + return int(d.index) + } + + panic("attempt to index nonexistent ID") +} + +func (d DMID) String() string { + if !d.Pool() { + return fmt.Sprintf("%d", d.index) + } + + return "pool" +} diff --git a/pkg/apis/meta/v1alpha1/zz_generated.deepcopy.go b/pkg/apis/meta/v1alpha1/zz_generated.deepcopy.go new file mode 100644 index 000000000..5752980d3 --- /dev/null +++ b/pkg/apis/meta/v1alpha1/zz_generated.deepcopy.go @@ -0,0 +1,53 @@ +// +build !ignore_autogenerated + +// Code generated by deepcopy-gen. DO NOT EDIT. + +package v1alpha1 + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *DMID) DeepCopyInto(out *DMID) { + *out = *in + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new DMID. +func (in *DMID) DeepCopy() *DMID { + if in == nil { + return nil + } + out := new(DMID) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ObjectMeta) DeepCopyInto(out *ObjectMeta) { + *out = *in + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ObjectMeta. +func (in *ObjectMeta) DeepCopy() *ObjectMeta { + if in == nil { + return nil + } + out := new(ObjectMeta) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *Size) DeepCopyInto(out *Size) { + *out = *in + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Size. +func (in *Size) DeepCopy() *Size { + if in == nil { + return nil + } + out := new(Size) + in.DeepCopyInto(out) + return out +} diff --git a/pkg/apis/meta/v1alpha1/zz_generated.defaults.go b/pkg/apis/meta/v1alpha1/zz_generated.defaults.go new file mode 100644 index 000000000..7985166a6 --- /dev/null +++ b/pkg/apis/meta/v1alpha1/zz_generated.defaults.go @@ -0,0 +1,16 @@ +// +build !ignore_autogenerated + +// Code generated by defaulter-gen. DO NOT EDIT. + +package v1alpha1 + +import ( + runtime "k8s.io/apimachinery/pkg/runtime" +) + +// RegisterDefaults adds defaulters functions to the given scheme. +// Public to allow building arbitrary schemes. +// All generated defaulters are covering - they call all nested defaulters. +func RegisterDefaults(scheme *runtime.Scheme) error { + return nil +}