forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pull_secret.go
198 lines (169 loc) · 5.98 KB
/
pull_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
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
package controller_manager
import (
"strings"
"time"
g "github.com/onsi/ginkgo"
o "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
e2e "k8s.io/kubernetes/test/e2e/framework"
exutil "github.com/openshift/origin/test/extended/util"
)
func waitForServiceAccountToken(client kubernetes.Interface, ns, name string, attempts int, interval time.Duration) (string, error) {
for i := 0; i <= attempts; i++ {
time.Sleep(interval)
token, err := getServiceAccountToken(client, ns, name)
if err != nil {
return "", err
}
if len(token) > 0 {
return token, nil
}
}
return "", nil
}
func getServiceAccountToken(client kubernetes.Interface, ns, name string) (string, error) {
secrets, err := client.CoreV1().Secrets(ns).List(metav1.ListOptions{})
if err != nil {
return "", err
}
for _, secret := range secrets.Items {
if secret.Type == corev1.SecretTypeServiceAccountToken && secret.Annotations[corev1.ServiceAccountNameKey] == name {
sa, err := client.CoreV1().ServiceAccounts(ns).Get(name, metav1.GetOptions{})
if err != nil {
return "", err
}
for _, ref := range sa.Secrets {
if ref.Name == secret.Name {
return string(secret.Data[corev1.ServiceAccountTokenKey]), nil
}
}
}
}
return "", nil
}
var _ = g.Describe("[Feature:OpenShiftControllerManager]", func() {
defer g.GinkgoRecover()
oc := exutil.NewCLI("pull-secrets", exutil.KubeConfigPath())
g.It("TestAutomaticCreationOfPullSecrets", func() {
t := g.GinkgoT()
clusterAdminKubeClient := oc.AdminKubeClient()
saNamespace := oc.Namespace()
saName := "default"
// Get a service account token
saToken, err := waitForServiceAccountToken(clusterAdminKubeClient, saNamespace, saName, 20, time.Second)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if len(saToken) == 0 {
t.Errorf("token was not created")
}
// Get the matching dockercfg secret
_, saPullSecret, err := waitForServiceAccountPullSecret(clusterAdminKubeClient, saNamespace, saName, 20, time.Second)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if len(saPullSecret) == 0 {
t.Errorf("pull secret was not created")
}
imageConfig, err := oc.AdminConfigClient().ConfigV1().Images().Get("cluster", metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
if !strings.Contains(saPullSecret, imageConfig.Status.InternalRegistryHostname) {
t.Errorf("missing %q in %v", imageConfig.Status.InternalRegistryHostname, saPullSecret)
}
if len(imageConfig.Spec.ExternalRegistryHostnames) > 0 {
if !strings.Contains(saPullSecret, imageConfig.Spec.ExternalRegistryHostnames[0]) {
t.Errorf("missing %q in %v", imageConfig.Spec.ExternalRegistryHostnames[0], saPullSecret)
}
}
})
})
func waitForServiceAccountPullSecret(client kubernetes.Interface, ns, name string, attempts int, interval time.Duration) (string, string, error) {
for i := 0; i <= attempts; i++ {
time.Sleep(interval)
secretName, dockerCfg, err := getServiceAccountPullSecret(client, ns, name)
if err != nil {
return "", "", err
}
if len(dockerCfg) > 2 {
return secretName, dockerCfg, nil
}
}
return "", "", nil
}
func getServiceAccountPullSecret(client kubernetes.Interface, ns, name string) (string, string, error) {
secrets, err := client.CoreV1().Secrets(ns).List(metav1.ListOptions{})
if err != nil {
return "", "", err
}
for _, secret := range secrets.Items {
if secret.Type == corev1.SecretTypeDockercfg && secret.Annotations[corev1.ServiceAccountNameKey] == name {
return secret.Name, string(secret.Data[corev1.DockerConfigKey]), nil
}
}
return "", "", nil
}
var _ = g.Describe("[Feature:OpenShiftControllerManager]", func() {
defer g.GinkgoRecover()
oc := exutil.NewCLI("pull-secrets", exutil.KubeConfigPath())
g.AfterEach(func() {
if g.CurrentGinkgoTestDescription().Failed {
g.By("dumping openshift-controller-manager logs")
exutil.DumpPodLogsStartingWithInNamespace("controller-manager", "openshift-controller-manager", oc.AsAdmin())
}
})
g.It("TestDockercfgTokenDeletedController", func() {
t := g.GinkgoT()
clusterAdminKubeClient := oc.AdminKubeClient()
saNamespace := oc.Namespace()
g.By("creating test service account")
sa := &corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{Name: "sa1", Namespace: saNamespace},
}
sa, err := clusterAdminKubeClient.CoreV1().ServiceAccounts(sa.Namespace).Create(sa)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
g.By("waiting for service account's pull secret to be created")
dockercfgSecretName, _, err := waitForServiceAccountPullSecret(clusterAdminKubeClient, sa.Namespace, sa.Name, 20, time.Second)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(dockercfgSecretName) == 0 {
t.Fatal("pull secret was not created")
}
// Get the matching secret's name
dockercfgSecret, err := clusterAdminKubeClient.CoreV1().Secrets(sa.Namespace).Get(dockercfgSecretName, metav1.GetOptions{})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
secretName := dockercfgSecret.Annotations["openshift.io/token-secret.name"]
if len(secretName) == 0 {
t.Fatal("secret was not created")
}
g.By("deleting the token used to generate the pull secret")
if err := clusterAdminKubeClient.CoreV1().Secrets(sa.Namespace).Delete(secretName, nil); err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Expect the matching dockercfg secret to also be deleted
if err := wait.Poll(10*time.Second, 10*time.Minute, func() (bool, error) {
_, err := clusterAdminKubeClient.CoreV1().Secrets(sa.Namespace).Get(
dockercfgSecretName,
metav1.GetOptions{},
)
if err == nil {
e2e.Logf("secret %s/%s exists", sa.Namespace, dockercfgSecretName)
return false, nil
}
if errors.IsNotFound(err) {
return true, nil
}
return false, err
}); err != nil {
t.Fatalf("error waiting for pull secret deletion: %v", err)
}
})
})