Skip to content

Commit

Permalink
chore: remove update primary index in component controller (#7131)
Browse files Browse the repository at this point in the history
  • Loading branch information
Y-Rookie authored Apr 25, 2024
1 parent 39f08fa commit 625d25f
Showing 1 changed file with 0 additions and 69 deletions.
69 changes: 0 additions & 69 deletions controllers/apps/transformer_component_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"strings"
"time"

"golang.org/x/exp/slices"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -219,13 +218,6 @@ func (r *componentStatusHandler) reconcileComponentStatus() error {
r.setComponentStatusPhase(appsv1alpha1.AbnormalClusterCompPhase, nil, "component is Abnormal")
}

// set primary-pod annotation
// TODO(free6om): primary-pod is only used in redis to bootstrap the redis cluster correctly.
// it is too hacky to be replaced by a better design.
if err := r.updatePrimaryIndex(); err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -486,67 +478,6 @@ func (r *componentStatusHandler) updateComponentStatus(phaseTransitionMsg string
return nil
}

// updatePrimaryIndex updates the primary pod index to the pod annotations.
// TODO: the need to update primary info is because some database engines currently require external specification of the primary node.
// Based on the specified primary node, the primary-secondary relationship can be established during startup. Examples of such engines include primary-secondary Redis.
// In the future, there is a need for a better design to replace this kind of workaround.
func (r *componentStatusHandler) updatePrimaryIndex() error {
// TODO(xingran): consider if there are alternative ways to determine whether it is necessary to specify primary info in the Controller
if r.synthesizeComp.RoleArbitrator == nil || *r.synthesizeComp.RoleArbitrator != appsv1alpha1.LorryRoleArbitrator {
return nil
}
podList, err := component.ListPodOwnedByComponent(r.reqCtx.Ctx, r.cli, r.cluster.Namespace,
constant.GetComponentWellKnownLabels(r.cluster.Name, r.synthesizeComp.Name), inDataContext4C())
if err != nil {
return err
}
if len(podList) == 0 {
return nil
}
slices.SortFunc(podList, func(a, b *corev1.Pod) bool {
return a.GetName() < b.GetName()
})
primaryPods := make([]string, 0)
emptyRolePods := make([]string, 0)
for _, pod := range podList {
role, ok := pod.Labels[constant.RoleLabelKey]
if !ok || role == "" {
emptyRolePods = append(emptyRolePods, pod.Name)
continue
}
if role == constant.Primary {
primaryPods = append(primaryPods, pod.Name)
}
}
primaryPodName, err := func() (string, error) {
switch {
// if the workload is newly created, and the role label is not set, we set the pod with index=0 as the primary by default.
case len(emptyRolePods) == len(podList):
return podList[0].Name, nil
case len(primaryPods) != 1:
return "", fmt.Errorf("the number of primary pod is not equal to 1, primary pods: %v, emptyRole pods: %v", primaryPods, emptyRolePods)
default:
return primaryPods[0], nil
}
}()
if err != nil {
return err
}
graphCli := model.NewGraphClient(r.cli)
for _, pod := range podList {
if pod.Annotations == nil {
pod.Annotations = map[string]string{}
}
pi, ok := pod.Annotations[constant.PrimaryAnnotationKey]
if !ok || pi != primaryPodName {
origPod := pod.DeepCopy()
pod.Annotations[constant.PrimaryAnnotationKey] = primaryPodName
graphCli.Do(r.dag, origPod, pod, model.ActionUpdatePtr(), nil, inDataContext4G())
}
}
return nil
}

// hasFailedAndTimedOutPod returns whether the pods of components are still failed after a PodFailedTimeout period.
func hasFailedAndTimedOutPod(pods []*corev1.Pod) (bool, appsv1alpha1.ComponentMessageMap, time.Duration) {
var (
Expand Down

0 comments on commit 625d25f

Please sign in to comment.