-
Notifications
You must be signed in to change notification settings - Fork 368
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #999 from FabianKramm/fixes
fix issue with wrongly cached image tags (#998)
- Loading branch information
Showing
5 changed files
with
168 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package util | ||
|
||
import ( | ||
"github.com/devspace-cloud/devspace/pkg/devspace/config/generated" | ||
"github.com/devspace-cloud/devspace/pkg/devspace/config/versions/latest" | ||
"github.com/devspace-cloud/devspace/pkg/devspace/deploy/deployer/kubectl/walk" | ||
"github.com/devspace-cloud/devspace/pkg/devspace/registry" | ||
) | ||
|
||
// ReplaceImageNames replaces images within a certain manifest with the correct tags from the cache | ||
func ReplaceImageNames(manifest map[interface{}]interface{}, cache *generated.CacheConfig, imagesConf map[string]*latest.ImageConfig, builtImages map[string]string, keys map[string]bool) bool { | ||
if imagesConf == nil { | ||
imagesConf = map[string]*latest.ImageConfig{} | ||
} | ||
if keys == nil { | ||
keys = map[string]bool{} | ||
} | ||
|
||
// strip out images from cache that are not in the imagesconf anymore | ||
for name := range cache.Images { | ||
if _, ok := imagesConf[name]; !ok { | ||
delete(cache.Images, name) | ||
} | ||
} | ||
|
||
shouldRedeploy := false | ||
|
||
match := func(path, key, value string) bool { | ||
if len(keys) > 0 && keys[key] == false { | ||
return false | ||
} | ||
|
||
// Strip tag from image | ||
image, err := registry.GetStrippedDockerImageName(value) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
// Search for image name | ||
for _, imageCache := range cache.Images { | ||
if imageCache.ImageName == image && imageCache.Tag != "" { | ||
if builtImages != nil { | ||
if _, ok := builtImages[image]; ok { | ||
shouldRedeploy = true | ||
} | ||
} | ||
|
||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
replace := func(path, value string) (interface{}, error) { | ||
image, err := registry.GetStrippedDockerImageName(value) | ||
if err != nil { | ||
return false, nil | ||
} | ||
|
||
// Search for image name | ||
for _, imageCache := range cache.Images { | ||
if imageCache.ImageName == image { | ||
return image + ":" + imageCache.Tag, nil | ||
} | ||
} | ||
|
||
return value, nil | ||
} | ||
|
||
// We ignore the error here because the replace function can never throw an error | ||
_ = walk.Walk(manifest, match, replace) | ||
|
||
return shouldRedeploy | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package util | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/devspace-cloud/devspace/pkg/devspace/config/generated" | ||
"github.com/devspace-cloud/devspace/pkg/devspace/config/versions/latest" | ||
"gopkg.in/yaml.v2" | ||
"gotest.tools/assert" | ||
) | ||
|
||
type replaceContainerNamesTestCase struct { | ||
name string | ||
|
||
overwriteValues map[interface{}]interface{} | ||
cache *generated.CacheConfig | ||
imagesConf map[string]*latest.ImageConfig | ||
builtImages map[string]string | ||
|
||
expectedShouldRedeploy bool | ||
expectedOverwriteValues map[interface{}]interface{} | ||
} | ||
|
||
func TestReplaceContainerNames(t *testing.T) { | ||
testCases := []replaceContainerNamesTestCase{ | ||
replaceContainerNamesTestCase{ | ||
name: "invalid image name", | ||
overwriteValues: map[interface{}]interface{}{ | ||
"": "", | ||
}, | ||
cache: &generated.CacheConfig{ | ||
Images: map[string]*generated.ImageCache{ | ||
"": &generated.ImageCache{}, | ||
}, | ||
}, | ||
expectedOverwriteValues: map[interface{}]interface{}{ | ||
"": "", | ||
}, | ||
}, | ||
replaceContainerNamesTestCase{ | ||
name: "Image not in cache", | ||
overwriteValues: map[interface{}]interface{}{ | ||
"": "myimage", | ||
}, | ||
cache: &generated.CacheConfig{ | ||
Images: map[string]*generated.ImageCache{}, | ||
}, | ||
expectedOverwriteValues: map[interface{}]interface{}{ | ||
"": "myimage", | ||
}, | ||
}, | ||
replaceContainerNamesTestCase{ | ||
name: "Image in cache", | ||
overwriteValues: map[interface{}]interface{}{ | ||
"": "myimage", | ||
}, | ||
imagesConf: map[string]*latest.ImageConfig{ | ||
"test": &latest.ImageConfig{}, | ||
}, | ||
cache: &generated.CacheConfig{ | ||
Images: map[string]*generated.ImageCache{ | ||
"test": &generated.ImageCache{ | ||
ImageName: "myimage", | ||
Tag: "someTag", | ||
}, | ||
}, | ||
}, | ||
builtImages: map[string]string{ | ||
"myimage": "", | ||
}, | ||
expectedShouldRedeploy: true, | ||
expectedOverwriteValues: map[interface{}]interface{}{ | ||
"": "myimage:someTag", | ||
}, | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
shouldRedeploy := ReplaceImageNames(testCase.overwriteValues, testCase.cache, testCase.imagesConf, testCase.builtImages, nil) | ||
|
||
assert.Equal(t, shouldRedeploy, testCase.expectedShouldRedeploy, "Unexpected deployed-bool in testCase %s", testCase.name) | ||
|
||
ovAsYaml, err := yaml.Marshal(testCase.overwriteValues) | ||
assert.NilError(t, err, "Error marshaling overwriteValues in testCase %s", testCase.name) | ||
expectationAsYaml, err := yaml.Marshal(testCase.expectedOverwriteValues) | ||
assert.NilError(t, err, "Error marshaling expectation in testCase %s", testCase.name) | ||
assert.Equal(t, string(ovAsYaml), string(expectationAsYaml), "Unexpected overwriteValues in testCase %s", testCase.name) | ||
} | ||
} |