Skip to content

Add makefile to scaffolding #11

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

Merged
merged 5 commits into from
May 24, 2021
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
2 changes: 0 additions & 2 deletions pkg/quarkus/v1alpha/scaffolds/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
4 changes: 4 additions & 0 deletions pkg/quarkus/v1alpha/scaffolds/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,9 @@ func (s *initScaffolder) Scaffold() error {
&templates.ApplicationPropertiesFile{
ProjectName: s.config.GetProjectName(),
},
&templates.Makefile{
Image: "",
KustomizeVersion: "v3.5.4",
},
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
`
107 changes: 107 additions & 0 deletions pkg/quarkus/v1alpha/scaffolds/internal/templates/makefile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// 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<target>\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

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: ## Install CRDs into the K8s cluster specified in ~/.kube/config.
@$(foreach file, $(wildcard target/kubernetes/*-v1.yml), kubectl apply -f $(file);)

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

undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config.
kubectl delete -f target/kubernetes/kubernetes.yml
`
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {}

Expand Down