-
Notifications
You must be signed in to change notification settings - Fork 20
/
sks_authority_cert.go
81 lines (66 loc) · 1.84 KB
/
sks_authority_cert.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
package cmd
import (
"encoding/base64"
"fmt"
"strings"
exoapi "github.com/exoscale/egoscale/v2/api"
"github.com/spf13/cobra"
)
var sksAuthorityCertAuthorities = []string{
"aggregation",
"kubelet",
}
var sksAuthorityCertCmd = &cobra.Command{
Use: "authority-cert CLUSTER-NAME|ID AUTHORITY",
Short: "Retrieve an authority certificate for a SKS cluster",
Long: fmt.Sprintf(`This command retrieves the certificate content for the specified Kubernetes
cluster authority. Supported authorities:
Supported authorities: %s`,
strings.Join(sksAuthorityCertAuthorities, ", ")),
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 2 {
cmdExitOnUsageError(cmd, "invalid arguments")
}
var authOK bool
for _, v := range sksAuthorityCertAuthorities {
if args[1] == v {
authOK = true
break
}
}
if !authOK {
cmdExitOnUsageError(cmd, fmt.Sprintf("unsupported authority value %q", args[1]))
}
cmdSetZoneFlagFromDefault(cmd)
return cmdCheckRequiredFlags(cmd, []string{"zone"})
},
RunE: func(cmd *cobra.Command, args []string) error {
var (
c = args[0]
authority = args[1]
)
zone, err := cmd.Flags().GetString("zone")
if err != nil {
return err
}
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(gCurrentAccount.Environment, zone))
cluster, err := lookupSKSCluster(ctx, zone, c)
if err != nil {
return err
}
b64Cert, err := cluster.AuthorityCert(ctx, authority)
if err != nil {
return fmt.Errorf("error retrieving certificate: %s", err)
}
cert, err := base64.StdEncoding.DecodeString(b64Cert)
if err != nil {
return fmt.Errorf("error decoding certificate content: %s", err)
}
fmt.Print(string(cert))
return nil
},
}
func init() {
sksAuthorityCertCmd.Flags().StringP("zone", "z", "", "SKS cluster zone")
sksCmd.AddCommand(sksAuthorityCertCmd)
}