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

Update dist-spec version automatically

Warning if config has wrong dist-spec version

Signed-off-by: laurentiuNiculae <themelopeus@gmail.com>
This commit is contained in:
laurentiuNiculae 2022-03-07 14:50:15 +02:00 committed by Ramkumar Chinchani
parent fa27e22404
commit 63d94d4ac5
25 changed files with 108 additions and 40 deletions

View file

@ -1,5 +1,5 @@
{
"version": "0.1.0-dev",
"distSpecVersion": "1.0.0",
"storage": {
"rootDirectory": "/tmp/zot"
},

View file

@ -1,5 +1,5 @@
{
"version":"0.1.0-dev",
"distSpecVersion":"1.0.0",
"storage":{
"rootDirectory":"/tmp/zot"
},

View file

@ -1,5 +1,5 @@
{
"version": "0.1.0-dev",
"distSpecVersion": "1.0.0",
"storage": {
"rootDirectory": "/tmp/zot",
"commit": true

View file

@ -1,5 +1,5 @@
{
"version":"0.1.0-dev",
"distSpecVersion":"1.0.0",
"storage":{
"rootDirectory":"/tmp/zot",
"gc": false,

View file

@ -1,5 +1,5 @@
{
"version": "0.1.0-dev",
"distSpecVersion": "1.0.0",
"storage": {
"rootDirectory": "/tmp/zot"
},

View file

@ -1,5 +1,5 @@
{
"version":"0.1.0-dev",
"distSpecVersion":"1.0.0",
"storage":{
"rootDirectory":"/tmp/zot"
},

View file

@ -1,5 +1,5 @@
---
version:
distSpecVersion:
0.1.0-dev
storage:
rootDirectory: /tmp/zot

View file

@ -1,5 +1,5 @@
{
"version": "0.1.0-dev",
"distSpecVersion": "1.0.0",
"storage": {
"rootDirectory": "/tmp/zot",
"gc": true,

View file

@ -1,5 +1,5 @@
{
"version": "0.1.0-dev",
"distSpecVersion": "1.0.0",
"storage": {
"rootDirectory": "/tmp/zot"
},

View file

@ -1,5 +1,5 @@
{
"version": "0.1.0-dev",
"distSpecVersion": "1.0.0",
"storage": {
"rootDirectory": "/tmp/zot"
},

View file

@ -1,5 +1,5 @@
{
"version": "0.1.0-dev",
"distSpecVersion": "1.0.0",
"storage": {
"rootDirectory": "/tmp/zot",
"dedupe": true,

View file

@ -1,5 +1,5 @@
{
"version": "0.1.0-dev",
"distSpecVersion": "1.0.0",
"storage": {
"rootDirectory": "/tmp/zot",
"dedupe": true,

View file

@ -1,5 +1,5 @@
{
"version": "0.1.0-dev",
"distSpecVersion": "1.0.0",
"storage": {
"rootDirectory": "/tmp/zot"
},

View file

@ -1,5 +1,5 @@
{
"version": "0.1.0-dev",
"distSpecVersion": "1.0.0",
"storage": {
"rootDirectory": "/tmp/zot"
},

View file

@ -1,5 +1,5 @@
{
"version": "0.1.0-dev",
"distSpecVersion": "1.0.0",
"storage": {
"rootDirectory": "/zot",
"storageDriver": {

View file

@ -1,5 +1,5 @@
{
"version":"0.1.0-dev",
"distSpecVersion":"1.0.0",
"storage":{
"rootDirectory":"/tmp/zot"
},

View file

@ -1,5 +1,5 @@
{
"version":"0.1.0-dev",
"distSpecVersion":"1.0.0",
"storage":{
"rootDirectory":"/tmp/zot"
},

View file

@ -1,5 +1,5 @@
{
"version":"0.1.0-dev",
"distSpecVersion":"1.0.0",
"storage":{
"rootDirectory":"/tmp/zot"
},

2
go.mod
View file

@ -33,7 +33,6 @@ require (
github.com/notaryproject/notation v0.7.0-alpha.1
github.com/notaryproject/notation-go-lib v1.0.0-alpha-1
github.com/olekukonko/tablewriter v0.0.5
github.com/opencontainers/distribution-spec/specs-go v0.0.0-20220217185014-dd38b7ed8a99
github.com/opencontainers/go-digest v1.0.0
github.com/opencontainers/image-spec v1.0.3-0.20220114050600-8b9d41f48198
github.com/opencontainers/umoci v0.4.8-0.20210922062158-e60a0cc726e6
@ -287,6 +286,7 @@ require (
github.com/oklog/run v1.1.0 // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/open-policy-agent/opa v0.37.0 // indirect
github.com/opencontainers/distribution-spec v1.0.0
github.com/opencontainers/runc v1.1.0 // indirect
github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect

4
go.sum
View file

@ -2023,8 +2023,8 @@ github.com/open-policy-agent/opa v0.32.0/go.mod h1:5sJdtc+1/U8zy/j30njpQl6u9rM4M
github.com/open-policy-agent/opa v0.35.0/go.mod h1:xEmekKlk6/c+so5HF9wtPnGPXDfBuBsrMGhSHOHEF+U=
github.com/open-policy-agent/opa v0.37.0 h1:OUXB+RAcxQpmXeNW2BN1wYzQQvVCPF1T9zv+QXGr9Wg=
github.com/open-policy-agent/opa v0.37.0/go.mod h1:xX3NUCZuXK8f0CNhFQvhm4495mZLptf94pIkWRLaFqo=
github.com/opencontainers/distribution-spec/specs-go v0.0.0-20220217185014-dd38b7ed8a99 h1:yGqpYh1h3ZXxGg4Mqd5ZwhlwQ9wXOgvuodxbxJA7iMY=
github.com/opencontainers/distribution-spec/specs-go v0.0.0-20220217185014-dd38b7ed8a99/go.mod h1:aA4vdXRS8E1TG7pLZOz85InHi3BiPdErh8IpJN6E0x4=
github.com/opencontainers/distribution-spec v1.0.0 h1:3qi5mXdKrXCacBYm8y46okmGmCR0pSgfaEd2/T0QXWE=
github.com/opencontainers/distribution-spec v1.0.0/go.mod h1:copR2flp+jTEvQIFMb6MIx45OkrxzqyjszPDT3hx/5Q=
github.com/opencontainers/go-digest v0.0.0-20170106003457-a6d0ee40d420/go.mod h1:cMLVZDEM3+U2I4VmLI6N8jQYUd2OVphdqWwCJHrFt2s=
github.com/opencontainers/go-digest v0.0.0-20180430190053-c9281466c8b2/go.mod h1:cMLVZDEM3+U2I4VmLI6N8jQYUd2OVphdqWwCJHrFt2s=
github.com/opencontainers/go-digest v1.0.0-rc1/go.mod h1:cMLVZDEM3+U2I4VmLI6N8jQYUd2OVphdqWwCJHrFt2s=

View file

@ -122,26 +122,26 @@ type Policy struct {
}
type Config struct {
Version string
GoVersion string
Commit string
BinaryType string
AccessControl *AccessControlConfig
Storage GlobalStorageConfig
HTTP HTTPConfig
Log *LogConfig
Extensions *extconf.ExtensionConfig
DistSpecVersion string `json:"distSpecVersion" mapstructure:"distSpecVersion"`
GoVersion string
Commit string
BinaryType string
AccessControl *AccessControlConfig
Storage GlobalStorageConfig
HTTP HTTPConfig
Log *LogConfig
Extensions *extconf.ExtensionConfig
}
func New() *Config {
return &Config{
Version: distspec.Version,
GoVersion: GoVersion,
Commit: Commit,
BinaryType: BinaryType,
Storage: GlobalStorageConfig{GC: true, GCDelay: storage.DefaultGCDelay, Dedupe: true},
HTTP: HTTPConfig{Address: "127.0.0.1", Port: "8080"},
Log: &LogConfig{Level: "debug"},
DistSpecVersion: distspec.Version,
GoVersion: GoVersion,
Commit: Commit,
BinaryType: BinaryType,
Storage: GlobalStorageConfig{GC: true, GCDelay: storage.DefaultGCDelay, Dedupe: true},
HTTP: HTTPConfig{Address: "127.0.0.1", Port: "8080"},
Log: &LogConfig{Level: "debug"},
}
}

View file

@ -156,7 +156,9 @@ func (c *Controller) Run() error {
return err
}
monitoring.SetServerInfo(c.Metrics, c.Config.Commit, c.Config.BinaryType, c.Config.GoVersion, c.Config.Version)
monitoring.SetServerInfo(c.Metrics, c.Config.Commit, c.Config.BinaryType, c.Config.GoVersion,
c.Config.DistSpecVersion)
_ = NewRouteHandler(c)
addr := fmt.Sprintf("%s:%s", c.Config.HTTP.Address, c.Config.HTTP.Port)

View file

@ -363,6 +363,23 @@ func applyDefaultValues(config *config.Config, viperInstance *viper.Viper) {
}
}
}
if config.DistSpecVersion != distspec.Version {
log.Warn().Err(errors.ErrBadConfig).
Msgf("config dist-spec version: %s differs from version actually used: %s, will be corrected automatically",
config.DistSpecVersion, distspec.Version)
// rewrite the config file
viperInstance.Set("distSpecVersion", distspec.Version)
err := viperInstance.WriteConfig()
if err != nil {
log.Warn().Err(errors.ErrBadConfig).
Msg("can't rewrite the config file")
}
config.DistSpecVersion = distspec.Version
}
}
func LoadConfiguration(config *config.Config, configPath string) {

View file

@ -10,6 +10,7 @@ import (
"testing"
"time"
distspec "github.com/opencontainers/distribution-spec/specs-go"
. "github.com/smartystreets/goconvey/convey"
"gopkg.in/resty.v1"
"zotregistry.io/zot/pkg/api"
@ -282,7 +283,7 @@ func TestVerify(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "zot-test*.json")
So(err, ShouldBeNil)
defer os.Remove(tmpfile.Name()) // clean up
content := []byte(`{"version": "0.1.0-dev", "storage": {"rootDirectory": "/tmp/zot"},
content := []byte(`{"distSpecVersion": "1.0.0", "storage": {"rootDirectory": "/tmp/zot"},
"http": {"address": "127.0.0.1", "port": "8080", "ReadOnly": false},
"log": {"level": "debug"}}`)
_, err = tmpfile.Write(content)
@ -527,3 +528,51 @@ func TestScrub(t *testing.T) {
})
})
}
func TestApplyDefaultValues(t *testing.T) {
Convey("Test rewriting of config file if version is wrong", t, func() {
configContent := []byte(`{"distSpecVersion": "1.0.0555", "storage": {"rootDirectory": "/tmp/zot"},
"http": {"address": "127.0.0.1", "port": "8080", "ReadOnly": false},
"log": {"level": "debug"}}
`)
oldConfig := config.New()
file, err := ioutil.TempFile("", "default-val-test-config*.json")
So(err, ShouldBeNil)
defer os.Remove(file.Name())
_, err = file.Write(configContent)
So(err, ShouldBeNil)
Convey("The file can be rewritten", func() {
err = os.Chmod(file.Name(), 0o777)
So(err, ShouldBeNil)
cli.LoadConfiguration(oldConfig, file.Name())
configContent, err = ioutil.ReadFile(file.Name())
So(err, ShouldBeNil)
newConfig := config.New()
err = json.Unmarshal(configContent, newConfig)
So(err, ShouldBeNil)
So(newConfig.DistSpecVersion, ShouldEqual, distspec.Version)
So(newConfig.DistSpecVersion, ShouldEqual, distspec.Version)
})
Convey("The file can't be rewritten", func() {
err = os.Chmod(file.Name(), 0o444)
So(err, ShouldBeNil)
cli.LoadConfiguration(oldConfig, file.Name())
configContent, err = ioutil.ReadFile(file.Name())
So(err, ShouldBeNil)
newConfig := config.New()
err = json.Unmarshal(configContent, newConfig)
So(err, ShouldBeNil)
So(newConfig.DistSpecVersion, ShouldEqual, "1.0.0555")
So(oldConfig.DistSpecVersion, ShouldEqual, distspec.Version)
})
})
}

View file

@ -1,5 +1,5 @@
{
"version": "0.1.0-dev",
"distSpecVersion": "1.0.0",
"storage": {
"rootDirectory": "/zot",
"gc": false,