Skip to content

Commit

Permalink
Migrate project to Operator SDK v1.2.0 (#10)
Browse files Browse the repository at this point in the history
* Migrate project to Operator SDK v1.2.0

Migrate project to Operator SDK v1.2.0 following the doc:
https://sdk.operatorframework.io/docs/building-operators/golang/migration/

Two new make targets are added to help generate bundle and package manifests:
- Bundle format
  make bundle
- Package manifests format
  make make packagemanifests

Fixes: #8
Signed-off-by: rcao <[email protected]>
  • Loading branch information
ruicao93 authored Dec 15, 2020
1 parent caac0e6 commit 7296c32
Show file tree
Hide file tree
Showing 82 changed files with 2,817 additions and 610 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: Build antrea-operator Docker image
run: make
run: make docker-build
6 changes: 2 additions & 4 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,5 @@ jobs:
uses: actions/checkout@v2
- name: Build antrea-operator binary
run: make bin
- name: Run unit tests
run: make test-unit
- name: Run golangci-lint
run: make golangci
- name: Run tests
run: make test
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ build/_output/bin
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
# Code coverage report
cover.out
# Test bin directory
testbin
### Vim ###
# swap
.sw[a-p]
Expand Down
160 changes: 133 additions & 27 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,31 +1,42 @@
# go options
GO ?= go
LDFLAGS :=
GOFLAGS :=
BINDIR ?= $(CURDIR)/build/_output/bin
GO_FILES := $(shell find . -type d -name '.cache' -prune -o -type f -name '*.go' -print)
GOPATH ?= $$($(GO) env GOPATH)
SHELL := /bin/bash
# Current Operator version
VERSION ?= latest
# Default bundle image tag
BUNDLE_IMG ?= antrea-operator-bundle:$(VERSION)
# Options for 'bundle-build'
ifneq ($(origin CHANNELS), undefined)
BUNDLE_CHANNELS := --channels=$(CHANNELS)
endif
ifneq ($(origin DEFAULT_CHANNEL), undefined)
BUNDLE_DEFAULT_CHANNEL := --default-channel=$(DEFAULT_CHANNEL)
endif
BUNDLE_METADATA_OPTS ?= $(BUNDLE_CHANNELS) $(BUNDLE_DEFAULT_CHANNEL)

.PHONY: all
all: build
# Image URL to use all building/pushing image targets
IMG ?= antrea-operator:latest
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
CRD_OPTIONS ?= "crd:trivialVersions=true"

LDFLAGS += $(VERSION_LDFLAGS)
OPERATOR_NAME = antrea-operator
# 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

.PHONY: build
build:
@echo "===> Building antrea-operator Docker image <==="
docker build -f build/Dockerfile . -t $(OPERATOR_NAME)
# Options for "packagemanifests".
ifneq ($(origin FROM_VERSION), undefined)
PKG_FROM_VERSION := --from-version=$(FROM_VERSION)
endif
ifneq ($(origin CHANNEL), undefined)
PKG_CHANNELS := --channel=$(CHANNEL)
endif
ifeq ($(IS_CHANNEL_DEFAULT), 1)
PKG_IS_DEFAULT_CHANNEL := --default-channel
endif
PKG_MAN_OPTS ?= $(FROM_VERSION) $(PKG_CHANNELS) $(PKG_IS_DEFAULT_CHANNEL)

.PHONY: bin
bin:
@echo "===> Building antrea-operator binary <==="
GOOS=linux $(GO) build -o $(BINDIR)/$(OPERATOR_NAME) $(GOFLAGS) -ldflags '$(LDFLAGS)' ./cmd/manager

.PHONY: test-unit
test-unit:
@echo "===> Running unit tests <==="
GOOS=linux $(GO) test -race -cover github.com/vmware/antrea-operator-for-kubernetes/pkg...
all: manager

.golangci-bin:
@echo "===> Installing Golangci-lint <==="
Expand All @@ -35,6 +46,101 @@ test-unit:
golangci: .golangci-bin
@GOOS=linux CGO_ENABLED=1 .golangci-bin/golangci-lint run -c .golangci.yml

.PHONY: clean
clean:
rm -f $(BINDIR)/$(OPERATOR_NAME)
# Run tests
ENVTEST_ASSETS_DIR = $(shell pwd)/testbin
test: generate golangci manifests
mkdir -p $(ENVTEST_ASSETS_DIR)
test -f $(ENVTEST_ASSETS_DIR)/setup-envtest.sh || curl -sSLo $(ENVTEST_ASSETS_DIR)/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/v0.6.3/hack/setup-envtest.sh
source $(ENVTEST_ASSETS_DIR)/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./... -coverprofile cover.out

# Build manager binary
manager: generate golangci
@echo "===> Building antrea-operator binary <==="
go build -o bin/manager main.go

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

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

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

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

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

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

# Build the docker image
docker-build: test
docker build -f build/Dockerfile . -t ${IMG}

# Push the docker image
docker-push:
docker push ${IMG}

# 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/[email protected] ;\
rm -rf $$CONTROLLER_GEN_TMP_DIR ;\
}
CONTROLLER_GEN=$(GOBIN)/controller-gen
else
CONTROLLER_GEN=$(shell which controller-gen)
endif

kustomize:
ifeq (, $(shell which kustomize))
@{ \
set -e ;\
KUSTOMIZE_GEN_TMP_DIR=$$(mktemp -d) ;\
cd $$KUSTOMIZE_GEN_TMP_DIR ;\
go mod init tmp ;\
go get sigs.k8s.io/kustomize/kustomize/[email protected] ;\
rm -rf $$KUSTOMIZE_GEN_TMP_DIR ;\
}
KUSTOMIZE=$(GOBIN)/kustomize
else
KUSTOMIZE=$(shell which kustomize)
endif

