Skip to content

Commit a26e609

Browse files
committed
Unpack Image
Builds and loads the unpacker binary into the operator-controller image so we don't need to use the rukpak image. Signed-off-by: dtfranz <[email protected]>
1 parent 8afcb15 commit a26e609

File tree

6 files changed

+152
-8
lines changed

6 files changed

+152
-8
lines changed

Dockerfile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ FROM gcr.io/distroless/static:nonroot
66
WORKDIR /
77

88
COPY manager manager
9+
COPY unpack unpack
910

1011
EXPOSE 8080
1112

1213
USER 65532:65532
13-
14-
ENTRYPOINT ["/manager"]

Makefile

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,22 +203,24 @@ export GO_BUILD_GCFLAGS ?= all=-trimpath=${PWD}
203203
export GO_BUILD_TAGS ?= upstream
204204
export GO_BUILD_FLAGS ?=
205205

206-
BUILDCMD = go build $(GO_BUILD_FLAGS) -tags '$(GO_BUILD_TAGS)' -ldflags '$(GO_BUILD_LDFLAGS)' -gcflags '$(GO_BUILD_GCFLAGS)' -asmflags '$(GO_BUILD_ASMFLAGS)' -o $(BUILDBIN)/manager ./cmd/manager
206+
BINARIES=manager unpack
207+
208+
$(BINARIES):
209+
go build $(GO_BUILD_FLAGS) -tags '$(GO_BUILD_TAGS)' -ldflags '$(GO_BUILD_LDFLAGS)' -gcflags '$(GO_BUILD_GCFLAGS)' -asmflags '$(GO_BUILD_ASMFLAGS)' -o $(BUILDBIN)/$@ ./cmd/$@
207210

208211
.PHONY: build-deps
209212
build-deps: manifests generate fmt vet
210213

211214
.PHONY: build go-build-local
212215
build: build-deps go-build-local #HELP Build manager binary for current GOOS and GOARCH. Default target.
213216
go-build-local: BUILDBIN = bin
214-
go-build-local:
215-
$(BUILDCMD)
217+
go-build-local: $(BINARIES)
216218

217219
.PHONY: build-linux go-build-linux
218220
build-linux: build-deps go-build-linux #EXHELP Build manager binary for GOOS=linux and local GOARCH.
219221
go-build-linux: BUILDBIN = bin/linux
220-
go-build-linux:
221-
GOOS=linux $(BUILDCMD)
222+
go-build-linux: GOOS=linux
223+
go-build-linux: $(BINARIES)
222224

223225
.PHONY: run
224226
run: docker-build kind-cluster kind-load kind-deploy #HELP Build the operator-controller then deploy it into a new kind cluster.

