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