forked from gardener/gardener
/
imagevector.go
154 lines (133 loc) · 4.88 KB
/
imagevector.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright (c) 2018 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// 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 imagevector
// ReadImageVector reads the image vector yaml file in the charts directory, unmarshals the content
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/gardener/gardener/pkg/operation/common"
"github.com/gardener/gardener/pkg/utils"
yaml "gopkg.in/yaml.v2"
)
// ReadImageVector reads the image.yaml in the chart directory, unmarshals it
// into a []*ImageSource type and returns it.
func ReadImageVector() (ImageVector, error) {
path := filepath.Join(common.ChartPath, "images.yaml")
vector, err := readImageVector(path)
if err != nil {
return nil, err
}
overwritePath := os.Getenv("IMAGEVECTOR_OVERWRITE")
if len(overwritePath) == 0 {
return vector, nil
}
overwrite, err := readImageVector(overwritePath)
if err != nil {
return nil, err
}
overwrittenImages := make(map[string]*ImageSource, len(overwrite))
for _, image := range overwrite {
overwrittenImages[image.Name] = image
}
var out ImageVector
for _, image := range vector {
if overwritten, ok := overwrittenImages[image.Name]; ok {
out = append(out, overwritten)
continue
}
out = append(out, image)
}
return out, nil
}
func readImageVector(filePath string) (ImageVector, error) {
vector := struct {
Images ImageVector `json:"images" yaml:"images"`
}{}
bytes, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, err
}
if err := yaml.Unmarshal(bytes, &vector); err != nil {
return nil, err
}
return vector.Images, nil
}
func checkConstraintMatchesK8sVersion(constraint, k8sVersion string) (bool, error) {
if constraint == "" {
return true, nil
}
return utils.CheckVersionMeetsConstraint(k8sVersion, constraint)
}
// FindImage returns an image with the given <name> from the sources in the image vector.
// The <k8sVersion> specifies the kubernetes version the image will be running on.
// The <targetK8sVersion> specifies the kubernetes version the image shall target.
// If multiple entries were found, the provided <k8sVersion> is compared with the constraints
// stated in the image definition.
// In case multiple images match the search, the first which was found is returned.
// In case no image was found, an error is returned.
func (v ImageVector) FindImage(name, k8sVersionRuntime, k8sVersionTarget string) (*Image, error) {
for _, source := range v {
if source.Name == name {
matches, err := checkConstraintMatchesK8sVersion(source.Versions, k8sVersionRuntime)
if err != nil {
return nil, err
}
if matches {
return source.ToImage(k8sVersionTarget), nil
}
}
}
return nil, fmt.Errorf("could not find image %q for Kubernetes runtime version %q in the image vector", name, k8sVersionRuntime)
}
// FindImages returns an image map with the given <names> from the sources in the image vector.
// The <k8sVersion> specifies the kubernetes version the image will be running on.
// The <targetK8sVersion> specifies the kubernetes version the image shall target.
// If multiple entries were found, the provided <k8sVersion> is compared with the constraints
// stated in the image definition.
// In case multiple images match the search, the first which was found is returned.
// In case no image was found, an error is returned.
func (v ImageVector) FindImages(names []string, k8sVersionRuntime, k8sVersionTarget string) (map[string]interface{}, error) {
images := map[string]interface{}{}
for _, imageName := range names {
image, err := v.FindImage(imageName, k8sVersionRuntime, k8sVersionTarget)
if err != nil {
return nil, err
}
images[imageName] = image.String()
}
return images, nil
}
// ToImage applies the given <targetK8sVersion> to the source to produce an output image.
// If the tag of an image source is empty, it will use the given <k8sVersion> as tag.
func (i *ImageSource) ToImage(targetK8sVersion string) *Image {
tag := i.Tag
if tag == "" {
tag = fmt.Sprintf("v%s", strings.TrimLeft(targetK8sVersion, "v"))
}
return &Image{
Name: i.Name,
Repository: i.Repository,
Tag: tag,
}
}
// String will returns the string representation of the image.
func (i *Image) String() string {
if len(i.Tag) == 0 {
return i.Repository
}
return fmt.Sprintf("%s:%s", i.Repository, i.Tag)
}