Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Commit

Permalink
Rework makefile to support k8s apimachinery. Add Ignite meta package
Browse files Browse the repository at this point in the history
  • Loading branch information
luxas committed Jul 2, 2019
1 parent 8557f9e commit 09d51ab
Show file tree
Hide file tree
Showing 5 changed files with 268 additions and 4 deletions.
47 changes: 43 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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/$*
2 changes: 2 additions & 0 deletions pkg/apis/meta/v1alpha1/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// +k8s:deepcopy-gen=package
package v1alpha1
154 changes: 154 additions & 0 deletions pkg/apis/meta/v1alpha1/meta.go
Original file line number Diff line number Diff line change
@@ -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"
}
53 changes: 53 additions & 0 deletions pkg/apis/meta/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions pkg/apis/meta/v1alpha1/zz_generated.defaults.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 09d51ab

Please # to comment.