From 4275f8ab23c1a46cc19fb543ee3b703857726674 Mon Sep 17 00:00:00 2001 From: "jesus m. rodriguez" Date: Sat, 15 May 2021 22:00:58 -0400 Subject: [PATCH 1/5] change name to be operator instead of service --- .../scaffolds/internal/templates/applicationproperties.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/quarkus/v1alpha/scaffolds/internal/templates/applicationproperties.go b/pkg/quarkus/v1alpha/scaffolds/internal/templates/applicationproperties.go index 628fba1..e421f82 100644 --- a/pkg/quarkus/v1alpha/scaffolds/internal/templates/applicationproperties.go +++ b/pkg/quarkus/v1alpha/scaffolds/internal/templates/applicationproperties.go @@ -47,5 +47,5 @@ func (f *ApplicationPropertiesFile) SetTemplateDefaults() error { // TODO: pass in the name of the operator i.e. replace Memcached const ApplicationPropertiesTemplate = `quarkus.container-image.build=true #quarkus.container-image.group= -quarkus.container-image.name={{ .ProjectName }}-service +quarkus.container-image.name={{ .ProjectName }}-operator ` From 81ac09adb062e99e0162d983f57f02595d8c1376 Mon Sep 17 00:00:00 2001 From: "jesus m. rodriguez" Date: Sat, 15 May 2021 22:05:49 -0400 Subject: [PATCH 2/5] Add makefile --- pkg/quarkus/v1alpha/scaffolds/init.go | 4 + .../scaffolds/internal/templates/makefile.go | 129 ++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 pkg/quarkus/v1alpha/scaffolds/internal/templates/makefile.go diff --git a/pkg/quarkus/v1alpha/scaffolds/init.go b/pkg/quarkus/v1alpha/scaffolds/init.go index 96bbec6..90073d3 100644 --- a/pkg/quarkus/v1alpha/scaffolds/init.go +++ b/pkg/quarkus/v1alpha/scaffolds/init.go @@ -75,5 +75,9 @@ func (s *initScaffolder) Scaffold() error { &templates.ApplicationPropertiesFile{ ProjectName: s.config.GetProjectName(), }, + &templates.Makefile{ + Image: "", + KustomizeVersion: "v3.5.4", + }, ) } diff --git a/pkg/quarkus/v1alpha/scaffolds/internal/templates/makefile.go b/pkg/quarkus/v1alpha/scaffolds/internal/templates/makefile.go new file mode 100644 index 0000000..7e3aed4 --- /dev/null +++ b/pkg/quarkus/v1alpha/scaffolds/internal/templates/makefile.go @@ -0,0 +1,129 @@ +// Copyright 2021 The Operator-SDK 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 templates + +import ( + "errors" + + "sigs.k8s.io/kubebuilder/v3/pkg/machinery" +) + +var _ machinery.Template = &Makefile{} + +// Makefile scaffolds the Makefile +type Makefile struct { + machinery.TemplateMixin + + // Image is controller manager image name + Image string + + // Kustomize version to use in the project + KustomizeVersion string + + // // AnsibleOperatorVersion is the version of the ansible-operator binary downloaded by the Makefile. + // AnsibleOperatorVersion string +} + +// SetTemplateDefaults implements machinery.Template +func (f *Makefile) SetTemplateDefaults() error { + if f.Path == "" { + f.Path = "Makefile" + } + + f.TemplateBody = makefileTemplate + + f.IfExistsAction = machinery.Error + + if f.Image == "" { + f.Image = "controller:latest" + } + + if f.KustomizeVersion == "" { + return errors.New("kustomize version is required in scaffold") + } + + // if f.AnsibleOperatorVersion == "" { + // return errors.New("ansible-operator version is required in scaffold") + // } + + return nil +} + +const makefileTemplate = ` +# Image URL to use all building/pushing image targets +IMG ?= {{ .Image }} + +all: docker-build + +##@ General + +# The help target prints out all targets with their descriptions organized +# beneath their categories. The categories are represented by '##@' and the +# target descriptions by '##'. The awk commands is responsible for reading the +# entire set of makefiles included in this invocation, looking for lines of the +# file as xyz: ## something, and then pretty-format the target and help. Then, +# if there's a line with ##@ something, that gets pretty-printed as a category. +# More info on the usage of ANSI control characters for terminal formatting: +# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters +# More info on the awk command: +# http://linuxcommand.org/lc3_adv_awk.php + +help: ## Display this help. + @awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST) + +##@ Build + +# run: ansible-operator ## Run against the configured Kubernetes cluster in ~/.kube/config +# $(ANSIBLE_OPERATOR) run + +docker-build: ## Build docker image with the manager. + mvn package -Dquarkus.container-image.build=true -Dquarkus.container-image.image=${IMG} + +docker-push: ## Push docker image with the manager. + mvn package -Dquarkus.container-image.push=true -Dquarkus.container-image.image=${IMG} + +##@ Deployment + +install: kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config. + $(KUSTOMIZE) build config/crd | kubectl apply -f - + +uninstall: kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config. + $(KUSTOMIZE) build config/crd | kubectl delete -f - + +deploy: ## Deploy controller to the K8s cluster specified in ~/.kube/config. + kubectl apply -f target/kubernetes/kubernetes.yml + +undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config. + kubectl delete -f target/kubernetes/kubernetes.yml + +OS := $(shell uname -s | tr '[:upper:]' '[:lower:]') +ARCH := $(shell uname -m | sed 's/x86_64/amd64/') + +.PHONY: kustomize +KUSTOMIZE = $(shell pwd)/bin/kustomize +kustomize: ## Download kustomize locally if necessary. +ifeq (,$(wildcard $(KUSTOMIZE))) +ifeq (,$(shell which kustomize 2>/dev/null)) + @{ \ + set -e ;\ + mkdir -p $(dir $(KUSTOMIZE)) ;\ + curl -sSLo - https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize/{{ .KustomizeVersion }}/kustomize_{{ .KustomizeVersion }}_$(OS)_$(ARCH).tar.gz | \ + tar xzf - -C bin/ ;\ + } +else +KUSTOMIZE = $(shell which kustomize) +endif +endif +` From f8d5cb2b5b8699648afa02c495afefc2112b0712 Mon Sep 17 00:00:00 2001 From: "jesus m. rodriguez" Date: Sat, 15 May 2021 23:27:01 -0400 Subject: [PATCH 3/5] Update model so that it generates CRD correctly --- pkg/quarkus/v1alpha/scaffolds/api.go | 2 -- .../scaffolds/internal/templates/model/model.go | 12 +++++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/pkg/quarkus/v1alpha/scaffolds/api.go b/pkg/quarkus/v1alpha/scaffolds/api.go index 37ac659..f771bff 100644 --- a/pkg/quarkus/v1alpha/scaffolds/api.go +++ b/pkg/quarkus/v1alpha/scaffolds/api.go @@ -69,8 +69,6 @@ func (s *apiScaffolder) Scaffold() error { &model.Model{ Package: util.ReverseDomain(s.config.GetDomain()), ClassName: util.ToClassname(s.resource.Kind), - Version: s.resource.Version, - Group: s.resource.Group, }, &model.ModelSpec{ Package: util.ReverseDomain(s.config.GetDomain()), diff --git a/pkg/quarkus/v1alpha/scaffolds/internal/templates/model/model.go b/pkg/quarkus/v1alpha/scaffolds/internal/templates/model/model.go index 1d191e7..f16f571 100644 --- a/pkg/quarkus/v1alpha/scaffolds/internal/templates/model/model.go +++ b/pkg/quarkus/v1alpha/scaffolds/internal/templates/model/model.go @@ -26,15 +26,13 @@ var _ machinery.Template = &Model{} type Model struct { machinery.TemplateMixin + machinery.ResourceMixin // Package is the source files package Package string // Name of the operator used for the main file. ClassName string - - Version string - Group string } func (f *Model) SetTemplateDefaults() error { @@ -57,10 +55,14 @@ const modelTemplate = `package {{ .Package }}; import io.fabric8.kubernetes.api.model.Namespaced; import io.fabric8.kubernetes.client.CustomResource; import io.fabric8.kubernetes.model.annotation.Group; +import io.fabric8.kubernetes.model.annotation.Kind; +import io.fabric8.kubernetes.model.annotation.Plural; import io.fabric8.kubernetes.model.annotation.Version; -@Version("{{ .Version }}") -@Group("{{ .Group }}") +@Version("{{ .Resource.API.CRDVersion }}") +@Group("{{ .Resource.QualifiedGroup }}") +@Kind("{{ .Resource.Kind }}") +@Plural("{{ .Resource.Plural }}") public class {{ .ClassName }} extends CustomResource<{{ .ClassName }}Spec, {{ .ClassName }}Status> implements Namespaced {} From e2ebe1ec22eae42902be4019bf779b2dbee5f542 Mon Sep 17 00:00:00 2001 From: "jesus m. rodriguez" Date: Mon, 17 May 2021 11:01:34 -0400 Subject: [PATCH 4/5] Install all available CRDs --- .../v1alpha/scaffolds/internal/templates/makefile.go | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/pkg/quarkus/v1alpha/scaffolds/internal/templates/makefile.go b/pkg/quarkus/v1alpha/scaffolds/internal/templates/makefile.go index 7e3aed4..e891602 100644 --- a/pkg/quarkus/v1alpha/scaffolds/internal/templates/makefile.go +++ b/pkg/quarkus/v1alpha/scaffolds/internal/templates/makefile.go @@ -85,9 +85,6 @@ help: ## Display this help. ##@ Build -# run: ansible-operator ## Run against the configured Kubernetes cluster in ~/.kube/config -# $(ANSIBLE_OPERATOR) run - docker-build: ## Build docker image with the manager. mvn package -Dquarkus.container-image.build=true -Dquarkus.container-image.image=${IMG} @@ -96,11 +93,11 @@ docker-push: ## Push docker image with the manager. ##@ Deployment -install: kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config. - $(KUSTOMIZE) build config/crd | kubectl apply -f - +install: ## Install CRDs into the K8s cluster specified in ~/.kube/config. + @$(foreach file, $(wildcard target/kubernetes/*-v1.yml), kubectl apply -f $(file);) -uninstall: kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config. - $(KUSTOMIZE) build config/crd | kubectl delete -f - +uninstall: ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config. + @$(foreach file, $(wildcard target/kubernetes/*-v1.yml), kubectl delete -f $(file);) deploy: ## Deploy controller to the K8s cluster specified in ~/.kube/config. kubectl apply -f target/kubernetes/kubernetes.yml From 5ef0fa2b1b3adc0f5e28c1589dfee761bc66fc69 Mon Sep 17 00:00:00 2001 From: "jesus m. rodriguez" Date: Thu, 20 May 2021 11:37:34 -0400 Subject: [PATCH 5/5] remove kustomize --- .../scaffolds/internal/templates/makefile.go | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/pkg/quarkus/v1alpha/scaffolds/internal/templates/makefile.go b/pkg/quarkus/v1alpha/scaffolds/internal/templates/makefile.go index e891602..f6bca30 100644 --- a/pkg/quarkus/v1alpha/scaffolds/internal/templates/makefile.go +++ b/pkg/quarkus/v1alpha/scaffolds/internal/templates/makefile.go @@ -104,23 +104,4 @@ deploy: ## Deploy controller to the K8s cluster specified in ~/.kube/config. undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config. kubectl delete -f target/kubernetes/kubernetes.yml - -OS := $(shell uname -s | tr '[:upper:]' '[:lower:]') -ARCH := $(shell uname -m | sed 's/x86_64/amd64/') - -.PHONY: kustomize -KUSTOMIZE = $(shell pwd)/bin/kustomize -kustomize: ## Download kustomize locally if necessary. -ifeq (,$(wildcard $(KUSTOMIZE))) -ifeq (,$(shell which kustomize 2>/dev/null)) - @{ \ - set -e ;\ - mkdir -p $(dir $(KUSTOMIZE)) ;\ - curl -sSLo - https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize/{{ .KustomizeVersion }}/kustomize_{{ .KustomizeVersion }}_$(OS)_$(ARCH).tar.gz | \ - tar xzf - -C bin/ ;\ - } -else -KUSTOMIZE = $(shell which kustomize) -endif -endif `