# Generate bundle manifests and metadata, then validate generated files.
.PHONY: bundle
bundle: manifests kustomize
operator-sdk generate kustomize manifests -q
cd config/manager && $(KUSTOMIZE) edit set image antrea-operator=$(IMG)
$(KUSTOMIZE) build config/manifests | operator-sdk generate bundle -q --overwrite --version $(VERSION) $(BUNDLE_METADATA_OPTS)
operator-sdk bundle validate ./bundle

# Build the bundle image.
.PHONY: bundle-build
bundle-build:
docker build -f bundle.Dockerfile -t $(BUNDLE_IMG) .

# Generate package manifests.
packagemanifests: kustomize manifests
operator-sdk generate kustomize manifests -q
cd config/manager && $(KUSTOMIZE) edit set image antrea-operator=$(IMG)
$(KUSTOMIZE) build config/manifests | operator-sdk generate packagemanifests -q --version $(VERSION) $(PKG_MAN_OPTS)

.PHONY: bin
bin: manager
11 changes: 11 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
domain: vmware.com
layout: go.kubebuilder.io/v2
projectName: antrea-operator-for-kubernetes
repo: github.com/vmware/antrea-operator-for-kubernetes
resources:
- group: operator.antrea
kind: AntreaInstall
version: v1
version: 3-alpha
plugins:
go.sdk.operatorframework.io/v2-alpha: {}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,54 @@ 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.

// AntreaInstallSpec defines the desired state of AntreaInstall
type AntreaInstallSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "operator-sdk generate k8s" to regenerate code after modifying this file
// Add custom validation using kubebuilder tags: https://book-v1.book.kubebuilder.io/beyond_basics/generating_crd.html
// Important: Run "make" to regenerate code after modifying this file

// AntreaAgentConfig holds the configurations for antrea-agent.
// +operator-sdk:csv:customresourcedefinitions:type=spec
// +required
AntreaAgentConfig string `json:"antreaAgentConfig"`

// AntreaCNIConfig holds the configuration of CNI.
// +operator-sdk:csv:customresourcedefinitions:type=spec
// +required
AntreaCNIConfig string `json:"antreaCNIConfig"`

// AntreaControllerConfig holds the configurations for antrea-controller.
// +operator-sdk:csv:customresourcedefinitions:type=spec
// +required
AntreaControllerConfig string `json:"antreaControllerConfig"`

// AntreaImage is the Docker image name used by antrea-agent and antrea-controller.
// +operator-sdk:csv:customresourcedefinitions:type=spec
// +optional
AntreaImage string `json:"antreaImage,omitempty"`
}

// AntreaInstallStatus defines the observed state of AntreaInstall
type AntreaInstallStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "operator-sdk generate k8s" to regenerate code after modifying this file
// Add custom validation using kubebuilder tags: https://book-v1.book.kubebuilder.io/beyond_basics/generating_crd.html
// Important: Run "make" to regenerate code after modifying this file

// Conditions describes the state of Antrea installation.
// +operator-sdk:csv:customresourcedefinitions:type=status
// +optional
Conditions []InstallCondition `json:"conditions,omitempty"`
}

// +kubebuilder:object:generate=false
type InstallCondition = configv1.ClusterOperatorStatusCondition

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

// AntreaInstall is the Schema for the antreainstalls API
// +kubebuilder:subresource:status
// +kubebuilder:resource:path=antreainstalls,scope=Namespaced
// +operator-sdk:csv:customresourcedefinitions:resources={{Deployment,v1,"A Kubernetes Deployment for the Operator"},{AntreaInstall,v1,"this operator's CR"},{ClusterOperator,v1,"antrea cluster operator"},{Network,v1,"Openshift's cluster network"}}
type AntreaInstall struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Expand All @@ -57,7 +64,7 @@ type AntreaInstall struct {
Status AntreaInstallStatus `json:"status,omitempty"`
}

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

// AntreaInstallList contains a list of AntreaInstall
type AntreaInstallList struct {
Expand Down
23 changes: 23 additions & 0 deletions api/v1/groupversion_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* Copyright © 2020 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0 */

// Package v1 contains API Schema definitions for the operator v1 API group
// +kubebuilder:object:generate=true
// +groupName=operator.antrea.vmware.com
package v1

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: "operator.antrea.vmware.com", Version: "v1"}

// 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
)

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

24 changes: 16 additions & 8 deletions build/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
FROM golang:1.13 as antrea-operator-build

WORKDIR /antrea-operator

COPY go.mod /antrea-operator/go.mod

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
RUN go mod download

COPY . /antrea-operator
# Copy the go source
COPY main.go main.go
COPY api/ api/
COPY controllers/ controllers/
COPY version/ version/

# Build
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o manager main.go

RUN make bin

FROM registry.access.redhat.com/ubi8/ubi-minimal:latest

Expand All @@ -28,8 +36,8 @@ ENV OPERATOR=/usr/local/bin/antrea-operator \

COPY build/bin /usr/local/bin
# install operator binary
COPY --from=antrea-operator-build /antrea-operator/build/_output/bin/antrea-operator ${OPERATOR}
COPY manifest /manifest
COPY --from=antrea-operator-build /workspace/manager ${OPERATOR}
COPY antrea-manifest /antrea-manifest
RUN /usr/local/bin/user_setup

ENTRYPOINT ["/usr/local/bin/entrypoint"]
Expand Down
Loading

0 comments on commit 7296c32

Please sign in to comment.