Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert project to kubebuilder version 2 #994

Merged
merged 6 commits into from
Aug 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@ kubeconfig
# Ignore output manifests
examples/out
examples/provider-components-base.yaml
config/samples
72 changes: 72 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
load("@bazel_gazelle//:def.bzl", "gazelle")
load("//build:run_in_workspace_with_goroot.bzl", "workspace_binary")

Expand Down Expand Up @@ -80,3 +81,74 @@ filegroup(
exports_files(glob([
"WORKSPACE",
]))

go_library(
name = "go_default_library",
srcs = ["main.go"],
importpath = "sigs.k8s.io/cluster-api-provider-aws",
deps = [
"//api/v1alpha2:go_default_library",
"//controllers:go_default_library",
"//pkg/record:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//vendor/k8s.io/client-go/kubernetes/scheme:go_default_library",
"//vendor/k8s.io/klog:go_default_library",
"//vendor/k8s.io/klog/klogr:go_default_library",
"//vendor/sigs.k8s.io/controller-runtime:go_default_library",
],
)

go_binary(
name = "cluster-api-provider-aws",
embed = [":go_default_library"],
)

# Build the manager
load("@io_bazel_rules_docker//go:image.bzl", "go_image")
load("@io_bazel_rules_docker//contrib:push-all.bzl", "docker_push")
load("@io_bazel_rules_docker//container:container.bzl", "container_bundle")
load("//build:stateful_set_patch.bzl", "stateful_set_patch")

go_image(
name = "manager-amd64",
embed = [":go_default_library"],
goarch = "amd64",
goos = "linux",
pure = "on",
visibility = ["//visibility:public"],
)

tags = [
"{GIT_VERSION}",
"$(MANAGER_IMAGE_TAG)",
]

images = {
detiber marked this conversation as resolved.
Show resolved Hide resolved
"{registry}/{name}:{tag}".format(
registry = "$(REGISTRY)",
name = "$(MANAGER_IMAGE_NAME)",
tag = tag,
): ":manager-amd64"
for tag in tags
}

container_bundle(
name = "manager-image",
images = images,
tags = ["manual"],
visibility = ["//visibility:public"],
)

docker_push(
name = "manager-push",
bundle = "manager-image",
tags = ["manual"],
)

stateful_set_patch(
chuckha marked this conversation as resolved.
Show resolved Hide resolved
name = "manager-version-patch",
pull_policy = "$(PULL_POLICY)",
registry = "$(REGISTRY)",
tags = ["generated"],
visibility = ["//visibility:public"],
)
7 changes: 5 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,23 @@
# limitations under the License.

# Build the manager binary
FROM golang:1.12.6 as builder
FROM golang:1.12.7 as builder

# Copy in the go src
WORKDIR ${GOPATH}/src/sigs.k8s.io/cluster-api-provider-aws
COPY pkg/ pkg/
COPY cmd/ cmd/
COPY vendor/ vendor/
COPY api/ api/
COPY controllers/ controllers/
COPY main.go main.go
COPY go.mod go.mod
COPY go.sum go.sum

# Build
RUN CGO_ENABLED=0 GOOS=linux GO111MODULE=on GOFLAGS="-mod=vendor" \
go build -a -ldflags '-extldflags "-static"' \
-o manager sigs.k8s.io/cluster-api-provider-aws/cmd/manager
-o manager sigs.k8s.io/cluster-api-provider-aws

# Copy the controller-manager into a thin image
FROM gcr.io/distroless/static:latest
Expand Down
45 changes: 27 additions & 18 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ ifndef BAZEL_VERSION
https://docs.bazel.build/versions/master/install.html")
endif

# Set B64_CREDENTIALS to set an actual value in a secret
B64_CREDENTIALS ?= CREDENTIALS

# Allow overriding manifest generation destination directory
MANIFEST_ROOT ?= config
CRD_ROOT ?= $(MANIFEST_ROOT)/crd/bases
WEBHOOK_ROOT ?= $(MANIFEST_ROOT)/webhook
RBAC_ROOT ?= $(MANIFEST_ROOT)/rbac

.PHONY: all
all: verify-install test binaries

Expand All @@ -65,7 +74,7 @@ test: generate lint ## Run tests

.PHONY: test-go
test-go: ## Run tests
go test -v -tags=integration ./pkg/... ./cmd/...
go test -v -tags=integration ./pkg/... ./cmd/... ./api/... ./controllers/...

.PHONY: integration
integration: generate verify ## Run integraion tests
Expand All @@ -87,24 +96,14 @@ e2e-janitor:
.PHONY: docker-build
docker-build: generate lint-full ## Build the docker image for controller-manager
docker build --pull . -t $(MANAGER_IMAGE)
# TODO: sed probably needs to be gnu sed not bsd sed
sed -i '' -e 's@image: .*@image: '"${MANAGER_IMAGE}"'@' ./config/default/manager_image_patch.yaml
sed -i '' -e 's@credentials: .*@credentials: '"${B64_CREDENTIALS}"'@' ./config/default/aws_credentials_patch.yaml
chuckha marked this conversation as resolved.
Show resolved Hide resolved

.PHONY: docker-push
docker-push: docker-build ## Push the docker image
docker push $(MANAGER_IMAGE)

## --------------------------------------
## Manifests
## --------------------------------------

.PHONY: manifests
manifests: examples/provider-components-base.yaml ## Build example set of manifests from the current source
./examples/generate-yaml.sh

.PHONY: examples/provider-components-base.yaml
examples/provider-components-base.yaml:
bazel build //examples:provider-components-base $(BAZEL_BUILD_ARGS)
install bazel-genfiles/examples/provider-components-base.yaml examples

## --------------------------------------
## Generate
## --------------------------------------
Expand All @@ -123,6 +122,7 @@ generate: ## Generate code
$(MAKE) generate-go
$(MAKE) generate-mocks
$(MAKE) generate-manifests
$(MAKE) generate-kubebuilder-code
$(MAKE) gazelle

.PHONY: generate-go
Expand All @@ -138,13 +138,22 @@ generate-mocks: clean-bazel-mocks ## Generate mocks, CRDs and runs `go generate`
.PHONY: generate-manifests
generate-manifests: ## Generate manifests e.g. CRD, RBAC etc.
go run vendor/sigs.k8s.io/controller-tools/cmd/controller-gen/main.go \
paths=./pkg/apis/infrastructure/... \
paths=./api/... \
crd:trivialVersions=true \
output:crd:dir=./config/crds
output:crd:dir=$(CRD_ROOT) \
output:webhook:dir=$(WEBHOOK_ROOT) \
webhook
go run vendor/sigs.k8s.io/controller-tools/cmd/controller-gen/main.go \
paths=./pkg/controller/... \
paths=./controllers/... \
output:rbac:dir=$(RBAC_ROOT) \
rbac:roleName=manager-role

.PHONY: generate-kubebuilder-code
generate-kubebuilder-code: ## Runs controller-gen
go run vendor/sigs.k8s.io/controller-tools/cmd/controller-gen/main.go \
paths=./api/... \
object:headerFile=./hack/boilerplate/boilerplate.generatego.txt

## --------------------------------------
## Linting
## --------------------------------------
Expand All @@ -165,7 +174,7 @@ binaries: manager clusterawsadm clusterctl ## Builds and installs all binaries

.PHONY: manager
manager: ## Build manager binary.
go build -o bin/manager ./cmd/manager
go build -o bin/manager .

.PHONY: clusterctl
clusterctl: ## Build clusterctl binary.
Expand Down
12 changes: 11 additions & 1 deletion PROJECT
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
version: "1"
version: "2"
domain: cluster.x-k8s.io
repo: sigs.k8s.io/cluster-api-provider-aws
resources:
- group: infrastructure
version: v1alpha2
kind: AWSMachine
- group: infrastructure
version: v1alpha2
kind: AWSCluster
- group: infrastructure
version: v1alpha2
kind: AWSMachineTemplate
25 changes: 25 additions & 0 deletions api/v1alpha2/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = [
"awscluster_types.go",
"awsmachine_types.go",
"awsmachinetemplate_types.go",
"groupversion_info.go",
"tags.go",
"types.go",
"zz_generated.deepcopy.go",
],
importpath = "sigs.k8s.io/cluster-api-provider-aws/api/v1alpha2",
visibility = ["//visibility:public"],
deps = [
"//vendor/k8s.io/api/core/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
"//vendor/sigs.k8s.io/cluster-api/api/v1alpha2:go_default_library",
"//vendor/sigs.k8s.io/cluster-api/pkg/errors:go_default_library",
"//vendor/sigs.k8s.io/controller-runtime/pkg/scheme:go_default_library",
],
)
76 changes: 76 additions & 0 deletions api/v1alpha2/awscluster_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
Copyright 2019 The Kubernetes Authors.

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 v1alpha2

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

