Skip to content

Commit

Permalink
feat: more generic owner label key (#469)
Browse files Browse the repository at this point in the history
Signed-off-by: Aylei <[email protected]>
  • Loading branch information
aylei authored Jan 16, 2024
1 parent cdbd44f commit 8c98f9e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion api/core/v1alpha1/cnclaim_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const (

CNClaimPhaseOutdated CNClaimPhase = "Outdated"

ClaimOwnerNameLabel = "matrixorigin.io/claim-owner"
PodOwnerNameLabel = "matrixorigin.io/owner"
)

type CNClaimSpec struct {
Expand Down
4 changes: 2 additions & 2 deletions pkg/controllers/cnclaim/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func (r *Actor) doClaimCN(ctx *recon.Context[*v1alpha1.CNClaim], orphans []corev
pod.Labels[v1alpha1.CNPodPhaseLabel] = v1alpha1.CNPodPhaseBound
pod.Labels[v1alpha1.PodClaimedByLabel] = c.Name
if c.Spec.OwnerName != nil {
pod.Labels[v1alpha1.ClaimOwnerNameLabel] = *c.Spec.OwnerName
pod.Labels[v1alpha1.PodOwnerNameLabel] = *c.Spec.OwnerName
}
// atomic operation with optimistic concurrency control, succeed means claimed
if err := ctx.Update(pod); err != nil {
Expand Down Expand Up @@ -362,7 +362,7 @@ func priorityFunc(c *v1alpha1.CNClaim) func(a, b corev1.Pod) int {
}

func previouslyOwned(c *v1alpha1.CNClaim, p corev1.Pod) int {
if c.Spec.OwnerName != nil && p.Labels[v1alpha1.ClaimOwnerNameLabel] == *c.Spec.OwnerName {
if c.Spec.OwnerName != nil && p.Labels[v1alpha1.PodOwnerNameLabel] == *c.Spec.OwnerName {
return 0
}
return 1
Expand Down
8 changes: 4 additions & 4 deletions pkg/controllers/cnclaim/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ func Test_sortCNByPriority(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: "previously-claimed",
Labels: map[string]string{
v1alpha1.CNPodPhaseLabel: v1alpha1.CNPodPhaseIdle,
v1alpha1.ClaimOwnerNameLabel: "set1",
v1alpha1.CNPodPhaseLabel: v1alpha1.CNPodPhaseIdle,
v1alpha1.PodOwnerNameLabel: "set1",
},
CreationTimestamp: metav1.Unix(10, 0),
},
Expand All @@ -62,8 +62,8 @@ func Test_sortCNByPriority(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: "previously-claimed-by-other-set",
Labels: map[string]string{
v1alpha1.CNPodPhaseLabel: v1alpha1.CNPodPhaseIdle,
v1alpha1.ClaimOwnerNameLabel: "set2",
v1alpha1.CNPodPhaseLabel: v1alpha1.CNPodPhaseIdle,
v1alpha1.PodOwnerNameLabel: "set2",
},
CreationTimestamp: metav1.Unix(10, 0),
},
Expand Down
9 changes: 6 additions & 3 deletions pkg/controllers/cnclaimset/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ func (r *Actor) Sync(ctx *recon.Context[*v1alpha1.CNClaimSet]) error {
}
// used to resolve all CN pods belonged to this CNSet
podSelector := common.MustAsSelector(&metav1.LabelSelector{MatchLabels: map[string]string{
v1alpha1.ClaimOwnerNameLabel: s.Name,
v1alpha1.CNPodPhaseLabel: v1alpha1.CNPodPhaseBound,
v1alpha1.PodOwnerNameLabel: s.Name,
v1alpha1.CNPodPhaseLabel: v1alpha1.CNPodPhaseBound,
}})
s.Status.ReadyReplicas = readyReplicas
s.Status.Claims = claimStatuses
Expand Down Expand Up @@ -184,7 +184,10 @@ func makeClaim(cs *v1alpha1.CNClaimSet, id string) *v1alpha1.CNClaim {
tpl := cs.Spec.Template
labels := tpl.Labels
labels[ClaimInstanceIDLabel] = id
tpl.Spec.OwnerName = &cs.Name
// allow client to override ownerName in claimTemplate
if tpl.Spec.OwnerName == nil {
tpl.Spec.OwnerName = &cs.Name
}
return &v1alpha1.CNClaim{
ObjectMeta: metav1.ObjectMeta{
Namespace: cs.Namespace,
Expand Down

0 comments on commit 8c98f9e

Please sign in to comment.