Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
获取模块安装数量修改为从获取到的module实例计算
Browse files Browse the repository at this point in the history
  • Loading branch information
liu-657667 committed Oct 25, 2023
1 parent 7ebc680 commit dcadbd1
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 164 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package handler

import (
"fmt"
"github.com/sofastack/sofa-serverless/api/v1alpha1"
"github.com/sofastack/sofa-serverless/internal/constants/label"
"github.com/sofastack/sofa-serverless/internal/event"
"github.com/sofastack/sofa-serverless/internal/utils"
v1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
Expand Down Expand Up @@ -50,13 +52,15 @@ func (h ModuleReplicaSetReplicasChangedHandler) Handle(e event.Event) error {
var totalInstanceCount int
for index, item := range allPods.Items {
var moduleInstanceCount int
if cntStr, ok := item.Labels[label.ModuleInstanceCount]; ok {
moduleInstanceCount, err = strconv.Atoi(cntStr)
if err != nil {
log.Log.Error(err, fmt.Sprintf("invalid ModuleInstanceCount in pod %v", item.Name))
continue
}
moduleList := &v1alpha1.ModuleList{}
err := k8sClient.List(ctx, moduleList, &client.ListOptions{LabelSelector: labels.SelectorFromSet(map[string]string{
label.BaseInstanceIpLabel: item.Status.PodIP,
})}, client.InNamespace(moduleReplicaSet.Namespace))
if err != nil {
log.Log.Error(err, fmt.Sprintf("can't find any module in pod %v", item.Name))
continue

Check warning on line 61 in module-controller/internal/handler/modulereplicaset_replicas_changed_handler.go

View check run for this annotation

Codecov / codecov/patch

module-controller/internal/handler/modulereplicaset_replicas_changed_handler.go#L60-L61

Added lines #L60 - L61 were not covered by tests
}
moduleInstanceCount = len(moduleList.Items)
// 赋值第一个pod的安装数量为最小值
if index == 0 {
minInstanceCount = moduleInstanceCount
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ func TestModuleReplicaSetReplicasChangedHandler(t *testing.T) {
}

type TestModuleReplicaSetReplicasChangedClient struct {
utils.MockOnBaseClient
utils.MockClient
}
125 changes: 0 additions & 125 deletions module-controller/internal/utils/count_on_base_utils.go

This file was deleted.

32 changes: 0 additions & 32 deletions module-controller/internal/utils/count_on_base_utils_test.go

This file was deleted.

93 changes: 93 additions & 0 deletions module-controller/internal/utils/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ package utils

import (
"github.com/sofastack/sofa-serverless/api/v1alpha1"
"github.com/sofastack/sofa-serverless/internal/constants/label"
"golang.org/x/net/context"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"reflect"
"sigs.k8s.io/controller-runtime/pkg/client"
"strconv"
)

func PrepareModuleDeployment(namespace, moduleDeploymentName string) v1alpha1.ModuleDeployment {
Expand Down Expand Up @@ -41,6 +45,36 @@ func PrepareModuleDeployment(namespace, moduleDeploymentName string) v1alpha1.Mo
return moduleDeployment
}

func PrepareModuleReplicaSet(namespace, moduleReplicaSetName string) v1alpha1.ModuleReplicaSet {

moduleReplicaSet := v1alpha1.ModuleReplicaSet{
Spec: v1alpha1.ModuleReplicaSetSpec{
Replicas: 1,
Template: v1alpha1.ModuleTemplateSpec{
Spec: v1alpha1.ModuleSpec{
Module: v1alpha1.ModuleInfo{
Name: "dynamic-provider",
Version: "1.0.0",
Url: "http://serverless-opensource.oss-cn-shanghai.aliyuncs.com/module-packages/stable/dynamic-provider-1.0.0-ark-biz.jar",
},
},
},
},
ObjectMeta: metav1.ObjectMeta{
Name: moduleReplicaSetName,
Namespace: namespace,
Labels: map[string]string{
"app": "dynamic-stock",
label.MaxModuleCount: "10",
label.ModuleSchedulingStrategy: string(v1alpha1.Scatter),
label.DeploymentNameLabel: "test-deployment-name",
},
Annotations: map[string]string{},
},
}
return moduleReplicaSet
}

type MockClient struct {
}

Expand All @@ -65,9 +99,68 @@ func (m MockClient) Get(ctx context.Context, key client.ObjectKey, obj client.Ob
}

func (m MockClient) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error {
if list == nil {
return nil
}

listValue := reflect.ValueOf(list)

itemsType, itemsField := getListType(listValue)
if itemsType == nil {
return nil
}

Check warning on line 111 in module-controller/internal/utils/test_utils.go

View check run for this annotation

Codecov / codecov/patch

module-controller/internal/utils/test_utils.go#L110-L111

Added lines #L110 - L111 were not covered by tests

// 检查切片中的元素类型
switch itemsType {
case reflect.TypeOf(corev1.Pod{}):
var mockPodList []corev1.Pod
for i := 3; i > 0; i-- {
podName := "mock-pod-" + strconv.Itoa(i)
podIp := "127.0.0." + strconv.Itoa(i)
pod := &corev1.Pod{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{
Name: podName,
Labels: map[string]string{
label.ModuleInstanceCount: strconv.Itoa(i),
},
},
Spec: corev1.PodSpec{},
Status: corev1.PodStatus{
PodIP: podIp,
},
}
mockPodList = append(mockPodList, *pod)
}
itemsField.Set(reflect.ValueOf(mockPodList))
case reflect.TypeOf(v1alpha1.Module{}):
var mockModuleList []v1alpha1.Module
moduleName := "mock-module-name"
module := &v1alpha1.Module{
ObjectMeta: metav1.ObjectMeta{
Name: moduleName,
},
}
mockModuleList = append(mockModuleList, *module)
itemsField.Set(reflect.ValueOf(mockModuleList))
}
return nil
}

func getListType(listValue reflect.Value) (reflect.Type, reflect.Value) {

itemsField := listValue.Elem().FieldByName("Items")
if !itemsField.IsValid() {
return nil, reflect.Value{}
}

Check warning on line 155 in module-controller/internal/utils/test_utils.go

View check run for this annotation

Codecov / codecov/patch

module-controller/internal/utils/test_utils.go#L154-L155

Added lines #L154 - L155 were not covered by tests
itemsType := itemsField.Type()
// 列表的类型是切片
if itemsType.Kind() != reflect.Slice {
return nil, reflect.Value{}
}

Check warning on line 160 in module-controller/internal/utils/test_utils.go

View check run for this annotation

Codecov / codecov/patch

module-controller/internal/utils/test_utils.go#L159-L160

Added lines #L159 - L160 were not covered by tests
return itemsType.Elem(), itemsField
}

func (m MockClient) Create(ctx context.Context, obj client.Object, opts ...client.CreateOption) error {
return nil
}
Expand Down
12 changes: 12 additions & 0 deletions module-controller/internal/utils/test_utils_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package utils

import (
"github.com/sofastack/sofa-serverless/api/v1alpha1"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"testing"
)
Expand All @@ -12,6 +14,12 @@ func TestPrepareModuleDeployment(t *testing.T) {
assert.Equal(t, moduleDeploymentName, moduleDeployment.Name)
}

func TestPrepareModuleReplicaSet(t *testing.T) {
moduleReplicaSetName := "testModuleReplicaSet"
moduleReplicaSet := PrepareModuleReplicaSet("default", moduleReplicaSetName)
assert.Equal(t, moduleReplicaSetName, moduleReplicaSet.Name)
}

func TestMockClient(t *testing.T) {
mockClient := MockClient{}
assert.True(t, mockClient.Scheme() == nil)
Expand All @@ -29,4 +37,8 @@ func TestMockClient(t *testing.T) {
assert.Equal(t, nil, mockClient.DeleteAllOf(nil, nil))
assert.Equal(t, nil, mockClient.Status())
assert.Equal(t, nil, mockClient.SubResource(""))

assert.Equal(t, nil, mockClient.List(nil, &v1alpha1.ModuleList{}))
assert.Equal(t, nil, mockClient.List(nil, &corev1.PodList{}))

}

0 comments on commit dcadbd1

Please sign in to comment.