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 703597f
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 12 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.

59 changes: 59 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,62 @@ 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())
cluster3Builder := builder.Cluster(metav1.NamespaceDefault, clusterName1).
WithTopology(topologyBase.DeepCopy().WithClassNamespace("other").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: "ClusterClass with the same name but another namespace should not trigger reconcile",
objects: []client.Object{clusterBuilder.DeepCopy().Build(), cluster2Builder.DeepCopy().Build()},
clusterClass: classBuilder.DeepCopy().Build(),
cluster: cluster3Builder.DeepCopy().Build(),
expected: []reconcile.Request{},
},
{
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))
})
}
}
15 changes: 11 additions & 4 deletions util/test/builder/builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (c *ClusterBuilder) Build() *clusterv1.Cluster {

// ClusterTopologyBuilder contains the fields needed to build a testable ClusterTopology.
type ClusterTopologyBuilder struct {
class string
class, classNamespace string
workers *clusterv1.WorkersTopology
version string
controlPlaneReplicas int32
Expand All @@ -136,6 +136,12 @@ func (c *ClusterTopologyBuilder) WithClass(class string) *ClusterTopologyBuilder
return c
}

// WithClassNamespace adds the passed classNamespace to the ClusterTopologyBuilder.
func (c *ClusterTopologyBuilder) WithClassNamespace(ns string) *ClusterTopologyBuilder {
c.classNamespace = ns
return c
}

// WithVersion adds the passed version to the ClusterTopologyBuilder.
func (c *ClusterTopologyBuilder) WithVersion(version string) *ClusterTopologyBuilder {
c.version = version
Expand Down Expand Up @@ -181,9 +187,10 @@ func (c *ClusterTopologyBuilder) WithVariables(vars ...clusterv1.ClusterVariable
// Build returns a testable cluster Topology object with any values passed to the builder.
func (c *ClusterTopologyBuilder) Build() *clusterv1.Topology {
t := &clusterv1.Topology{
Class: c.class,
Workers: c.workers,
Version: c.version,
Class: c.class,
ClassNamespace: c.classNamespace,
Workers: c.workers,
Version: c.version,
ControlPlane: clusterv1.ControlPlaneTopology{
Replicas: &c.controlPlaneReplicas,
MachineHealthCheck: c.controlPlaneMHC,
Expand Down

0 comments on commit 703597f

Please sign in to comment.