Skip to content

Commit 404b435

Browse files
leon-apeapecloud-bot
authored andcommitted
chore: reject node-selector-once on instance api (#10144)
(cherry picked from commit cf10be1)
1 parent 025b568 commit 404b435

5 files changed

Lines changed: 75 additions & 53 deletions

File tree

pkg/controller/instance/reconciler_status.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,6 @@ func (r *statusReconciler) Reconcile(tree *kubebuilderx.ObjectTree) (kubebuilder
6565
ready, available, updated := false, false, false
6666
notReadyName, notAvailableName := "", ""
6767

68-
// podToNodeMapping, err := ParseNodeSelectorOnceAnnotation(inst)
69-
// if err != nil {
70-
// return kubebuilderx.Continue, err
71-
// }
72-
7368
if isCreated(pod) {
7469
notReadyName = pod.Name
7570
}
@@ -88,17 +83,6 @@ func (r *statusReconciler) Reconcile(tree *kubebuilderx.ObjectTree) (kubebuilder
8883
return kubebuilderx.Continue, err
8984
}
9085
}
91-
92-
// TODO: ???
93-
// if nodeName, ok := podToNodeMapping[pod.Name]; ok {
94-
// // there's chance that a pod is currently running and wait to be deleted so that it can be rescheduled
95-
// if pod.Spec.NodeName == nodeName {
96-
// if err := deleteNodeSelectorOnceAnnotation(its, pod.Name); err != nil {
97-
// return kubebuilderx.Continue, err
98-
// }
99-
// }
100-
// }
101-
10286
inst.Status.CurrentRevision = getPodRevision(pod)
10387
if updated {
10488
inst.Status.CurrentRevision = inst.Status.UpdateRevision

pkg/controller/instanceset2/instance_util.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ package instanceset2
2121

2222
import (
2323
"encoding/json"
24-
"fmt"
2524
"reflect"
2625
"sort"
2726
"strconv"
@@ -108,18 +107,6 @@ func baseSort(x any, getNameNOrdinalFunc func(i int) (string, int), getRolePrior
108107
})
109108
}
110109

111-
func parseNodeSelectorOnceAnnotation(its *workloads.InstanceSet) (map[string]string, error) {
112-
podToNodeMapping := make(map[string]string)
113-
data, ok := its.Annotations[constant.NodeSelectorOnceAnnotationKey]
114-
if !ok {
115-
return podToNodeMapping, nil
116-
}
117-
if err := json.Unmarshal([]byte(data), &podToNodeMapping); err != nil {
118-
return nil, fmt.Errorf("can't unmarshal scheduling information: %w", err)
119-
}
120-
return podToNodeMapping, nil
121-
}
122-
123110
func buildInstanceByTemplate(tree *kubebuilderx.ObjectTree,
124111
instName string, template *instancetemplate.InstanceTemplateExt, its *workloads.InstanceSet) (*workloads.Instance, error) {
125112
labels := getMatchLabels(its.Name)
@@ -143,14 +130,6 @@ func buildInstanceByTemplate(tree *kubebuilderx.ObjectTree,
143130
// set these immutable fields only on initial Pod creation, not updates.
144131
b.SetHostname(instName).
145132
SetSubdomain(getHeadlessSvcName(its.Name))
146-
podToNodeMapping, err := parseNodeSelectorOnceAnnotation(its)
147-
if err != nil {
148-
return nil, err
149-
}
150-
if nodeName, ok := podToNodeMapping[instName]; ok {
151-
// don't specify nodeName directly here, because it may affect WaitForFirstConsumer StorageClass
152-
b.SetNodeSelector(map[string]string{corev1.LabelHostname: nodeName})
153-
}
154133

155134
for i := range template.VolumeClaimTemplates {
156135
b.AddVolumeClaimTemplate(template.VolumeClaimTemplates[i])

pkg/controller/instanceset2/reconciler_status.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,6 @@ func (r *statusReconciler) Reconcile(tree *kubebuilderx.ObjectTree) (kubebuilder
7777
template2TotalReplicas[template.Name] = templateReplicas
7878
}
7979

80-
// podToNodeMapping, err := ParseNodeSelectorOnceAnnotation(its)
81-
// if err != nil {
82-
// return kubebuilderx.Continue, err
83-
// }
84-
8580
for _, inst := range instanceList {
8681
templateName := inst.Labels[instancetemplate.TemplateNameLabelKey]
8782
if template2TemplatesStatus[templateName] == nil {
@@ -114,16 +109,6 @@ func (r *statusReconciler) Reconcile(tree *kubebuilderx.ObjectTree) (kubebuilder
114109
template2TemplatesStatus[templateName].CurrentReplicas++
115110
}
116111
}
117-
118-
// TODO: ???
119-
// if nodeName, ok := podToNodeMapping[inst.Name]; ok {
120-
// // there's chance that a pod is currently running and wait to be deleted so that it can be rescheduled
121-
// if inst.Spec.NodeName == nodeName {
122-
// if err := deleteNodeSelectorOnceAnnotation(its, inst.Name); err != nil {
123-
// return kubebuilderx.Continue, err
124-
// }
125-
// }
126-
// }
127112
}
128113
its.Status.Replicas = replicas
129114
its.Status.ReadyReplicas = readyReplicas

pkg/controller/instanceset2/reconciler_validation.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
2020
package instanceset2
2121

2222
import (
23+
"fmt"
24+
2325
workloads "github.com/apecloud/kubeblocks/apis/workloads/v1"
26+
"github.com/apecloud/kubeblocks/pkg/constant"
2427
"github.com/apecloud/kubeblocks/pkg/controller/instancetemplate"
2528
"github.com/apecloud/kubeblocks/pkg/controller/kubebuilderx"
2629
)
@@ -41,8 +44,22 @@ func (r *validationReconciler) PreCondition(tree *kubebuilderx.ObjectTree) *kube
4144
}
4245

4346
func (r *validationReconciler) Reconcile(tree *kubebuilderx.ObjectTree) (kubebuilderx.Result, error) {
44-
if err := instancetemplate.ValidateInstanceTemplates(tree.GetRoot().(*workloads.InstanceSet), tree); err != nil {
47+
its := tree.GetRoot().(*workloads.InstanceSet)
48+
if err := validateUnsupportedFeatures(its); err != nil {
49+
return kubebuilderx.Commit, err
50+
}
51+
if err := instancetemplate.ValidateInstanceTemplates(its, tree); err != nil {
4552
return kubebuilderx.Commit, err
4653
}
4754
return kubebuilderx.Continue, nil
4855
}
56+
57+
func validateUnsupportedFeatures(its *workloads.InstanceSet) error {
58+
if its == nil || its.Annotations == nil {
59+
return nil
60+
}
61+
if _, ok := its.Annotations[constant.NodeSelectorOnceAnnotationKey]; ok {
62+
return fmt.Errorf("annotation %q is not supported when enableInstanceAPI=true", constant.NodeSelectorOnceAnnotationKey)
63+
}
64+
return nil
65+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
Copyright (C) 2022-2025 ApeCloud Co., Ltd
3+
4+
This file is part of KubeBlocks project
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU Affero General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU Affero General Public License for more details.
15+
16+
You should have received a copy of the GNU Affero General Public License
17+
along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
package instanceset2
21+
22+
import (
23+
"strings"
24+
"testing"
25+
26+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27+
"k8s.io/utils/ptr"
28+
29+
workloads "github.com/apecloud/kubeblocks/apis/workloads/v1"
30+
"github.com/apecloud/kubeblocks/pkg/constant"
31+
"github.com/apecloud/kubeblocks/pkg/controller/kubebuilderx"
32+
)
33+
34+
func TestValidationRejectsNodeSelectorOnce(t *testing.T) {
35+
tree := kubebuilderx.NewObjectTree()
36+
tree.SetRoot(&workloads.InstanceSet{
37+
ObjectMeta: metav1.ObjectMeta{
38+
Name: "test-its",
39+
Namespace: "default",
40+
Annotations: map[string]string{
41+
constant.NodeSelectorOnceAnnotationKey: `{"test-its-0":"node-a"}`,
42+
},
43+
},
44+
Spec: workloads.InstanceSetSpec{
45+
Replicas: ptr.To[int32](1),
46+
EnableInstanceAPI: ptr.To(true),
47+
},
48+
})
49+
50+
_, err := NewValidationReconciler().Reconcile(tree)
51+
if err == nil {
52+
t.Fatalf("expected validation error")
53+
}
54+
if !strings.Contains(err.Error(), constant.NodeSelectorOnceAnnotationKey) {
55+
t.Fatalf("expected error to mention %s, got: %v", constant.NodeSelectorOnceAnnotationKey, err)
56+
}
57+
}

0 commit comments

Comments
 (0)