Skip to content

Commit 26d04f1

Browse files
authored
Add makefile to scaffolding (operator-framework#11)
* Change name to be operator instead of service * Add makefile * Update model so that it generates CRD correctly * Install all available CRDs * Remove kustomize section
1 parent 3cae9f6 commit 26d04f1

File tree

5 files changed

+119
-8
lines changed

5 files changed

+119
-8
lines changed

pkg/quarkus/v1alpha/scaffolds/api.go

-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ func (s *apiScaffolder) Scaffold() error {
6969
&model.Model{
7070
Package: util.ReverseDomain(s.config.GetDomain()),
7171
ClassName: util.ToClassname(s.resource.Kind),
72-
Version: s.resource.Version,
73-
Group: s.resource.Group,
7472
},
7573
&model.ModelSpec{
7674
Package: util.ReverseDomain(s.config.GetDomain()),

pkg/quarkus/v1alpha/scaffolds/init.go

+4
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,9 @@ func (s *initScaffolder) Scaffold() error {
7575
&templates.ApplicationPropertiesFile{
7676
ProjectName: s.config.GetProjectName(),
7777
},
78+
&templates.Makefile{
79+
Image: "",
80+
KustomizeVersion: "v3.5.4",
81+
},
7882
)
7983
}

pkg/quarkus/v1alpha/scaffolds/internal/templates/applicationproperties.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,5 @@ func (f *ApplicationPropertiesFile) SetTemplateDefaults() error {
4747
// TODO: pass in the name of the operator i.e. replace Memcached
4848
const ApplicationPropertiesTemplate = `quarkus.container-image.build=true
4949
#quarkus.container-image.group=
50-
quarkus.container-image.name={{ .ProjectName }}-service
50+
quarkus.container-image.name={{ .ProjectName }}-operator
5151
`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// Copyright 2021 The Operator-SDK Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package templates
16+
17+
import (
18+
"errors"
19+
20+
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
21+
)
22+
23+
var _ machinery.Template = &Makefile{}
24+
25+
// Makefile scaffolds the Makefile
26+
type Makefile struct {
27+
machinery.TemplateMixin
28+
29+
// Image is controller manager image name
30+
Image string
31+
32+
// Kustomize version to use in the project
33+
KustomizeVersion string
34+
35+
// // AnsibleOperatorVersion is the version of the ansible-operator binary downloaded by the Makefile.
36+
// AnsibleOperatorVersion string
37+
}
38+
39+
// SetTemplateDefaults implements machinery.Template
40+
func (f *Makefile) SetTemplateDefaults() error {
41+
if f.Path == "" {
42+
f.Path = "Makefile"
43+
}
44+
45+
f.TemplateBody = makefileTemplate
46+
47+
f.IfExistsAction = machinery.Error
48+
49+
if f.Image == "" {
50+
f.Image = "controller:latest"
51+
}
52+
53+
if f.KustomizeVersion == "" {
54+
return errors.New("kustomize version is required in scaffold")
55+
}
56+
57+
// if f.AnsibleOperatorVersion == "" {
58+
// return errors.New("ansible-operator version is required in scaffold")
59+
// }
60+
61+
return nil
62+
}
63+
64+
const makefileTemplate = `
65+
# Image URL to use all building/pushing image targets
66+
IMG ?= {{ .Image }}
67+
68+
all: docker-build
69+
70+
##@ General
71+
72+
# The help target prints out all targets with their descriptions organized
73+
# beneath their categories. The categories are represented by '##@' and the
74+
# target descriptions by '##'. The awk commands is responsible for reading the
75+
# entire set of makefiles included in this invocation, looking for lines of the
76+
# file as xyz: ## something, and then pretty-format the target and help. Then,
77+
# if there's a line with ##@ something, that gets pretty-printed as a category.
78+
# More info on the usage of ANSI control characters for terminal formatting:
79+
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
80+
# More info on the awk command:
81+
# http://linuxcommand.org/lc3_adv_awk.php
82+
83+
help: ## Display this help.
84+
@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)
85+
86+
##@ Build
87+
88+
docker-build: ## Build docker image with the manager.
89+
mvn package -Dquarkus.container-image.build=true -Dquarkus.container-image.image=${IMG}
90+
91+
docker-push: ## Push docker image with the manager.
92+
mvn package -Dquarkus.container-image.push=true -Dquarkus.container-image.image=${IMG}
93+
94+
##@ Deployment
95+
96+
install: ## Install CRDs into the K8s cluster specified in ~/.kube/config.
97+
@$(foreach file, $(wildcard target/kubernetes/*-v1.yml), kubectl apply -f $(file);)
98+
99+
uninstall: ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config.
100+
@$(foreach file, $(wildcard target/kubernetes/*-v1.yml), kubectl delete -f $(file);)
101+
102+
deploy: ## Deploy controller to the K8s cluster specified in ~/.kube/config.
103+
kubectl apply -f target/kubernetes/kubernetes.yml
104+
105+
undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config.
106+
kubectl delete -f target/kubernetes/kubernetes.yml
107+
`

pkg/quarkus/v1alpha/scaffolds/internal/templates/model/model.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,13 @@ var _ machinery.Template = &Model{}
2626

2727
type Model struct {
2828
machinery.TemplateMixin
29+
machinery.ResourceMixin
2930

3031
// Package is the source files package
3132
Package string
3233

3334
// Name of the operator used for the main file.
3435
ClassName string
35-
36-
Version string
37-
Group string
3836
}
3937

4038
func (f *Model) SetTemplateDefaults() error {
@@ -57,10 +55,14 @@ const modelTemplate = `package {{ .Package }};
5755
import io.fabric8.kubernetes.api.model.Namespaced;
5856
import io.fabric8.kubernetes.client.CustomResource;
5957
import io.fabric8.kubernetes.model.annotation.Group;
58+
import io.fabric8.kubernetes.model.annotation.Kind;
59+
import io.fabric8.kubernetes.model.annotation.Plural;
6060
import io.fabric8.kubernetes.model.annotation.Version;
6161
62-
@Version("{{ .Version }}")
63-
@Group("{{ .Group }}")
62+
@Version("{{ .Resource.API.CRDVersion }}")
63+
@Group("{{ .Resource.QualifiedGroup }}")
64+
@Kind("{{ .Resource.Kind }}")
65+
@Plural("{{ .Resource.Plural }}")
6466
public class {{ .ClassName }} extends CustomResource<{{ .ClassName }}Spec, {{ .ClassName }}Status>
6567
implements Namespaced {}
6668

0 commit comments

Comments
 (0)