Skip to content

Commit 8853d20

Browse files
committed
(manifests/v1) add 'opm index add' and 'opm' download targets to Makefile
1 parent 34a55dd commit 8853d20

File tree

5 files changed

+341
-31
lines changed

5 files changed

+341
-31
lines changed

internal/plugins/manifests/init.go

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

2325
"sigs.k8s.io/kubebuilder/v3/pkg/config"
2426
cfgv3 "sigs.k8s.io/kubebuilder/v3/pkg/config/v3"
2527

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

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

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

5162
// 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...)
5372

5473
// Append bundle recipes.
55-
operatorType := projutil.PluginKeyToOperatorType(cfg.GetLayout())
5674
switch operatorType {
57-
case projutil.OperatorTypeUnknown:
58-
return fmt.Errorf("unsupported plugin key %q", cfg.GetLayout())
5975
case projutil.OperatorTypeGo:
6076
makefileBytes = append(makefileBytes, []byte(makefileBundleFragmentGo)...)
6177
default:
6278
makefileBytes = append(makefileBytes, []byte(makefileBundleFragmentNonGo)...)
6379
}
80+
makefileBytes = append(makefileBytes, []byte(makefileBundleBuildPushFragment)...)
6481

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

6791
var mode os.FileMode = 0644
6892
if info, err := os.Stat(filePath); err != nil {
@@ -73,14 +97,14 @@ func initUpdateMakefile(cfg config.Config, filePath string) error {
7397

7498
// Makefile fragments to add to the base Makefile.
7599
const (
76-
makefileBundleVarFragment = `# VERSION defines the project version for the bundle.
100+
makefileBundleVarFragment = `# VERSION defines the project version for the bundle.
77101
# Update this value when you upgrade the version of your project.
78102
# To re-generate a bundle for another specific version without changing the standard setup, you can:
79103
# - use the VERSION as arg of the bundle target (e.g make bundle VERSION=0.0.2)
80104
# - use environment variables to overwrite this value (e.g export VERSION=0.0.2)
81105
VERSION ?= 0.0.1
82106
83-
# CHANNELS define the bundle channels used in the bundle.
107+
# CHANNELS define the bundle channels used in the bundle.
84108
# Add a new line here if you would like to change its default config. (E.g CHANNELS = "preview,fast,stable")
85109
# To re-generate a bundle for other specific channels without changing the standard setup, you can:
86110
# - 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)
89113
BUNDLE_CHANNELS := --channels=$(CHANNELS)
90114
endif
91115
92-
# DEFAULT_CHANNEL defines the default channel used in the bundle.
116+
# DEFAULT_CHANNEL defines the default channel used in the bundle.
93117
# Add a new line here if you would like to change its default config. (E.g DEFAULT_CHANNEL = "stable")
94118
# To re-generate a bundle for any other default channel without changing the default setup, you can:
95119
# - 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)
99123
endif
100124
BUNDLE_METADATA_OPTS ?= $(BUNDLE_CHANNELS) $(BUNDLE_DEFAULT_CHANNEL)
101125
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.
103134
# 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)
105136
`
106137

107138
makefileBundleFragmentGo = `
@@ -122,9 +153,78 @@ bundle: kustomize
122153
operator-sdk bundle validate ./bundle
123154
`
124155

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

testdata/ansible/memcached-operator/Makefile

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

testdata/go/v2/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
@@ -148,3 +155,49 @@ bundle: manifests kustomize
148155
.PHONY: bundle-build ## Build the bundle image.
149156
bundle-build:
150157
docker build -f bundle.Dockerfile -t $(BUNDLE_IMG) .
158+
159+
.PHONY: bundle-push ## Push the bundle image.
160+
bundle-push:
161+
$(MAKE) docker-push IMG=$(BUNDLE_IMG)
162+
163+
# Download opm locally if necessary.
164+
.PHONY: opm
165+
OPM = ./bin/opm
166+
opm:
167+
ifeq (,$(wildcard $(OPM)))
168+
ifeq (,$(shell which opm 2>/dev/null))
169+
@{ \
170+
set -e ;\
171+
mkdir -p $(dir $(OPM)) ;\
172+
OS=$(shell go env GOOS) && ARCH=$(shell go env GOARCH) && \
173+
curl -sSLo $(OPM) https://github.com/operator-framework/operator-registry/releases/download/v1.15.1/$${OS}-$${ARCH}-opm ;\
174+
chmod +x $(OPM) ;\
175+
}
176+
else
177+
OPM = $(shell which opm)
178+
endif
179+
endif
180+
181+
# 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).
182+
# These images MUST exist in a registry and be pull-able.
183+
BUNDLE_IMGS ?= $(BUNDLE_IMG)
184+
185+
# The image tag given to the resulting catalog image (e.g. make catalog-build CATALOG_IMG=example.com/operator-catalog:v0.2.0).
186+
CATALOG_IMG ?= $(IMAGE_TAG_BASE)-catalog:v$(VERSION)
187+
188+
# Set CATALOG_BASE_IMG to an existing catalog image tag to add $BUNDLE_IMGS to that image.
189+
ifneq ($(origin CATALOG_BASE_IMG), undefined)
190+
FROM_INDEX_OPT := --from-index $(CATALOG_BASE_IMG)
191+
endif
192+
193+
# Build a catalog image by adding bundle images to an empty catalog using the operator package manager tool, 'opm'.
194+
# This recipe invokes 'opm' in 'semver' bundle add mode. For more information on add modes, see:
195+
# https://github.com/operator-framework/community-operators/blob/7f1438c/docs/packaging-operator.md#updating-your-existing-operator
196+
.PHONY: catalog-build
197+
catalog-build: opm
198+
$(OPM) index add --mode semver --tag $(CATALOG_IMG) --bundles $(BUNDLE_IMGS) $(FROM_INDEX_OPT)
199+
200+
# Push the catalog image.
201+
.PHONY: catalog-push
202+
catalog-push:
203+
$(MAKE) docker-push IMG=$(CATALOG_IMG)

0 commit comments

Comments
 (0)