Skip to content

Commit

Permalink
adding gzip content support
Browse files Browse the repository at this point in the history
Signed-off-by: Jordan Keister <jordan@nimblewidget.com>
  • Loading branch information
grokspawn committed Jun 28, 2024
1 parent c0ed3fa commit dce3b39
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 1 deletion.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ KIND_CLUSTER_IMAGE := kindest/node:v1.30.0@sha256:047357ac0cfea04663786a612ba1ea
.PHONY: help
help: ## Display this help.
awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
.DEFAULT_GOAL := help

##@ Development

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/operator-framework/catalogd
go 1.22.0

require (
github.com/NYTimes/gziphandler v1.1.1
github.com/blang/semver/v4 v4.0.0
github.com/containerd/containerd v1.7.18
github.com/go-logr/logr v1.4.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERo
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
github.com/Microsoft/hcsshim v0.12.0-rc.3 h1:5GNGrobGs/sN/0nFO21W9k4lFn+iXXZAE8fCZbmdRak=
github.com/Microsoft/hcsshim v0.12.0-rc.3/go.mod h1:WuNfcaYNaw+KpCEsZCIM6HCEmu0c5HfXpi+dDSmveP0=
github.com/NYTimes/gziphandler v1.1.1 h1:ZUDjpQae29j0ryrS0u/B8HZfJBtBQHjqw2rQ2cqUQ3I=
github.com/NYTimes/gziphandler v1.1.1/go.mod h1:n/CVRwUEOgIxrgPvAQhUUr9oeUtvrhMomdKFjzJNB0c=
github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230305170008-8188dc5388df h1:7RFfzj4SSt6nnvCPbCqijJi1nWCd+TqAT3bYCStRC18=
github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230305170008-8188dc5388df/go.mod h1:pSwJ0fSY5KhvocuWSx4fz3BA8OrA1bQn+K1Eli3BRwM=
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 h1:DklsrG3dyBCFEj5IhUbnKptjxatkF07cF2ak3yi77so=
Expand Down
4 changes: 3 additions & 1 deletion pkg/storage/localdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"os"
"path/filepath"

"github.com/NYTimes/gziphandler"

"github.com/operator-framework/operator-registry/alpha/declcfg"
)

