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
/
harbor.go
203 lines (173 loc) · 5.31 KB
/
harbor.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
196
197
198
199
200
201
202
203
package cmd
import (
"fmt"
"os"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/flanksource/karina/pkg/phases/harbor"
)
// Harbor is the parent command for interactor with the harbor docker registry
var Harbor = &cobra.Command{
Use: "harbor",
Short: "Commmands for deploying and interacting with harbor",
}
func init() {
Harbor.AddCommand(&cobra.Command{
Use: "deploy",
Short: "Build and deploy the harbor registry",
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
if err := harbor.Deploy(getPlatform(cmd)); err != nil {
log.Fatalf("Error building harbor %s\n", err)
}
},
})
Harbor.AddCommand(&cobra.Command{
Use: "update-settings",
Short: "Update harbor settings",
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
if err := harbor.UpdateSettings(getPlatform(cmd)); err != nil {
log.Fatalf("Error backing up harbor %s\n", err)
}
},
})
Harbor.AddCommand(&cobra.Command{
Use: "replicate-all",
Short: "Trigger a manual replication for all enabled jobs",
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
if err := harbor.ReplicateAll(getPlatform(cmd)); err != nil {
log.Fatalf("Error building harbor %s\n", err)
}
},
})
list := &cobra.Command{
Use: "list",
Short: "Commands to list objects",
}
list.AddCommand()
Harbor.AddCommand(list)
listProjects := &cobra.Command{
Use: "projects",
Short: "List projects in harbor",
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
platform := getPlatform(cmd)
projects, err := harbor.ListProjects(platform)
if err != nil {
log.Fatalf("Error listing projects: %v", err)
}
for _, project := range projects {
fmt.Println(project.Name)
}
},
}
listImages := &cobra.Command{
Use: "images",
Short: "List images in harbor",
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
platform := getPlatform(cmd)
listTags, _ := cmd.Flags().GetBool("tags")
concurrency, _ := cmd.Flags().GetInt("concurrency")
if listTags {
tagsCh, err := harbor.ListImagesWithTags(platform, concurrency)
if err != nil {
log.Fatalf("Error listing images: %v", err)
}
for {
tag, more := <-tagsCh
if more {
fmt.Printf("%s/%s:%s\n", tag.ProjectName, tag.RepositoryName, tag.Name)
} else {
break
}
}
} else {
imagesCh, err := harbor.ListImages(platform, concurrency)
if err != nil {
log.Fatalf("Error listing images: %v", err)
}
for {
image, more := <-imagesCh
if more {
fmt.Printf("%s/%s\n", image.ProjectName, image.Name)
} else {
break
}
}
}
},
}
listImages.Flags().Bool("tags", false, "List also tags for each image")
listImages.Flags().IntP("concurrency", "x", 8, "Number of goroutines to use")
list.AddCommand(listImages)
list.AddCommand(listProjects)
integrityCheck := &cobra.Command{
Use: "integrity-check",
Short: "Commands to check integrity of each manifest",
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
platform := getPlatform(cmd)
concurrency, _ := cmd.Flags().GetInt("concurrency")
outputFile, _ := cmd.Flags().GetString("output")
filename, _ := cmd.Flags().GetString("filename")
var brokenTagsCh chan harbor.Tag
var err error
if filename != "" {
brokenTagsCh, err = harbor.IntegrityCheckFromFile(platform, concurrency, filename)
} else {
brokenTagsCh, err = harbor.IntegrityCheck(platform, concurrency)
}
if err != nil {
log.Fatalf("Error checking tags: %v", err)
}
output := os.Stdout
if outputFile != "" {
f, err := os.Create(outputFile)
if err != nil {
log.Fatalf("Failed to write to filename %s: %v", outputFile, err)
}
output = f
}
count := 0
fmt.Fprintf(output, "artifacts:")
for {
tag, more := <-brokenTagsCh
if more {
count++
fmt.Fprintf(output, "\n- project: %s\n repository: %s\n tag: %s\n digest: %s", tag.ProjectName, tag.RepositoryName, tag.Name, tag.Digest)
} else {
break
}
}
if count == 0 {
fmt.Fprintf(output, " []\n")
}
fmt.Fprintf(output, "count: %d\n", count)
},
}
integrityCheck.Flags().IntP("concurrency", "x", 8, "Number of goroutines to use")
integrityCheck.Flags().StringP("filename", "f", "", "Filename to parse broken tags from")
integrityCheck.Flags().StringP("output", "o", "", "Write output to file")
Harbor.AddCommand(integrityCheck)
bulkDelete := &cobra.Command{
Use: "bulk-delete",
Short: "Delete tags/digests from an integrity-check output file",
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
platform := getPlatform(cmd)
concurrency, _ := cmd.Flags().GetInt("concurrency")
filename, _ := cmd.Flags().GetString("filename")
count, _ := cmd.Flags().GetInt("count")
if err := harbor.BulkDelete(platform, concurrency, filename, count); err != nil {
log.Fatalf("failed to delete tags: %v", err)
}
},
}
bulkDelete.Flags().IntP("concurrency", "x", 8, "Number of goroutines to use")
bulkDelete.Flags().StringP("filename", "f", "", "Filename to parse broken tags from")
bulkDelete.Flags().Int("count", 0, "Expected number of tags to be deleted")
Harbor.AddCommand(bulkDelete)
}