Skip to content

Commit

Permalink
refactor: Reduce zli binary size (#1805)
Browse files Browse the repository at this point in the history
Signed-off-by: Alexei Dodon <[email protected]>
  • Loading branch information
adodon2go authored Sep 15, 2023
1 parent 8e18917 commit f58597a
Show file tree
Hide file tree
Showing 45 changed files with 166 additions and 155 deletions.
26 changes: 24 additions & 2 deletions .github/workflows/compare-binary-size.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
echo "PR binary size: $BINSIZE Bytes"
echo "main branch binary size: $BINSIZE_MAIN Bytes"
[[ $BINSIZE -eq $BINSIZE_MAIN ]] && echo "zot-minimal binary size is not affected by PR" && exit 0
[[ $BINSIZE -eq $BINSIZE_MAIN ]] && echo "zot-minimal binary size is not affected by PR" && exit 0
if [[ $BINSIZE -gt $BINSIZE_MAIN ]]; then \
PERCENTAGE=$(echo "scale=2; (($BINSIZE-$BINSIZE_MAIN)*100)/$BINSIZE_MAIN" | bc); \
Expand All @@ -58,7 +58,7 @@ jobs:
echo "PR binary size: $BINSIZE Bytes"
echo "main branch binary size: $BINSIZE_MAIN Bytes"
[[ $BINSIZE -eq $BINSIZE_MAIN ]] && echo "zb binary size is not affected by PR" && exit 0
[[ $BINSIZE -eq $BINSIZE_MAIN ]] && echo "zb binary size is not affected by PR" && exit 0
if [[ $BINSIZE -gt $BINSIZE_MAIN ]]; then \
PERCENTAGE=$(echo "scale=2; (($BINSIZE-$BINSIZE_MAIN)*100)/$BINSIZE_MAIN" | bc); \
Expand All @@ -68,6 +68,28 @@ jobs:
PERCENTAGE=$(echo "scale=2; (($BINSIZE_MAIN-$BINSIZE)*100)/$BINSIZE_MAIN" | bc); \
echo "zb binary decreased by $PERCENTAGE% comparing with main"; \
fi
- if: always()
name: Check if zli binary increased with more than 1%
run: |
echo "Building zli and check size"
cd $GITHUB_WORKSPACE
make cli
BINSIZE=$(stat -c%s "bin/zli-linux-amd64")
echo "Building zli on main branch and check size"
cd zot_main
make cli
BINSIZE_MAIN=$(stat -c%s "bin/zli-linux-amd64")
echo "PR binary size: $BINSIZE Bytes"
echo "main branch binary size: $BINSIZE_MAIN Bytes"
[[ $BINSIZE -eq $BINSIZE_MAIN ]] && echo "zli binary size is not affected by PR" && exit 0
if [[ $BINSIZE -gt $BINSIZE_MAIN ]]; then \
PERCENTAGE=$(echo "scale=2; (($BINSIZE-$BINSIZE_MAIN)*100)/$BINSIZE_MAIN" | bc); \
echo "zli binary increased by $PERCENTAGE% comparing with main"; \
if ((`bc <<< "$PERCENTAGE>=1.0"`)); then exit 1; fi; \
else \
PERCENTAGE=$(echo "scale=2; (($BINSIZE_MAIN-$BINSIZE)*100)/$BINSIZE_MAIN" | bc); \
echo "zli binary decreased by $PERCENTAGE% comparing with main"; \
fi
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,12 @@ test: check-skopeo $(TESTDATA) $(ORAS)
rm -rf /tmp/getter*; rm -rf /tmp/trivy*
go test -failfast -tags dev,containers_image_openpgp -v -trimpath -race -cover -coverpkg ./... -coverprofile=coverage-dev-minimal.txt -covermode=atomic ./pkg/test/... ./pkg/storage/... ./pkg/extensions/sync/... -run ^TestInject
rm -rf /tmp/getter*; rm -rf /tmp/trivy*
go test -failfast -tags stress,$(BUILD_LABELS),containers_image_openpgp -v -trimpath -race -timeout 15m ./pkg/cli/stress_test.go
go test -failfast -tags stress,$(BUILD_LABELS),containers_image_openpgp -v -trimpath -race -timeout 15m ./pkg/cli/server/stress_test.go

.PHONY: privileged-test
privileged-test: $(if $(findstring ui,$(BUILD_LABELS)), ui)
privileged-test: check-skopeo $(TESTDATA)
go test -failfast -tags needprivileges,$(BUILD_LABELS),containers_image_openpgp -v -trimpath -race -timeout 15m -cover -coverpkg ./... -coverprofile=coverage-dev-needprivileges.txt -covermode=atomic ./pkg/storage/... ./pkg/cli/... -run ^TestElevatedPrivileges
go test -failfast -tags needprivileges,$(BUILD_LABELS),containers_image_openpgp -v -trimpath -race -timeout 15m -cover -coverpkg ./... -coverprofile=coverage-dev-needprivileges.txt -covermode=atomic ./pkg/storage/local/... ./pkg/cli/client/... -run ^TestElevatedPrivileges

$(TESTDATA): check-skopeo
mkdir -p ${TESTDATA}; \
Expand Down
36 changes: 2 additions & 34 deletions cmd/zb/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package main

import (
"bytes"
crand "crypto/rand"
"encoding/json"
"fmt"
"io"
"log"
"math/big"
"math/rand"
"net/http"
"os"
Expand All @@ -24,6 +22,7 @@ import (
zerr "zotregistry.io/zot/errors"
"zotregistry.io/zot/pkg/common"
testc "zotregistry.io/zot/pkg/test/common"
"zotregistry.io/zot/pkg/test/image-utils"
)

func makeHTTPGetRequest(url string, resultPtr interface{}, client *resty.Client) error {
Expand Down Expand Up @@ -1023,21 +1022,7 @@ func loadOrStore(statusRequests *sync.Map, key string, value int) int { //nolint

// TO DO: replace with pkg/test/images when available.
func getRandomImageConfig() ([]byte, godigest.Digest) {
const maxLen = 16

randomAuthor := randomString(maxLen)

config := ispec.Image{
Platform: ispec.Platform{
Architecture: "amd64",
OS: "linux",
},
RootFS: ispec.RootFS{
Type: "layers",
DiffIDs: []godigest.Digest{},
},
Author: randomAuthor,
}
config := image.GetDefaultConfig()

configBlobContent, err := json.MarshalIndent(&config, "", "\t")
if err != nil {
Expand All @@ -1048,20 +1033,3 @@ func getRandomImageConfig() ([]byte, godigest.Digest) {

return configBlobContent, configBlobDigestRaw
}

func randomString(n int) string {
const letters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-"

ret := make([]byte, n)

for count := 0; count < n; count++ {
num, err := crand.Int(crand.Reader, big.NewInt(int64(len(letters))))
if err != nil {
panic(err)
}

ret[count] = letters[num.Int64()]
}

return string(ret)
}
5 changes: 4 additions & 1 deletion cmd/zli/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
//go:build search
// +build search

package main

import (
"os"

"zotregistry.io/zot/pkg/cli"
cli "zotregistry.io/zot/pkg/cli/client"
)

func main() {
Expand Down
5 changes: 4 additions & 1 deletion cmd/zli/main_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build search
// +build search

package main_test

import (
Expand All @@ -7,7 +10,7 @@ import (

"zotregistry.io/zot/pkg/api"
"zotregistry.io/zot/pkg/api/config"
"zotregistry.io/zot/pkg/cli"
cli "zotregistry.io/zot/pkg/cli/client"
)

func TestIntegration(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/zot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"os"

"zotregistry.io/zot/pkg/cli"
cli "zotregistry.io/zot/pkg/cli/server"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion cmd/zot/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"zotregistry.io/zot/pkg/api"
"zotregistry.io/zot/pkg/api/config"
"zotregistry.io/zot/pkg/cli"
cli "zotregistry.io/zot/pkg/cli/server"
)

func TestIntegration(t *testing.T) {
Expand Down
1 change: 0 additions & 1 deletion codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ ignore:
- "*_gen.go"
- "./pkg/extensions/search/gql_generated"
- "./pkg/extensions/minimal.go"
- "./pkg/cli/minimal.go"
- "./cmd/zb/*.go"
- "./pkg/test/mocks/*.go"
- "./swagger/*.go"
Expand Down
2 changes: 1 addition & 1 deletion pkg/cli/cli.go → pkg/cli/client/cli.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//go:build search
// +build search

package cli
package client

import "github.com/spf13/cobra"

Expand Down
2 changes: 1 addition & 1 deletion pkg/cli/client.go → pkg/cli/client/client.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//go:build search
// +build search

package cli
package client

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion pkg/cli/config_cmd.go → pkg/cli/client/config_cmd.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//go:build search
// +build search

package cli
package client

import (
"errors"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//go:build search
// +build search

package cli //nolint:testpackage
package client

import (
"bytes"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//go:build search
// +build search

package cli //nolint:testpackage
package client

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion pkg/cli/cves_cmd.go → pkg/cli/client/cves_cmd.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//go:build search
// +build search

package cli
package client

import (
"github.com/spf13/cobra"
Expand Down
2 changes: 1 addition & 1 deletion pkg/cli/cves_sub_cmd.go → pkg/cli/client/cves_sub_cmd.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//go:build search
// +build search

package cli
package client

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion pkg/cli/discover.go → pkg/cli/client/discover.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//go:build search
// +build search

package cli
package client

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//go:build search && needprivileges
// +build search,needprivileges

package cli //nolint:testpackage
package client

import (
"bytes"
Expand Down Expand Up @@ -33,7 +33,7 @@ func TestElevatedPrivilegesTLSNewControllerPrivilegedCert(t *testing.T) {
defer exec.Command("rm", "-rf", "/etc/containers/certs.d/127.0.0.1:8089/")

workDir, _ := os.Getwd()
_ = os.Chdir("../../test/data")
_ = os.Chdir("../../../test/data")

clientGlob, _ := filepath.Glob("client.*")
caGlob, _ := filepath.Glob("ca.*")
Expand Down
2 changes: 1 addition & 1 deletion pkg/cli/gql_queries.go → pkg/cli/client/gql_queries.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//go:build search
// +build search

package cli
package client

type GQLField struct {
Name string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//go:build search
// +build search

package cli //nolint:testpackage
package client

import (
"io"
Expand Down
2 changes: 1 addition & 1 deletion pkg/cli/image_cmd.go → pkg/cli/client/image_cmd.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//go:build search
// +build search

package cli
package client

import (
"strconv"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//go:build search
// +build search

package cli //nolint:testpackage
package client

import (
"bytes"
Expand Down Expand Up @@ -2686,11 +2686,11 @@ func makeConfigFile(content string) string {

func getTestSearchConfig(url string, searchService SearchService) searchConfig {
var (
user string = ""
outputFormat string = ""
verbose bool = false
debug bool = false
verifyTLS bool = false
user string
outputFormat string
verbose bool
debug bool
verifyTLS bool
)

return searchConfig{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//go:build search
// +build search

package cli
package client

import (
"fmt"
Expand Down
10 changes: 5 additions & 5 deletions pkg/cli/client_test.go → pkg/cli/client/internal_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//go:build search
// +build search

package cli //nolint:testpackage
package client

import (
"bytes"
Expand Down Expand Up @@ -35,10 +35,10 @@ const (
SecurePort3 = "8090"
username = "test"
passphrase = "test"
ServerCert = "../../test/data/server.cert"
ServerKey = "../../test/data/server.key"
CACert = "../../test/data/ca.crt"
sourceCertsDir = "../../test/data"
ServerCert = "../../../test/data/server.cert"
ServerKey = "../../../test/data/server.key"
CACert = "../../../test/data/ca.crt"
sourceCertsDir = "../../../test/data"
certsDir1 = "/.config/containers/certs.d/127.0.0.1:8088/"
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/cli/repo_cmd.go → pkg/cli/client/repo_cmd.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//go:build search
// +build search

package cli
package client

import (
"github.com/spf13/cobra"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//go:build search
// +build search

package cli //nolint:testpackage
package client

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion pkg/cli/repo_sub_cmd.go → pkg/cli/client/repo_sub_cmd.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//go:build search
// +build search

package cli
package client

import (
"fmt"
Expand Down
Loading

0 comments on commit f58597a

Please sign in to comment.