Skip to content

Commit f8a08b7

Browse files
committed
(manifests/v1) add 'opm index add' and 'opm' download targets to Makefile
1 parent 67f9c8b commit f8a08b7

File tree

5 files changed

+349
-31
lines changed

5 files changed

+349
-31
lines changed

internal/plugins/manifests/init.go

Lines changed: 113 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,18 @@ import (
1919
"fmt"
2020
"io/ioutil"
2121
"os"
22+
"path/filepath"
23+
"strings"
2224

2325
"sigs.k8s.io/kubebuilder/v2/pkg/model/config"
2426

2527
"github.com/operator-framework/operator-sdk/internal/util/projutil"
2628
)
2729

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+
2834
// RunInit modifies the project scaffolded by kubebuilder's Init plugin.
2935
func RunInit(cfg *config.Config) error {
3036
// Only run these if project version is v3.
@@ -41,26 +47,44 @@ func RunInit(cfg *config.Config) error {
4147

4248
// initUpdateMakefile updates a vanilla kubebuilder Makefile with operator-sdk recipes.
4349
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+
4455
makefileBytes, err := ioutil.ReadFile(filePath)
4556
if err != nil {
4657
return err
4758
}
4859

4960
// 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...)
5170

5271
// Append bundle recipes.
53-
operatorType := projutil.PluginKeyToOperatorType(cfg.Layout)
5472
switch operatorType {
55-
case projutil.OperatorTypeUnknown:
56-
return fmt.Errorf("unsupported plugin key %q", cfg.Layout)
5773
case projutil.OperatorTypeGo:
5874
makefileBytes = append(makefileBytes, []byte(makefileBundleFragmentGo)...)
5975
default:
6076
makefileBytes = append(makefileBytes, []byte(makefileBundleFragmentNonGo)...)
6177
}
78+
makefileBytes = append(makefileBytes, []byte(makefileBundleBuildPushFragment)...)
6279

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)...)
6488

