@@ -2,23 +2,46 @@ package k3k_test
2
2
3
3
import (
4
4
"context"
5
+ "crypto/x509"
6
+ "errors"
5
7
"fmt"
6
- "strings"
7
8
"time"
8
9
9
10
. "github.com/onsi/ginkgo/v2"
10
11
. "github.com/onsi/gomega"
11
12
"github.com/rancher/k3k/k3k-kubelet/translate"
12
13
"github.com/rancher/k3k/pkg/apis/k3k.io/v1alpha1"
13
- "github.com/rancher/k3k/pkg/controller/certs"
14
- "github.com/rancher/k3k/pkg/controller/kubeconfig"
15
14
corev1 "k8s.io/api/core/v1"
16
15
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
17
- "k8s.io/client-go/kubernetes"
18
- "k8s.io/client-go/tools/clientcmd"
19
- clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
20
16
)
21
17
18
+ var _ = When ("k3k is installed" , func () {
19
+ It ("is in Running status" , func () {
20
+
21
+ // check that the controller is running
22
+ Eventually (func () bool {
23
+ opts := v1.ListOptions {LabelSelector : "app.kubernetes.io/name=k3k" }
24
+ podList , err := k8s .CoreV1 ().Pods ("k3k-system" ).List (context .Background (), opts )
25
+
26
+ Expect (err ).To (Not (HaveOccurred ()))
27
+ Expect (podList .Items ).To (Not (BeEmpty ()))
28
+
29
+ var isRunning bool
30
+ for _ , pod := range podList .Items {
31
+ if pod .Status .Phase == corev1 .PodRunning {
32
+ isRunning = true
33
+ break
34
+ }
35
+ }
36
+
37
+ return isRunning
38
+ }).
39
+ WithTimeout (time .Second * 10 ).
40
+ WithPolling (time .Second ).
41
+ Should (BeTrue ())
42
+ })
43
+ })
44
+
22
45
var _ = When ("a cluster is installed" , func () {
23
46
24
47
var namespace string
@@ -30,28 +53,29 @@ var _ = When("a cluster is installed", func() {
30
53
namespace = createdNS .Name
31
54
})
32
55
33
- It ("will be created in shared mode " , func () {
56
+ It ("can create a nginx pod " , func () {
34
57
ctx := context .Background ()
35
- containerIP , err := k3sContainer .ContainerIP (ctx )
36
- Expect (err ).To (Not (HaveOccurred ()))
37
-
38
- fmt .Fprintln (GinkgoWriter , "K3s containerIP: " + containerIP )
39
58
40
59
cluster := v1alpha1.Cluster {
41
60
ObjectMeta : v1.ObjectMeta {
42
61
Name : "mycluster" ,
43
62
Namespace : namespace ,
44
63
},
45
64
Spec : v1alpha1.ClusterSpec {
46
- TLSSANs : []string {containerIP },
65
+ TLSSANs : []string {hostIP },
47
66
Expose : & v1alpha1.ExposeConfig {
48
67
NodePort : & v1alpha1.NodePortConfig {
49
68
Enabled : true ,
50
69
},
51
70
},
52
71
},
53
72
}
54
- virtualK8sClient := CreateCluster (containerIP , cluster )
73
+
74
+ By (fmt .Sprintf ("Creating virtual cluster %s/%s" , cluster .Namespace , cluster .Name ))
75
+ NewVirtualCluster (cluster )
76
+
77
+ By ("Waiting to get a kubernetes client for the virtual cluster" )
78
+ virtualK8sClient := NewVirtualK8sClient (cluster )
55
79
56
80
nginxPod := & corev1.Pod {
57
81
ObjectMeta : v1.ObjectMeta {
@@ -65,7 +89,7 @@ var _ = When("a cluster is installed", func() {
65
89
}},
66
90
},
67
91
}
68
- nginxPod , err = virtualK8sClient .CoreV1 ().Pods (nginxPod .Namespace ).Create (ctx , nginxPod , v1.CreateOptions {})
92
+ nginxPod , err : = virtualK8sClient .CoreV1 ().Pods (nginxPod .Namespace ).Create (ctx , nginxPod , v1.CreateOptions {})
69
93
Expect (err ).To (Not (HaveOccurred ()))
70
94
71
95
// check that the nginx Pod is up and running in the host cluster
@@ -78,12 +102,12 @@ var _ = When("a cluster is installed", func() {
78
102
resourceName := pod .Annotations [translate .ResourceNameAnnotation ]
79
103
resourceNamespace := pod .Annotations [translate .ResourceNamespaceAnnotation ]
80
104
81
- fmt .Fprintf (GinkgoWriter ,
82
- "pod=%s resource=%s/%s status=%s\n " ,
83
- pod .Name , resourceNamespace , resourceName , pod .Status .Phase ,
84
- )
85
-
86
105
if resourceName == nginxPod .Name && resourceNamespace == nginxPod .Namespace {
106
+ fmt .Fprintf (GinkgoWriter ,
107
+ "pod=%s resource=%s/%s status=%s\n " ,
108
+ pod .Name , resourceNamespace , resourceName , pod .Status .Phase ,
109
+ )
110
+
87
111
return pod .Status .Phase == corev1 .PodRunning
88
112
}
89
113
}
@@ -94,73 +118,81 @@ var _ = When("a cluster is installed", func() {
94
118
WithPolling (time .Second * 5 ).
95
119
Should (BeTrue ())
96
120
})
97
- })
98
121
99
- func CreateCluster ( hostIP string , cluster v1alpha1. Cluster ) * kubernetes. Clientset {
100
- GinkgoHelper ()
122
+ It ( "regenerates the bootstrap secret after a restart" , func () {
123
+ ctx := context . Background ()
101
124
102
- By (fmt .Sprintf ("Creating virtual cluster %s/%s" , cluster .Namespace , cluster .Name ))
125
+ cluster := v1alpha1.Cluster {
126
+ ObjectMeta : v1.ObjectMeta {
127
+ Name : "mycluster" ,
128
+ Namespace : namespace ,
129
+ },
130
+ Spec : v1alpha1.ClusterSpec {
131
+ TLSSANs : []string {hostIP },
132
+ Expose : & v1alpha1.ExposeConfig {
133
+ NodePort : & v1alpha1.NodePortConfig {
134
+ Enabled : true ,
135
+ },
136
+ },
137
+ },
138
+ }
139
+
140
+ By (fmt .Sprintf ("Creating virtual cluster %s/%s" , cluster .Namespace , cluster .Name ))
141
+ NewVirtualCluster (cluster )
103
142
104
- ctx := context .Background ()
105
- err := k8sClient .Create (ctx , & cluster )
106
- Expect (err ).To (Not (HaveOccurred ()))
143
+ By ("Waiting to get a kubernetes client for the virtual cluster" )
144
+ virtualK8sClient := NewVirtualK8sClient (cluster )
107
145
108
- By ("Waiting for server and kubelet to be ready" )
146
+ _ , err := virtualK8sClient .DiscoveryClient .ServerVersion ()
147
+ Expect (err ).To (Not (HaveOccurred ()))
109
148
110
- // check that the server Pod and the Kubelet are in Ready state
111
- Eventually (func () bool {
112
- podList , err := k8s .CoreV1 ().Pods (cluster .Namespace ).List (ctx , v1.ListOptions {})
149
+ labelSelector := "cluster=" + cluster .Name + ",role=server"
150
+ serverPods , err := k8s .CoreV1 ().Pods (namespace ).List (ctx , v1.ListOptions {LabelSelector : labelSelector })
113
151
Expect (err ).To (Not (HaveOccurred ()))
114
152
115
- serverRunning := false
116
- kubeletRunning := false
153
+ Expect ( len ( serverPods . Items )). To ( Equal ( 1 ))
154
+ serverPod := serverPods . Items [ 0 ]
117
155
118
- for _ , pod := range podList .Items {
119
- imageName := pod .Spec .Containers [0 ].Image
120
- imageName = strings .Split (imageName , ":" )[0 ] // remove tag
156
+ fmt .Fprintf (GinkgoWriter , "deleting pod %s/%s\n " , serverPod .Namespace , serverPod .Name )
157
+ // GracePeriodSeconds: ptr.To[int64](0)
158
+ err = k8s .CoreV1 ().Pods (namespace ).Delete (ctx , serverPod .Name , v1.DeleteOptions {})
159
+ Expect (err ).To (Not (HaveOccurred ()))
121
160
122
- switch imageName {
123
- case "rancher/k3s" :
124
- serverRunning = pod .Status .Phase == corev1 .PodRunning
125
- case "rancher/k3k-kubelet" :
126
- kubeletRunning = pod .Status .Phase == corev1 .PodRunning
127
- }
161
+ By ("Deleting server pod" )
128
162
129
- if serverRunning && kubeletRunning {
130
- return true
131
- }
132
- }
163
+ // check that the server pods restarted
164
+ Eventually (func () any {
165
+ serverPods , err = k8s .CoreV1 ().Pods (namespace ).List (ctx , v1.ListOptions {LabelSelector : labelSelector })
166
+ Expect (err ).To (Not (HaveOccurred ()))
167
+ Expect (len (serverPods .Items )).To (Equal (1 ))
168
+ return serverPods .Items [0 ].DeletionTimestamp
169
+ }).
170
+ WithTimeout (time .Minute ).
171
+ WithPolling (time .Second * 5 ).
172
+ Should (BeNil ())
173
+
174
+ By ("Server pod up and running again" )
175
+
176
+ By ("Using old k8s client configuration should fail" )
177
+
178
+ Eventually (func () bool {
179
+ _ , err = virtualK8sClient .DiscoveryClient .ServerVersion ()
180
+ var unknownAuthorityErr x509.UnknownAuthorityError
181
+ return errors .As (err , & unknownAuthorityErr )
182
+ }).
183
+ WithTimeout (time .Minute * 2 ).
184
+ WithPolling (time .Second * 5 ).
185
+ Should (BeTrue ())
133
186
134
- return false
135
- }).
136
- WithTimeout (time .Minute ).
137
- WithPolling (time .Second * 5 ).
138
- Should (BeTrue ())
139
-
140
- By ("Waiting for server to be up and running" )
141
-
142
- var config * clientcmdapi.Config
143
- Eventually (func () error {
144
- vKubeconfig := kubeconfig .New ()
145
- vKubeconfig .AltNames = certs .AddSANs ([]string {hostIP , "k3k-mycluster-kubelet" })
146
- config , err = vKubeconfig .Extract (ctx , k8sClient , & cluster , hostIP )
147
- return err
148
- }).
149
- WithTimeout (time .Minute * 2 ).
150
- WithPolling (time .Second * 5 ).
151
- Should (BeNil ())
152
-
153
- configData , err := clientcmd .Write (* config )
154
- Expect (err ).To (Not (HaveOccurred ()))
155
-
156
- restcfg , err := clientcmd .RESTConfigFromKubeConfig (configData )
157
- Expect (err ).To (Not (HaveOccurred ()))
158
- virtualK8sClient , err := kubernetes .NewForConfig (restcfg )
159
- Expect (err ).To (Not (HaveOccurred ()))
160
-
161
- serverVersion , err := virtualK8sClient .DiscoveryClient .ServerVersion ()
162
- Expect (err ).To (Not (HaveOccurred ()))
163
- fmt .Fprintf (GinkgoWriter , "serverVersion: %+v\n " , serverVersion )
164
-
165
- return virtualK8sClient
166
- }
187
+ By ("Recover new config should succeed" )
188
+
189
+ Eventually (func () error {
190
+ virtualK8sClient = NewVirtualK8sClient (cluster )
191
+ _ , err = virtualK8sClient .DiscoveryClient .ServerVersion ()
192
+ return err
193
+ }).
194
+ WithTimeout (time .Minute * 2 ).
195
+ WithPolling (time .Second * 5 ).
196
+ Should (BeNil ())
197
+ })
198
+ })
0 commit comments