forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
options.go
205 lines (178 loc) · 5.15 KB
/
options.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
204
205
package secrets
import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
kapi "k8s.io/kubernetes/pkg/api"
kcoreclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/internalversion"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/resource"
)
// SecretOptions Structure holding state for processing secret linking and
// unlinking.
type SecretOptions struct {
TargetName string
SecretNames []string
typeFlags []string
Namespace string
BuilderFunc func(bool) *resource.Builder
KubeCoreClient kcoreclient.CoreInterface
Out io.Writer
}
// Complete Parses the command line arguments and populates SecretOptions
func (o *SecretOptions) Complete(f kcmdutil.Factory, args []string) error {
if len(args) < 2 {
return errors.New("must have service account name and at least one secret name")
}
o.TargetName = args[0]
o.SecretNames = args[1:]
o.BuilderFunc = f.NewBuilder
var err error
kubeClientSet, err := f.ClientSet()
if err != nil {
return err
}
o.KubeCoreClient = kubeClientSet.Core()
o.Namespace, _, err = f.DefaultNamespace()
if err != nil {
return err
}
return nil
}
// Validate Ensures that all arguments have appropriate values
func (o SecretOptions) Validate() error {
if len(o.TargetName) == 0 {
return errors.New("service account name must be present")
}
if len(o.SecretNames) == 0 {
return errors.New("secret name must be present")
}
if o.KubeCoreClient == nil {
return errors.New("KubeCoreClient must be present")
}
// if any secret names are of the form <resource>/<name>,
// ensure <resource> is a secret.
for _, secretName := range o.SecretNames {
if segs := strings.Split(secretName, "/"); len(segs) > 1 {
if segs[0] != "secret" && segs[0] != "secrets" {
return errors.New(fmt.Sprintf("expected resource of type secret, got %q", secretName))
}
}
}
return nil
}
// GetServiceAccount Retrieve the service account object specified by the command
func (o SecretOptions) GetServiceAccount() (*kapi.ServiceAccount, error) {
r := o.BuilderFunc(true).
NamespaceParam(o.Namespace).
ResourceNames("serviceaccounts", o.TargetName).
SingleResourceType().
Do()
if r.Err() != nil {
return nil, r.Err()
}
obj, err := r.Object()
if err != nil {
return nil, err
}
switch t := obj.(type) {
case *kapi.ServiceAccount:
return t, nil
default:
return nil, fmt.Errorf("unhandled object: %#v", t)
}
}
// GetSecretNames Get a list of the names of the secrets in a set of them
func (o SecretOptions) GetSecretNames(secrets []*kapi.Secret) sets.String {
names := sets.String{}
for _, secret := range secrets {
names.Insert(parseSecretName(secret.Name))
}
return names
}
// parseSecretName receives a resource name as either
// <resource type> / <name> or <name> and returns only the resource <name>.
func parseSecretName(name string) string {
segs := strings.Split(name, "/")
if len(segs) < 2 {
return name
}
return segs[1]
}
// GetMountSecretNames Get a list of the names of the mount secrets associated
// with a service account
func (o SecretOptions) GetMountSecretNames(serviceaccount *kapi.ServiceAccount) sets.String {
names := sets.String{}
for _, secret := range serviceaccount.Secrets {
names.Insert(secret.Name)
}
return names
}
// GetPullSecretNames Get a list of the names of the pull secrets associated
// with a service account.
func (o SecretOptions) GetPullSecretNames(serviceaccount *kapi.ServiceAccount) sets.String {
names := sets.String{}
for _, secret := range serviceaccount.ImagePullSecrets {
names.Insert(secret.Name)
}
return names
}
// GetOut Retrieve the output writer
func (o SecretOptions) GetOut() io.Writer {
if o.Out == nil {
return ioutil.Discard
}
return o.Out
}
// GetSecrets Return a list of secret objects in the default namespace
// If allowNonExisting is set to true, we will return the non-existing secrets as well.
func (o SecretOptions) GetSecrets(allowNonExisting bool) ([]*kapi.Secret, bool, error) {
secrets := []*kapi.Secret{}
hasNotFound := false
for _, secretName := range o.SecretNames {
r := o.BuilderFunc(true).
NamespaceParam(o.Namespace).
ResourceNames("secrets", secretName).
SingleResourceType().
Do()
if r.Err() != nil {
return nil, false, r.Err()
}
obj, err := r.Object()
if err != nil {
// If the secret is not found it means it was deleted but we want still to allow to
// unlink a removed secret from the service account
if kerrors.IsNotFound(err) {
fmt.Fprintf(os.Stderr, "secret %q not found\n", secretName)
hasNotFound = true
if allowNonExisting {
obj = &kapi.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: secretName,
},
}
} else {
continue
}
} else if err != nil {
return nil, false, err
}
}
switch t := obj.(type) {
case *kapi.Secret:
secrets = append(secrets, t)
default:
return nil, false, fmt.Errorf("unhandled object: %#v", t)
}
}
if len(secrets) == 0 {
return nil, false, errors.New("No valid secrets found")
}
return secrets, hasNotFound, nil
}