Skip to content

Commit

Permalink
fix ServiceExtendedIndicator return nil object
Browse files Browse the repository at this point in the history
  • Loading branch information
luomingmeng authored and waynepeking348 committed Mar 25, 2024
1 parent eab8ab0 commit c10d1d1
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 9 deletions.
35 changes: 26 additions & 9 deletions pkg/metaserver/spd/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ import (

v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"

workloadapis "github.com/kubewharf/katalyst-api/pkg/apis/workload/v1alpha1"
"github.com/kubewharf/katalyst-api/pkg/client/clientset/versioned/scheme"
"github.com/kubewharf/katalyst-core/pkg/util"
)

Expand Down Expand Up @@ -139,21 +141,36 @@ func (m *serviceProfilingManager) ServiceExtendedIndicator(ctx context.Context,
}

object := indicator.Indicators.Object
if object == nil {
raw := indicator.Indicators.Raw
if object == nil && raw == nil {
return false, fmt.Errorf("%s inidators object is nil", name)
}

t := reflect.TypeOf(indicators)
if t.Kind() != reflect.Ptr {
return false, fmt.Errorf("indicators must be pointers to structs")
}
if object != nil {
t := reflect.TypeOf(indicators)
if t.Kind() != reflect.Ptr {
return false, fmt.Errorf("indicators must be pointers to structs")
}

v := reflect.ValueOf(object)
if !v.CanConvert(t) {
return false, fmt.Errorf("%s indicators object cannot convert to %v", name, t.Name())
}

v := reflect.ValueOf(object)
if !v.CanConvert(t) {
return false, fmt.Errorf("%s indicators object cannot convert to %v", name, t.Name())
reflect.ValueOf(indicators).Elem().Set(v.Convert(t).Elem())
} else {
object, ok := indicators.(runtime.Object)
if !ok {
return false, fmt.Errorf("%s indicators object cannot convert to runtime.Object", name)
}

deserializer := scheme.Codecs.UniversalDeserializer()
_, _, err := deserializer.Decode(raw, nil, object)
if err != nil {
return false, err
}
}

reflect.ValueOf(indicators).Elem().Set(v.Convert(t).Elem())
return util.IsExtendedBaselinePod(pod, indicator.BaselinePercent, extendedBaselineSentinel, name)
}

Expand Down
66 changes: 66 additions & 0 deletions pkg/metaserver/spd/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package spd

import (
"context"
"encoding/json"
"io/ioutil"
"os"
"testing"
Expand Down Expand Up @@ -555,6 +556,71 @@ func Test_serviceProfilingManager_ServiceExtendedIndicator(t *testing.T) {
},
isBaseline: true,
},
{
name: "use raw data",
fields: fields{
nodeName: "node-1",
spd: &workloadapis.ServiceProfileDescriptor{
ObjectMeta: metav1.ObjectMeta{
Name: "spd-1",
Namespace: "default",
Annotations: map[string]string{
pkgconsts.ServiceProfileDescriptorAnnotationKeyConfigHash: "3c7e3ff3f218",
consts.SPDAnnotationExtendedBaselineSentinelKey: "{\"TestExtended\":{\"timeStamp\":\"2023-08-01T00:00:01Z\",\"podName\":\"pod1\"}}",
},
},
Spec: workloadapis.ServiceProfileDescriptorSpec{
ExtendedIndicator: []workloadapis.ServiceExtendedIndicatorSpec{
{
Name: "TestExtended",
BaselinePercent: pointer.Int32(10),
Indicators: runtime.RawExtension{
Raw: func(object runtime.Object) []byte {
marshal, err := json.Marshal(object)
if err != nil {
return nil
}
return marshal
}(
&workloadapis.TestExtendedIndicators{
Indicators: &workloadapis.TestIndicators{}},
),
},
},
},
},
},
cnc: &v1alpha1.CustomNodeConfig{
ObjectMeta: metav1.ObjectMeta{
Name: "node-1",
},
Status: v1alpha1.CustomNodeConfigStatus{
ServiceProfileConfigList: []v1alpha1.TargetConfig{
{
ConfigName: "spd-1",
ConfigNamespace: "default",
Hash: "3c7e3ff3f218",
},
},
},
},
},
args: args{
pod: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "pod-1",
Namespace: "default",
Annotations: map[string]string{
consts.PodAnnotationSPDNameKey: "spd-1",
},
},
},
},
want: &workloadapis.TestExtendedIndicators{
Indicators: &workloadapis.TestIndicators{},
},
isBaseline: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit c10d1d1

Please sign in to comment.