This repository has been archived by the owner on Apr 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
docker-images.go
144 lines (129 loc) · 3.82 KB
/
docker-images.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
package cmd
import (
"fmt"
"sort"
"strings"
"github.com/flanksource/commons/exec"
"github.com/flanksource/karina/pkg/phases/harbor"
"github.com/flanksource/karina/pkg/phases/order"
"github.com/flanksource/karina/pkg/platform"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"gopkg.in/flanksource/yaml.v3"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
var Images = &cobra.Command{
Use: "images",
Short: "Commands for working with docker images",
}
func getImages(p *platform.Platform) ([]string, error) {
images := []string{}
// in order to list all images we perform an dry-run deployment
// with an ApplyHook
p.DryRun = true
p.ApplyDryRun = true
p.TerminationProtection = true
p.ApplyHook = func(ns string, obj unstructured.Unstructured) {
containers := []interface{}{}
if image, found := obj.GetAnnotations()["image"]; found {
images = append(images, image)
}
list, found, _ := unstructured.NestedSlice(obj.UnstructuredContent(), "spec", "template", "spec", "containers")
if found {
containers = append(containers, list...)
}
list, found, _ = unstructured.NestedSlice(obj.UnstructuredContent(), "spec", "template", "spec", "initContainers")
if found {
containers = append(containers, list...)
}
for _, container := range containers {
image, found := container.(map[string]interface{})["image"].(string)
if found {
images = append(images, image)
}
}
}
for name, fn := range order.Phases {
if err := fn(p); err != nil {
return nil, errors.Wrapf(err, "error deploying %s", name)
}
}
return images, nil
}
func uniqueStrings(list []string) []string {
if len(list) < 2 {
return list
}
sort.Strings(list)
newList := []string{list[0]}
length := len(list)
for i := 1; i < length; i++ {
if list[i] != list[i-1] {
newList = append(newList, list[i])
}
}
return newList
}
func init() {
listCmd := &cobra.Command{
Use: "list",
Short: "List all docker images used by the platform",
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
outputFormat, _ := cmd.Flags().GetString("output")
p := getPlatform(cmd)
images, err := getImages(p)
if err != nil {
p.Fatalf("Failed to dry-run deploy : %v", err)
}
if p.DockerRegistry != "" {
l := len(images)
for i := 0; i < l; i++ {
if !strings.HasPrefix(images[i], p.DockerRegistry) {
images[i] = p.DockerRegistry + "/" + images[i]
}
}
}
if outputFormat == "text" {
fmt.Println(strings.Join(images, "\n"))
} else if outputFormat == "yaml" {
yml, _ := yaml.Marshal(images)
fmt.Println(string(yml))
}
},
}
listCmd.PersistentFlags().StringP("output", "o", "text", "Output format (string, yaml)")
var imagesToSync []string
syncCmd := &cobra.Command{
Use: "sync",
Short: "Synchronize all platform docker images to a local registry",
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
p := getPlatform(cmd)
if len(imagesToSync) == 0 {
images, err := getImages(p)
imagesToSync = images
if err != nil {
p.Fatalf("Failed to dry-run deploy : %v", err)
}
}
imagesToSync = uniqueStrings(imagesToSync)
for _, image := range imagesToSync {
if strings.HasPrefix(image, p.DockerRegistry) {
p.Infof("Image %s is used from target registry", image)
continue
}
if err := harbor.CheckManifest(p, image); err != nil {
p.Infof("Syncing %s", image)
_ = exec.Execf("docker pull %s", image)
_ = exec.Execf("docker tag %s %s/%s", image, p.DockerRegistry, image)
_ = exec.Execf("docker push %s/%s", p.DockerRegistry, image)
} else {
p.Infof("Image %s already exists in target repository", image)
}
}
},
}
syncCmd.Flags().StringArrayVarP(&imagesToSync, "image", "i", []string{}, "A list of images to sync")
Images.AddCommand(listCmd, syncCmd)
}