const (
// ClusterFinalizer allows ReconcileAWSCluster to clean up AWS resources associated with AWSCluster before
// removing it from the apiserver.
ClusterFinalizer = "awscluster.infrastructure.cluster.x-k8s.io"
)

// AWSClusterSpec defines the desired state of AWSCluster
type AWSClusterSpec struct {
// NetworkSpec encapsulates all things related to AWS network.
NetworkSpec NetworkSpec `json:"networkSpec,omitempty"`

// The AWS Region the cluster lives in.
Region string `json:"region,omitempty"`

// SSHKeyName is the name of the ssh key to attach to the bastion host.
SSHKeyName string `json:"sshKeyName,omitempty"`
}

// AWSClusterStatus defines the observed state of AWSCluster
type AWSClusterStatus struct {
Network Network `json:"network,omitempty"`
Bastion Instance `json:"bastion,omitempty"`
Ready bool `json:"ready"`
// APIEndpoints represents the endpoints to communicate with the control plane.
// +optional
APIEndpoints []APIEndpoint `json:"apiEndpoints,omitempty"`
}

// +kubebuilder:object:root=true
// +kubebuilder:resource:path=awsclusters,scope=Namespaced
// +kubebuilder:storageversion
// +kubebuilder:subresource:status

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

Spec AWSClusterSpec `json:"spec,omitempty"`
Status AWSClusterStatus `json:"status,omitempty"`
}

