-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
145 lines (121 loc) · 3.47 KB
/
main.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
package main
import (
"fmt"
"os"
"strings"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/remote"
"gopkg.in/yaml.v3"
)
type Image struct {
Name string `yaml:"name"`
Source Source `yaml:"source"`
Target Target `yaml:"target"`
// Url string `yaml:"url"`
// Tag string `yaml:"tag"`
// Digest string `yaml:"digest"`
Cosigned struct {
Enabled string `yaml:"enabled"`
Signature string `yaml:"signature"`
}
TargetLocation struct {
Registry string `yaml:"registry"`
Path string `yaml:"path"`
} `yaml:"target-location"`
}
type Source struct {
Registry string `yaml:"registry"`
Image string `yaml:"image"`
}
type Target struct {
Registry string `yaml:"registry"`
Image string `yaml:"image"`
AdditionalTags []string `yaml:"additionalTags"`
}
type Registry struct {
Name string `yaml:"name"`
Url string `yaml:"url"`
}
// correctly populate the data.
type T struct {
Images []Image `yaml:"images"`
Registries []Registry `yaml:"registries"`
}
func buildImageUrl(imageUrl string, imageTag string, imageDigest string) string {
if imageDigest == "" {
return fmt.Sprintf("%s:%s", imageUrl, imageTag)
}
return fmt.Sprintf("%s:%s@%s", imageUrl, imageTag, imageDigest)
}
func getImageTag(image string) string {
if strings.ContainsAny(image, "@sha256:") {
tag := strings.Split(image, ":")[1]
tag = strings.Split(tag, "@")[0]
fmt.Printf("Tag is: %s \n", tag)
return tag
}
tag := strings.Split(image, ":")[1]
fmt.Printf("Tag is: %s \n", tag)
return tag
}
func main() {
t := readFile()
for _, image := range t.Images {
getImageTag(image.Source.Image)
// imageUrl := buildImageUrl(image.Url, image.Tag, image.Digest)
ref, err := name.ParseReference(image.Source.Image)
check(err)
fmt.Printf("Downloading image: %s... \n", image.Name)
img, err := remote.Image(ref, remote.WithAuthFromKeychain(authn.DefaultKeychain))
check(err)
fmt.Printf("Downloaded image: %s \n", image.Name)
fmt.Printf("image details: %v \n", img)
// hash, err := img.Digest()
// check(err)
// if image.Digest == hash.String() {
// fmt.Println("digest verified")
// }
// newPlace := getRegistryUrl(t, image)
// newUrl := fmt.Sprintf("%s/%s:%s", newPlace, image.TargetLocation.Path, image.Tag)
tag, err := name.ParseReference(image.Target.Image)
check(err)
fmt.Printf("Pushing image: %s ... \n", image.Target.Image)
remote.Write(tag, img, remote.WithAuthFromKeychain(authn.DefaultKeychain))
fmt.Printf("Pushed image: %s \n", image.Target.Image)
}
}
func getRegistryUrl(t T, image Image) string {
for _, r := range t.Registries {
if r.Name == image.TargetLocation.Registry {
return r.Url
}
}
panic(fmt.Errorf("no registries found for image push registry"))
}
func readFile() T {
dat, err := os.ReadFile("./config.yaml")
check(err)
t := T{}
err = yaml.Unmarshal(dat, &t)
check(err)
return t
}
func check(e error) {
if e != nil {
panic(e)
}
}
// unused for now
// func createFile(image Image, img v1.Image) {
// fp, err := os.CreateTemp(".", fmt.Sprintf("%s-%s", image.Name, image.Tag))
// check(err)
// newTag, err := name.NewTag(fmt.Sprintf("%s:%s", image.Name, image.Tag))
// check(err)
// defer fp.Close()
// fmt.Println("Writing to tarball...")
// if err := tarball.Write(newTag, img, fp); err != nil {
// panic(fmt.Errorf("error writing image to tarball: %v", err))
// }
// fmt.Println("Written")
// }