This repository has been archived by the owner on Feb 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #208 from sofastack/feat.count_on_base
添加 event新类型,添加 计算 基座 Pod上 最大/最小/平均 已被调度模块实例个数 修复ModuleDeployment单测
- Loading branch information
Showing
11 changed files
with
303 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
module-controller/internal/event/modulereplicaset_replicas_changed_event.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package event | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/sofastack/sofa-serverless/api/v1alpha1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
type ModuleReplicaSetReplicasChangedEvent struct { | ||
client.Client | ||
Context context.Context | ||
ModuleReplicaSet *v1alpha1.ModuleReplicaSet | ||
} | ||
|
||
func PublishModuleReplicaSetReplicasChangedEvent(client client.Client, ctx context.Context, moduleReplicaSet *v1alpha1.ModuleReplicaSet) error { | ||
return PublishEvent(ModuleReplicaSetReplicasChangedEvent{ModuleReplicaSet: moduleReplicaSet, Client: client, Context: ctx}) | ||
} | ||
|
||
func (e ModuleReplicaSetReplicasChangedEvent) GetEventType() EventType { | ||
return ModuleReplicaSetReplicasChanged | ||
} |
109 changes: 109 additions & 0 deletions
109
module-controller/internal/handler/modulereplicaset_replicas_changed_handler.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
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" | ||
"strconv" | ||
) | ||
|
||
type ModuleReplicaSetReplicasChangedHandler struct { | ||
} | ||
|
||
func (h ModuleReplicaSetReplicasChangedHandler) Async() bool { | ||
return true | ||
} | ||
|
||
func (h ModuleReplicaSetReplicasChangedHandler) Handle(e event.Event) error { | ||
moduleReplicaSetReplicasChangedEvent := e.(event.ModuleReplicaSetReplicasChangedEvent) | ||
moduleReplicaSet := moduleReplicaSetReplicasChangedEvent.ModuleReplicaSet | ||
ctx := moduleReplicaSetReplicasChangedEvent.Context | ||
k8sClient := moduleReplicaSetReplicasChangedEvent.Client | ||
baseDeploymentName := moduleReplicaSet.Labels[label.DeploymentNameLabel] | ||
if baseDeploymentName == "" { | ||
return nil | ||
} | ||
deployment := &v1.Deployment{} | ||
err := k8sClient.Get(ctx, | ||
types.NamespacedName{Namespace: moduleReplicaSet.Namespace, Name: baseDeploymentName}, deployment) | ||
if err != nil { | ||
return utils.Error(err, "Failed to get deployment", "deploymentName", baseDeploymentName) | ||
} | ||
allPodSelector, err := metav1.LabelSelectorAsSelector(&moduleReplicaSet.Spec.Selector) | ||
allPods := &corev1.PodList{} | ||
if err = k8sClient.List(ctx, allPods, &client.ListOptions{Namespace: moduleReplicaSet.Namespace, LabelSelector: allPodSelector}); err != nil { | ||
return utils.Error(err, "Failed to list pod", "moduleReplicaSetName", moduleReplicaSet.Name) | ||
} | ||
|
||
if len(allPods.Items) <= 0 { | ||
return nil | ||
} | ||
var minInstanceCount int | ||
var maxInstanceCount int | ||
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 | ||
} | ||
} else { | ||
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 | ||
} | ||
moduleInstanceCount = len(moduleList.Items) | ||
} | ||
|
||
// 赋值第一个pod的安装数量为最小值 | ||
if index == 0 { | ||
minInstanceCount = moduleInstanceCount | ||
} | ||
// 对比获取最小值 | ||
if minInstanceCount > moduleInstanceCount { | ||
minInstanceCount = moduleInstanceCount | ||
} | ||
// 对比获取最大值 | ||
if moduleInstanceCount > maxInstanceCount { | ||
maxInstanceCount = moduleInstanceCount | ||
} | ||
// 获取全部安装数量 | ||
totalInstanceCount += moduleInstanceCount | ||
} | ||
|
||
if deployment.Labels == nil { | ||
deployment.Labels = map[string]string{} | ||
} | ||
deployment.Labels[label.MaxModuleInstanceCount] = strconv.Itoa(maxInstanceCount) | ||
deployment.Labels[label.MinModuleInstanceCount] = strconv.Itoa(minInstanceCount) | ||
avgInstanceCount := float64(totalInstanceCount) / float64(len(allPods.Items)) | ||
deployment.Labels[label.AverageModuleInstanceCount] = fmt.Sprintf("%.2f", avgInstanceCount) | ||
|
||
if err = k8sClient.Update(ctx, deployment); err != nil { | ||
return utils.Error(err, "Failed to update Deployment", "Deployment", baseDeploymentName) | ||
} | ||
return nil | ||
} | ||
|
||
func (h ModuleReplicaSetReplicasChangedHandler) InterestIn(e event.Event) bool { | ||
return e.GetEventType() == event.ModuleReplicaSetReplicasChanged | ||
} | ||
|
||
func init() { | ||
event.Handlers = append(event.Handlers, ModuleReplicaSetReplicasChangedHandler{}) | ||
} |
16 changes: 16 additions & 0 deletions
16
module-controller/internal/handler/modulereplicaset_replicas_changed_handler_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package handler | ||
|
||
import ( | ||
"github.com/sofastack/sofa-serverless/internal/event" | ||
"github.com/sofastack/sofa-serverless/internal/utils" | ||
"testing" | ||
) | ||
|
||
func TestModuleReplicaSetReplicasChangedHandler(t *testing.T) { | ||
moduleReplicaSet := utils.PrepareModuleReplicaSet("default", "test-module-replica-set-name") | ||
event.PublishModuleReplicaSetReplicasChangedEvent(TestModuleReplicaSetReplicasChangedClient{}, nil, &moduleReplicaSet) | ||
} | ||
|
||
type TestModuleReplicaSetReplicasChangedClient struct { | ||
utils.MockClient | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.