Skip to content

Commit

Permalink
Add tests for clusterclassToCluster, godoc changes
Browse files Browse the repository at this point in the history
Signed-off-by: Danil-Grigorev <[email protected]>
  • Loading branch information
Danil-Grigorev committed Dec 3, 2024
1 parent b4b6131 commit 5fef5d3
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 8 deletions.
8 changes: 3 additions & 5 deletions api/v1beta1/cluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1beta1

import (
"cmp"
"fmt"
"net"
"strings"
Expand Down Expand Up @@ -511,6 +512,7 @@ type Topology struct {

// The namespace of the ClusterClass object to create the topology.
// Empty namespace assumes the namespace of the cluster object.
// Class namespace changes are not supported by the rebase procedure.
// +optional
// +kubebuilder:validation:MinLength=1
// +kubebuilder:validation:MaxLength=253
Expand Down Expand Up @@ -1046,11 +1048,7 @@ func (c *Cluster) GetClassKey() types.NamespacedName {
return types.NamespacedName{}
}

namespace := c.Spec.Topology.ClassNamespace
if c.Spec.Topology == nil || c.Spec.Topology.ClassNamespace == "" {
namespace = c.Namespace
}

namespace := cmp.Or(c.Spec.Topology.ClassNamespace, c.Namespace)
return types.NamespacedName{Namespace: namespace, Name: c.Spec.Topology.Class}
}

Expand Down
2 changes: 1 addition & 1 deletion api/v1beta1/zz_generated.openapi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions config/crd/bases/cluster.x-k8s.io_clusters.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions internal/controllers/topology/cluster/cluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1577,3 +1577,53 @@ func TestReconciler_ValidateCluster(t *testing.T) {
})
}
}

func TestClusterClassToCluster(t *testing.T) {
g := NewWithT(t)
classBuilder := builder.ClusterClass(metav1.NamespaceDefault, clusterClassName1)
topologyBase := builder.ClusterTopology().
WithClass(clusterClassName1).
WithVersion("1.22.2").
WithControlPlaneReplicas(3)
topologyBase2 := topologyBase.DeepCopy().WithClass(clusterClassName2)
class2Builder := builder.ClusterClass(metav1.NamespaceDefault, clusterClassName2)
clusterBuilder := builder.Cluster(metav1.NamespaceDefault, clusterName1).
WithTopology(topologyBase.DeepCopy().Build())
cluster2Builder := builder.Cluster(metav1.NamespaceDefault, clusterName2).
WithTopology(topologyBase2.DeepCopy().Build())
tests := []struct {
name string
objects []client.Object
clusterClass *clusterv1.ClusterClass
cluster *clusterv1.Cluster
expected []reconcile.Request
}{
{
name: "ClusterClass change should request reconcile for the referenced class",
objects: []client.Object{clusterBuilder.DeepCopy().Build(), cluster2Builder.DeepCopy().Build()},
clusterClass: classBuilder.DeepCopy().Build(),
cluster: clusterBuilder.DeepCopy().Build(),
expected: []reconcile.Request{{NamespacedName: client.ObjectKeyFromObject(clusterBuilder.DeepCopy().Build())}},
},
{
name: "Different ClusterClass change should not request reconcile if no class references it",
objects: []client.Object{clusterBuilder.DeepCopy().Build(), cluster2Builder.DeepCopy().Build()},
clusterClass: class2Builder.DeepCopy().Build(),
cluster: clusterBuilder.DeepCopy().Build(),
expected: []reconcile.Request{},
},
}

for _, tt := range tests {
t.Run(tt.name, func(*testing.T) {
fakeClient := fake.NewClientBuilder().WithScheme(fakeScheme).WithObjects(tt.objects...).Build()
r := &Reconciler{
Client: fakeClient,
APIReader: fakeClient,
}

requests := r.clusterClassToCluster(ctx, tt.clusterClass)
g.Expect(requests).To(Equal(tt.expected))
})
}
}

0 comments on commit 5fef5d3

Please sign in to comment.