Skip to content

Commit

Permalink
Merge branch 'controller' of github.com:pluralsh/console into controller
Browse files Browse the repository at this point in the history
  • Loading branch information
floreks committed Dec 14, 2023
2 parents 614e0f3 + 99b3c0f commit d8f3d60
Show file tree
Hide file tree
Showing 2 changed files with 354 additions and 19 deletions.
217 changes: 199 additions & 18 deletions controller/pkg/reconciler/cluster_reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,30 +62,22 @@ func TestCreateNewCluster(t *testing.T) {
returnCreateCluster: &gqlclient.ClusterFragment{ID: clusterConsoleID},
existingObjects: []ctrlruntimeclient.Object{
&v1alpha1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: clusterName,
},
ObjectMeta: metav1.ObjectMeta{Name: clusterName},
Spec: v1alpha1.ClusterSpec{
Handle: lo.ToPtr(clusterName),
Version: lo.ToPtr("1.24"),
Cloud: "aws",
ProviderRef: &corev1.ObjectReference{
Name: providerName,
},
Handle: lo.ToPtr(clusterName),
Version: lo.ToPtr("1.24"),
Cloud: "aws",
ProviderRef: &corev1.ObjectReference{Name: providerName},
},
},
&v1alpha1.Provider{
ObjectMeta: metav1.ObjectMeta{
Name: providerName,
},
ObjectMeta: metav1.ObjectMeta{Name: providerName},
Spec: v1alpha1.ProviderSpec{
Cloud: "aws",
Name: providerName,
Namespace: providerNamespace,
},
Status: v1alpha1.ProviderStatus{
ID: lo.ToPtr(providerConsoleID),
},
Status: v1alpha1.ProviderStatus{ID: lo.ToPtr(providerConsoleID)},
},
},
},
Expand All @@ -103,9 +95,7 @@ func TestCreateNewCluster(t *testing.T) {
returnCreateCluster: &gqlclient.ClusterFragment{ID: clusterConsoleID},
existingObjects: []ctrlruntimeclient.Object{
&v1alpha1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: clusterName,
},
ObjectMeta: metav1.ObjectMeta{Name: clusterName},
Spec: v1alpha1.ClusterSpec{
Handle: lo.ToPtr(clusterName),
Cloud: "byok",
Expand Down Expand Up @@ -143,3 +133,194 @@ func TestCreateNewCluster(t *testing.T) {
})
}
}

func TestUpdateCluster(t *testing.T) {
const (
clusterName = "test-cluster"
clusterConsoleID = "12345-67890"
providerName = "test-provider"
providerNamespace = "test-provider"
providerConsoleID = "12345-67890"
)

tests := []struct {
name string
cluster string
returnIsClusterExisting bool
returnUpdateCluster *gqlclient.ClusterFragment
returnErrorUpdateCluster error
existingObjects []ctrlruntimeclient.Object
expectedStatus v1alpha1.ClusterStatus
}{
{
name: "scenario 1: update AWS cluster",
cluster: clusterName,
expectedStatus: v1alpha1.ClusterStatus{
ID: lo.ToPtr(clusterConsoleID),
SHA: lo.ToPtr("DU5PTA62PGOS35CPPCNSRG6PGXUUIWTXVBK5BFXCCGCAAM2K6HYA===="),
Existing: lo.ToPtr(false),
},
returnIsClusterExisting: true,
returnUpdateCluster: &gqlclient.ClusterFragment{ID: clusterConsoleID},
existingObjects: []ctrlruntimeclient.Object{
&v1alpha1.Cluster{
ObjectMeta: metav1.ObjectMeta{Name: clusterName},
Spec: v1alpha1.ClusterSpec{
Handle: lo.ToPtr(clusterName),
Version: lo.ToPtr("1.24"),
Cloud: "aws",
ProviderRef: &corev1.ObjectReference{Name: providerName},
},
Status: v1alpha1.ClusterStatus{
ID: lo.ToPtr(clusterConsoleID),
SHA: lo.ToPtr("XGLLQCLXY5LEQV2UAQDUSOZ2MN24L67HDIGWRK2MA5STBBRNMVDA===="),
Existing: lo.ToPtr(false),
},
},
&v1alpha1.Provider{
ObjectMeta: metav1.ObjectMeta{Name: providerName},
Spec: v1alpha1.ProviderSpec{
Cloud: "aws",
Name: providerName,
Namespace: providerNamespace,
},
Status: v1alpha1.ProviderStatus{ID: lo.ToPtr(providerConsoleID)},
},
},
},
{
name: "scenario 2: update BYOK cluster",
cluster: clusterName,
expectedStatus: v1alpha1.ClusterStatus{
ID: lo.ToPtr(clusterConsoleID),
SHA: lo.ToPtr("XGLLQCLXY5LEQV2UAQDUSOZ2MN24L67HDIGWRK2MA5STBBRNMVDA===="),
Existing: lo.ToPtr(false),
},
returnIsClusterExisting: true,
returnUpdateCluster: &gqlclient.ClusterFragment{ID: clusterConsoleID},
existingObjects: []ctrlruntimeclient.Object{
&v1alpha1.Cluster{
ObjectMeta: metav1.ObjectMeta{Name: clusterName},
Spec: v1alpha1.ClusterSpec{
Handle: lo.ToPtr(clusterName),
Cloud: "byok",
},
Status: v1alpha1.ClusterStatus{
ID: lo.ToPtr(clusterConsoleID),
SHA: lo.ToPtr("DU5PTA62PGOS35CPPCNSRG6PGXUUIWTXVBK5BFXCCGCAAM2K6HYA===="),
Existing: lo.ToPtr(false),
},
},
},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
fakeClient := fake.NewClientBuilder().WithScheme(scheme.Scheme).WithObjects(test.existingObjects...).Build()

fakeConsoleClient := mocks.NewConsoleClient(t)
fakeConsoleClient.On("IsClusterExisting", mock.AnythingOfType("*string")).Return(test.returnIsClusterExisting)
fakeConsoleClient.On("UpdateCluster", mock.AnythingOfType("string"), mock.Anything).Return(test.returnUpdateCluster, test.returnErrorUpdateCluster)

ctx := context.Background()

target := &reconciler.ClusterReconciler{
Client: fakeClient,
Log: ctrl.Log.WithName("reconcilers").WithName("ClusterReconciler"),
Scheme: scheme.Scheme,
ConsoleClient: fakeConsoleClient,
}

_, err := target.Reconcile(ctx, reconcile.Request{NamespacedName: types.NamespacedName{Name: test.cluster}})
assert.NoError(t, err)

existingCluster := &v1alpha1.Cluster{}
err = fakeClient.Get(ctx, ctrlruntimeclient.ObjectKey{Name: test.cluster}, existingCluster)
assert.NoError(t, err)
assert.EqualValues(t, test.expectedStatus, existingCluster.Status)
})
}
}

