-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
195 lines (169 loc) · 5.63 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package main
import (
"flag"
"fmt"
"log"
"os"
"path"
"strings"
"github.com/alex123012/external-modules-transfer/cr"
"github.com/alex123012/external-modules-transfer/templates"
v1 "github.com/google/go-containerregistry/pkg/v1"
)
var (
releaseChannel = "alpha"
moduleName string
pullRepo string
pushRepo string
pullInsecure bool
pushInsecure bool
pullDisableAuth bool
pushDisableAuth bool
pullRunImageUseDigest bool
pullRegistryCa string
pushRegistryCa string
)
func parseFlags() {
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0])
fmt.Fprintln(flag.CommandLine.Output(), "\n This tool helps to transfer deckhouse external modules images")
fmt.Fprintf(flag.CommandLine.Output(), " from one container registry to another.\n\n")
flag.PrintDefaults()
}
flag.StringVar(&pullRepo, "pull-registry", pullRepo, "registry address, that contains external modules\n(you should be logged in to registry via docker login)")
flag.StringVar(&pushRepo, "push-registry", pushRepo, "registry address to push external module from pull repo\n(you should be logged in to registry via docker login)")
flag.StringVar(&moduleName, "module", moduleName, "external module name")
flag.StringVar(&releaseChannel, "release", releaseChannel, "release channel to use")
flag.BoolVar(&pullDisableAuth, "pull-disable-auth", pullDisableAuth, "disable auth for pull registry")
flag.BoolVar(&pushDisableAuth, "push-disable-auth", pushDisableAuth, "disable auth for push registry")
flag.BoolVar(&pullInsecure, "pull-insecure", pullInsecure, "use http protocol for pull registry")
flag.BoolVar(&pushInsecure, "push-insecure", pushInsecure, "use http protocol for push registry")
flag.StringVar(&pullRegistryCa, "pull-ca", pullRegistryCa, "ca certificate for pull registry")
flag.StringVar(&pushRegistryCa, "push-ca", pushRegistryCa, "ca certificate for push registry")
flag.BoolVar(&pullRunImageUseDigest, "pull-run-image-use-digest", pullRunImageUseDigest,
`use digests instead of tags for pulling images
if flag is set - pushing images to 'push' repo will be with
keys (image names) from images_digests.json file from module bundle image.
This would prevent images cleanup in 'push' registry`,
)
flag.Parse()
switch "" {
case moduleName:
log.Fatal("no module name provided")
case pullRepo:
log.Fatal("no repo address provided for pull")
case pushRepo:
log.Fatal("no repo address provided for push")
case releaseChannel:
log.Fatal("no release channel provided")
}
}
type image struct {
repo string
tag string
image v1.Image
}
func main() {
parseFlags()
pullRepoOptions := parseOptions(pullInsecure, pullDisableAuth, pullRegistryCa)
imagesToPush, err := pullImages(pullRepo, pushRepo, moduleName, releaseChannel, pullRunImageUseDigest, pullRepoOptions...)
if err != nil {
log.Fatal(err)
}
pushRepoOptions := parseOptions(pushInsecure, pushDisableAuth, pushRegistryCa)
if err := pushImages(imagesToPush, pushRepoOptions...); err != nil {
log.Fatal(err)
}
if err := renderTemplates(moduleName, pushRepo, releaseChannel, pushRepoOptions...); err != nil {
log.Fatal(err)
}
}
func parseOptions(insecure, disableAuth bool, registryCa string) []cr.Option {
opts := make([]cr.Option, 0)
if insecure {
opts = append(opts, cr.WithInsecureSchema())
}
if disableAuth {
opts = append(opts, cr.WithDisabledAuth())
}
if registryCa != "" {
opts = append(opts, cr.WithCA(registryCa))
}
return opts
}
func pullImages(pullRepo, pushRepo, moduleName, releaseChannel string, pullRunImageUseDigest bool, opts ...cr.Option) ([]image, error) {
moduleListingImage, err := cr.FetchModuleListingImage(pullRepo, moduleName, opts...)
if err != nil {
return nil, err
}
moduleReleaseImage, err := cr.FetchModuleReleaseImage(pullRepo, moduleName, releaseChannel, opts...)
if err != nil {
return nil, err
}
moduleVersion, err := cr.ModuleReleaseImageMetadata(moduleReleaseImage)
if err != nil {
return nil, fmt.Errorf("fetch release metadata error: %v", err)
}
moduleImage, err := cr.FetchModuleImage(pullRepo, moduleName, moduleVersion, opts...)
if err != nil {
return nil, err
}
if pullRunImageUseDigest {
opts = append(opts, cr.WithUseDigest())
}
runImages, err := cr.FetchModuleRunImages(pullRepo, moduleName, moduleImage, opts...)
if err != nil {
return nil, err
}
imagesToPush := []image{
{
repo: pushRepo,
tag: moduleName,
image: moduleListingImage,
},
{
repo: path.Join(pushRepo, moduleName, "release"),
tag: releaseChannel,
image: moduleReleaseImage,
},
{
repo: path.Join(pushRepo, moduleName),
tag: moduleVersion,
image: moduleImage,
},
}
for tag, runImage := range runImages {
imagesToPush = append(imagesToPush, image{
repo: path.Join(pushRepo, moduleName),
tag: tag,
image: runImage,
})
}
return imagesToPush, nil
}
func pushImages(imagesToPush []image, pushRepoOptions ...cr.Option) error {
for _, imgRef := range imagesToPush {
if err := cr.PushImage(imgRef.repo, imgRef.tag, imgRef.image, pushRepoOptions...); err != nil {
return err
}
}
return nil
}
func renderTemplates(name, repo, releaseChannel string, opts ...cr.Option) error {
moduleSource, err := templates.RenderExternalModuleSource(name, repo, releaseChannel, opts...)
if err != nil {
return err
}
moduleConfig, err := templates.RenderModuleConfig(name)
if err != nil {
return err
}
builder := strings.Builder{}
builder.WriteString("\n\n")
builder.WriteString(moduleSource)
builder.WriteString("\n")
builder.WriteString(moduleConfig)
builder.WriteString("\n\n")
fmt.Println(builder.String())
return nil
}