Skip to content

Commit

Permalink
fix: find spec.replicas workload
Browse files Browse the repository at this point in the history
  • Loading branch information
0xgj committed Nov 29, 2023
1 parent f8994e7 commit 70ee846
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 2 deletions.
11 changes: 9 additions & 2 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 {
if val, err := getSpecReplicas(workload); err == nil {
scaleSelector.Scale = int32(val)

Check failure on line 443 in pkg/util/controllerfinder/controller_finder.go

View workflow job for this annotation

GitHub Actions / golangci-lint

unnecessary conversion (unconvert)
}
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 70ee846

Please sign in to comment.