Skip to content

Commit 47eba6d

Browse files
committed
fix: inverted condition
1 parent 573b01a commit 47eba6d

2 files changed

Lines changed: 62 additions & 1 deletion

File tree

pkg/reconcilers/etcd_cluster/reconciler.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ import (
4747
var (
4848
etcdVolumeResizeEvent = "EtcdVolumeAutoResize"
4949
etcdVolumeSizeReCalculatedEvent = "EtcdVolumeSizeRecalculated"
50+
51+
etcdClientVersion3_7 = semver.MustParse(version.V3_7.String())
5052
)
5153

5254
type EtcdClusterReconciler interface {
@@ -814,7 +816,7 @@ func (er *etcdClusterReconciler) buildEtcdArgs(
814816
"quota-backend-bytes": strconv.FormatInt(storageQuota, 10),
815817
}
816818

817-
if int64(etcdVersion.Minor) >= version.V3_7.Minor { //nolint:gosec // semver minor will never overflow int64
819+
if etcdVersion.LT(etcdClientVersion3_7) {
818820
// this is deprecated and will be removed in 3.7
819821
// TODO: remove this when we roll 3.7
820822
args["snapshot-count"] = "10000"

pkg/reconcilers/etcd_cluster/reconciler_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ import (
1212
"k8s.io/utils/ptr"
1313
capiv2 "sigs.k8s.io/cluster-api/api/core/v1beta2"
1414

15+
semver "github.com/blang/semver/v4"
1516
. "github.com/onsi/gomega"
1617
"github.com/teutonet/cluster-api-provider-hosted-control-plane/api/v1alpha1"
1718
. "github.com/teutonet/cluster-api-provider-hosted-control-plane/test"
1819
"go.etcd.io/etcd/api/v3/etcdserverpb"
1920
clientv3 "go.etcd.io/etcd/client/v3"
2021
"k8s.io/apimachinery/pkg/api/resource"
2122
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23+
corev1ac "k8s.io/client-go/applyconfigurations/core/v1"
2224
)
2325

2426
func TestEtcdClusterReconciler_getETCDVolumeSize(t *testing.T) {
@@ -883,3 +885,60 @@ func TestEtcdClusterReconciler_reconcileETCDBackup(t *testing.T) {
883885
g.Expect(hcp.Status.ETCDNextBackupTime).NotTo(BeZero())
884886
})
885887
}
888+
889+
func TestBuildEtcdArgs_SnapshotCount(t *testing.T) {
890+
tests := []struct {
891+
name string
892+
etcdVersion semver.Version
893+
expectSnapshotCount bool
894+
}{
895+
{
896+
name: "version < 3.7 sets snapshot-count",
897+
etcdVersion: semver.MustParse("3.6.0"),
898+
expectSnapshotCount: true,
899+
},
900+
{
901+
name: "version >= 3.7 omits snapshot-count",
902+
etcdVersion: semver.MustParse("3.7.0"),
903+
expectSnapshotCount: false,
904+
},
905+
}
906+
907+
for _, tt := range tests {
908+
t.Run(tt.name, func(t *testing.T) {
909+
g := NewWithT(t)
910+
911+
reconciler := &etcdClusterReconciler{}
912+
hcp := &v1alpha1.HostedControlPlane{
913+
ObjectMeta: metav1.ObjectMeta{Namespace: "test-ns"},
914+
Status: v1alpha1.HostedControlPlaneStatus{ETCDVolumeSize: resource.MustParse("10Gi")},
915+
}
916+
cluster := &capiv2.Cluster{
917+
ObjectMeta: metav1.ObjectMeta{Name: "test-cluster", Namespace: "test-ns"},
918+
}
919+
serverPort := corev1ac.ContainerPort().WithContainerPort(2379)
920+
peerPort := corev1ac.ContainerPort().WithContainerPort(2380)
921+
metricsPort := corev1ac.ContainerPort().WithContainerPort(2381)
922+
dataMount := corev1ac.VolumeMount().WithMountPath("/var/lib/etcd")
923+
certMount := corev1ac.VolumeMount().WithMountPath("/etc/etcd")
924+
925+
args := reconciler.buildEtcdArgs(
926+
context.Background(),
927+
hcp,
928+
cluster,
929+
tt.etcdVersion,
930+
dataMount,
931+
certMount,
932+
serverPort,
933+
peerPort,
934+
metricsPort,
935+
)
936+
937+
if tt.expectSnapshotCount {
938+
g.Expect(args).To(ContainElement("--snapshot-count=10000"))
939+
} else {
940+
g.Expect(args).NotTo(ContainElement(ContainSubstring("snapshot-count")))
941+
}
942+
})
943+
}
944+
}

0 commit comments

Comments
 (0)