-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: differentiated updates to GameServers
Signed-off-by: ChrisLiu <[email protected]>
- Loading branch information
1 parent
250bd86
commit f75df8e
Showing
6 changed files
with
337 additions
and
1 deletion.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package webhook | ||
|
||
import ( | ||
"context" | ||
gameKruiseV1alpha1 "github.com/openkruise/kruise-game/apis/v1alpha1" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/resource" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
utilruntime "k8s.io/apimachinery/pkg/util/runtime" | ||
"reflect" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
"testing" | ||
) | ||
|
||
var ( | ||
scheme = runtime.NewScheme() | ||
) | ||
|
||
func init() { | ||
utilruntime.Must(gameKruiseV1alpha1.AddToScheme(scheme)) | ||
utilruntime.Must(corev1.AddToScheme(scheme)) | ||
} | ||
|
||
func TestPatchContainers(t *testing.T) { | ||
tests := []struct { | ||
gs *gameKruiseV1alpha1.GameServer | ||
oldPod *corev1.Pod | ||
newContainers []corev1.Container | ||
}{ | ||
// case 0 | ||
{ | ||
gs: nil, | ||
oldPod: &corev1.Pod{ | ||
Spec: corev1.PodSpec{ | ||
Containers: []corev1.Container{ | ||
{ | ||
Name: "A", | ||
Image: "A-v1", | ||
}, | ||
}, | ||
}, | ||
}, | ||
newContainers: []corev1.Container{ | ||
{ | ||
Name: "A", | ||
Image: "A-v1", | ||
}, | ||
}, | ||
}, | ||
|
||
// case 1 | ||
{ | ||
gs: &gameKruiseV1alpha1.GameServer{ | ||
Spec: gameKruiseV1alpha1.GameServerSpec{ | ||
Containers: []gameKruiseV1alpha1.GameServerContainer{ | ||
{ | ||
Name: "A", | ||
Image: "A-v2", | ||
Resources: corev1.ResourceRequirements{ | ||
Requests: map[corev1.ResourceName]resource.Quantity{ | ||
corev1.ResourceMemory: resource.MustParse("2Gi"), | ||
}, | ||
Limits: map[corev1.ResourceName]resource.Quantity{ | ||
corev1.ResourceCPU: resource.MustParse("1"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
oldPod: &corev1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Labels: map[string]string{ | ||
gameKruiseV1alpha1.GameServerOwnerGssKey: "xxx", | ||
}, | ||
}, | ||
Spec: corev1.PodSpec{ | ||
Containers: []corev1.Container{ | ||
{ | ||
Name: "A", | ||
Image: "A-v1", | ||
Resources: corev1.ResourceRequirements{ | ||
Requests: map[corev1.ResourceName]resource.Quantity{ | ||
corev1.ResourceCPU: resource.MustParse("500m"), | ||
corev1.ResourceMemory: resource.MustParse("2Gi"), | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "B", | ||
Image: "B-v1", | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
||
newContainers: []corev1.Container{ | ||
{ | ||
Name: "A", | ||
Image: "A-v2", | ||
Resources: corev1.ResourceRequirements{ | ||
Requests: map[corev1.ResourceName]resource.Quantity{ | ||
corev1.ResourceCPU: resource.MustParse("500m"), | ||
corev1.ResourceMemory: resource.MustParse("2Gi"), | ||
}, | ||
Limits: map[corev1.ResourceName]resource.Quantity{ | ||
corev1.ResourceCPU: resource.MustParse("1"), | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "B", | ||
Image: "B-v1", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for i, test := range tests { | ||
expect := test.newContainers | ||
var objs []client.Object | ||
if test.gs != nil { | ||
objs = append(objs, test.gs) | ||
} | ||
c := fake.NewClientBuilder().WithScheme(scheme).WithObjects(objs...).Build() | ||
newPod, err := patchContainers(c, test.oldPod, context.Background()) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
actual := newPod.Spec.Containers | ||
if !reflect.DeepEqual(expect, actual) { | ||
t.Errorf("case %d: expect new containers %v, but actually got %v", i, expect, actual) | ||
} | ||
} | ||
} |