Skip to content

feat(build): introduces controller-gen scaffolding #121

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

37 changes: 18 additions & 19 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ PROJECT_DIR:=$(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
OUT_DIR:=out

export ISTIO_VERSION ?= 1.23.0
CONTROLLER_TOOLS_VERSION ?= v0.16.4

## Required tooling.
## Needs to be defined early so that any target depending on given binary can resolve it when not present.
Expand All @@ -13,12 +14,18 @@ PROTOC := $(LOCALBIN)/protoc
PROTOC_GEN_GO := $(LOCALBIN)/protoc-gen-go
PROTOC_GEN_GRPC := $(LOCALBIN)/protoc-gen-go-grpc
PROTOC_GEN_DEEPCOPY := $(LOCALBIN)/protoc-gen-golang-deepcopy
CONTROLLER_GEN := $(LOCALBIN)/controller-gen

PROTOBUF_API_DIR := $(PROJECT_DIR)/api/proto/federation
PROTOBUF_API_SRC := $(shell find $(PROTOBUF_API_DIR) -type f -name "*.proto")
API_GEN_DIR=$(PROJECT_DIR)/internal/api
PROTOBUF_GEN := $(shell find $(API_GEN_DIR) -type f -name "*.go")

CRD_SRC_DIR := $(PROJECT_DIR)/api/v1alpha1
CRD_SRC := $(shell find $(CRD_SRC_DIR) -type f -name "*.go")
CRD_GEN_DIR := $(PROJECT_DIR)/chart/crds
CRD_GEN := $(shell find $(CRD_GEN_DIR) -type f -name "*.yaml")

.PHONY: default
default: build add-license fix-imports test

Expand All @@ -36,7 +43,7 @@ deps: ## Downloads required dependencies

EXTRA_BUILD_ARGS?=
.PHONY: build
build: deps $(PROTOBUF_GEN) ## Builds the project
build: deps $(PROTOBUF_GEN) $(DEEP_COPY_GEN) $(CRD_GEN) ## Builds the project
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
build: deps $(PROTOBUF_GEN) $(DEEP_COPY_GEN) $(CRD_GEN) ## Builds the project
build: deps $(PROTOBUF_GEN) $(CRD_GEN) ## Builds the project

go build -C $(PROJECT_DIR)/cmd/federation-controller -o $(PROJECT_DIR)/$(OUT_DIR)/federation-controller $(EXTRA_BUILD_ARGS)

.PHONY: test
Expand Down Expand Up @@ -123,43 +130,35 @@ $(PROTOC_GEN_DEEPCOPY):
$(KIND):
@GOBIN=$(LOCALBIN) go install -mod=readonly sigs.k8s.io/[email protected]

$(CONTROLLER_GEN):
GOBIN=$(LOCALBIN) go install -mod=readonly sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)

.PHONY: clean
clean:
clean:
@rm -rf $(LOCALBIN) $(PROJECT_DIR)/$(OUT_DIR)

##@ Code Gen

$(PROTOBUF_GEN): $(PROTOBUF_API_SRC) $(PROTOC) $(PROTOC_GEN_GO) $(PROTOC_GEN_GRPC) $(PROTOC_GEN_DEEPCOPY) ## Generates Go files from protobuf-based API files
@PATH=$(LOCALBIN):$$PATH $(PROTOC) --proto_path=$(PROTOBUF_API_DIR) --go_out=$(API_GEN_DIR) --go-grpc_out=$(API_GEN_DIR) --golang-deepcopy_out=:$(API_GEN_DIR) $(PROTOBUF_API_DIR)/**/*.proto

$(CRD_GEN): $(CRD_SRC) $(CONTROLLER_GEN) ## Generates CRDs and DeepCopy method implementations.
$(CONTROLLER_GEN) paths="$(CRD_SRC_DIR)/..." \
crd output:crd:artifacts:config="$(CRD_GEN_DIR)" \
object:headerFile="$(LICENSE_FILE)"

.PHONY: fix-imports
fix-imports: $(GOIMPORTS) ## Fixes imports
$(GOIMPORTS) -local "github.com/openshift-service-mesh/federation" -w $(PROJECT_DIR)/

LICENSE_FILE := /tmp/license.txt
LICENSE_FILE := $(PROJECT_DIR)/hack/boilerplate.go.txt
GO_FILES := $(shell find $(PROJECT_DIR)/ -name '*.go')

