Skip to content

Commit cbb591a

Browse files
committed
fix: inverted condition
1 parent 214cc3c commit cbb591a

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
@@ -45,6 +45,8 @@ import (
4545
var (
4646
etcdVolumeResizeEvent = "EtcdVolumeAutoResize"
4747
etcdVolumeSizeReCalculatedEvent = "EtcdVolumeSizeRecalculated"
48+
49+
etcdClientVersion3_7 = semver.MustParse(version.V3_7.String())
4850
)
4951

5052
type EtcdClusterReconciler interface {
@@ -753,7 +755,7 @@ func (er *etcdClusterReconciler) buildEtcdArgs(
753755
"quota-backend-bytes": strconv.FormatInt(storageQuota, 10),
754756
}
755757

756-
if int64(etcdVersion.Minor) >= version.V3_7.Minor { //nolint:gosec // semver minor will never overflow int64
758+
if etcdVersion.LT(etcdClientVersion3_7) {
757759
// this is deprecated and will be removed in 3.7
758760
// TODO: remove this when we roll 3.7
759761
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,12 +12,14 @@ 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
"k8s.io/apimachinery/pkg/api/resource"
2021
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
22+
corev1ac "k8s.io/client-go/applyconfigurations/core/v1"
2123
)
2224

2325
func TestEtcdClusterReconciler_getETCDVolumeSize(t *testing.T) {
@@ -779,3 +781,60 @@ func TestEtcdClusterReconciler_reconcileETCDBackup(t *testing.T) {
779781
g.Expect(hcp.Status.ETCDNextBackupTime).NotTo(BeZero())
780782
})
781783
}
784+
785+
func TestBuildEtcdArgs_SnapshotCount(t *testing.T) {
786+
tests := []struct {
787+
name string
788+
etcdVersion semver.Version
789+
expectSnapshotCount bool
790+
}{
791+
{
792+
name: "version < 3.7 sets snapshot-count",
793+
etcdVersion: semver.MustParse("3.6.0"),
794+
expectSnapshotCount: true,
795+
},
796+
{
797+
name: "version >= 3.7 omits snapshot-count",
798+
etcdVersion: semver.MustParse("3.7.0"),
799+
expectSnapshotCount: false,
800+
},
801+
}
802+
803+
for _, tt := range tests {
804+
t.Run(tt.name, func(t *testing.T) {
805+
g := NewWithT(t)
806+
807+
reconciler := &etcdClusterReconciler{}
808+
hcp := &v1alpha1.HostedControlPlane{
809+
ObjectMeta: metav1.ObjectMeta{Namespace: "test-ns"},
810+
Status: v1alpha1.HostedControlPlaneStatus{ETCDVolumeSize: resource.MustParse("10Gi")},
811+
}
812+
cluster := &capiv2.Cluster{
813+
ObjectMeta: metav1.ObjectMeta{Name: "test-cluster", Namespace: "test-ns"},
814+
}
815+
serverPort := corev1ac.ContainerPort().WithContainerPort(2379)
816+
peerPort := corev1ac.ContainerPort().WithContainerPort(2380)
817+
metricsPort := corev1ac.ContainerPort().WithContainerPort(2381)
818+
dataMount := corev1ac.VolumeMount().WithMountPath("/var/lib/etcd")
819+
certMount := corev1ac.VolumeMount().WithMountPath("/etc/etcd")
820+
821+
args := reconciler.buildEtcdArgs(
822+
context.Background(),
823+
hcp,
824+
cluster,
825+
tt.etcdVersion,
826+
dataMount,
827+
certMount,
828+
serverPort,
829+
peerPort,
830+
metricsPort,
831+
)
832+
833+
if tt.expectSnapshotCount {
834+
g.Expect(args).To(ContainElement("--snapshot-count=10000"))
835+
} else {
836+
g.Expect(args).NotTo(ContainElement(ContainSubstring("snapshot-count")))
837+
}
838+
})
839+
}
840+
}

0 commit comments

Comments
 (0)