diff --git a/pkg/util/controllerfinder/controller_finder.go b/pkg/util/controllerfinder/controller_finder.go index d0f089a7c7..7d49b3e601 100644 --- a/pkg/util/controllerfinder/controller_finder.go +++ b/pkg/util/controllerfinder/controller_finder.go @@ -439,13 +439,20 @@ func (r *ControllerFinder) getPodStatefulSetLike(ref ControllerReference, namesp UID: workload.GetUID(), }, } - obj := workload.UnstructuredContent() - if val, found, err := unstructured.NestedInt64(obj, "spec.replicas"); err == nil && found { + if val, err := getSpecReplicas(workload); err == nil { scaleSelector.Scale = int32(val) } return scaleSelector, nil } +func getSpecReplicas(workload *unstructured.Unstructured) (int32, error) { + obj := workload.UnstructuredContent() + if val, found, err := unstructured.NestedInt64(obj, "spec", "replicas"); err == nil && found { + return int32(val), nil + } + return 0, nil +} + func (r *ControllerFinder) getScaleController(ref ControllerReference, namespace string) (*ScaleAndSelector, error) { if isValidGroupVersionKind(ref.APIVersion, ref.Kind) { return nil, nil diff --git a/pkg/util/controllerfinder/controller_finder_test.go b/pkg/util/controllerfinder/controller_finder_test.go new file mode 100644 index 0000000000..49b44b9a30 --- /dev/null +++ b/pkg/util/controllerfinder/controller_finder_test.go @@ -0,0 +1,84 @@ +/* +Copyright 2021 The Kruise Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package controllerfinder + +import ( + "testing" + + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" +) + +func Test_getSpecReplicas(t *testing.T) { + type args struct { + workload *unstructured.Unstructured + } + tests := []struct { + name string + args args + want int32 + wantErr bool + }{ + { + name: "replicas exist", + args: args{ + workload: &unstructured.Unstructured{ + Object: map[string]interface{}{ + "spec": map[string]interface{}{ + "replicas": int64(3), + }, + }, + }, + }, + want: 3, + wantErr: false, + }, + { + name: "replicas do not exist", + args: args{ + workload: &unstructured.Unstructured{ + Object: map[string]interface{}{ + "spec": map[string]interface{}{}, + }, + }, + }, + want: 0, + wantErr: false, + }, + { + name: "spec does not exist", + args: args{ + workload: &unstructured.Unstructured{ + Object: map[string]interface{}{}, + }, + }, + want: 0, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := getSpecReplicas(tt.args.workload) + if (err != nil) != tt.wantErr { + t.Errorf("getSpecReplicas() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("getSpecReplicas() = %v, want %v", got, tt.want) + } + }) + } +}