func TestAdoptExistingCluster(t *testing.T) {
const (
clusterName = "test-cluster"
clusterConsoleID = "12345-67890"
)

tests := []struct {
name string
cluster string
returnGetClusterByHandle *gqlclient.ClusterFragment
returnErrorGetClusterByHandle error
existingObjects []ctrlruntimeclient.Object
expectedStatus v1alpha1.ClusterStatus
}{
{
name: "scenario 1: adopt existing AWS cluster",
cluster: clusterName,
expectedStatus: v1alpha1.ClusterStatus{
ID: lo.ToPtr(clusterConsoleID),
Existing: lo.ToPtr(true),
},
returnGetClusterByHandle: &gqlclient.ClusterFragment{ID: clusterConsoleID},
returnErrorGetClusterByHandle: nil,
existingObjects: []ctrlruntimeclient.Object{
&v1alpha1.Cluster{
ObjectMeta: metav1.ObjectMeta{Name: clusterName},
Spec: v1alpha1.ClusterSpec{Handle: lo.ToPtr(clusterName)},
},
},
},
{
name: "scenario 2: adopt existing BYOK cluster",
cluster: clusterName,
expectedStatus: v1alpha1.ClusterStatus{
ID: lo.ToPtr(clusterConsoleID),
Existing: lo.ToPtr(true),
CurrentVersion: lo.ToPtr("1.24.11"),
},
returnGetClusterByHandle: &gqlclient.ClusterFragment{
ID: clusterConsoleID,
CurrentVersion: lo.ToPtr("1.24.11"),
},
returnErrorGetClusterByHandle: nil,
existingObjects: []ctrlruntimeclient.Object{
&v1alpha1.Cluster{
ObjectMeta: metav1.ObjectMeta{Name: clusterName},
Spec: v1alpha1.ClusterSpec{
Handle: lo.ToPtr(clusterName),
Cloud: "byok",
},
},
},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
fakeClient := fake.NewClientBuilder().WithScheme(scheme.Scheme).WithObjects(test.existingObjects...).Build()

fakeConsoleClient := mocks.NewConsoleClient(t)
fakeConsoleClient.On("GetClusterByHandle", mock.AnythingOfType("*string")).Return(test.returnGetClusterByHandle, test.returnErrorGetClusterByHandle)

ctx := context.Background()

target := &reconciler.ClusterReconciler{
Client: fakeClient,
Log: ctrl.Log.WithName("reconcilers").WithName("ClusterReconciler"),
Scheme: scheme.Scheme,
ConsoleClient: fakeConsoleClient,
}

_, err := target.Reconcile(ctx, reconcile.Request{NamespacedName: types.NamespacedName{Name: test.cluster}})
assert.NoError(t, err)

existingCluster := &v1alpha1.Cluster{}
err = fakeClient.Get(ctx, ctrlruntimeclient.ObjectKey{Name: test.cluster}, existingCluster)
assert.NoError(t, err)
assert.EqualValues(t, test.expectedStatus, existingCluster.Status)
})
}
}
Loading

0 comments on commit d8f3d60

Please sign in to comment.