Skip to content

Commit 7b7b8e0

Browse files
committed
Address follow-ups: metadata reading, cert/key path for registry, remove crane.Insecure
Signed-off-by: dtfranz <[email protected]>
1 parent 66e4307 commit 7b7b8e0

File tree

7 files changed

+46
-35
lines changed

7 files changed

+46
-35
lines changed

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ test-unit: $(SETUP_ENVTEST) #HELP Run the unit tests
167167

168168
E2E_REGISTRY_IMAGE=localhost/e2e-test-registry:devel
169169
image-registry: ## Build the testdata catalog used for e2e tests and push it to the image registry
170-
go build -o ./testdata/registry/bin/registry ./testdata/registry/registry.go
171-
go build -o ./testdata/push/bin/push ./testdata/push/push.go
170+
go build $(GO_BUILD_FLAGS) -tags '$(GO_BUILD_TAGS)' -ldflags '$(GO_BUILD_LDFLAGS)' -gcflags '$(GO_BUILD_GCFLAGS)' -asmflags '$(GO_BUILD_ASMFLAGS)' -o ./testdata/registry/bin/registry ./testdata/registry/registry.go
171+
go build $(GO_BUILD_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
172172
$(CONTAINER_RUNTIME) build -f ./testdata/Dockerfile -t $(E2E_REGISTRY_IMAGE) ./testdata
173173
$(CONTAINER_RUNTIME) save $(E2E_REGISTRY_IMAGE) | $(KIND) load image-archive /dev/stdin --name $(KIND_CLUSTER_NAME)
174174
./testdata/build-test-registry.sh $(E2E_REGISTRY_NAMESPACE) $(E2E_REGISTRY_NAME) $(E2E_REGISTRY_IMAGE)

testdata/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from gcr.io/distroless/static:debug-nonroot
1+
from gcr.io/distroless/static:nonroot
22

33
WORKDIR /
44

testdata/build-test-registry.sh

+3-5
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,14 @@ spec:
122122
command:
123123
- /push
124124
args:
125-
- "--registry-address=${name}:5000"
125+
- "--registry-address=${name}.${namespace}.svc:5000"
126126
- "--images-path=/images"
127127
volumeMounts:
128128
- name: certs-vol
129129
mountPath: "/certs"
130130
env:
131-
- name: REGISTRY_HTTP_TLS_CERTIFICATE
132-
value: "/certs/tls.crt"
133-
- name: REGISTRY_HTTP_TLS_KEY
134-
value: "/certs/tls.key"
131+
- name: SSL_CERT_DIR
132+
value: "/certs/"
135133
volumes:
136134
- name: certs-vol
137135
secret:

testdata/push/README.md

+16-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ $ tree ./testdata/images/
1515
./testdata/images/
1616
├── bundles
1717
│   └── prometheus-operator
18-
│   └── v2.0.0
18+
│   └── v1.0.0
19+
│ ├── metadata
20+
│       │ └── annotations.yaml
1921
│      └── manifests
2022
│       └── example.yaml
2123
└── catalogs
@@ -29,4 +31,16 @@ $ tree ./testdata/images/
2931
```
3032
The inside of each tag folder will be placed directly into `/` of the built container i.e. `test-catalog:v1` will have `/configs/catalog.yaml`.
3133

32-
To add a new image or tag to the tool, simply create the folders required and populate them with the files to be mounted - no other action should be necessary.
34+
To add a new image or tag for the tool, create the folders required and populate them with the files to be mounted. Bundle images requiring metadata should contain a `metadata` folder with `annotations.yaml`. Example content:
35+
```yaml
36+
annotations:
37+
# Core bundle annotations.
38+
operators.operatorframework.io.bundle.mediatype.v1: registry+v1
39+
operators.operatorframework.io.bundle.manifests.v1: manifests/
40+
operators.operatorframework.io.bundle.metadata.v1: metadata/
41+
operators.operatorframework.io.bundle.package.v1: prometheus
42+
operators.operatorframework.io.bundle.channels.v1: beta
43+
operators.operatorframework.io.metrics.builder: operator-sdk-v1.28.0
44+
operators.operatorframework.io.metrics.mediatype.v1: metrics+v1
45+
operators.operatorframework.io.metrics.project_layout: unknown
46+
```

testdata/push/push.go

+17-18
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io/fs"
77
"log"
88
"os"
9+
"strings"
910

1011
"github.com/google/go-containerregistry/pkg/crane"
1112
v1 "github.com/google/go-containerregistry/pkg/v1"
@@ -29,7 +30,7 @@ func main() {
2930
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
3031
pflag.Parse()
3132

32-
log.Printf("registry configured with path %s, listening on %s", imagesPath, registryAddr)
33+
log.Printf("push operation configured with images path %s and destination %s", imagesPath, registryAddr)
3334

3435
bundlesFullPath := fmt.Sprintf("%s/%s", imagesPath, bundlesSubPath)
3536
catalogsFullPath := fmt.Sprintf("%s/%s", imagesPath, catalogsSubPath)
@@ -42,16 +43,14 @@ func main() {
4243
if err != nil {
4344
log.Fatalf("failed to build catalogs: %s", err.Error())
4445
}
45-
4646
// Push the images
47-
// TODO without insecure option
4847
for name, image := range bundles {
49-
if err := crane.Push(image, fmt.Sprintf("%s/%s", registryAddr, name), crane.Insecure); err != nil {
48+
if err := crane.Push(image, fmt.Sprintf("%s/%s", registryAddr, name)); err != nil {
5049
log.Fatalf("failed to push bundle images: %s", err.Error())
5150
}
5251
}
5352
for name, image := range catalogs {
54-
if err := crane.Push(image, fmt.Sprintf("%s/%s", registryAddr, name), crane.Insecure); err != nil {
53+
if err := crane.Push(image, fmt.Sprintf("%s/%s", registryAddr, name)); err != nil {
5554
log.Fatalf("failed to push catalog images: %s", err.Error())
5655
}
5756
}
@@ -60,26 +59,25 @@ func main() {
6059
}
6160

6261
func buildBundles(path string) (map[string]v1.Image, error) {
63-
bundles, err := processImageDirTree(path, "bundles/registry-v1/")
62+
bundles, err := processImageDirTree(path)
6463
if err != nil {
6564
return nil, err
6665
}
66+
mutatedMap := make(map[string]v1.Image, 0)
6767
// Apply required bundle labels
6868
for key, img := range bundles {
69-
//TODO
70-
labels, err := getBundleLabels(fmt.Sprintf("%s/%s", path, "prometheus-operator/v1.0.0/metadata/annotations.yaml"))
69+
// Replace ':' between image name and image tag for file path
70+
metadataPath := strings.Replace(key, ":", "/", 1)
71+
labels, err := getBundleLabels(fmt.Sprintf("%s/%s/%s", path, metadataPath, "metadata/annotations.yaml"))
7172
if err != nil {
7273
return nil, err
7374
}
74-
cfg := v1.Config{
75-
Labels: labels,
76-
}
77-
bundles[key], err = mutate.Config(img, cfg)
75+
mutatedMap[fmt.Sprintf("bundles/registry-v1/%s", key)], err = mutate.Config(img, v1.Config{Labels: labels})
7876
if err != nil {
7977
return nil, fmt.Errorf("failed to apply image labels: %w", err)
8078
}
8179
}
82-
return bundles, nil
80+
return mutatedMap, nil
8381
}
8482

8583
type bundleAnnotations struct {
@@ -100,26 +98,27 @@ func getBundleLabels(path string) (map[string]string, error) {
10098
}
10199

102100
func buildCatalogs(path string) (map[string]v1.Image, error) {
103-
catalogs, err := processImageDirTree(path, "e2e/")
101+
catalogs, err := processImageDirTree(path)
104102
if err != nil {
105103
return nil, err
106104
}
105+
mutatedMap := make(map[string]v1.Image, 0)
107106
// Apply required catalog label
108107
for key, img := range catalogs {
109108
cfg := v1.Config{
110109
Labels: map[string]string{
111110
"operators.operatorframework.io.index.configs.v1": "/configs",
112111
},
113112
}
114-
catalogs[key], err = mutate.Config(img, cfg)
113+
mutatedMap[fmt.Sprintf("e2e/%s", key)], err = mutate.Config(img, cfg)
115114
if err != nil {
116115
return nil, fmt.Errorf("failed to apply image labels: %w", err)
117116
}
118117
}
119-
return catalogs, nil
118+
return mutatedMap, nil
120119
}
121120

122-
func processImageDirTree(path string, repoPrefix string) (map[string]v1.Image, error) {
121+
func processImageDirTree(path string) (map[string]v1.Image, error) {
123122
imageMap := make(map[string]v1.Image, 0)
124123
images, err := os.ReadDir(path)
125124
if err != nil {
@@ -153,7 +152,7 @@ func processImageDirTree(path string, repoPrefix string) (map[string]v1.Image, e
153152
if err != nil {
154153
return nil, fmt.Errorf("failed to generate image: %w", err)
155154
}
156-
imageMap[fmt.Sprintf("%s%s:%s", repoPrefix, entry.Name(), tag.Name())] = image
155+
imageMap[fmt.Sprintf("%s:%s", entry.Name(), tag.Name())] = image
157156
}
158157
}
159158
return imageMap, nil

testdata/registry/README.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ Usage of registry:
88
--registry-address string The address the registry binds to. (default ":12345")
99
```
1010

11-
<!--TODO makes these arguments-->
12-
The server key and cert should be placed in:
11+
The server key and cert locations should be set under the following environment variables:
1312
```
14-
certPath = "/certs/tls.crt"
15-
keyPath = "/certs/tls.key"
13+
REGISTRY_HTTP_TLS_CERTIFICATE
14+
REGISTRY_HTTP_TLS_KEY
1615
```

testdata/registry/registry.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ import (
44
"flag"
55
"log"
66
"net/http"
7+
"os"
78
"time"
89

910
"github.com/google/go-containerregistry/pkg/registry"
1011
"github.com/spf13/pflag"
1112
)
1213

1314
const (
14-
certPath = "/certs/tls.crt"
15-
keyPath = "/certs/tls.key"
15+
certEnv = "REGISTRY_HTTP_TLS_CERTIFICATE"
16+
keyEnv = "REGISTRY_HTTP_TLS_KEY"
1617
)
1718

1819
func main() {
@@ -30,7 +31,7 @@ func main() {
3031
WriteTimeout: 60 * time.Second,
3132
}
3233

33-
err := s.ListenAndServeTLS(certPath, keyPath)
34+
err := s.ListenAndServeTLS(os.Getenv(certEnv), os.Getenv(keyEnv))
3435
if err != nil {
3536
log.Fatalf("failed to start image registry: %s", err.Error())
3637
}

0 commit comments

Comments
 (0)