Skip to content

Commit

Permalink
chore: remove mock StatefulSet in pkg controllerutil&testutil (#7174)
Browse files Browse the repository at this point in the history
  • Loading branch information
free6om authored Apr 25, 2024
1 parent f5f08c9 commit 8526bf5
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 570 deletions.
7 changes: 4 additions & 3 deletions controllers/apps/operations/switchover_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
appsv1alpha1 "github.com/apecloud/kubeblocks/apis/apps/v1alpha1"
"github.com/apecloud/kubeblocks/pkg/constant"
"github.com/apecloud/kubeblocks/pkg/controller/component"
"github.com/apecloud/kubeblocks/pkg/controller/instanceset"
intctrlutil "github.com/apecloud/kubeblocks/pkg/controllerutil"
)

Expand Down Expand Up @@ -90,9 +91,9 @@ func needDoSwitchover(ctx context.Context,
if err != nil {
return false, err
}
podParent, _ := intctrlutil.ParseParentNameAndOrdinal(pod.Name)
siParent, o := intctrlutil.ParseParentNameAndOrdinal(switchover.InstanceName)
if podParent != siParent || o < 0 || o >= int32(len(podList.Items)) {
podParent, _ := instanceset.ParseParentNameAndOrdinal(pod.Name)
siParent, o := instanceset.ParseParentNameAndOrdinal(switchover.InstanceName)
if podParent != siParent || o < 0 || o >= len(podList.Items) {
return false, errors.New("switchover.InstanceName is invalid")
}
// If the current instance is already the primary, then no switchover will be performed.
Expand Down
26 changes: 0 additions & 26 deletions pkg/controllerutil/pod_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package controllerutil

import (
"fmt"
"regexp"
"strconv"
"strings"
"time"
Expand All @@ -44,26 +43,6 @@ const (
PodScheduledFailedTimeout = 30 * time.Second
)

// statefulPodRegex is a regular expression that extracts the parent StatefulSet and ordinal from the Name of a Pod
var statefulPodRegex = regexp.MustCompile("(.*)-([0-9]+)$")

// GetParentNameAndOrdinal gets the name of pod's parent StatefulSet and pod's ordinal as extracted from its Name. If
// the Pod was not created by a StatefulSet, its parent is considered to be empty string, and its ordinal is considered
// to be -1.
func GetParentNameAndOrdinal(pod *corev1.Pod) (string, int) {
parent := ""
ordinal := -1
subMatches := statefulPodRegex.FindStringSubmatch(pod.Name)
if len(subMatches) < 3 {
return parent, ordinal
}
parent = subMatches[1]
if i, err := strconv.ParseInt(subMatches[2], 10, 32); err == nil {
ordinal = int(i)
}
return parent, ordinal
}

// GetContainerByConfigSpec searches for container using the configmap of config from the pod
//
// e.g.:
Expand Down Expand Up @@ -403,11 +382,6 @@ func PodIsReadyWithLabel(pod corev1.Pod) bool {
return PodIsReady(&pod)
}

// PodIsControlledByLatestRevision checks if the pod is controlled by latest controller revision.
func PodIsControlledByLatestRevision(pod *corev1.Pod, sts *appsv1.StatefulSet) bool {
return GetPodRevision(pod) == sts.Status.UpdateRevision && sts.Status.ObservedGeneration == sts.Generation
}

// GetPodRevision gets the revision of Pod by inspecting the StatefulSetRevisionLabel. If pod has no revision empty
// string is returned.
func GetPodRevision(pod *corev1.Pod) string {
Expand Down
23 changes: 2 additions & 21 deletions pkg/controllerutil/pod_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ type TestResourceUnit struct {
}

func TestPodIsReady(t *testing.T) {
set := testk8s.NewFakeStatefulSet("foo", 3)
pod := testk8s.NewFakeStatefulSetPod(set, 1)
pod := testk8s.NewFakePod("foo", 1)
pod.Status.Conditions = []corev1.PodCondition{
{
Type: corev1.PodReady,
Expand Down Expand Up @@ -83,26 +82,8 @@ func TestPodIsReady(t *testing.T) {
}
}

func TestPodIsControlledByLatestRevision(t *testing.T) {
set := testk8s.NewFakeStatefulSet("foo", 3)
pod := testk8s.NewFakeStatefulSetPod(set, 1)
pod.Labels = map[string]string{
appsv1.ControllerRevisionHashLabelKey: "test",
}
set.Generation = 1
set.Status.UpdateRevision = "test"
if PodIsControlledByLatestRevision(pod, set) {
t.Errorf("PodIsControlledByLatestRevision returned false positive")
}
set.Status.ObservedGeneration = 1
if !PodIsControlledByLatestRevision(pod, set) {
t.Errorf("PodIsControlledByLatestRevision returned false positive")
}
}

func TestGetPodRevision(t *testing.T) {
set := testk8s.NewFakeStatefulSet("foo", 3)
pod := testk8s.NewFakeStatefulSetPod(set, 1)
pod := testk8s.NewFakePod("foo", 1)
if GetPodRevision(pod) != "" {
t.Errorf("revision should be empty")
}
Expand Down
88 changes: 0 additions & 88 deletions pkg/controllerutil/stateful_set_utils.go

This file was deleted.

55 changes: 0 additions & 55 deletions pkg/controllerutil/stateful_set_utils_test.go

This file was deleted.

12 changes: 5 additions & 7 deletions pkg/testutil/apps/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@ const (
ServiceVPCName = "vpc-lb"
ServiceInternetName = "internet-lb"

ReplicationPodRoleVolume = "pod-role"
ReplicationRoleLabelFieldPath = "metadata.labels['kubeblocks.io/role']"
DefaultReplicationCandidateIndex = 0
DefaultReplicationReplicas = 2
ReplicationPodRoleVolume = "pod-role"
ReplicationRoleLabelFieldPath = "metadata.labels['kubeblocks.io/role']"
DefaultReplicationReplicas = 2

ApeCloudMySQLImage = "docker.io/apecloud/apecloud-mysql-server:latest"
DefaultMySQLContainerName = "mysql"
Expand All @@ -55,9 +54,8 @@ const (
DefaultRedisContainerName = "redis"
DefaultRedisInitContainerName = "redis-init-container"

StorageClassName = "test-sc"
EnvKeyImageTag = "IMAGE_TAG"
DefaultImageTag = "test"
EnvKeyImageTag = "IMAGE_TAG"
DefaultImageTag = "test"

DefaultConfigSpecName = "config-cm"
DefaultConfigSpecTplRef = "env-from-config-tpl"
Expand Down
74 changes: 0 additions & 74 deletions pkg/testutil/k8s/deployment_util.go

This file was deleted.

Loading

0 comments on commit 8526bf5

Please sign in to comment.