-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add karpenter labels to nodeClaim (#373)
**Reason for Change**: - Add `DoNotDisrupt=true` label to prevent Karpenter from scaling down. - Make sure to add Fake nodepool name to prevent Karpenter from scaling up. - Add Unit tests for generate Nodeclaim and for workspace status. **Requirements** - [x] added unit tests and e2e tests (if applicable). **Issue Fixed**: <!-- If this PR fixes GitHub issue 4321, add "Fixes #4321" to the next line. --> **Notes for Reviewers**: Signed-off-by: Heba Elayoty <hebaelayoty@gmail.com>
- Loading branch information
Showing
4 changed files
with
183 additions
and
25 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
package controllers | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
kaitov1alpha1 "github.com/azure/kaito/api/v1alpha1" | ||
"github.com/azure/kaito/pkg/utils/test" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
func TestUpdateWorkspaceStatus(t *testing.T) { | ||
t.Run("Should update workspace status successfully", func(t *testing.T) { | ||
mockClient := test.NewClient() | ||
reconciler := &WorkspaceReconciler{ | ||
Client: mockClient, | ||
Scheme: test.NewTestScheme(), | ||
} | ||
ctx := context.Background() | ||
workspace := test.MockWorkspaceDistributedModel | ||
condition := metav1.Condition{ | ||
Type: "TestCondition", | ||
Status: metav1.ConditionStatus("True"), | ||
Reason: "TestReason", | ||
Message: "TestMessage", | ||
} | ||
workerNodes := []string{"node1", "node2"} | ||
|
||
mockClient.On("Get", mock.IsType(context.Background()), mock.Anything, mock.IsType(&kaitov1alpha1.Workspace{}), mock.Anything).Return(nil) | ||
mockClient.StatusMock.On("Update", mock.IsType(context.Background()), mock.IsType(&kaitov1alpha1.Workspace{}), mock.Anything).Return(nil) | ||
|
||
err := reconciler.updateWorkspaceStatus(ctx, &client.ObjectKey{Name: workspace.Name, Namespace: workspace.Namespace}, &condition, workerNodes) | ||
assert.Nil(t, err) | ||
}) | ||
|
||
t.Run("Should return error when Get operation fails", func(t *testing.T) { | ||
mockClient := test.NewClient() | ||
reconciler := &WorkspaceReconciler{ | ||
Client: mockClient, | ||
Scheme: test.NewTestScheme(), | ||
} | ||
ctx := context.Background() | ||
workspace := test.MockWorkspaceDistributedModel | ||
condition := metav1.Condition{ | ||
Type: "TestCondition", | ||
Status: metav1.ConditionStatus("True"), | ||
Reason: "TestReason", | ||
Message: "TestMessage", | ||
} | ||
workerNodes := []string{"node1", "node2"} | ||
|
||
mockClient.On("Get", mock.IsType(context.Background()), mock.Anything, mock.IsType(&kaitov1alpha1.Workspace{}), mock.Anything).Return(errors.New("Get operation failed")) | ||
|
||
err := reconciler.updateWorkspaceStatus(ctx, &client.ObjectKey{Name: workspace.Name, Namespace: workspace.Namespace}, &condition, workerNodes) | ||
assert.NotNil(t, err) | ||
}) | ||
|
||
t.Run("Should return nil when workspace is not found", func(t *testing.T) { | ||
mockClient := test.NewClient() | ||
reconciler := &WorkspaceReconciler{ | ||
Client: mockClient, | ||
Scheme: test.NewTestScheme(), | ||
} | ||
ctx := context.Background() | ||
workspace := test.MockWorkspaceDistributedModel | ||
condition := metav1.Condition{ | ||
Type: "TestCondition", | ||
Status: metav1.ConditionStatus("True"), | ||
Reason: "TestReason", | ||
Message: "TestMessage", | ||
} | ||
workerNodes := []string{"node1", "node2"} | ||
|
||
mockClient.On("Get", mock.IsType(context.Background()), mock.Anything, mock.IsType(&kaitov1alpha1.Workspace{}), mock.Anything).Return(apierrors.NewNotFound(schema.GroupResource{}, "workspace")) | ||
|
||
err := reconciler.updateWorkspaceStatus(ctx, &client.ObjectKey{Name: workspace.Name, Namespace: workspace.Namespace}, &condition, workerNodes) | ||
assert.Nil(t, err) | ||
}) | ||
} | ||
|
||
func TestUpdateStatusConditionIfNotMatch(t *testing.T) { | ||
t.Run("Should not update when condition matches", func(t *testing.T) { | ||
mockClient := test.NewClient() | ||
reconciler := &WorkspaceReconciler{ | ||
Client: mockClient, | ||
Scheme: test.NewTestScheme(), | ||
} | ||
ctx := context.Background() | ||
workspace := test.MockWorkspaceDistributedModel | ||
conditionType := kaitov1alpha1.ConditionType("TestCondition") | ||
conditionStatus := metav1.ConditionStatus("True") | ||
conditionReason := "TestReason" | ||
conditionMessage := "TestMessage" | ||
|
||
workspace.Status.Conditions = []metav1.Condition{ | ||
{ | ||
Type: string(conditionType), | ||
Status: conditionStatus, | ||
Reason: conditionReason, | ||
Message: conditionMessage, | ||
}, | ||
} | ||
|
||
err := reconciler.updateStatusConditionIfNotMatch(ctx, workspace, conditionType, conditionStatus, conditionReason, conditionMessage) | ||
assert.Nil(t, err) | ||
}) | ||
|
||
t.Run("Should update when condition does not match", func(t *testing.T) { | ||
mockClient := test.NewClient() | ||
reconciler := &WorkspaceReconciler{ | ||
Client: mockClient, | ||
Scheme: test.NewTestScheme(), | ||
} | ||
ctx := context.Background() | ||
workspace := test.MockWorkspaceDistributedModel | ||
conditionType := kaitov1alpha1.ConditionType("TestCondition") | ||
conditionStatus := metav1.ConditionStatus("True") | ||
conditionReason := "TestReason" | ||
conditionMessage := "TestMessage" | ||
|
||
workspace.Status.Conditions = []metav1.Condition{ | ||
{ | ||
Type: string(conditionType), | ||
Status: conditionStatus, | ||
Reason: conditionReason, | ||
Message: "DifferentMessage", | ||
}, | ||
} | ||
mockClient.On("Get", mock.IsType(context.Background()), mock.Anything, mock.IsType(&kaitov1alpha1.Workspace{}), mock.Anything).Return(nil) | ||
mockClient.StatusMock.On("Update", mock.IsType(context.Background()), mock.IsType(&kaitov1alpha1.Workspace{}), mock.Anything).Return(nil) | ||
|
||
err := reconciler.updateStatusConditionIfNotMatch(ctx, workspace, conditionType, conditionStatus, conditionReason, conditionMessage) | ||
assert.Nil(t, err) | ||
}) | ||
|
||
t.Run("Should update when condition is not found", func(t *testing.T) { | ||
mockClient := test.NewClient() | ||
reconciler := &WorkspaceReconciler{ | ||
Client: mockClient, | ||
Scheme: test.NewTestScheme(), | ||
} | ||
ctx := context.Background() | ||
workspace := test.MockWorkspaceDistributedModel | ||
conditionType := kaitov1alpha1.ConditionType("TestCondition") | ||
conditionStatus := metav1.ConditionStatus("True") | ||
conditionReason := "TestReason" | ||
conditionMessage := "TestMessage" | ||
|
||
workspace.Status.Conditions = []metav1.Condition{ | ||
{ | ||
Type: "DifferentCondition", | ||
Status: conditionStatus, | ||
Reason: conditionReason, | ||
Message: conditionMessage, | ||
}, | ||
} | ||
mockClient.On("Get", mock.IsType(context.Background()), mock.Anything, mock.IsType(&kaitov1alpha1.Workspace{}), mock.Anything).Return(nil) | ||
mockClient.StatusMock.On("Update", mock.IsType(context.Background()), mock.IsType(&kaitov1alpha1.Workspace{}), mock.Anything).Return(nil) | ||
|
||
err := reconciler.updateStatusConditionIfNotMatch(ctx, workspace, conditionType, conditionStatus, conditionReason, conditionMessage) | ||
assert.Nil(t, err) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters