Skip to content

Commit 693a355

Browse files
author
Per Goncalves da Silva
committed
Add controller image build and publish support to test registry
Signed-off-by: Per Goncalves da Silva <pegoncal@redhat.com>
1 parent fd8f6a4 commit 693a355

File tree

3 files changed

+92
-15
lines changed

3 files changed

+92
-15
lines changed

Makefile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,18 @@ test-unit: $(SETUP_ENVTEST) envtest-k8s-bins #HELP Run the unit tests
246246
$(UNIT_TEST_DIRS) \
247247
-test.gocoverdir=$(COVERAGE_UNIT_DIR)
248248

249+
TEST_OPERATOR_CONTROLLERS_HOME=./testdata/images/controllers
250+
TEST_OPERATOR_CONTROLLERS=v1.0.0 v2.0.0
251+
252+
.PHONY: $(TEST_OPERATOR_CONTROLLERS)
253+
$(TEST_OPERATOR_CONTROLLERS):
254+
go build $(GO_BUILD_FLAGS) $(GO_BUILD_EXTRA_FLAGS) -tags '$(GO_BUILD_TAGS)' -ldflags '$(GO_BUILD_LDFLAGS)' -gcflags '$(GO_BUILD_GCFLAGS)' -asmflags '$(GO_BUILD_ASMFLAGS)' -o $(TEST_OPERATOR_CONTROLLERS_HOME)/test-operator/$@/manager ./testdata/images/bundles/test-operator/$@/cmd/main.go
255+
249256
.PHONY: image-registry
250257
E2E_REGISTRY_IMAGE=localhost/e2e-test-registry:devel
251258
image-registry: export GOOS=linux
252259
image-registry: export GOARCH=amd64
253-
image-registry: ## Build the testdata catalog used for e2e tests and push it to the image registry
260+
image-registry: $(TEST_OPERATOR_CONTROLLERS) ## Build the testdata catalog used for e2e tests and push it to the image registry
254261
go build $(GO_BUILD_FLAGS) $(GO_BUILD_EXTRA_FLAGS) -tags '$(GO_BUILD_TAGS)' -ldflags '$(GO_BUILD_LDFLAGS)' -gcflags '$(GO_BUILD_GCFLAGS)' -asmflags '$(GO_BUILD_ASMFLAGS)' -o ./testdata/push/bin/push ./testdata/push/push.go
255262
$(CONTAINER_RUNTIME) build -f ./testdata/Dockerfile -t $(E2E_REGISTRY_IMAGE) ./testdata
256263
$(CONTAINER_RUNTIME) save $(E2E_REGISTRY_IMAGE) | $(KIND) load image-archive /dev/stdin --name $(KIND_CLUSTER_NAME)

testdata/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
push/bin
2+
images/controllers

testdata/push/push.go

Lines changed: 83 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
11
package main
22

33
import (
4+
"archive/tar"
5+
"bytes"
46
"flag"
57
"fmt"
8+
"github.com/google/go-containerregistry/pkg/v1/empty"
9+
"github.com/google/go-containerregistry/pkg/v1/tarball"
10+
"io"
611
"io/fs"
712
"log"
813
"os"
14+
"path/filepath"
15+
"sort"
916
"strings"
1017

1118
"github.com/google/go-containerregistry/pkg/crane"
12-
v1 "github.com/google/go-containerregistry/pkg/v1"
19+
"github.com/google/go-containerregistry/pkg/v1"
1320
"github.com/google/go-containerregistry/pkg/v1/mutate"
1421
"github.com/spf13/pflag"
1522
"gopkg.in/yaml.v2"
1623
)
1724

1825
const (
19-
bundlesSubPath string = "bundles"
20-
catalogsSubPath string = "catalogs"
26+
controllersSubPath string = "controllers"
27+
bundlesSubPath string = "bundles"
28+
catalogsSubPath string = "catalogs"
2129
)
2230

2331
func main() {
@@ -34,6 +42,7 @@ func main() {
3442

3543
bundlesFullPath := fmt.Sprintf("%s/%s", imagesPath, bundlesSubPath)
3644
catalogsFullPath := fmt.Sprintf("%s/%s", imagesPath, catalogsSubPath)
45+
controllersFullPath := fmt.Sprintf("%s/%s", imagesPath, controllersSubPath)
3746

3847
bundles, err := buildBundles(bundlesFullPath)
3948
if err != nil {
@@ -43,6 +52,10 @@ func main() {
4352
if err != nil {
4453
log.Fatalf("failed to build catalogs: %s", err.Error())
4554
}
55+
controllers, err := buildControllers(controllersFullPath)
56+
if err != nil {
57+
log.Fatalf("failed to build controllers: %s", err.Error())
58+
}
4659
// Push the images
4760
for name, image := range bundles {
4861
dest := fmt.Sprintf("%s/%s", registryAddr, name)
@@ -58,6 +71,13 @@ func main() {
5871
log.Fatalf("failed to push catalog images: %s", err.Error())
5972
}
6073
}
74+
for name, image := range controllers {
75+
dest := fmt.Sprintf("%s/%s", registryAddr, name)
76+
log.Printf("pushing controller %s to %s", name, dest)
77+
if err := crane.Push(image, dest); err != nil {
78+
log.Fatalf("failed to push controller images: %s", err.Error())
79+
}
80+
}
6181
log.Printf("finished")
6282
os.Exit(0)
6383
}
@@ -122,6 +142,27 @@ func buildCatalogs(path string) (map[string]v1.Image, error) {
122142
return mutatedMap, nil
123143
}
124144

145+
func buildControllers(path string) (map[string]v1.Image, error) {
146+
controllers, err := processImageDirTree(path)
147+
if err != nil {
148+
return nil, err
149+
}
150+
mutatedMap := make(map[string]v1.Image, 0)
151+
// Apply required catalog label
152+
for key, img := range controllers {
153+
cfg := v1.Config{
154+
WorkingDir: "/",
155+
Entrypoint: []string{"/manager"},
156+
User: "65532:65532",
157+
}
158+
mutatedMap[fmt.Sprintf("controllers/%s", key)], err = mutate.Config(img, cfg)
159+
if err != nil {
160+
return nil, fmt.Errorf("failed to apply image labels: %w", err)
161+
}
162+
}
163+
return mutatedMap, nil
164+
}
165+
125166
func processImageDirTree(path string) (map[string]v1.Image, error) {
126167
imageMap := make(map[string]v1.Image, 0)
127168
images, err := os.ReadDir(path)
@@ -145,38 +186,66 @@ func processImageDirTree(path string) (map[string]v1.Image, error) {
145186
continue
146187
}
147188
tagFullPath := fmt.Sprintf("%s/%s", entryFullPath, tag.Name())
189+
b := &bytes.Buffer{}
190+
w := tar.NewWriter(b)
148191

149-
var fileMap map[string][]byte
150-
fileMap, err = createFileMap(tagFullPath)
192+
files, err := collectFiles(tagFullPath)
151193
if err != nil {
152194
return nil, fmt.Errorf("failed to read files for image: %w", err)
153195
}
196+
sort.Strings(files)
197+
198+
for _, f := range files {
199+
filePath := filepath.Join(tagFullPath, f)
200+
fileBytes, err := os.ReadFile(filePath)
201+
if err != nil {
202+
return nil, fmt.Errorf("failed to read file %q for image: %w", filePath, err)
203+
}
204+
if err := w.WriteHeader(&tar.Header{
205+
Name: f,
206+
Mode: 0755,
207+
Size: int64(len(fileBytes)),
208+
}); err != nil {
209+
return nil, err
210+
}
211+
if _, err := w.Write(fileBytes); err != nil {
212+
return nil, err
213+
}
214+
}
215+
if err := w.Close(); err != nil {
216+
return nil, err
217+
}
218+
219+
// Return a new copy of the buffer each time it's opened.
220+
layer, err := tarball.LayerFromOpener(func() (io.ReadCloser, error) {
221+
return io.NopCloser(bytes.NewBuffer(b.Bytes())), nil
222+
})
223+
if err != nil {
224+
return nil, fmt.Errorf("failed to create image layer: %w", err)
225+
}
154226

155-
image, err := crane.Image(fileMap)
227+
image, err := mutate.AppendLayers(empty.Image, layer)
156228
if err != nil {
157-
return nil, fmt.Errorf("failed to generate image: %w", err)
229+
return nil, fmt.Errorf("failed to append layer to image: %w", err)
158230
}
159231
imageMap[fmt.Sprintf("%s:%s", entry.Name(), tag.Name())] = image
160232
}
161233
}
162234
return imageMap, nil
163235
}
164236

165-
func createFileMap(originPath string) (map[string][]byte, error) {
166-
fileMap := make(map[string][]byte)
237+
func collectFiles(originPath string) ([]string, error) {
238+
var files []string
167239
if err := fs.WalkDir(os.DirFS(originPath), ".", func(path string, d fs.DirEntry, err error) error {
168240
if err != nil {
169241
return err
170242
}
171243
if d != nil && !d.IsDir() {
172-
fileMap[path], err = os.ReadFile(fmt.Sprintf("%s/%s", originPath, path))
173-
if err != nil {
174-
return err
175-
}
244+
files = append(files, path)
176245
}
177246
return nil
178247
}); err != nil {
179248
return nil, err
180249
}
181-
return fileMap, nil
250+
return files, nil
182251
}

0 commit comments

Comments
 (0)