// +kubebuilder:object:root=true

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

func init() {
SchemeBuilder.Register(&AWSCluster{}, &AWSClusterList{})
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,12 @@ type AWSMachineStatus struct {
ErrorMessage *string `json:"errorMessage,omitempty"`
}

// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +kubebuilder:object:root=true
// +kubebuilder:resource:path=awsmachines,scope=Namespaced
// +kubebuilder:storageversion
// +kubebuilder:subresource:status

// AWSMachine is the Schema for the awsmachines API
// +k8s:openapi-gen=true
type AWSMachine struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Expand All @@ -152,7 +152,7 @@ type AWSMachine struct {
Status AWSMachineStatus `json:"status,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +kubebuilder:object:root=true

// AWSMachineList contains a list of AWSMachine
type AWSMachineList struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,26 @@ package v1alpha2

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha2"
)

// AWSMachineTemplateSpec defines the desired state of AWSMachineTemplate
type AWSMachineTemplateSpec struct {
Template AWSMachineTemplateResource `json:"template"`
}

// AWSMachineTemplateResource describes the data needed to create am AWSMachine from a template
type AWSMachineTemplateResource struct {
metav1.TypeMeta `json:",inline"`

// Standard object's metadata.
clusterv1.ObjectMeta `json:"metadata,omitempty"`

// Spec is the specification of the desired behavior of the machine.
Spec AWSMachineSpec `json:"spec"`
}

// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +kubebuilder:object:root=true
// +kubebuilder:resource:path=awsmachinetemplates,scope=Namespaced
// +kubebuilder:storageversion

// AWSMachineTemplate is the Schema for the awsmachinetemplates API
// +k8s:openapi-gen=true
type AWSMachineTemplate struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec AWSMachineTemplateSpec `json:"spec,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +kubebuilder:object:root=true

// AWSMachineTemplateList contains a list of AWSMachineTemplate
type AWSMachineTemplateList struct {
Expand Down
Loading