-
Notifications
You must be signed in to change notification settings - Fork 474
/
secret.go
80 lines (69 loc) · 2.19 KB
/
secret.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
package kubernetes
import (
"io/ioutil"
yaml "gopkg.in/yaml.v2"
core_v1 "k8s.io/api/core/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type RemoteSecretCluster struct {
CertificateAuthorityData string `yaml:"certificate-authority-data"`
Server string `yaml:"server"`
}
type RemoteSecretClusterListItem struct {
Cluster RemoteSecretCluster `yaml:"cluster"`
Name string `yaml:"name"`
}
type RemoteSecretUser struct {
Name string `yaml:"name"`
User RemoteSecretUserToken `yaml:"user"`
}
type RemoteSecretUserToken struct {
Token string `yaml:"token"`
}
type RemoteSecret struct {
APIVersion string `yaml:"apiVersion"`
Clusters []RemoteSecretClusterListItem `yaml:"clusters"`
Contexts []struct {
Context struct {
Cluster string `yaml:"cluster"`
User string `yaml:"user"`
} `yaml:"context"`
Name string `yaml:"name"`
} `yaml:"contexts"`
CurrentContext string `yaml:"current-context"`
Kind string `yaml:"kind"`
Preferences struct {
} `yaml:"preferences"`
Users []RemoteSecretUser `yaml:"users"`
}
func GetRemoteSecret(path string) (*RemoteSecret, error) {
secretFile, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
return ParseRemoteSecretBytes(secretFile)
}
// GetSecrets returns a list of secrets for a given namespace.
// If selectorLabels is defined, the list will only contain services matching
// the specified label selector.
func (in *K8SClient) GetSecrets(namespace string, labelSelector string) ([]core_v1.Secret, error) {
listOptions := emptyListOptions
if len(labelSelector) > 0 {
listOptions = meta_v1.ListOptions{LabelSelector: labelSelector}
}
if secretsList, err := in.k8s.CoreV1().Secrets(namespace).List(in.ctx, listOptions); err == nil {
return secretsList.Items, nil
} else {
return []core_v1.Secret{}, err
}
}
// ParseRemoteSecretBytes parses a raw file containing a <Kubeconfig file> and returns
// the parsed file in a RemoteSecret structure.
func ParseRemoteSecretBytes(secretBytes []byte) (*RemoteSecret, error) {
secret := &RemoteSecret{}
err := yaml.Unmarshal(secretBytes, &secret)
if err != nil {
return nil, err
}
return secret, nil
}