@@ -19,12 +19,18 @@ import (
19
19
"fmt"
20
20
"io/ioutil"
21
21
"os"
22
+ "path/filepath"
23
+ "strings"
22
24
23
25
"sigs.k8s.io/kubebuilder/v2/pkg/model/config"
24
26
25
27
"github.com/operator-framework/operator-sdk/internal/util/projutil"
26
28
)
27
29
30
+ // Version of `opm` to download and use for building index images.
31
+ // This version's release artifacts *must* contain a binary for multiple arches; certain releases do not.
32
+ const opmVersion = "v1.15.1"
33
+
28
34
// RunInit modifies the project scaffolded by kubebuilder's Init plugin.
29
35
func RunInit (cfg * config.Config ) error {
30
36
// Only run these if project version is v3.
@@ -41,26 +47,44 @@ func RunInit(cfg *config.Config) error {
41
47
42
48
// initUpdateMakefile updates a vanilla kubebuilder Makefile with operator-sdk recipes.
43
49
func initUpdateMakefile (cfg * config.Config , filePath string ) error {
50
+ operatorType := projutil .PluginKeyToOperatorType (cfg .Layout )
51
+ if operatorType == projutil .OperatorTypeUnknown {
52
+ return fmt .Errorf ("unsupported plugin key %q" , cfg .Layout )
53
+ }
54
+
44
55
makefileBytes , err := ioutil .ReadFile (filePath )
45
56
if err != nil {
46
57
return err
47
58
}
48
59
49
60
// Prepend bundle variables.
50
- makefileBytes = append ([]byte (makefileBundleVarFragment ), makefileBytes ... )
61
+ projectName := cfg .ProjectName
62
+ if projectName == "" {
63
+ dir , err := os .Getwd ()
64
+ if err != nil {
65
+ return fmt .Errorf ("error getting current directory: %v" , err )
66
+ }
67
+ projectName = strings .ToLower (filepath .Base (dir ))
68
+ }
69
+ makefileBytes = append ([]byte (fmt .Sprintf (makefileBundleVarFragment , cfg .Domain , projectName )), makefileBytes ... )
51
70
52
71
// Append bundle recipes.
53
- operatorType := projutil .PluginKeyToOperatorType (cfg .Layout )
54
72
switch operatorType {
55
- case projutil .OperatorTypeUnknown :
56
- return fmt .Errorf ("unsupported plugin key %q" , cfg .Layout )
57
73
case projutil .OperatorTypeGo :
58
74
makefileBytes = append (makefileBytes , []byte (makefileBundleFragmentGo )... )
59
75
default :
60
76
makefileBytes = append (makefileBytes , []byte (makefileBundleFragmentNonGo )... )
61
77
}
78
+ makefileBytes = append (makefileBytes , []byte (makefileBundleBuildPushFragment )... )
62
79
63
- makefileBytes = append (makefileBytes , []byte (makefileBundleBuildFragment )... )
80
+ // Append catalog recipes.
81
+ switch operatorType {
82
+ case projutil .OperatorTypeGo :
83
+ makefileBytes = append (makefileBytes , []byte (fmt .Sprintf (makefileOPMFragmentGo , opmVersion ))... )
84
+ default :
85
+ makefileBytes = append (makefileBytes , []byte (fmt .Sprintf (makefileOPMFragmentNonGo , opmVersion ))... )
86
+ }
87
+ makefileBytes = append (makefileBytes , []byte (makefileCatalogBuildFragment )... )
64
88
65
89
var mode os.FileMode = 0644
66
90
if info , err := os .Stat (filePath ); err != nil {
@@ -71,14 +95,14 @@ func initUpdateMakefile(cfg *config.Config, filePath string) error {
71
95
72
96
// Makefile fragments to add to the base Makefile.
73
97
const (
74
- makefileBundleVarFragment = `# VERSION defines the project version for the bundle.
98
+ makefileBundleVarFragment = `# VERSION defines the project version for the bundle.
75
99
# Update this value when you upgrade the version of your project.
76
100
# To re-generate a bundle for another specific version without changing the standard setup, you can:
77
101
# - use the VERSION as arg of the bundle target (e.g make bundle VERSION=0.0.2)
78
102
# - use environment variables to overwrite this value (e.g export VERSION=0.0.2)
79
103
VERSION ?= 0.0.1
80
104
81
- # CHANNELS define the bundle channels used in the bundle.
105
+ # CHANNELS define the bundle channels used in the bundle.
82
106
# Add a new line here if you would like to change its default config. (E.g CHANNELS = "preview,fast,stable")
83
107
# To re-generate a bundle for other specific channels without changing the standard setup, you can:
84
108
# - use the CHANNELS as arg of the bundle target (e.g make bundle CHANNELS=preview,fast,stable)
@@ -87,7 +111,7 @@ ifneq ($(origin CHANNELS), undefined)
87
111
BUNDLE_CHANNELS := --channels=$(CHANNELS)
88
112
endif
89
113
90
- # DEFAULT_CHANNEL defines the default channel used in the bundle.
114
+ # DEFAULT_CHANNEL defines the default channel used in the bundle.
91
115
# Add a new line here if you would like to change its default config. (E.g DEFAULT_CHANNEL = "stable")
92
116
# To re-generate a bundle for any other default channel without changing the default setup, you can:
93
117
# - use the DEFAULT_CHANNEL as arg of the bundle target (e.g make bundle DEFAULT_CHANNEL=stable)
@@ -97,9 +121,16 @@ BUNDLE_DEFAULT_CHANNEL := --default-channel=$(DEFAULT_CHANNEL)
97
121
endif
98
122
BUNDLE_METADATA_OPTS ?= $(BUNDLE_CHANNELS) $(BUNDLE_DEFAULT_CHANNEL)
99
123
100
- # BUNDLE_IMG defines the image:tag used for the bundle.
124
+ # IMAGE_TAG_BASE defines the docker.io namespace and part of the image name for remote images.
125
+ # This variable is used to construct full image tags for bundle and catalog images.
126
+ #
127
+ # For example, running 'make bundle-build bundle-push catalog-build catalog-push' will build and push both
128
+ # %[1]s/%[2]s-bundle:$VERSION and %[1]s/%[2]s-catalog:$VERSION.
129
+ IMAGE_TAG_BASE ?= %[1]s/%[2]s
130
+
131
+ # BUNDLE_IMG defines the image:tag used for the bundle.
101
132
# You can use it as an arg. (E.g make bundle-build BUNDLE_IMG=<some-registry>/<project-name-bundle>:<tag>)
102
- BUNDLE_IMG ?= controller -bundle:$(VERSION)
133
+ BUNDLE_IMG ?= $(IMAGE_TAG_BASE) -bundle:v $(VERSION)
103
134
`
104
135
105
136
makefileBundleFragmentGo = `
@@ -122,10 +153,81 @@ bundle: kustomize
122
153
operator-sdk bundle validate ./bundle
123
154
`
124
155
125
- makefileBundleBuildFragment = `
156
+ makefileBundleBuildPushFragment = `
126
157
# Build the bundle image.
127
158
.PHONY: bundle-build
128
159
bundle-build:
129
160
docker build -f bundle.Dockerfile -t $(BUNDLE_IMG) .
161
+
162
+ # Push the bundle image.
163
+ .PHONY: bundle-push
164
+ bundle-push:
165
+ $(MAKE) docker-push IMG=$(BUNDLE_IMG)
166
+ `
167
+
168
+ makefileOPMFragmentGo = `
169
+ # Download opm locally if necessary.
170
+ .PHONY: opm
171
+ OPM = ./bin/opm
172
+ opm:
173
+ ifeq (,$(wildcard $(OPM)))
174
+ ifeq (,$(shell which opm 2>/dev/null))
175
+ @{ \
176
+ set -e ;\
177
+ mkdir -p $(dir $(OPM)) ;\
178
+ OS=$$(uname -s | tr '[:upper:]' '[:lower:]') && \
179
+ ARCH=$$(case $$(arch) in x86_64) echo -n amd64 ;; aarch64) echo -n arm64 ;; *) echo -n $$(arch) ;; esac) && \
180
+ curl -sSLo $(OPM) https://github.com/operator-framework/operator-registry/releases/download/%[1]s/$${OS}-$${ARCH}-opm ;\
181
+ chmod +x $(OPM) ;\
182
+ }
183
+ else
184
+ OPM = $(shell which opm)
185
+ endif
186
+ endif
187
+ `
188
+
189
+ makefileOPMFragmentNonGo = `
190
+ # Download opm locally if necessary.
191
+ .PHONY: opm
192
+ OPM = ./bin/opm
193
+ opm:
194
+ ifeq (,$(wildcard $(OPM)))
195
+ ifeq (,$(shell which opm 2>/dev/null))
196
+ @{ \
197
+ set -e ;\
198
+ mkdir -p $(dir $(OPM)) ;\
199
+ curl -sSLo $(OPM) https://github.com/operator-framework/operator-registry/releases/download/%[1]s/$(OS)-$(ARCH)-opm ;\
200
+ chmod +x $(OPM) ;\
201
+ }
202
+ else
203
+ OPM = $(shell which opm)
204
+ endif
205
+ endif
206
+ `
207
+
208
+ makefileCatalogBuildFragment = `
209
+ # A comma-separated list of bundle images (e.g. make catalog-build BUNDLE_IMGS=example.com/operator-bundle:v0.1.0,example.com/operator-bundle:v0.2.0).
210
+ # These images MUST exist in a registry and be pull-able.
211
+ BUNDLE_IMGS ?= $(BUNDLE_IMG)
212
+
213
+ # The image tag given to the resulting catalog image (e.g. make catalog-build CATALOG_IMG=example.com/operator-catalog:v0.2.0).
214
+ CATALOG_IMG ?= $(IMAGE_TAG_BASE)-catalog:v$(VERSION)
215
+
216
+ # Set CATALOG_BASE_IMG to an existing catalog image tag to add $BUNDLE_IMGS to that image.
217
+ ifneq ($(origin CATALOG_BASE_IMG), undefined)
218
+ FROM_INDEX_OPT := --from-index $(CATALOG_BASE_IMG)
219
+ endif
220
+
221
+ # Build a catalog image by adding bundle images to an empty catalog using the operator package manager tool, 'opm'.
222
+ # This recipe invokes 'opm' in 'semver' bundle add mode. For more information on add modes, see:
223
+ # https://github.com/operator-framework/community-operators/blob/master/docs/contributing.md#updating-your-existing-operator
224
+ .PHONY: catalog-build
225
+ catalog-build: opm
226
+ $(OPM) index add --mode semver --tag $(CATALOG_IMG) --bundles $(BUNDLE_IMGS) $(FROM_INDEX_OPT)
227
+
228
+ # Push the catalog image.
229
+ .PHONY: catalog-push
230
+ catalog-push:
231
+ $(MAKE) docker-push IMG=$(CATALOG_IMG)
130
232
`
131
233
)
0 commit comments