Expand Down Expand Up @@ -56,7 +58,7 @@ func (s LocalDir) ContentURL(catalog string) string {

func (s LocalDir) StorageServerHandler() http.Handler {
mux := http.NewServeMux()
mux.Handle(s.BaseURL.Path, http.StripPrefix(s.BaseURL.Path, http.FileServer(http.FS(&filesOnlyFilesystem{os.DirFS(s.RootDir)}))))
mux.Handle(s.BaseURL.Path, gziphandler.GzipHandler(http.StripPrefix(s.BaseURL.Path, http.FileServer(http.FS(&filesOnlyFilesystem{os.DirFS(s.RootDir)})))))
return mux
}

Expand Down
181 changes: 181 additions & 0 deletions pkg/storage/localdir_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package storage

import (
"bytes"

Check failure on line 4 in pkg/storage/localdir_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s dot -s default -s prefix(github.com/operator-framework) -s prefix(github.com/operator-framework/catalogd) --custom-order (gci)
"context"
"fmt"
"io"
Expand All @@ -15,6 +16,8 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"compress/gzip"

Check failure on line 19 in pkg/storage/localdir_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s dot -s default -s prefix(github.com/operator-framework) -s prefix(github.com/operator-framework/catalogd) --custom-order (gci)

"github.com/google/go-cmp/cmp"

"github.com/operator-framework/operator-registry/alpha/declcfg"
Expand Down Expand Up @@ -132,6 +135,40 @@ var _ = Describe("LocalDir Server Handler tests", func() {
Expect(os.WriteFile(filepath.Join(store.RootDir, "test-catalog", "foo.txt"), expectedContent, 0600)).To(Succeed())
expectFound(fmt.Sprintf("%s/%s", testServer.URL, "/catalogs/test-catalog/foo.txt"), expectedContent)
})
It("ignores accept-encoding for the path /catalogs/test-catalog/all.json with size < 1400 bytes", func() {
expectedContent := []byte("bar")
Expect(os.WriteFile(filepath.Join(store.RootDir, "test-catalog", "all.json"), expectedContent, 0600)).To(Succeed())
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/%s", testServer.URL, "/catalogs/test-catalog/all.json"), nil)
Expect(err).To(Not(HaveOccurred()))
req.Header.Set("Accept-Encoding", "gzip")
resp, err := http.DefaultClient.Do(req)
Expect(err).To(Not(HaveOccurred()))
Expect(resp.StatusCode).To(Equal(http.StatusOK))
Expect(resp.Header.Get("Content-Encoding")).To(Not(Equal("gzip")))
actualContent, err := io.ReadAll(resp.Body)
Expect(err).To(Not(HaveOccurred()))
Expect(actualContent).To(Equal(expectedContent))
Expect(resp.Body.Close()).To(Succeed())
})
It("provides gzipped content for the path /catalogs/test-catalog/all.json with size > 1400 bytes", func() {
expectedContent := []byte(testCompressablePackage)
Expect(os.WriteFile(filepath.Join(store.RootDir, "test-catalog", "all.json"), expectedContent, 0600)).To(Succeed())
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/%s", testServer.URL, "/catalogs/test-catalog/all.json"), nil)
Expect(err).To(Not(HaveOccurred()))
req.Header.Set("Accept-Encoding", "gzip")
resp, err := http.DefaultClient.Do(req)
Expect(err).To(Not(HaveOccurred()))
Expect(resp.StatusCode).To(Equal(http.StatusOK))
Expect(resp.Header.Get("Content-Encoding")).To(Equal("gzip"))
actualContent, err := io.ReadAll(resp.Body)
Expect(err).To(Not(HaveOccurred()))
gz, err := gzip.NewReader(io.NopCloser(io.MultiReader(bytes.NewReader(actualContent))))
Expect(err).To(Not(HaveOccurred()))
decompBytes, err := io.ReadAll(gz)
Expect(err).To(Not(HaveOccurred()))
Expect(decompBytes).To(Equal(expectedContent))
Expect(resp.Body.Close()).To(Succeed())
})
AfterEach(func() {
testServer.Close()
})
Expand Down Expand Up @@ -184,3 +221,147 @@ name: %s
entries:
- name: %s
`

// by default the compressor will only trigger for files larger than 1400 bytes
const testCompressablePackage = `{
"schema": "olm.package",
"name": "cockroachdb",
"defaultChannel": "stable-v6.x",
}
{
"schema": "olm.channel",
"name": "stable-5.x",
"package": "cockroachdb",
"entries": [
{
"name": "cockroachdb.v5.0.3"
},
{
"name": "cockroachdb.v5.0.4",
"replaces": "cockroachdb.v5.0.3"
}
]
}
{
"schema": "olm.channel",
"name": "stable-v6.x",
"package": "cockroachdb",
"entries": [
{
"name": "cockroachdb.v6.0.0",
"skipRange": "<6.0.0"
}
]
}
{
"schema": "olm.bundle",
"name": "cockroachdb.v5.0.3",
"package": "cockroachdb",
"image": "quay.io/openshift-community-operators/cockroachdb@sha256:a5d4f4467250074216eb1ba1c36e06a3ab797d81c431427fc2aca97ecaf4e9d8",
"properties": [
{
"type": "olm.gvk",
"value": {
"group": "charts.operatorhub.io",
"kind": "Cockroachdb",
"version": "v1alpha1"
}
},
{
"type": "olm.package",
"value": {
"packageName": "cockroachdb",
"version": "5.0.3"
}
}
],
"relatedImages": [
{
"name": "",
"image": "gcr.io/kubebuilder/kube-rbac-proxy:v0.5.0"
},
{
"name": "",
"image": "quay.io/helmoperators/cockroachdb:v5.0.3"
},
{
"name": "",
"image": "quay.io/openshift-community-operators/cockroachdb@sha256:a5d4f4467250074216eb1ba1c36e06a3ab797d81c431427fc2aca97ecaf4e9d8"
}
]
}
{
"schema": "olm.bundle",
"name": "cockroachdb.v5.0.4",
"package": "cockroachdb",
"image": "quay.io/openshift-community-operators/cockroachdb@sha256:f42337e7b85a46d83c94694638e2312e10ca16a03542399a65ba783c94a32b63",
"properties": [
{
"type": "olm.gvk",
"value": {
"group": "charts.operatorhub.io",
"kind": "Cockroachdb",
"version": "v1alpha1"
}
},
{
"type": "olm.package",
"value": {
"packageName": "cockroachdb",
"version": "5.0.4"
}
},
],
"relatedImages": [
{
"name": "",
"image": "gcr.io/kubebuilder/kube-rbac-proxy:v0.5.0"
},
{
"name": "",
"image": "quay.io/helmoperators/cockroachdb:v5.0.4"
},
{
"name": "",
"image": "quay.io/openshift-community-operators/cockroachdb@sha256:f42337e7b85a46d83c94694638e2312e10ca16a03542399a65ba783c94a32b63"
}
]
}
{
"schema": "olm.bundle",
"name": "cockroachdb.v6.0.0",
"package": "cockroachdb",
"image": "quay.io/openshift-community-operators/cockroachdb@sha256:d3016b1507515fc7712f9c47fd9082baf9ccb070aaab58ed0ef6e5abdedde8ba",
"properties": [
{
"type": "olm.gvk",
"value": {
"group": "charts.operatorhub.io",
"kind": "Cockroachdb",
"version": "v1alpha1"
}
},
{
"type": "olm.package",
"value": {
"packageName": "cockroachdb",
"version": "6.0.0"
}
},
],
"relatedImages": [
{
"name": "",
"image": "gcr.io/kubebuilder/kube-rbac-proxy:v0.5.0"
},
{
"name": "",
"image": "quay.io/cockroachdb/cockroach-helm-operator:6.0.0"
},
{
"name": "",
"image": "quay.io/openshift-community-operators/cockroachdb@sha256:d3016b1507515fc7712f9c47fd9082baf9ccb070aaab58ed0ef6e5abdedde8ba"
}
]
}
`

0 comments on commit dce3b39

Please sign in to comment.