Skip to content

Commit

Permalink
fix: avoid patching gameserver continuously
Browse files Browse the repository at this point in the history
Signed-off-by: ChrisLiu <[email protected]>
  • Loading branch information
chrisliu1995 committed Jan 17, 2024
1 parent 02c0009 commit ce8ed5e
Show file tree
Hide file tree
Showing 6 changed files with 500 additions and 63 deletions.
10 changes: 10 additions & 0 deletions pkg/controllers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ limitations under the License.
package controller

import (
"context"
"github.com/openkruise/kruise-game/pkg/controllers/gameserver"
"github.com/openkruise/kruise-game/pkg/controllers/gameserverset"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/manager"
)

Expand All @@ -32,6 +35,13 @@ func init() {
}

func SetupWithManager(m manager.Manager) error {
if err := m.GetFieldIndexer().IndexField(context.Background(), &corev1.Pod{}, "spec.nodeName", func(rawObj client.Object) []string {
pod := rawObj.(*corev1.Pod)
return []string{pod.Spec.NodeName}
}); err != nil {
return err
}

for _, f := range controllerAddFuncs {
if err := f(m); err != nil {
if kindMatchErr, ok := err.(*meta.NoKindMatchError); ok {
Expand Down
85 changes: 50 additions & 35 deletions pkg/controllers/gameserver/gameserver_conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,18 +172,16 @@ func getPodConditions(pod *corev1.Pod) gamekruiseiov1alpha1.GameServerCondition

if message == "" && reason == "" {
return gamekruiseiov1alpha1.GameServerCondition{
Type: gamekruiseiov1alpha1.PodNormal,
Status: corev1.ConditionTrue,
LastProbeTime: metav1.Now(),
Type: gamekruiseiov1alpha1.PodNormal,
Status: corev1.ConditionTrue,
}
}

return gamekruiseiov1alpha1.GameServerCondition{
Type: gamekruiseiov1alpha1.PodNormal,
Status: corev1.ConditionFalse,
Reason: reason,
Message: message,
LastProbeTime: metav1.Now(),
Type: gamekruiseiov1alpha1.PodNormal,
Status: corev1.ConditionFalse,
Reason: reason,
Message: message,
}
}

Expand Down Expand Up @@ -216,7 +214,7 @@ func getNodeConditions(node *corev1.Node) gamekruiseiov1alpha1.GameServerConditi

for _, condition := range node.Status.Conditions {
switch condition.Type {
case corev1.NodeReady:
case corev1.NodeReady, "SufficientIP":
if condition.Status != corev1.ConditionTrue {
message, reason = polyMessageReason(message, reason, condition.Message, string(condition.Type)+":"+condition.Reason)
}
Expand All @@ -229,18 +227,16 @@ func getNodeConditions(node *corev1.Node) gamekruiseiov1alpha1.GameServerConditi

if message == "" && reason == "" {
return gamekruiseiov1alpha1.GameServerCondition{
Type: gamekruiseiov1alpha1.NodeNormal,
Status: corev1.ConditionTrue,
LastProbeTime: metav1.Now(),
Type: gamekruiseiov1alpha1.NodeNormal,
Status: corev1.ConditionTrue,
}
}

return gamekruiseiov1alpha1.GameServerCondition{
Type: gamekruiseiov1alpha1.NodeNormal,
Status: corev1.ConditionFalse,
Reason: reason,
Message: message,
LastProbeTime: metav1.Now(),
Type: gamekruiseiov1alpha1.NodeNormal,
Status: corev1.ConditionFalse,
Reason: reason,
Message: message,
}
}

Expand All @@ -256,38 +252,34 @@ func getPersistentVolumeConditions(pvs []*corev1.PersistentVolume) gamekruiseiov

if message == "" && reason == "" {
return gamekruiseiov1alpha1.GameServerCondition{
Type: gamekruiseiov1alpha1.PersistentVolumeNormal,
Status: corev1.ConditionTrue,
LastProbeTime: metav1.Now(),
Type: gamekruiseiov1alpha1.PersistentVolumeNormal,
Status: corev1.ConditionTrue,
}
}

return gamekruiseiov1alpha1.GameServerCondition{
Type: gamekruiseiov1alpha1.PersistentVolumeNormal,
Status: corev1.ConditionFalse,
Reason: reason,
Message: message,
LastProbeTime: metav1.Now(),
Type: gamekruiseiov1alpha1.PersistentVolumeNormal,
Status: corev1.ConditionFalse,
Reason: reason,
Message: message,
}
}

func pvcNotFoundCondition(namespace, pvcName string) gamekruiseiov1alpha1.GameServerCondition {
return gamekruiseiov1alpha1.GameServerCondition{
Type: gamekruiseiov1alpha1.PersistentVolumeNormal,
Status: corev1.ConditionFalse,
Reason: pvcNotFoundReason,
Message: fmt.Sprintf("There is no pvc named %s/%s in cluster", namespace, pvcName),
LastProbeTime: metav1.Now(),
Type: gamekruiseiov1alpha1.PersistentVolumeNormal,
Status: corev1.ConditionFalse,
Reason: pvcNotFoundReason,
Message: fmt.Sprintf("There is no pvc named %s/%s in cluster", namespace, pvcName),
}
}

func pvNotFoundCondition(namespace, pvcName string) gamekruiseiov1alpha1.GameServerCondition {
return gamekruiseiov1alpha1.GameServerCondition{
Type: gamekruiseiov1alpha1.PersistentVolumeNormal,
Status: corev1.ConditionFalse,
Reason: pvNotFoundReason,
Message: fmt.Sprintf("There is no pv which pvc %s/%s is bound with", namespace, pvcName),
LastProbeTime: metav1.Now(),
Type: gamekruiseiov1alpha1.PersistentVolumeNormal,
Status: corev1.ConditionFalse,
Reason: pvNotFoundReason,
Message: fmt.Sprintf("There is no pv which pvc %s/%s is bound with", namespace, pvcName),
}
}

Expand Down Expand Up @@ -371,3 +363,26 @@ func isConditionEqual(a, b gamekruiseiov1alpha1.GameServerCondition) bool {
}
return true
}

func isConditionsEqual(a, b []gamekruiseiov1alpha1.GameServerCondition) bool {
if len(a) != len(b) {
return false
}

Check warning on line 370 in pkg/controllers/gameserver/gameserver_conditions.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/gameserver/gameserver_conditions.go#L369-L370

Added lines #L369 - L370 were not covered by tests

for _, aCondition := range a {
found := false
for _, bCondition := range b {
if aCondition.Type == bCondition.Type {
found = true
if !isConditionEqual(aCondition, bCondition) {
return false
}

Check warning on line 379 in pkg/controllers/gameserver/gameserver_conditions.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/gameserver/gameserver_conditions.go#L378-L379

Added lines #L378 - L379 were not covered by tests
}
}
if !found {
return false
}

Check warning on line 384 in pkg/controllers/gameserver/gameserver_conditions.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/gameserver/gameserver_conditions.go#L383-L384

Added lines #L383 - L384 were not covered by tests
}

return true
}
42 changes: 42 additions & 0 deletions pkg/controllers/gameserver/gameserver_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/workqueue"
"k8s.io/klog/v2"
"k8s.io/utils/pointer"
"reflect"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
Expand Down Expand Up @@ -80,6 +84,10 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error {
klog.Error(err)
return err
}
if err = watchNode(c, mgr.GetClient()); err != nil {
klog.Error(err)
return err
}

Check warning on line 90 in pkg/controllers/gameserver/gameserver_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/gameserver/gameserver_controller.go#L87-L90

Added lines #L87 - L90 were not covered by tests

return nil
}
Expand Down Expand Up @@ -128,6 +136,40 @@ func watchPod(c controller.Controller) error {
return nil
}

func watchNode(c controller.Controller, cli client.Client) error {
if err := c.Watch(&source.Kind{Type: &corev1.Node{}}, &handler.Funcs{
UpdateFunc: func(updateEvent event.UpdateEvent, limitingInterface workqueue.RateLimitingInterface) {
nodeNew := updateEvent.ObjectNew.(*corev1.Node)
nodeOld := updateEvent.ObjectOld.(*corev1.Node)
if reflect.DeepEqual(nodeNew.Status.Conditions, nodeOld.Status.Conditions) {
return
}
podList := &corev1.PodList{}
ownerGss, _ := labels.NewRequirement(gamekruiseiov1alpha1.GameServerOwnerGssKey, selection.Exists, []string{})
err := cli.List(context.Background(), podList, &client.ListOptions{
LabelSelector: labels.NewSelector().Add(*ownerGss),
FieldSelector: fields.Set{"spec.nodeName": nodeNew.Name}.AsSelector(),
})
if err != nil {
klog.Errorf("List Pods By NodeName failed: %s", err.Error())
return
}
for _, pod := range podList.Items {
klog.Infof("Watch Node %s Conditions Changed, adding pods %s/%s in reconcile queue", nodeNew.Name, pod.Namespace, pod.Name)
limitingInterface.Add(reconcile.Request{
NamespacedName: types.NamespacedName{
Namespace: pod.GetNamespace(),
Name: pod.GetName(),
},
})
}

Check warning on line 165 in pkg/controllers/gameserver/gameserver_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/gameserver/gameserver_controller.go#L139-L165

Added lines #L139 - L165 were not covered by tests
},
}); err != nil {
return err
}
return nil

Check warning on line 170 in pkg/controllers/gameserver/gameserver_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controllers/gameserver/gameserver_controller.go#L167-L170

Added lines #L167 - L170 were not covered by tests
}

//+kubebuilder:rbac:groups=game.kruise.io,resources=gameservers,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=game.kruise.io,resources=gameservers/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=game.kruise.io,resources=gameservers/finalizers,verbs=update
Expand Down
Loading

0 comments on commit ce8ed5e

Please sign in to comment.