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