-
Notifications
You must be signed in to change notification settings - Fork 23
/
registry_add_gcr.go
88 lines (70 loc) · 2.63 KB
/
registry_add_gcr.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
package cmd
import (
"strings"
"github.com/pkg/errors"
"github.com/replicatedhq/replicated/cli/print"
"github.com/replicatedhq/replicated/pkg/kotsclient"
"github.com/replicatedhq/replicated/pkg/types"
"github.com/spf13/cobra"
)
func (r *runners) InitRegistryAddGCR(parent *cobra.Command) {
cmd := &cobra.Command{
Use: "gcr",
Short: "Add a Google Container Registry",
Long: `Add a Google Container Registry using a service account key`,
SilenceUsage: true,
}
parent.AddCommand(cmd)
cmd.Flags().StringVar(&r.args.addRegistryEndpoint, "endpoint", "", "The GCR endpoint")
cmd.Flags().StringVar(&r.args.addRegistryServiceAccountKey, "serviceaccountkey", "", "The service account key to authenticate to the registry with")
cmd.Flags().BoolVar(&r.args.addRegistryServiceAccountKeyFromStdIn, "serviceaccountkey-stdin", false, "Take the service account key from stdin")
cmd.Flags().StringVar(&r.outputFormat, "output", "table", "The output format to use. One of: json|table (default: table)")
cmd.RunE = r.registryAddGCR
}
func (r *runners) registryAddGCR(cmd *cobra.Command, args []string) error {
if r.args.addRegistryServiceAccountKeyFromStdIn {
var err error
serviceAccountKey, err := r.readPasswordFromStdIn("Service Account Key")
if err != nil {
return errors.Wrap(err, "read secret service account key from stdin")
}
r.args.addRegistryServiceAccountKey = serviceAccountKey
}
addRegistryRequest, errs := r.validateRegistryAddGCR()
if len(errs) > 0 {
joinedErrs := []string{}
for _, err := range errs {
joinedErrs = append(joinedErrs, err.Error())
}
return errors.New(strings.Join(joinedErrs, ", "))
}
addRegistryRequest.SkipValidation = r.args.addRegistrySkipValidation
kotsRestClient := kotsclient.VendorV3Client{HTTPClient: *r.platformAPI}
registry, err := kotsRestClient.AddKOTSRegistry(addRegistryRequest)
if err != nil {
return errors.Wrap(err, "add registry")
}
registries := []types.Registry{
*registry,
}
return print.Registries(r.outputFormat, r.w, registries)
}
func (r *runners) validateRegistryAddGCR() (kotsclient.AddKOTSRegistryRequest, []error) {
req := kotsclient.AddKOTSRegistryRequest{
Provider: "gcr",
AuthType: "serviceaccount",
Username: "_json_key",
}
errs := []error{}
if r.args.addRegistryEndpoint == "" {
errs = append(errs, errors.New("endpoint must be specified"))
} else {
req.Endpoint = r.args.addRegistryEndpoint
}
if r.args.addRegistryServiceAccountKey == "" {
errs = append(errs, errors.New("serviceaccountkey or serviceaccountkey-stdin must be specified"))
} else {
req.Password = r.args.addRegistryServiceAccountKey
}
return req, errs
}