@@ -31,134 +31,57 @@ import (
3131)
3232
3333const (
34- timeout = time .Second * 20
34+ timeout = time .Second * 10
3535 duration = time .Second * 2
3636 interval = time .Millisecond * 250
3737 workspaceNamespace = "default"
3838)
3939
4040var _ = Describe ("WorkspaceCountController" , func () {
41- It ("should set scale-down-disabled when workspace is assigned to node" , func () {
42- // Create a workspace
43- name := uuid .NewString ()
44- ws := newWorkspace (name , workspaceNamespace , workspacev1 .WorkspacePhaseRunning )
45- createWorkspace (ws )
46-
47- // Update its runtime status with a node
48- updateObjWithRetries (k8sClient , ws , true , func (ws * workspacev1.Workspace ) {
49- ws .Status .Runtime = & workspacev1.WorkspaceRuntimeStatus {
50- NodeName : NodeName ,
51- }
52- })
53-
54- // Verify node gets annotated
55- Eventually (func (g Gomega ) {
56- var node corev1.Node
57- g .Expect (k8sClient .Get (ctx , types.NamespacedName {Name : NodeName }, & node )).To (Succeed ())
58- g .Expect (node .Annotations ).To (HaveKeyWithValue ("cluster-autoscaler.kubernetes.io/scale-down-disabled" , "true" ))
59- }, timeout , interval ).Should (Succeed ())
60- })
61-
6241 It ("should remove scale-down-disabled when last workspace is removed" , func () {
63- // Create two workspaces on the same node
64- ws1 := newWorkspace (uuid .NewString (), workspaceNamespace , workspacev1 .WorkspacePhaseRunning )
65- ws2 := newWorkspace (uuid .NewString (), workspaceNamespace , workspacev1 .WorkspacePhaseRunning )
42+ ws1 := newWorkspace (uuid .NewString (), workspaceNamespace , NodeName , workspacev1 .WorkspacePhaseRunning )
43+ ws2 := newWorkspace (uuid .NewString (), workspaceNamespace , NodeName , workspacev1 .WorkspacePhaseRunning )
44+ ws1 .Status .Runtime = nil
45+ ws2 .Status .Runtime = nil
6646 createWorkspace (ws1 )
6747 createWorkspace (ws2 )
6848
69- // Assign them to the node
49+ By ( "Assigning nodes to workspaces" )
7050 updateObjWithRetries (k8sClient , ws1 , true , func (ws * workspacev1.Workspace ) {
51+ ws .Status .Conditions = []metav1.Condition {}
7152 ws .Status .Runtime = & workspacev1.WorkspaceRuntimeStatus {
7253 NodeName : NodeName ,
7354 }
7455 })
56+
7557 updateObjWithRetries (k8sClient , ws2 , true , func (ws * workspacev1.Workspace ) {
58+ ws .Status .Conditions = []metav1.Condition {}
7659 ws .Status .Runtime = & workspacev1.WorkspaceRuntimeStatus {
7760 NodeName : NodeName ,
7861 }
7962 })
8063
81- // Verify node has annotation
64+ By ( "Verifying node annotation" )
8265 Eventually (func (g Gomega ) {
8366 var node corev1.Node
8467 g .Expect (k8sClient .Get (ctx , types.NamespacedName {Name : NodeName }, & node )).To (Succeed ())
8568 g .Expect (node .Annotations ).To (HaveKeyWithValue ("cluster-autoscaler.kubernetes.io/scale-down-disabled" , "true" ))
8669 }, timeout , interval ).Should (Succeed ())
8770
88- // Delete first workspace
71+ By ( "Deleting workspaces" )
8972 Expect (k8sClient .Delete (ctx , ws1 )).To (Succeed ())
90-
91- // Verify annotation still exists (second workspace still there)
92- Consistently (func (g Gomega ) {
93- var node corev1.Node
94- g .Expect (k8sClient .Get (ctx , types.NamespacedName {Name : NodeName }, & node )).To (Succeed ())
95- g .Expect (node .Annotations ).To (HaveKeyWithValue ("cluster-autoscaler.kubernetes.io/scale-down-disabled" , "true" ))
96- }, "2s" , "100ms" ).Should (Succeed ())
97-
98- // Delete second workspace
9973 Expect (k8sClient .Delete (ctx , ws2 )).To (Succeed ())
10074
101- // Verify annotation is removed
75+ By ( "Verifying final state" )
10276 Eventually (func (g Gomega ) {
10377 var node corev1.Node
10478 g .Expect (k8sClient .Get (ctx , types.NamespacedName {Name : NodeName }, & node )).To (Succeed ())
10579 g .Expect (node .Annotations ).ToNot (HaveKey ("cluster-autoscaler.kubernetes.io/scale-down-disabled" ))
10680 }, timeout , interval ).Should (Succeed ())
10781 })
108-
109- It ("should handle workspaces across multiple nodes" , func () {
110- const SecondNode = "second-node"
111-
112- // Create nodes
113- node2 := & corev1.Node {
114- ObjectMeta : metav1.ObjectMeta {
115- Name : SecondNode ,
116- },
117- }
118- Expect (k8sClient .Create (ctx , node2 )).To (Succeed ())
119-
120- // Create workspaces
121- ws1 := newWorkspace (uuid .NewString (), workspaceNamespace , workspacev1 .WorkspacePhaseRunning )
122- ws2 := newWorkspace (uuid .NewString (), workspaceNamespace , workspacev1 .WorkspacePhaseRunning )
123- createWorkspace (ws1 )
124- createWorkspace (ws2 )
125-
126- // Assign to different nodes
127- updateObjWithRetries (k8sClient , ws1 , true , func (ws * workspacev1.Workspace ) {
128- ws .Status .Runtime = & workspacev1.WorkspaceRuntimeStatus {
129- NodeName : NodeName ,
130- }
131- })
132- updateObjWithRetries (k8sClient , ws2 , true , func (ws * workspacev1.Workspace ) {
133- ws .Status .Runtime = & workspacev1.WorkspaceRuntimeStatus {
134- NodeName : SecondNode ,
135- }
136- })
137-
138- // Verify both nodes get annotated
139- Eventually (func (g Gomega ) {
140- var node1 , node2 corev1.Node
141- g .Expect (k8sClient .Get (ctx , types.NamespacedName {Name : NodeName }, & node1 )).To (Succeed ())
142- g .Expect (k8sClient .Get (ctx , types.NamespacedName {Name : SecondNode }, & node2 )).To (Succeed ())
143- g .Expect (node1 .Annotations ).To (HaveKeyWithValue ("cluster-autoscaler.kubernetes.io/scale-down-disabled" , "true" ))
144- g .Expect (node2 .Annotations ).To (HaveKeyWithValue ("cluster-autoscaler.kubernetes.io/scale-down-disabled" , "true" ))
145- }, timeout , interval ).Should (Succeed ())
146-
147- // Delete workspace on first node
148- Expect (k8sClient .Delete (ctx , ws1 )).To (Succeed ())
149-
150- // Verify first node's annotation is removed but second remains
151- Eventually (func (g Gomega ) {
152- var node1 , node2 corev1.Node
153- g .Expect (k8sClient .Get (ctx , types.NamespacedName {Name : NodeName }, & node1 )).To (Succeed ())
154- g .Expect (k8sClient .Get (ctx , types.NamespacedName {Name : SecondNode }, & node2 )).To (Succeed ())
155- g .Expect (node1 .Annotations ).ToNot (HaveKey ("cluster-autoscaler.kubernetes.io/scale-down-disabled" ))
156- g .Expect (node2 .Annotations ).To (HaveKeyWithValue ("cluster-autoscaler.kubernetes.io/scale-down-disabled" , "true" ))
157- }, timeout , interval ).Should (Succeed ())
158- })
15982})
16083
161- func newWorkspace (name , namespace string , phase workspacev1.WorkspacePhase ) * workspacev1.Workspace {
84+ func newWorkspace (name , namespace , nodeName string , phase workspacev1.WorkspacePhase ) * workspacev1.Workspace {
16285 GinkgoHelper ()
16386 initializer := & csapi.WorkspaceInitializer {
16487 Spec : & csapi.WorkspaceInitializer_Empty {Empty : & csapi.EmptyInitializer {}},
@@ -168,17 +91,19 @@ func newWorkspace(name, namespace string, phase workspacev1.WorkspacePhase) *wor
16891
16992 return & workspacev1.Workspace {
17093 Status : workspacev1.WorkspaceStatus {
171- Phase : phase ,
94+ Phase : phase ,
95+ Runtime : & workspacev1.WorkspaceRuntimeStatus {
96+ NodeName : nodeName ,
97+ },
17298 Conditions : []metav1.Condition {},
17399 },
174100 TypeMeta : metav1.TypeMeta {
175101 APIVersion : "workspace.gitpod.io/v1" ,
176102 Kind : "Workspace" ,
177103 },
178104 ObjectMeta : metav1.ObjectMeta {
179- Name : name ,
180- Namespace : namespace ,
181- Finalizers : []string {workspacev1 .GitpodFinalizerName },
105+ Name : name ,
106+ Namespace : namespace ,
182107 },
183108 Spec : workspacev1.WorkspaceSpec {
184109 Ownership : workspacev1.Ownership {
@@ -237,7 +162,7 @@ var (
237162 ctx context.Context
238163 cancel context.CancelFunc
239164 workspaceCtrl * WorkspaceCountController
240- NodeName = "ws-daemon -node"
165+ NodeName = "cool-ws -node"
241166)
242167
243168func TestAPIs (t * testing.T ) {
@@ -253,7 +178,7 @@ var _ = BeforeSuite(func() {
253178 testEnv = & envtest.Environment {
254179 ControlPlaneStartTimeout : 1 * time .Minute ,
255180 ControlPlaneStopTimeout : 1 * time .Minute ,
256- CRDDirectoryPaths : []string {filepath .Join (".." , ".." , " crd" )},
181+ CRDDirectoryPaths : []string {filepath .Join (".." , "crd" )},
257182 ErrorIfCRDPathMissing : true ,
258183 }
259184
@@ -276,6 +201,14 @@ var _ = BeforeSuite(func() {
276201 Expect (err ).ToNot (HaveOccurred ())
277202 ctx , cancel = context .WithCancel (context .Background ())
278203
204+ By ("Creating default ws node" )
205+ node := & corev1.Node {
206+ ObjectMeta : metav1.ObjectMeta {
207+ Name : NodeName ,
208+ },
209+ }
210+ Expect (k8sClient .Create (ctx , node )).To (Succeed ())
211+
279212 By ("Setting up workspace controller" )
280213 workspaceCtrl , err = NewWorkspaceCountController (k8sClient )
281214 Expect (err ).NotTo (HaveOccurred ())
0 commit comments