6589
var mode os.FileMode = 0644
6690
if info, err := os.Stat(filePath); err != nil {
@@ -71,14 +95,14 @@ func initUpdateMakefile(cfg *config.Config, filePath string) error {
7195

7296
// Makefile fragments to add to the base Makefile.
7397
const (
74-
makefileBundleVarFragment = `# VERSION defines the project version for the bundle.
98+
makefileBundleVarFragment = `# VERSION defines the project version for the bundle.
7599
# Update this value when you upgrade the version of your project.
76100
# To re-generate a bundle for another specific version without changing the standard setup, you can:
77101
# - use the VERSION as arg of the bundle target (e.g make bundle VERSION=0.0.2)
78102
# - use environment variables to overwrite this value (e.g export VERSION=0.0.2)
79103
VERSION ?= 0.0.1
80104
81-
# CHANNELS define the bundle channels used in the bundle.
105+
# CHANNELS define the bundle channels used in the bundle.
82106
# Add a new line here if you would like to change its default config. (E.g CHANNELS = "preview,fast,stable")
83107
# To re-generate a bundle for other specific channels without changing the standard setup, you can:
84108
# - 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)
87111
BUNDLE_CHANNELS := --channels=$(CHANNELS)
88112
endif
89113
90-
# DEFAULT_CHANNEL defines the default channel used in the bundle.
114+
# DEFAULT_CHANNEL defines the default channel used in the bundle.
91115
# Add a new line here if you would like to change its default config. (E.g DEFAULT_CHANNEL = "stable")
92116
# To re-generate a bundle for any other default channel without changing the default setup, you can:
93117
# - 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)
97121
endif
98122
BUNDLE_METADATA_OPTS ?= $(BUNDLE_CHANNELS) $(BUNDLE_DEFAULT_CHANNEL)
99123
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.
101132
# 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)
103134
`
104135

105136
makefileBundleFragmentGo = `
@@ -122,10 +153,81 @@ bundle: kustomize
122153
operator-sdk bundle validate ./bundle
123154
`
124155

125-
makefileBundleBuildFragment = `
156+
makefileBundleBuildPushFragment = `
126157
# Build the bundle image.
127158
.PHONY: bundle-build
128159
bundle-build:
129160
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)
130232
`
131233
)

testdata/ansible/memcached-operator/Makefile

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
# VERSION defines the project version for the bundle.
1+
# VERSION defines the project version for the bundle.
22
# Update this value when you upgrade the version of your project.
33
# To re-generate a bundle for another specific version without changing the standard setup, you can:
44
# - use the VERSION as arg of the bundle target (e.g make bundle VERSION=0.0.2)
55
# - use environment variables to overwrite this value (e.g export VERSION=0.0.2)
66
VERSION ?= 0.0.1
77

8-
# CHANNELS define the bundle channels used in the bundle.
8+
# CHANNELS define the bundle channels used in the bundle.
99
# Add a new line here if you would like to change its default config. (E.g CHANNELS = "preview,fast,stable")
1010
# To re-generate a bundle for other specific channels without changing the standard setup, you can:
1111
# - use the CHANNELS as arg of the bundle target (e.g make bundle CHANNELS=preview,fast,stable)
@@ -14,7 +14,7 @@ ifneq ($(origin CHANNELS), undefined)
1414
BUNDLE_CHANNELS := --channels=$(CHANNELS)
1515
endif
1616

17-
# DEFAULT_CHANNEL defines the default channel used in the bundle.
17+
# DEFAULT_CHANNEL defines the default channel used in the bundle.
1818
# Add a new line here if you would like to change its default config. (E.g DEFAULT_CHANNEL = "stable")
1919
# To re-generate a bundle for any other default channel without changing the default setup, you can:
2020
# - use the DEFAULT_CHANNEL as arg of the bundle target (e.g make bundle DEFAULT_CHANNEL=stable)
@@ -24,9 +24,16 @@ BUNDLE_DEFAULT_CHANNEL := --default-channel=$(DEFAULT_CHANNEL)
2424
endif
2525
BUNDLE_METADATA_OPTS ?= $(BUNDLE_CHANNELS) $(BUNDLE_DEFAULT_CHANNEL)
2626

27-
# BUNDLE_IMG defines the image:tag used for the bundle.
27+
# IMAGE_TAG_BASE defines the docker.io namespace and part of the image name for remote images.
28+
# This variable is used to construct full image tags for bundle and catalog images.
29+
#
30+
# For example, running 'make bundle-build bundle-push catalog-build catalog-push' will build and push both
31+
# example.com/memcached-operator-bundle:$VERSION and example.com/memcached-operator-catalog:$VERSION.
32+
IMAGE_TAG_BASE ?= example.com/memcached-operator
33+
34+
# BUNDLE_IMG defines the image:tag used for the bundle.
2835
# You can use it as an arg. (E.g make bundle-build BUNDLE_IMG=<some-registry>/<project-name-bundle>:<tag>)
29-
BUNDLE_IMG ?= controller-bundle:$(VERSION)
36+
BUNDLE_IMG ?= $(IMAGE_TAG_BASE)-bundle:v$(VERSION)
3037

3138
# Image URL to use all building/pushing image targets
3239
IMG ?= controller:latest
@@ -113,3 +120,49 @@ bundle: kustomize
113120
.PHONY: bundle-build
114121
bundle-build:
115122
docker build -f bundle.Dockerfile -t $(BUNDLE_IMG) .
123+
124+
# Push the bundle image.
125+
.PHONY: bundle-push
126+
bundle-push:
127+
$(MAKE) docker-push IMG=$(BUNDLE_IMG)
128+
129+
# Download opm locally if necessary.
130+
.PHONY: opm
131+
OPM = ./bin/opm
132+
opm:
133+
ifeq (,$(wildcard $(OPM)))
134+
ifeq (,$(shell which opm 2>/dev/null))
135+
@{ \
136+
set -e ;\
137+
mkdir -p $(dir $(OPM)) ;\
138+
curl -sSLo $(OPM) https://github.com/operator-framework/operator-registry/releases/download/v1.15.1/$(OS)-$(ARCH)-opm ;\
139+
chmod +x $(OPM) ;\
140+
}
141+
else
142+
OPM = $(shell which opm)
143+
endif
144+
endif
145+
146+
# 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).
147+
# These images MUST exist in a registry and be pull-able.
148+
BUNDLE_IMGS ?= $(BUNDLE_IMG)
149+
150+
# The image tag given to the resulting catalog image (e.g. make catalog-build CATALOG_IMG=example.com/operator-catalog:v0.2.0).
151+
CATALOG_IMG ?= $(IMAGE_TAG_BASE)-catalog:v$(VERSION)
152+
153+
# Set CATALOG_BASE_IMG to an existing catalog image tag to add $BUNDLE_IMGS to that image.
154+
ifneq ($(origin CATALOG_BASE_IMG), undefined)
155+
FROM_INDEX_OPT := --from-index $(CATALOG_BASE_IMG)
156+
endif
157+
158+
# Build a catalog image by adding bundle images to an empty catalog using the operator package manager tool, 'opm'.
159+
# This recipe invokes 'opm' in 'semver' bundle add mode. For more information on add modes, see:
160+
# https://github.com/operator-framework/community-operators/blob/master/docs/contributing.md#updating-your-existing-operator
161+
.PHONY: catalog-build
162+
catalog-build: opm
163+
$(OPM) index add --mode semver --tag $(CATALOG_IMG) --bundles $(BUNDLE_IMGS) $(FROM_INDEX_OPT)
164+
165+
# Push the catalog image.
166+
.PHONY: catalog-push
167+
catalog-push:
168+
$(MAKE) docker-push IMG=$(CATALOG_IMG)

testdata/go/v2/memcached-operator/Makefile

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
# VERSION defines the project version for the bundle.
1+
# VERSION defines the project version for the bundle.
22
# Update this value when you upgrade the version of your project.
33
# To re-generate a bundle for another specific version without changing the standard setup, you can:
44
# - use the VERSION as arg of the bundle target (e.g make bundle VERSION=0.0.2)
55
# - use environment variables to overwrite this value (e.g export VERSION=0.0.2)
66
VERSION ?= 0.0.1
77

8-
# CHANNELS define the bundle channels used in the bundle.
8+
# CHANNELS define the bundle channels used in the bundle.
99
# Add a new line here if you would like to change its default config. (E.g CHANNELS = "preview,fast,stable")
1010
# To re-generate a bundle for other specific channels without changing the standard setup, you can:
1111
# - use the CHANNELS as arg of the bundle target (e.g make bundle CHANNELS=preview,fast,stable)
@@ -14,7 +14,7 @@ ifneq ($(origin CHANNELS), undefined)
1414
BUNDLE_CHANNELS := --channels=$(CHANNELS)
1515
endif
1616

17-
# DEFAULT_CHANNEL defines the default channel used in the bundle.
17+
# DEFAULT_CHANNEL defines the default channel used in the bundle.
1818
# Add a new line here if you would like to change its default config. (E.g DEFAULT_CHANNEL = "stable")
1919
# To re-generate a bundle for any other default channel without changing the default setup, you can:
2020
# - use the DEFAULT_CHANNEL as arg of the bundle target (e.g make bundle DEFAULT_CHANNEL=stable)
@@ -24,9 +24,16 @@ BUNDLE_DEFAULT_CHANNEL := --default-channel=$(DEFAULT_CHANNEL)
2424
endif
2525
BUNDLE_METADATA_OPTS ?= $(BUNDLE_CHANNELS) $(BUNDLE_DEFAULT_CHANNEL)
2626

27-
# BUNDLE_IMG defines the image:tag used for the bundle.
27+
# IMAGE_TAG_BASE defines the docker.io namespace and part of the image name for remote images.
28+
# This variable is used to construct full image tags for bundle and catalog images.
29+
#
30+
# For example, running 'make bundle-build bundle-push catalog-build catalog-push' will build and push both
31+
# example.com/memcached-operator-bundle:$VERSION and example.com/memcached-operator-catalog:$VERSION.
32+
IMAGE_TAG_BASE ?= example.com/memcached-operator
33+
34+
# BUNDLE_IMG defines the image:tag used for the bundle.
2835
# You can use it as an arg. (E.g make bundle-build BUNDLE_IMG=<some-registry>/<project-name-bundle>:<tag>)
29-
BUNDLE_IMG ?= controller-bundle:$(VERSION)
36+
BUNDLE_IMG ?= $(IMAGE_TAG_BASE)-bundle:v$(VERSION)
3037

3138
# Image URL to use all building/pushing image targets
3239
IMG ?= controller:latest
@@ -138,3 +145,51 @@ bundle: manifests kustomize
138145
.PHONY: bundle-build
139146
bundle-build:
140147
docker build -f bundle.Dockerfile -t $(BUNDLE_IMG) .
148+
149+
# Push the bundle image.
150+
.PHONY: bundle-push
151+
bundle-push:
152+
$(MAKE) docker-push IMG=$(BUNDLE_IMG)
153+
154+
# Download opm locally if necessary.
155+
.PHONY: opm
156+
OPM = ./bin/opm
157+
opm:
158+
ifeq (,$(wildcard $(OPM)))
159+
ifeq (,$(shell which opm 2>/dev/null))
160+
@{ \
161+
set -e ;\
162+
mkdir -p $(dir $(OPM)) ;\
163+
OS=$$(uname -s | tr '[:upper:]' '[:lower:]') && \
164+
ARCH=$$(case $$(arch) in x86_64) echo -n amd64 ;; aarch64) echo -n arm64 ;; *) echo -n $$(arch) ;; esac) && \
165+
curl -sSLo $(OPM) https://github.com/operator-framework/operator-registry/releases/download/v1.15.1/$${OS}-$${ARCH}-opm ;\
166+
chmod +x $(OPM) ;\
167+
}
168+
else
169+
OPM = $(shell which opm)
170+
endif
171+
endif
172+
173+
# 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).
174+
# These images MUST exist in a registry and be pull-able.
175+
BUNDLE_IMGS ?= $(BUNDLE_IMG)
176+
177+
# The image tag given to the resulting catalog image (e.g. make catalog-build CATALOG_IMG=example.com/operator-catalog:v0.2.0).
178+
CATALOG_IMG ?= $(IMAGE_TAG_BASE)-catalog:v$(VERSION)
179+
180+
# Set CATALOG_BASE_IMG to an existing catalog image tag to add $BUNDLE_IMGS to that image.
181+
ifneq ($(origin CATALOG_BASE_IMG), undefined)
182+
FROM_INDEX_OPT := --from-index $(CATALOG_BASE_IMG)
183+
endif
184+
185+
# Build a catalog image by adding bundle images to an empty catalog using the operator package manager tool, 'opm'.
186+
# This recipe invokes 'opm' in 'semver' bundle add mode. For more information on add modes, see:
187+
# https://github.com/operator-framework/community-operators/blob/master/docs/contributing.md#updating-your-existing-operator
188+
.PHONY: catalog-build
189+
catalog-build: opm
190+
$(OPM) index add --mode semver --tag $(CATALOG_IMG) --bundles $(BUNDLE_IMGS) $(FROM_INDEX_OPT)
191+
192+
# Push the catalog image.
193+
.PHONY: catalog-push
194+
catalog-push:
195+
$(MAKE) docker-push IMG=$(CATALOG_IMG)

0 commit comments

Comments
 (0)