|
| 1 | +/* |
| 2 | +Copyright 2025 The cert-manager Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package baker |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "maps" |
| 23 | + "slices" |
| 24 | + "strings" |
| 25 | + |
| 26 | + "github.com/google/go-containerregistry/pkg/name" |
| 27 | + "github.com/google/go-containerregistry/pkg/v1/remote" |
| 28 | +) |
| 29 | + |
| 30 | +type BakeReference struct { |
| 31 | + Repository string |
| 32 | + Tag string |
| 33 | + Digest string |
| 34 | +} |
| 35 | + |
| 36 | +func ParseBakeReference(value string) (bakeInput BakeReference) { |
| 37 | + // extract digest from value |
| 38 | + if digestRef, err := name.NewDigest(value); err == nil { |
| 39 | + bakeInput.Repository = digestRef.Context().String() |
| 40 | + bakeInput.Digest = digestRef.DigestStr() |
| 41 | + } |
| 42 | + |
| 43 | + // extract tag from value |
| 44 | + if tagRef, err := name.NewTag(value); err == nil { |
| 45 | + bakeInput.Repository = tagRef.Context().String() |
| 46 | + bakeInput.Tag = tagRef.TagStr() |
| 47 | + } |
| 48 | + |
| 49 | + return bakeInput |
| 50 | +} |
| 51 | + |
| 52 | +func (br BakeReference) Reference() name.Reference { |
| 53 | + repo, _ := name.NewRepository(br.Repository) |
| 54 | + if br.Digest != "" { |
| 55 | + return repo.Digest(br.Digest) |
| 56 | + } |
| 57 | + return repo.Tag(br.Tag) |
| 58 | +} |
| 59 | + |
| 60 | +func (br BakeReference) String() string { |
| 61 | + var builder strings.Builder |
| 62 | + _, _ = builder.WriteString(br.Repository) |
| 63 | + if br.Tag != "" { |
| 64 | + _, _ = builder.WriteString(":") |
| 65 | + _, _ = builder.WriteString(br.Tag) |
| 66 | + } |
| 67 | + if br.Digest != "" { |
| 68 | + _, _ = builder.WriteString("@") |
| 69 | + _, _ = builder.WriteString(br.Digest) |
| 70 | + } |
| 71 | + return builder.String() |
| 72 | +} |
| 73 | + |
| 74 | +type BakeInput = BakeReference |
| 75 | + |
| 76 | +func (bi BakeInput) Find(ctx context.Context) (BakeOutput, error) { |
| 77 | + desc, err := remote.Head(bi.Reference(), remote.WithContext(ctx)) |
| 78 | + if err != nil { |
| 79 | + return BakeReference{}, fmt.Errorf("failed to pull %s", bi) |
| 80 | + } |
| 81 | + |
| 82 | + return BakeReference{ |
| 83 | + Repository: bi.Repository, |
| 84 | + Digest: desc.Digest.String(), |
| 85 | + Tag: bi.Tag, |
| 86 | + }, nil |
| 87 | +} |
| 88 | + |
| 89 | +type BakeOutput = BakeReference |
| 90 | + |
| 91 | +func Extract(ctx context.Context, inputPath string) (map[string]BakeInput, error) { |
| 92 | + results := map[string]BakeInput{} |
| 93 | + |
| 94 | + values, err := readValuesYAML(inputPath) |
| 95 | + if err != nil { |
| 96 | + return nil, err |
| 97 | + } |
| 98 | + |
| 99 | + if _, err := allNestedStringValues(values, nil, func(path []string, value string) (string, error) { |
| 100 | + if path[len(path)-1] != "_defaultReference" { |
| 101 | + return value, nil |
| 102 | + } |
| 103 | + |
| 104 | + bakeInput := ParseBakeReference(value) |
| 105 | + if bakeInput == (BakeInput{}) { |
| 106 | + return "", fmt.Errorf("invalid _defaultReference value: %q", value) |
| 107 | + } |
| 108 | + |
| 109 | + results[strings.Join(path, ".")] = bakeInput |
| 110 | + |
| 111 | + return value, nil |
| 112 | + }); err != nil { |
| 113 | + return nil, err |
| 114 | + } |
| 115 | + |
| 116 | + return results, nil |
| 117 | +} |
| 118 | + |
| 119 | +type BakeAction struct { |
| 120 | + In BakeInput `json:"in"` |
| 121 | + Out BakeOutput `json:"out"` |
| 122 | +} |
| 123 | + |
| 124 | +func Bake(ctx context.Context, inputPath string, valuesPaths []string) (map[string]BakeAction, error) { |
| 125 | + results := map[string]BakeAction{} |
| 126 | + return results, inplaceModifyValuesYAML(inputPath, func(values map[string]any) (map[string]any, error) { |
| 127 | + replacedValuePaths := map[string]struct{}{} |
| 128 | + newValues, err := allNestedStringValues(values, nil, func(path []string, value string) (string, error) { |
| 129 | + if path[len(path)-1] != "_defaultReference" { |
| 130 | + return value, nil |
| 131 | + } |
| 132 | + |
| 133 | + bakeInput := ParseBakeReference(value) |
| 134 | + if bakeInput == (BakeInput{}) { |
| 135 | + return "", fmt.Errorf("invalid _defaultReference value: %q", value) |
| 136 | + } |
| 137 | + |
| 138 | + bakeOutput, err := bakeInput.Find(ctx) |
| 139 | + if err != nil { |
| 140 | + return "", err |
| 141 | + } |
| 142 | + |
| 143 | + pathString := strings.Join(path, ".") |
| 144 | + replacedValuePaths[pathString] = struct{}{} |
| 145 | + results[pathString] = BakeAction{ |
| 146 | + In: bakeInput, |
| 147 | + Out: bakeOutput, |
| 148 | + } |
| 149 | + |
| 150 | + return bakeOutput.String(), nil |
| 151 | + }) |
| 152 | + if err != nil { |
| 153 | + return nil, err |
| 154 | + } |
| 155 | + |
| 156 | + if len(replacedValuePaths) > len(valuesPaths) { |
| 157 | + return nil, fmt.Errorf("too many value paths were replaced: %v", slices.Collect(maps.Keys(replacedValuePaths))) |
| 158 | + } |
| 159 | + for _, valuesPath := range valuesPaths { |
| 160 | + if _, ok := replacedValuePaths[valuesPath]; !ok { |
| 161 | + return nil, fmt.Errorf("path was not replaced: %s", valuesPath) |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + return newValues.(map[string]any), nil |
| 166 | + }) |
| 167 | +} |
| 168 | + |
| 169 | +func allNestedStringValues(object any, path []string, fn func(path []string, value string) (string, error)) (any, error) { |
| 170 | + switch t := object.(type) { |
| 171 | + case map[string]any: |
| 172 | + for key, value := range t { |
| 173 | + keyPath := append(path, key) |
| 174 | + if stringValue, ok := value.(string); ok { |
| 175 | + newValue, err := fn(slices.Clone(keyPath), stringValue) |
| 176 | + if err != nil { |
| 177 | + return nil, err |
| 178 | + } |
| 179 | + t[key] = newValue |
| 180 | + } else { |
| 181 | + newValue, err := allNestedStringValues(value, keyPath, fn) |
| 182 | + if err != nil { |
| 183 | + return nil, err |
| 184 | + } |
| 185 | + t[key] = newValue |
| 186 | + } |
| 187 | + } |
| 188 | + case map[string]string: |
| 189 | + for key, stringValue := range t { |
| 190 | + keyPath := append(path, key) |
| 191 | + newValue, err := fn(slices.Clone(keyPath), stringValue) |
| 192 | + if err != nil { |
| 193 | + return nil, err |
| 194 | + } |
| 195 | + t[key] = newValue |
| 196 | + } |
| 197 | + case []any: |
| 198 | + for i, value := range t { |
| 199 | + path = append(path, fmt.Sprintf("%d", i)) |
| 200 | + newValue, err := allNestedStringValues(value, path, fn) |
| 201 | + if err != nil { |
| 202 | + return nil, err |
| 203 | + } |
| 204 | + t[i] = newValue |
| 205 | + } |
| 206 | + default: |
| 207 | + // ignore object |
| 208 | + } |
| 209 | + |
| 210 | + return object, nil |
| 211 | +} |
0 commit comments