cmd/unpack/main.go

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package main
2+
3+
import (
4+
"archive/tar"
5+
"bytes"
6+
"compress/gzip"
7+
"encoding/json"
8+
"fmt"
9+
"io"
10+
"io/fs"
11+
"log"
12+
"os"
13+
"path/filepath"
14+
15+
"github.com/spf13/cobra"
16+
"k8s.io/apimachinery/pkg/util/sets"
17+
)
18+
19+
func main() {
20+
var bundleDir string
21+
var opConVersion bool
22+
23+
skipRootPaths := sets.NewString(
24+
"/dev",
25+
"/etc",
26+
"/proc",
27+
"/product_name",
28+
"/product_uuid",
29+
"/sys",
30+
"/bin",
31+
)
32+
cmd := &cobra.Command{
33+
Use: "unpack",
34+
Args: cobra.ExactArgs(0),
35+
RunE: func(cmd *cobra.Command, _ []string) error {
36+
if opConVersion {
37+
// TODO
38+
//fmt.Println(version.String())
39+
os.Exit(0)
40+
}
41+
var err error
42+
bundleDir, err = filepath.Abs(bundleDir)
43+
if err != nil {
44+
log.Fatalf("get absolute path of bundle directory %q: %v", bundleDir, err)
45+
}
46+
47+
bundleFS := os.DirFS(bundleDir)
48+
buf := &bytes.Buffer{}
49+
gzw := gzip.NewWriter(buf)
50+
tw := tar.NewWriter(gzw)
51+
if err := fs.WalkDir(bundleFS, ".", func(path string, d fs.DirEntry, err error) error {
52+
if err != nil {
53+
return err
54+
}
55+
56+
if d.Type()&os.ModeSymlink != 0 {
57+
return nil
58+
}
59+
if bundleDir == "/" {
60+
// If bundleDir is the filesystem root, skip some known unrelated directories
61+
fullPath := filepath.Join(bundleDir, path)
62+
if skipRootPaths.Has(fullPath) {
63+
return filepath.SkipDir
64+
}
65+
}
66+
info, err := d.Info()
67+
if err != nil {
68+
return fmt.Errorf("get file info for %q: %v", path, err)
69+
}
70+
71+
h, err := tar.FileInfoHeader(info, "")
72+
if err != nil {
73+
return fmt.Errorf("build tar file info header for %q: %v", path, err)
74+
}
75+
h.Uid = 0
76+
h.Gid = 0
77+
h.Uname = ""
78+
h.Gname = ""
79+
h.Name = path
80+
81+
if err := tw.WriteHeader(h); err != nil {
82+
return fmt.Errorf("write tar header for %q: %v", path, err)
83+
}
84+
if d.IsDir() {
85+
return nil
86+
}
87+
f, err := bundleFS.Open(path)
88+
if err != nil {
89+
return fmt.Errorf("open file %q: %v", path, err)
90+
}
91+
if _, err := io.Copy(tw, f); err != nil {
92+
return fmt.Errorf("write tar data for %q: %v", path, err)
93+
}
94+
return nil
95+
}); err != nil {
96+
log.Fatalf("generate tar.gz for bundle dir %q: %v", bundleDir, err)
97+
}
98+
if err := tw.Close(); err != nil {
99+
log.Fatal(err)
100+
}
101+
if err := gzw.Close(); err != nil {
102+
log.Fatal(err)
103+
}
104+
105+
bundleMap := map[string]interface{}{
106+
"content": buf.Bytes(),
107+
}
108+
enc := json.NewEncoder(os.Stdout)
109+
if err := enc.Encode(bundleMap); err != nil {
110+
log.Fatalf("encode bundle map as JSON: %v", err)
111+
}
112+
return nil
113+
},
114+
}
115+
cmd.Flags().StringVar(&bundleDir, "bundle-dir", "", "directory in which the bundle can be found")
116+
cmd.Flags().BoolVar(&opConVersion, "version", false, "displays operator-controller version information")
117+
118+
if err := cmd.Execute(); err != nil {
119+
log.Fatal(err)
120+
}
121+
}

config/manager/kustomization.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,23 @@ images:
66
- name: controller
77
newName: quay.io/operator-framework/operator-controller
88
newTag: devel
9+
replacements:
10+
- source: # replaces UNPACK_IMAGE in manager.yaml with image set by kustomize above
11+
kind: Deployment
12+
group: apps
13+
version: v1
14+
name: controller-manager
15+
namespace: system
16+
fieldPath: spec.template.spec.containers.[name=manager].image
17+
targets:
18+
- select:
19+
kind: Deployment
20+
group: apps
21+
version: v1
22+
name: controller-manager
23+
namespace: system
24+
fieldPaths:
25+
- spec.template.spec.containers.[name=manager].args.0
26+
options:
27+
delimiter: "="
28+
index: 1

config/manager/manager.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ spec:
5252
- command:
5353
- /manager
5454
args:
55+
# The unpack-image arg must remain at index 0 for the kustomize replacement to work
56+
- "--unpack-image=UNPACK_IMAGE"
5557
- "--health-probe-bind-address=:8081"
5658
- "--metrics-bind-address=127.0.0.1:8080"
5759
- "--leader-elect"

internal/rukpak/util/util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
// TODO verify these
1212
const (
1313
DefaultSystemNamespace = "operator-controller-system"
14-
DefaultUnpackImage = "quay.io/operator-framework/rukpak:main"
14+
DefaultUnpackImage = "quay.io/operator-framework/operator-controller:latest"
1515
)
1616

1717
func MergeMaps(maps ...map[string]string) map[string]string {

0 commit comments

Comments
 (0)