Skip to content

Commit

Permalink
fix: find spec.replicas from unstructured object
Browse files Browse the repository at this point in the history
Signed-off-by: kim <[email protected]>
  • Loading branch information
0xgj committed Dec 11, 2023
1 parent fa9a9a0 commit 0f407db
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 3 deletions.
13 changes: 10 additions & 3 deletions pkg/util/controllerfinder/controller_finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
scaleSelector.Scale = int32(val)
if val, err := getSpecReplicas(workload); err == nil {
scaleSelector.Scale = 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
Expand Down
84 changes: 84 additions & 0 deletions pkg/util/controllerfinder/controller_finder_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}

0 comments on commit 0f407db

Please sign in to comment.