.PHONY: add-license
add-license: ## Adds license to all Golang files
@echo "// Copyright Red Hat, Inc." > $(LICENSE_FILE)
@echo "//" >> $(LICENSE_FILE)
@echo "// Licensed under the Apache License, Version 2.0 (the "License");" >> $(LICENSE_FILE)
@echo "// you may not use this file except in compliance with the License." >> $(LICENSE_FILE)
@echo "// You may obtain a copy of the License at" >> $(LICENSE_FILE)
@echo "//" >> $(LICENSE_FILE)
@echo "// http://www.apache.org/licenses/LICENSE-2.0" >> $(LICENSE_FILE)
@echo "//" >> $(LICENSE_FILE)
@echo "// Unless required by applicable law or agreed to in writing, software" >> $(LICENSE_FILE)
@echo "// distributed under the License is distributed on an "AS IS" BASIS," >> $(LICENSE_FILE)
@echo "// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied." >> $(LICENSE_FILE)
@echo "// See the License for the specific language governing permissions and" >> $(LICENSE_FILE)
@echo "// limitations under the License." >> $(LICENSE_FILE)
@echo "" >> $(LICENSE_FILE)

@for file in $(GO_FILES); do \
if ! grep -q "Licensed under the Apache License" $$file; then \
echo "Adding license to $$file"; \
cat $(LICENSE_FILE) $$file > temp && mv temp $$file; \
fi \
done
@rm -f $(LICENSE_FILE)
19 changes: 19 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Code generated by tool. DO NOT EDIT.
# This file is used to track the info used to scaffold your project
# and allow the plugins properly work.
# More info: https://book.kubebuilder.io/reference/project-config.html
domain: openshift-service-mesh.io
layout:
- go.kubebuilder.io/v4
projectName: federation
repo: github.com/openshift-service-mesh/federation
resources:
- api:
crdVersion: v1
controller: true
domain: openshift-service-mesh.io
group: federation
kind: MeshFederation
path: github.com/openshift-service-mesh/federation/api/v1alpha1
version: v1alpha1
version: "3"
34 changes: 34 additions & 0 deletions api/v1alpha1/groupversion_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright Red Hat, Inc.
//
// 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 federation v1alpha1 API group.
// +kubebuilder:object:generate=true
// +groupName=federation.openshift-service-mesh.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: "federation.openshift-service-mesh.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
)
63 changes: 63 additions & 0 deletions api/v1alpha1/meshfederation_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright Red Hat, Inc.
//
// 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.

// MeshFederationSpec defines the desired state of MeshFederation.
type MeshFederationSpec 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 MeshFederation. Edit meshfederation_types.go to remove/update
Foo string `json:"foo,omitempty"`
}

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

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:resource:scope=Cluster

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

Spec MeshFederationSpec `json:"spec,omitempty"`
Status MeshFederationStatus `json:"status,omitempty"`
}

// +kubebuilder:object:root=true

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

func init() {
SchemeBuilder.Register(&MeshFederation{}, &MeshFederationList{})
}
112 changes: 112 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.

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.16.4
name: meshfederations.federation.openshift-service-mesh.io
spec:
group: federation.openshift-service-mesh.io
names:
kind: MeshFederation
listKind: MeshFederationList
plural: meshfederations
singular: meshfederation
scope: Cluster
versions:
- name: v1alpha1
schema:
openAPIV3Schema:
description: MeshFederation is the Schema for the meshfederations API.
properties:
apiVersion:
description: |-
APIVersion defines the versioned schema of this representation of an object.
Servers should convert recognized schemas to the latest internal value, and
may reject unrecognized values.
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
type: string
kind:
description: |-
Kind is a string value representing the REST resource this object represents.
Servers may infer this from the endpoint the client submits requests to.
Cannot be updated.
In CamelCase.
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
type: string
metadata:
type: object
spec:
description: MeshFederationSpec defines the desired state of MeshFederation.
properties:
foo:
description: Foo is an example field of MeshFederation. Edit meshfederation_types.go
to remove/update
type: string
type: object
status:
description: MeshFederationStatus defines the observed state of MeshFederation.
type: object
type: object
served: true
storage: true
subresources:
status: {}
14 changes: 14 additions & 0 deletions hack/boilerplate.go.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright Red Hat, Inc.
//
// 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.

Loading