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
+16 -16
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"},
}
}
+3 -1
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)
+17
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) {
+50 -1
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)
})
})
}