Skip to content

Commit

Permalink
Fix imageprefix in config ID generation
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Sluiter <[email protected]>
  • Loading branch information
slintes committed Jan 8, 2020
1 parent 622c3b8 commit ac73fc0
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
4 changes: 3 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -491,9 +491,9 @@ gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
k8s.io/api v0.0.0-20190222213804-5cb15d344471 h1:MzQGt8qWQCR+39kbYRd0uQqsvSidpYqJLFeWiJ9l4OE=
Expand Down Expand Up @@ -532,6 +532,8 @@ k8s.io/utils v0.0.0-20190607212802-c55fbcfc754a h1:2jUDc9gJja832Ftp+QbDV0tVhQHMI
k8s.io/utils v0.0.0-20190607212802-c55fbcfc754a/go.mod h1:sZAwmy6armz5eXlNoLmJcl4F1QuKu7sr+mFQ0byX7Ew=
kubevirt.io/containerized-data-importer v1.10.6 h1:xkqLb48pkbdoY8gB2VDP2o+KXpO18tgQuLjcXNn0qAI=
kubevirt.io/containerized-data-importer v1.10.6/go.mod h1:qF594BtRRkruyrqLwt3zbLCWdPIQNs1qWh4LR1cOzy0=
kubevirt.io/containerized-data-importer v1.10.9 h1:wYCPyMbCLdn5tlZWW/lPYTMPtIOSfdXDevyWw9rxl2s=
kubevirt.io/containerized-data-importer v1.10.9/go.mod h1:qF594BtRRkruyrqLwt3zbLCWdPIQNs1qWh4LR1cOzy0=
kubevirt.io/qe-tools v0.1.3-0.20190512140058-934db0579e0c h1:9FawJ0jS2NvLLE3oyBvnEgv+vVl0aIVKrgXuhLoWfjw=
kubevirt.io/qe-tools v0.1.3-0.20190512140058-934db0579e0c/go.mod h1:PJyH/YXC4W0AmxfheDmXWMbLNsMSboVGXKpMAwfKzVE=
modernc.org/cc v1.0.0/go.mod h1:1Sk4//wdnYJiUIxnW8ddKpaOJCF37yAdqYnkxUpaYxw=
Expand Down
13 changes: 10 additions & 3 deletions pkg/virt-operator/util/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,15 +398,22 @@ func (c *KubeVirtDeploymentConfig) generateInstallStrategyID() {
// and configmap
// Calculate a sha over all those properties
hasher := sha1.New()
values := c.getStringFromFields()
values := getStringFromFields(*c)
hasher.Write([]byte(values))

c.ID = hex.EncodeToString(hasher.Sum(nil))
}

func (c *KubeVirtDeploymentConfig) getStringFromFields() string {
// use KubeVirtDeploymentConfig by value because we modify sth just for the ID
func getStringFromFields(c KubeVirtDeploymentConfig) string {
result := ""
v := reflect.ValueOf(*c)

// image prefix might be empty. In order to get the same ID for missing and empty, remove an empty one
if prefix, ok := c.AdditionalProperties[ImagePrefixKey]; ok && prefix == "" {
delete(c.AdditionalProperties, ImagePrefixKey)
}

v := reflect.ValueOf(c)
for i := 0; i < v.NumField(); i++ {
fieldName := v.Type().Field(i).Name
result += fieldName
Expand Down
35 changes: 35 additions & 0 deletions pkg/virt-operator/util/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,39 @@ var _ = Describe("Operator Config", func() {
})
})

Describe("creating config ID", func() {

var idMissing, idEmpty, idFilled string

BeforeEach(func() {
cfgMissing := &KubeVirtDeploymentConfig{}
cfgMissing.AdditionalProperties = make(map[string]string)
cfgMissing.generateInstallStrategyID()
idMissing = cfgMissing.ID

cfgEmpty := &KubeVirtDeploymentConfig{}
cfgEmpty.AdditionalProperties = make(map[string]string)
cfgEmpty.AdditionalProperties[ImagePrefixKey] = ""
cfgEmpty.generateInstallStrategyID()
idEmpty = cfgEmpty.ID

cfgFilled := &KubeVirtDeploymentConfig{}
cfgFilled.AdditionalProperties = make(map[string]string)
cfgFilled.AdditionalProperties[ImagePrefixKey] = "something"
cfgFilled.generateInstallStrategyID()
idFilled = cfgFilled.ID
})

It("should result in same ID with missing or empty image prefix", func() {
Expect(idMissing).ToNot(BeEmpty())
Expect(idMissing).To(Equal(idEmpty))
})

It("should result in different ID with filled image prefix", func() {
Expect(idFilled).ToNot(BeEmpty())
Expect(idFilled).ToNot(Equal(idEmpty))
})

})

})

0 comments on commit ac73fc0

Please sign in to comment.