-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimageswap.go
103 lines (85 loc) · 2.55 KB
/
imageswap.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
package main
import (
"fmt"
"regexp"
"strconv"
"strings"
"github.com/google/go-containerregistry/pkg/name"
)
// Define the structure of the imageswap configuration
type ImageSwapConfig struct {
ImageSwap ImageSwap `yaml:"imageswap"`
}
type ImageSwap struct {
Default string `yaml:"default"`
Mappings ImageMapping `yaml:"mappings"`
Tests []string `yaml:"tests,flow"`
}
// Define the structure of an image mapping
type ImageMapping struct {
Swap []Swap `yaml:"swap"`
ExactSwap []ExactSwap `yaml:"exact-swap"`
RegexSwap []RegexSwap `yaml:"regex-swap"`
LegacySwap []string `yaml:"legacy-swap"`
}
// Swap represents the "swap" mapping in the YAML.
type Swap struct {
Registry string `yaml:"registry"`
Target string `yaml:"target"`
}
// ExactSwap represents the "exact-swap" mapping in the YAML.
type ExactSwap struct {
Image string `yaml:"image"`
Target string `yaml:"target"`
}
// RegexSwap represents the "regex-swap" mapping in the YAML.
type RegexSwap struct {
Expression string `yaml:"expression"`
Target string `yaml:"target"`
}
func (i *ImageSwap) SwapImage(image string) string {
ref, _ := name.ParseReference(image, name.WithDefaultRegistry(i.Default))
registry := ref.Context().RegistryStr()
// if registry == default registry return image
var newImage string
if string(registry) == i.Default {
newImage = ref.Name()
}
for _, swap := range i.Mappings.Swap {
if swap.Registry == registry {
identifier := ref.Identifier()
switch len(strings.Split(identifier, ":")) {
case 1:
newImage = fmt.Sprintf("%s/%s:%s", swap.Target, ref.Context().RepositoryStr(), ref.Identifier())
case 2:
tag := strings.Split(strings.Split(image, "@")[0], ":")[1]
newImage = fmt.Sprintf("%s/%s:%s@%s", swap.Target, ref.Context().RepositoryStr(), tag, ref.Identifier())
default:
newImage = fmt.Sprintf("%s/%s", swap.Target, ref.Context().RepositoryStr())
}
break
}
}
for _, exactSwap := range i.Mappings.ExactSwap {
if exactSwap.Image == ref.String() {
newImage = exactSwap.Target
break
}
}
for _, regexSwap := range i.Mappings.RegexSwap {
re := regexp.MustCompile(regexSwap.Expression)
match := re.FindStringSubmatch(ref.String())
if match != nil {
newRepository := strings.Replace(ref.String(), match[0], "", -1)
newImage = regexSwap.Target
if len(match) > 1 {
for m := 1; m < len(match); m++ {
newImage = strings.Replace(newImage, "$"+strconv.Itoa(m), match[m], -1)
newImage = fmt.Sprintf("%s%s", newImage, newRepository)
}
}
break
}
}
return newImage
}