-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(v2beta1): add new field of revisionHistoryLimit
Signed-off-by: Rory Z <[email protected]>
- Loading branch information
Showing
14 changed files
with
353 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package v2beta1 | ||
|
||
import ( | ||
"context" | ||
|
||
appsv2beta1 "github.com/emqx/emqx-operator/apis/apps/v2beta1" | ||
innerReq "github.com/emqx/emqx-operator/internal/requester" | ||
corev1 "k8s.io/api/core/v1" | ||
k8sErrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/klog/v2" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
) | ||
|
||
type syncSets struct { | ||
*EMQXReconciler | ||
} | ||
|
||
func (s *syncSets) reconcile(ctx context.Context, instance *appsv2beta1.EMQX, r innerReq.RequesterInterface) subResult { | ||
if !instance.Status.IsConditionTrue(appsv2beta1.Ready) { | ||
return subResult{} | ||
} | ||
logger := log.FromContext(ctx) | ||
|
||
_, _, oldRsList := getReplicaSetList(ctx, s.Client, instance) | ||
rsDiff := int32(len(oldRsList)) - *instance.Spec.RevisionHistoryLimit | ||
if rsDiff > 0 { | ||
for i := 0; i < int(rsDiff); i++ { | ||
rs := oldRsList[i].DeepCopy() | ||
// Avoid delete replica set with non-zero replica counts | ||
if rs.Status.Replicas != 0 || *(rs.Spec.Replicas) != 0 || rs.Generation > rs.Status.ObservedGeneration || rs.DeletionTimestamp != nil { | ||
continue | ||
} | ||
logger.Info("trying to cleanup replica set for EMQX", "replicaSet", klog.KObj(rs), "EMQX", klog.KObj(instance)) | ||
if err := s.Client.Delete(ctx, rs); err != nil && !k8sErrors.IsNotFound(err) { | ||
return subResult{err: err} | ||
} | ||
} | ||
} | ||
|
||
_, _, oldStsList := getStateFulSetList(ctx, s.Client, instance) | ||
stsDiff := int32(len(oldStsList)) - *instance.Spec.RevisionHistoryLimit | ||
if stsDiff > 0 { | ||
for i := 0; i < int(rsDiff); i++ { | ||
sts := oldStsList[i].DeepCopy() | ||
// Avoid delete stateful set with non-zero replica counts | ||
if sts.Status.Replicas != 0 || *(sts.Spec.Replicas) != 0 || sts.Generation > sts.Status.ObservedGeneration || sts.DeletionTimestamp != nil { | ||
continue | ||
} | ||
logger.Info("trying to cleanup stateful set for EMQX", "statefulSet", klog.KObj(sts), "EMQX", klog.KObj(instance)) | ||
if err := s.Client.Delete(ctx, sts); err != nil && !k8sErrors.IsNotFound(err) { | ||
return subResult{err: err} | ||
} | ||
|
||
// Delete PVCs | ||
pvcList := &corev1.PersistentVolumeClaimList{} | ||
_ = s.Client.List(ctx, pvcList, | ||
client.InNamespace(instance.Namespace), | ||
client.MatchingLabels(sts.Spec.Selector.MatchLabels), | ||
) | ||
|
||
for _, p := range pvcList.Items { | ||
pvc := p.DeepCopy() | ||
if pvc.DeletionTimestamp != nil { | ||
continue | ||
} | ||
logger.Info("trying to cleanup pvc for EMQX", "pvc", klog.KObj(pvc), "EMQX", klog.KObj(instance)) | ||
if err := s.Client.Delete(ctx, pvc); err != nil && !k8sErrors.IsNotFound(err) { | ||
return subResult{err: err} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return subResult{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
package v2beta1 | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
appsv2beta1 "github.com/emqx/emqx-operator/apis/apps/v2beta1" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
appsv1 "k8s.io/api/apps/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/resource" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/util/rand" | ||
"k8s.io/utils/pointer" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
var _ = Describe("Check sync rs", func() { | ||
var s *syncSets | ||
|
||
var instance *appsv2beta1.EMQX = new(appsv2beta1.EMQX) | ||
var ns *corev1.Namespace = &corev1.Namespace{} | ||
|
||
BeforeEach(func() { | ||
s = &syncSets{emqxReconciler} | ||
ns = &corev1.Namespace{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "controller-v2beta1-sync-sets-test-" + rand.String(5), | ||
Labels: map[string]string{ | ||
"test": "e2e", | ||
}, | ||
}, | ||
} | ||
instance = emqx.DeepCopy() | ||
instance.Namespace = ns.Name | ||
instance.Spec.RevisionHistoryLimit = pointer.Int32(3) | ||
instance.Status = appsv2beta1.EMQXStatus{ | ||
Conditions: []metav1.Condition{ | ||
{ | ||
Type: appsv2beta1.Ready, | ||
Status: metav1.ConditionTrue, | ||
LastTransitionTime: metav1.Time{Time: time.Now().AddDate(0, 0, -1)}, | ||
}, | ||
}, | ||
} | ||
|
||
Expect(k8sClient.Create(context.Background(), ns)).To(Succeed()) | ||
for i := 0; i < 5; i++ { | ||
name := fmt.Sprintf("%s-%d", instance.Name, i) | ||
|
||
rs := &appsv1.ReplicaSet{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: name, | ||
Namespace: instance.Namespace, | ||
Labels: appsv2beta1.CloneAndAddLabel( | ||
appsv2beta1.DefaultReplicantLabels(instance), | ||
appsv2beta1.LabelsPodTemplateHashKey, | ||
fmt.Sprintf("fake-%d", i), | ||
), | ||
}, | ||
Spec: appsv1.ReplicaSetSpec{ | ||
Replicas: pointer.Int32Ptr(0), | ||
Selector: &metav1.LabelSelector{ | ||
MatchLabels: appsv2beta1.CloneAndAddLabel( | ||
appsv2beta1.DefaultReplicantLabels(instance), | ||
appsv2beta1.LabelsPodTemplateHashKey, | ||
fmt.Sprintf("fake-%d", i), | ||
), | ||
}, | ||
Template: corev1.PodTemplateSpec{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Labels: appsv2beta1.CloneAndAddLabel( | ||
appsv2beta1.DefaultReplicantLabels(instance), | ||
appsv2beta1.LabelsPodTemplateHashKey, | ||
fmt.Sprintf("fake-%d", i), | ||
), | ||
}, | ||
Spec: corev1.PodSpec{ | ||
Containers: []corev1.Container{ | ||
{Name: "emqx", Image: "emqx"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
Expect(k8sClient.Create(context.Background(), rs.DeepCopy())).Should(Succeed()) | ||
rs.Status.Replicas = 0 | ||
rs.Status.ObservedGeneration = 1 | ||
Expect(k8sClient.Status().Patch(context.Background(), rs.DeepCopy(), client.Merge)).Should(Succeed()) | ||
|
||
sts := &appsv1.StatefulSet{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: name, | ||
Namespace: instance.Namespace, | ||
Labels: appsv2beta1.CloneAndAddLabel( | ||
appsv2beta1.DefaultCoreLabels(instance), | ||
appsv2beta1.LabelsPodTemplateHashKey, | ||
fmt.Sprintf("fake-%d", i), | ||
), | ||
}, | ||
Spec: appsv1.StatefulSetSpec{ | ||
Replicas: pointer.Int32Ptr(0), | ||
Selector: &metav1.LabelSelector{ | ||
MatchLabels: appsv2beta1.CloneAndAddLabel( | ||
appsv2beta1.DefaultCoreLabels(instance), | ||
appsv2beta1.LabelsPodTemplateHashKey, | ||
fmt.Sprintf("fake-%d", i), | ||
), | ||
}, | ||
Template: corev1.PodTemplateSpec{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Labels: appsv2beta1.CloneAndAddLabel( | ||
appsv2beta1.DefaultCoreLabels(instance), | ||
appsv2beta1.LabelsPodTemplateHashKey, | ||
fmt.Sprintf("fake-%d", i), | ||
), | ||
}, | ||
Spec: corev1.PodSpec{ | ||
Containers: []corev1.Container{ | ||
{Name: "emqx", Image: "emqx"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
Expect(k8sClient.Create(context.Background(), sts.DeepCopy())).Should(Succeed()) | ||
sts.Status.Replicas = 0 | ||
sts.Status.ObservedGeneration = 1 | ||
Expect(k8sClient.Status().Patch(context.Background(), sts.DeepCopy(), client.Merge)).Should(Succeed()) | ||
|
||
pvc := &corev1.PersistentVolumeClaim{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: sts.Name, | ||
Namespace: sts.Namespace, | ||
Labels: sts.Labels, | ||
}, | ||
Spec: corev1.PersistentVolumeClaimSpec{ | ||
AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce}, | ||
Resources: corev1.ResourceRequirements{ | ||
Requests: corev1.ResourceList{ | ||
corev1.ResourceStorage: resource.MustParse("1Gi"), | ||
}, | ||
}, | ||
}, | ||
} | ||
Expect(k8sClient.Create(context.Background(), pvc.DeepCopy())).Should(Succeed()) | ||
} | ||
}) | ||
|
||
It("should delete rs sts and pvc", func() { | ||
Expect(s.reconcile(context.Background(), instance, nil)).Should(Equal(subResult{})) | ||
|
||
Eventually(func() int { | ||
list := &appsv1.ReplicaSetList{} | ||
_ = k8sClient.List(context.Background(), list, | ||
client.InNamespace(instance.Namespace), | ||
client.MatchingLabels(appsv2beta1.DefaultReplicantLabels(instance)), | ||
) | ||
count := 0 | ||
for _, i := range list.Items { | ||
item := i.DeepCopy() | ||
if item.DeletionTimestamp == nil { | ||
count++ | ||
} | ||
} | ||
return count | ||
}).WithTimeout(timeout).WithPolling(interval).Should(BeEquivalentTo(*instance.Spec.RevisionHistoryLimit)) | ||
|
||
Eventually(func() int { | ||
list := &appsv1.StatefulSetList{} | ||
_ = k8sClient.List(context.Background(), list, | ||
client.InNamespace(instance.Namespace), | ||
client.MatchingLabels(appsv2beta1.DefaultCoreLabels(instance)), | ||
) | ||
count := 0 | ||
for _, i := range list.Items { | ||
item := i.DeepCopy() | ||
if item.DeletionTimestamp == nil { | ||
count++ | ||
} | ||
} | ||
return count | ||
}).WithTimeout(timeout).WithPolling(interval).Should(BeEquivalentTo(*instance.Spec.RevisionHistoryLimit)) | ||
|
||
Eventually(func() int { | ||
list := &corev1.PersistentVolumeClaimList{} | ||
_ = k8sClient.List(context.Background(), list, | ||
client.InNamespace(instance.Namespace), | ||
client.MatchingLabels(appsv2beta1.DefaultCoreLabels(instance)), | ||
) | ||
count := 0 | ||
for _, i := range list.Items { | ||
item := i.DeepCopy() | ||
if item.DeletionTimestamp == nil { | ||
count++ | ||
} | ||
} | ||
return count | ||
}).WithTimeout(timeout).WithPolling(interval).Should(BeEquivalentTo(*instance.Spec.RevisionHistoryLimit)) | ||
}) | ||
}) |
Oops, something went wrong.