Skip to content

Commit

Permalink
Use kubebuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
hangyan committed Dec 22, 2019
1 parent 49fff56 commit c4093b3
Show file tree
Hide file tree
Showing 3,715 changed files with 2,891 additions and 1,013,957 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
.helm/
.kube/
artifacts/coverage.out
cover.out
bin/manager
36 changes: 25 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
FROM golang:1.12.9
# Build the manager binary
FROM golang:1.13 as builder

COPY . $GOPATH/src/github.com/alauda/captain
WORKDIR $GOPATH/src/github.com/alauda/captain
RUN make build
WORKDIR /workspace
# Copy the Go Modules manifests
COPY go.mod go.mod
COPY go.sum go.sum
# cache deps before building and copying source so that we don't need to re-download as much
# and so that source changes don't invalidate our downloaded layer
ENV GOPROXY https://goproxy.cn/
RUN go mod download

FROM index.alauda.cn/alaudaorg/alaudabase-alpine-run:alpine3.10
# Copy the go source
COPY main.go main.go

WORKDIR /captain
#COPY api/ api/
COPY controllers/ controllers/
COPY pkg/ pkg/

COPY --from=0 /go/src/github.com/alauda/captain/captain /captain/
COPY artifacts/helm/repositories.yaml /root/.config/helm/
RUN chmod a+x /captain/captain
# Build
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o manager main.go

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
# FROM gcr.azk8s.cn/distroless/static:latest
FROM alpine:3.11
WORKDIR /
COPY --from=builder /workspace/manager .
# USER nonroot:nonroot

# ENTRYPOINT ["/captain/run.sh"]
CMD ["/captain/captain"]
ENTRYPOINT ["/manager"]
109 changes: 86 additions & 23 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,34 +1,97 @@
VERSION := $(shell git describe --always --long --dirty)
mod:
GO111MODULE=on go mod tidy
GO111MODULE=on go mod vendor

build:
GO111MODULE=on CGO_ENABLED=0 go build -mod vendor -ldflags "-w -s -X main.version=${VERSION}" -v -o captain
# Image URL to use all building/pushing image targets
IMG ?= captain:latest
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
CRD_OPTIONS ?= "crd:trivialVersions=true"

# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
GOBIN=$(shell go env GOPATH)/bin
else
GOBIN=$(shell go env GOBIN)
endif

fmt:
find ./pkg -name \*.go | xargs goimports -w
goimports -w main.go
all: manager

lint:
golangci-lint run -c hack/ci/golangci.yml
revive -exclude pkg/apis/... -exclude pkg/client/... -config hack/ci/revive.toml -formatter friendly ./pkg/...

test:
go test -v -cover -coverprofile=artifacts/coverage.out ./pkg/...
# Run tests
test: generate fmt vet manifests
go test ./... -coverprofile cover.out

int:
int-test:
bash tests.sh

image:
docker build -t captain .
# Build manager binary
manager: generate fmt vet
go build -o bin/manager main.go

# Run against the configured Kubernetes cluster in ~/.kube/config
run: generate fmt vet manifests
go run ./main.go

# Install CRDs into a cluster
install: manifests
kustomize build config/crd | kubectl apply -f -

# Uninstall CRDs from a cluster
uninstall: manifests
kustomize build config/crd | kubectl delete -f -

uninstall-all:
bash hack/uninstall.sh


# Deploy controller in the configured Kubernetes cluster in ~/.kube/config
deploy: manifests
cd config/manager && kustomize edit set image controller=${IMG}
kustomize build config/default | kubectl apply -f -


restart:
kubectl scale deploy captain-controller-manager -n captain-system --replicas=0
kubectl scale deploy captain-controller-manager -n captain-system --replicas=1


# Generate manifests e.g. CRD, RBAC etc.
manifests: controller-gen
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases

# Run go fmt against code
fmt:
go fmt ./...

# Run go vet against code
vet:
go vet ./...




# Generate code
generate: controller-gen
$(CONTROLLER_GEN) object:headerFile=./hack/boilerplate.go.txt paths="./..."

push:
docker tag captain alaudapublic/captain:latest
docker push alaudapublic/captain:latest
# Build the docker image
docker-build: test
docker build . -t ${IMG}

code-gen:
${GOPATH}/src/k8s.io/code-generator/generate-groups.sh all "github.com/alauda/captain/pkg/client" "github.com/alauda/captain/pkg/apis" app:v1alpha1
# Push the docker image
docker-push:
docker push ${IMG}

check: fmt build lint test
# find or download controller-gen
# download controller-gen if necessary
controller-gen:
ifeq (, $(shell which controller-gen))
@{ \
set -e ;\
CONTROLLER_GEN_TMP_DIR=$$(mktemp -d) ;\
cd $$CONTROLLER_GEN_TMP_DIR ;\
go mod init tmp ;\
go get sigs.k8s.io/controller-tools/cmd/controller-gen@v0.2.4 ;\
rm -rf $$CONTROLLER_GEN_TMP_DIR ;\
}
CONTROLLER_GEN=$(GOBIN)/controller-gen
else
CONTROLLER_GEN=$(shell which controller-gen)
endif
3 changes: 3 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
domain: alauda.io
repo: github.com/alauda/captain
version: "2"
35 changes: 35 additions & 0 deletions api/v1alpha1/groupversion_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package v1alpha1 contains API Schema definitions for the alaudaio v1alpha1 API group
// +kubebuilder:object:generate=true
// +groupName=alauda.io.alauda.io
package v1alpha1

import (
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/scheme"
)

var (
// GroupVersion is group version used to register these objects
GroupVersion = schema.GroupVersion{Group: "alauda.io.alauda.io", Version: "v1alpha1"}

// SchemeBuilder is used to add go types to the GroupVersionKind scheme
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}

// AddToScheme adds the types in this group-version to the given scheme.
AddToScheme = SchemeBuilder.AddToScheme
)
62 changes: 62 additions & 0 deletions api/v1alpha1/release_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

// ReleaseSpec defines the desired state of Release
type ReleaseSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file

// Foo is an example field of Release. Edit Release_types.go to remove/update
Foo string `json:"foo,omitempty"`
}

// ReleaseStatus defines the observed state of Release
type ReleaseStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
}

// +kubebuilder:object:root=true

// Release is the Schema for the releases API
type Release struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec ReleaseSpec `json:"spec,omitempty"`
Status ReleaseStatus `json:"status,omitempty"`
}

// +kubebuilder:object:root=true

// ReleaseList contains a list of Release
type ReleaseList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []Release `json:"items"`
}

func init() {
SchemeBuilder.Register(&Release{}, &ReleaseList{})
}
113 changes: 113 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

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

2 changes: 1 addition & 1 deletion charts/captain/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: captain
appVersion: "2.0"
version: v0.9.2
version: v0.9.3
description: A Helm3 Controller
Loading

0 comments on commit c4093b3

Please sign in to comment.