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