0
Fork 0
mirror of https://github.com/project-zot/zot.git synced 2024-12-16 21:56:37 -05:00

refactor: Reduce zli binary size (#1805)

Signed-off-by: Alexei Dodon <adodon@cisco.com>
This commit is contained in:
Alexei Dodon 2023-09-16 01:17:01 +03:00 committed by GitHub
parent 8e18917b07
commit f58597ade9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
45 changed files with 166 additions and 155 deletions

View file

@ -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); \
@ -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); \
@ -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

View file

@ -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}; \

View file

@ -2,12 +2,10 @@ package main
import (
"bytes"
crand "crypto/rand"
"encoding/json"
"fmt"
"io"
"log"
"math/big"
"math/rand"
"net/http"
"os"
@ -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 {
@ -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 {
@ -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)
}

View file

@ -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() {

View file

@ -1,3 +1,6 @@
//go:build search
// +build search
package main_test
import (
@ -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) {

View file

@ -3,7 +3,7 @@ package main
import (
"os"
"zotregistry.io/zot/pkg/cli"
cli "zotregistry.io/zot/pkg/cli/server"
)
func main() {

View file

@ -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) {

View file

@ -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"

View file

@ -1,7 +1,7 @@
//go:build search
// +build search
package cli
package client
import "github.com/spf13/cobra"

View file

@ -1,7 +1,7 @@
//go:build search
// +build search
package cli
package client
import (
"bytes"

View file

@ -1,7 +1,7 @@
//go:build search
// +build search
package cli
package client
import (
"errors"

View file

@ -1,7 +1,7 @@
//go:build search
// +build search
package cli //nolint:testpackage
package client
import (
"bytes"

View file

@ -1,7 +1,7 @@
//go:build search
// +build search
package cli //nolint:testpackage
package client
import (
"bytes"

View file

@ -1,7 +1,7 @@
//go:build search
// +build search
package cli
package client
import (
"github.com/spf13/cobra"

View file

@ -1,7 +1,7 @@
//go:build search
// +build search
package cli
package client
import (
"fmt"

View file

@ -1,7 +1,7 @@
//go:build search
// +build search
package cli
package client
import (
"context"

View file

@ -1,7 +1,7 @@
//go:build search && needprivileges
// +build search,needprivileges
package cli //nolint:testpackage
package client
import (
"bytes"
@ -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.*")

View file

@ -1,7 +1,7 @@
//go:build search
// +build search
package cli
package client
type GQLField struct {
Name string

View file

@ -1,7 +1,7 @@
//go:build search
// +build search
package cli //nolint:testpackage
package client
import (
"io"

View file

@ -1,7 +1,7 @@
//go:build search
// +build search
package cli
package client
import (
"strconv"

View file

@ -1,7 +1,7 @@
//go:build search
// +build search
package cli //nolint:testpackage
package client
import (
"bytes"
@ -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{

View file

@ -1,7 +1,7 @@
//go:build search
// +build search
package cli
package client
import (
"fmt"

View file

@ -1,7 +1,7 @@
//go:build search
// +build search
package cli //nolint:testpackage
package client
import (
"bytes"
@ -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/"
)

View file

@ -1,7 +1,7 @@
//go:build search
// +build search
package cli
package client
import (
"github.com/spf13/cobra"

View file

@ -1,7 +1,7 @@
//go:build search
// +build search
package cli //nolint:testpackage
package client
import (
"bytes"

View file

@ -1,7 +1,7 @@
//go:build search
// +build search
package cli
package client
import (
"fmt"

42
pkg/cli/client/root.go Normal file
View file

@ -0,0 +1,42 @@
//go:build search
// +build search
package client
import (
distspec "github.com/opencontainers/distribution-spec/specs-go"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"zotregistry.io/zot/pkg/api/config"
"zotregistry.io/zot/pkg/cli/cmdflags"
)
// "zli" - client-side cli.
func NewCliRootCmd() *cobra.Command {
showVersion := false
rootCmd := &cobra.Command{
Use: "zli",
Short: "`zli`",
Long: "`zli`",
Run: func(cmd *cobra.Command, args []string) {
if showVersion {
log.Info().Str("distribution-spec", distspec.Version).Str("commit", config.Commit).
Str("binary-type", config.BinaryType).Str("go version", config.GoVersion).Msg("version")
} else {
_ = cmd.Usage()
cmd.SilenceErrors = false
}
},
}
rootCmd.SilenceUsage = true
// additional cmds
enableCli(rootCmd)
// "version"
rootCmd.Flags().BoolVarP(&showVersion, cmdflags.VersionFlag, "v", false, "show the version and exit")
return rootCmd
}

View file

@ -0,0 +1,31 @@
//go:build search
// +build search
package client_test
import (
"os"
"testing"
. "github.com/smartystreets/goconvey/convey"
"zotregistry.io/zot/pkg/cli/client"
)
func TestCliUsage(t *testing.T) {
oldArgs := os.Args
defer func() { os.Args = oldArgs }()
Convey("Test usage", t, func(c C) {
os.Args = []string{"cli_test", "help"}
err := client.NewCliRootCmd().Execute()
So(err, ShouldBeNil)
})
Convey("Test version", t, func(c C) {
os.Args = []string{"cli_test", "--version"}
err := client.NewCliRootCmd().Execute()
So(err, ShouldBeNil)
})
}

View file

@ -1,7 +1,7 @@
//go:build search
// +build search
package cli
package client
import (
"github.com/spf13/cobra"

View file

@ -1,7 +1,7 @@
//go:build search
// +build search
package cli //nolint:testpackage
package client
import (
"bytes"

View file

@ -1,7 +1,7 @@
//go:build search
// +build search
package cli
package client
import (
"context"

View file

@ -3,7 +3,7 @@
//
//nolint:dupl
package cli //nolint:testpackage
package client
import (
"bytes"

View file

@ -1,7 +1,7 @@
//go:build search
// +build search
package cli
package client
import (
"fmt"

View file

@ -1,7 +1,7 @@
//go:build search
// +build search
package cli
package client
import (
"context"

View file

@ -1,7 +1,7 @@
//go:build search
// +build search
package cli
package client
import (
"context"

View file

@ -1,7 +1,7 @@
//go:build search
// +build search
package cli //nolint:testpackage
package client
import (
"context"

View file

@ -1,9 +0,0 @@
//go:build !search
// +build !search
package cli
import "github.com/spf13/cobra"
func enableCli(rootCmd *cobra.Command) {
}

View file

@ -1,4 +1,4 @@
package cli
package server
import (
"context"

View file

@ -1,7 +1,7 @@
//go:build search
// +build search
package cli_test
package server_test
import (
"fmt"
@ -13,7 +13,7 @@ import (
. "github.com/smartystreets/goconvey/convey"
"golang.org/x/crypto/bcrypt"
"zotregistry.io/zot/pkg/cli"
cli "zotregistry.io/zot/pkg/cli/server"
"zotregistry.io/zot/pkg/test"
)

View file

@ -1,7 +1,7 @@
//go:build sync && scrub && metrics && search && userprefs && mgmt && imagetrust
// +build sync,scrub,metrics,search,userprefs,mgmt,imagetrust
package cli_test
package server_test
import (
"fmt"
@ -13,7 +13,7 @@ import (
. "github.com/smartystreets/goconvey/convey"
"gopkg.in/resty.v1"
"zotregistry.io/zot/pkg/cli"
cli "zotregistry.io/zot/pkg/cli/server"
. "zotregistry.io/zot/pkg/test"
)

View file

@ -1,4 +1,4 @@
package cli
package server
import (
"context"
@ -22,7 +22,6 @@ import (
"zotregistry.io/zot/pkg/api"
"zotregistry.io/zot/pkg/api/config"
"zotregistry.io/zot/pkg/api/constants"
"zotregistry.io/zot/pkg/cli/cmdflags"
extconf "zotregistry.io/zot/pkg/extensions/config"
"zotregistry.io/zot/pkg/extensions/monitoring"
zlog "zotregistry.io/zot/pkg/log"
@ -191,35 +190,6 @@ func NewServerRootCmd() *cobra.Command {
return rootCmd
}
// "zli" - client-side cli.
func NewCliRootCmd() *cobra.Command {
showVersion := false
rootCmd := &cobra.Command{
Use: "zli",
Short: "`zli`",
Long: "`zli`",
Run: func(cmd *cobra.Command, args []string) {
if showVersion {
log.Info().Str("distribution-spec", distspec.Version).Str("commit", config.Commit).
Str("binary-type", config.BinaryType).Str("go version", config.GoVersion).Msg("version")
} else {
_ = cmd.Usage()
cmd.SilenceErrors = false
}
},
}
rootCmd.SilenceUsage = true
// additional cmds
enableCli(rootCmd)
// "version"
rootCmd.Flags().BoolVarP(&showVersion, cmdflags.VersionFlag, "v", false, "show the version and exit")
return rootCmd
}
func validateStorageConfig(cfg *config.Config, log zlog.Logger) error {
expConfigMap := make(map[string]config.StorageConfig, 0)

View file

@ -1,4 +1,4 @@
package cli_test
package server_test
import (
"encoding/json"
@ -12,7 +12,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"
storageConstants "zotregistry.io/zot/pkg/storage/constants"
. "zotregistry.io/zot/pkg/test"
)
@ -35,24 +35,6 @@ func TestServerUsage(t *testing.T) {
})
}
func TestCliUsage(t *testing.T) {
oldArgs := os.Args
defer func() { os.Args = oldArgs }()
Convey("Test usage", t, func(c C) {
os.Args = []string{"cli_test", "help"}
err := cli.NewCliRootCmd().Execute()
So(err, ShouldBeNil)
})
Convey("Test version", t, func(c C) {
os.Args = []string{"cli_test", "--version"}
err := cli.NewCliRootCmd().Execute()
So(err, ShouldBeNil)
})
}
func TestServe(t *testing.T) {
oldArgs := os.Args
@ -1335,7 +1317,7 @@ func TestServeAPIKey(t *testing.T) {
func TestLoadConfig(t *testing.T) {
Convey("Test viper load config", t, func(c C) {
config := config.New()
err := cli.LoadConfiguration(config, "../../examples/config-policy.json")
err := cli.LoadConfiguration(config, "../../../examples/config-policy.json")
So(err, ShouldBeNil)
})
Convey("Test subpath config combination", t, func(c C) {
@ -1445,18 +1427,18 @@ func TestLoadConfig(t *testing.T) {
func TestGC(t *testing.T) {
Convey("Test GC config", t, func(c C) {
config := config.New()
err := cli.LoadConfiguration(config, "../../examples/config-multiple.json")
err := cli.LoadConfiguration(config, "../../../examples/config-multiple.json")
So(err, ShouldBeNil)
So(config.Storage.GCDelay, ShouldEqual, storageConstants.DefaultGCDelay)
err = cli.LoadConfiguration(config, "../../examples/config-gc.json")
err = cli.LoadConfiguration(config, "../../../examples/config-gc.json")
So(err, ShouldBeNil)
So(config.Storage.GCDelay, ShouldNotEqual, storageConstants.DefaultGCDelay)
err = cli.LoadConfiguration(config, "../../examples/config-gc-periodic.json")
err = cli.LoadConfiguration(config, "../../../examples/config-gc-periodic.json")
So(err, ShouldBeNil)
})
Convey("Test GC config corner cases", t, func(c C) {
contents, err := os.ReadFile("../../examples/config-gc.json")
contents, err := os.ReadFile("../../../examples/config-gc.json")
So(err, ShouldBeNil)
Convey("GC delay without GC", func() {

View file

@ -1,7 +1,7 @@
//go:build stress
// +build stress
package cli_test
package server_test
import (
"fmt"
@ -16,7 +16,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"
"zotregistry.io/zot/pkg/test"
)

View file

@ -38,7 +38,7 @@ import (
"zotregistry.io/zot/pkg/api"
"zotregistry.io/zot/pkg/api/config"
"zotregistry.io/zot/pkg/api/constants"
"zotregistry.io/zot/pkg/cli"
cli "zotregistry.io/zot/pkg/cli/server"
zcommon "zotregistry.io/zot/pkg/common"
extconf "zotregistry.io/zot/pkg/extensions/config"
syncconf "zotregistry.io/zot/pkg/extensions/config/sync"

View file

@ -76,7 +76,7 @@ func createMockStorage(rootDir string, cacheDir string, dedupe bool, store drive
var cacheDriver cache.Cache
// from pkg/cli/root.go/applyDefaultValues, s3 magic
// from pkg/cli/server/root.go/applyDefaultValues, s3 magic
if _, err := os.Stat(path.Join(cacheDir,
storageConstants.BoltdbName+storageConstants.DBExtensionName)); dedupe || (!dedupe && err == nil) {
cacheDriver, _ = storage.Create("boltdb", cache.BoltDBDriverParameters{
@ -151,7 +151,7 @@ func createObjectsStore(rootDir string, cacheDir string, dedupe bool) (
var err error
// from pkg/cli/root.go/applyDefaultValues, s3 magic
// from pkg/cli/server/root.go/applyDefaultValues, s3 magic
s3CacheDBPath := path.Join(cacheDir, storageConstants.BoltdbName+storageConstants.DBExtensionName)
if _, err = os.Stat(s3CacheDBPath); dedupe || (!dedupe && err == nil) {
cacheDriver, _ = storage.Create("boltdb", cache.BoltDBDriverParameters{
@ -179,7 +179,7 @@ func createObjectsStoreDynamo(rootDir string, cacheDir string, dedupe bool, tabl
var cacheDriver cache.Cache
// from pkg/cli/root.go/applyDefaultValues, s3 magic
// from pkg/cli/server/root.go/applyDefaultValues, s3 magic
tableName = strings.ReplaceAll(tableName, "/", "")
cacheDriver, _ = storage.Create("dynamodb", cache.DynamoDBDriverParameters{