-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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 #909 from dgageot/manifest-visitor
Decouple visiting manifests and replacing images
- Loading branch information
Showing
4 changed files
with
177 additions
and
90 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
Copyright 2018 The Skaffold Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package kubectl | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
"github.com/sirupsen/logrus" | ||
|
||
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build" | ||
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker" | ||
) | ||
|
||
// for testing | ||
var warner Warner = &logrusWarner{} | ||
|
||
// ReplaceImages replaces image names in a list of manifests. | ||
func (l *ManifestList) ReplaceImages(builds []build.Artifact) (ManifestList, error) { | ||
replacer := newImageReplacer(builds) | ||
|
||
updated, err := l.Visit(replacer) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "replacing images") | ||
} | ||
|
||
replacer.Check() | ||
logrus.Debugln("manifests with tagged images", updated.String()) | ||
|
||
return updated, nil | ||
} | ||
|
||
type imageReplacer struct { | ||
tagsByImageName map[string]string | ||
found map[string]bool | ||
} | ||
|
||
func newImageReplacer(builds []build.Artifact) *imageReplacer { | ||
tagsByImageName := make(map[string]string) | ||
for _, build := range builds { | ||
tagsByImageName[build.ImageName] = build.Tag | ||
} | ||
|
||
return &imageReplacer{ | ||
tagsByImageName: tagsByImageName, | ||
found: make(map[string]bool), | ||
} | ||
} | ||
|
||
func (r *imageReplacer) Matches(key string) bool { | ||
return key == "image" | ||
} | ||
|
||
func (r *imageReplacer) NewValue(key string, old interface{}) (bool, interface{}) { | ||
image := old.(string) | ||
|
||
parsed, err := docker.ParseReference(image) | ||
if err != nil { | ||
warner.Warnf("Couldn't parse image: %s", image) | ||
return false, nil | ||
} | ||
|
||
if tag, present := r.tagsByImageName[parsed.BaseName]; present { | ||
if parsed.FullyQualified { | ||
if tag == image { | ||
r.found[parsed.BaseName] = true | ||
} | ||
} else { | ||
r.found[parsed.BaseName] = true | ||
return true, tag | ||
} | ||
} | ||
|
||
return false, nil | ||
} | ||
|
||
func (r *imageReplacer) Check() { | ||
for imageName := range r.tagsByImageName { | ||
if !r.found[imageName] { | ||
warner.Warnf("image [%s] is not used by the deployment", imageName) | ||
} | ||
} | ||
} |
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,79 @@ | ||
/* | ||
Copyright 2018 The Skaffold Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package kubectl | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
// Replacer is used to replace portions of yaml manifests that match a given key. | ||
type Replacer interface { | ||
Matches(key string) bool | ||
|
||
NewValue(key string, old interface{}) (bool, interface{}) | ||
} | ||
|
||
// Visit recursively visits a list of manifests and applies transformations of them. | ||
func (l *ManifestList) Visit(replacer Replacer) (ManifestList, error) { | ||
var updated ManifestList | ||
|
||
for _, manifest := range *l { | ||
m := make(map[interface{}]interface{}) | ||
if err := yaml.Unmarshal(manifest, &m); err != nil { | ||
return nil, errors.Wrap(err, "reading kubernetes YAML") | ||
} | ||
|
||
if len(m) == 0 { | ||
continue | ||
} | ||
|
||
recursiveVisit(m, replacer) | ||
|
||
updatedManifest, err := yaml.Marshal(m) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "marshalling yaml") | ||
} | ||
|
||
updated = append(updated, updatedManifest) | ||
} | ||
|
||
return updated, nil | ||
} | ||
|
||
func recursiveVisit(i interface{}, replacer Replacer) { | ||
switch t := i.(type) { | ||
case []interface{}: | ||
for _, v := range t { | ||
recursiveVisit(v, replacer) | ||
} | ||
case map[interface{}]interface{}: | ||
for k, v := range t { | ||
key := k.(string) | ||
|
||
if !replacer.Matches(key) { | ||
recursiveVisit(v, replacer) | ||
continue | ||
} | ||
|
||
ok, newValue := replacer.NewValue(key, v) | ||
if ok { | ||
t[k] = newValue | ||
} | ||
} | ||
} | ||
} |