-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Backport of [NET-6465] Consider init container resources when determi…
…ning if existing + desired deployments are equal into release/1.4.x (#3579) * backport of commit fafd900 * backport of commit f5918d8 * backport of commit 366ebec --------- Co-authored-by: Nathan Coleman <nathan.coleman@hashicorp.com>
- Loading branch information
1 parent
45e68f2
commit c1f763d
Showing
2 changed files
with
237 additions
and
3 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
216 changes: 216 additions & 0 deletions
216
control-plane/api-gateway/gatekeeper/deployment_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,216 @@ | ||
package gatekeeper | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
appsv1 "k8s.io/api/apps/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
|
||
"github.com/hashicorp/consul-k8s/control-plane/api-gateway/common" | ||
) | ||
|
||
func Test_compareDeployments(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
a, b *appsv1.Deployment | ||
shouldBeEqual bool | ||
}{ | ||
{ | ||
name: "zero-state deployments", | ||
a: &appsv1.Deployment{}, | ||
b: &appsv1.Deployment{}, | ||
shouldBeEqual: true, | ||
}, | ||
{ | ||
name: "different replicas", | ||
a: &appsv1.Deployment{ | ||
Spec: appsv1.DeploymentSpec{ | ||
Replicas: common.PointerTo(int32(1)), | ||
}, | ||
}, | ||
b: &appsv1.Deployment{ | ||
Spec: appsv1.DeploymentSpec{ | ||
Replicas: common.PointerTo(int32(2)), | ||
}, | ||
}, | ||
shouldBeEqual: false, | ||
}, | ||
{ | ||
name: "same replicas", | ||
a: &appsv1.Deployment{ | ||
Spec: appsv1.DeploymentSpec{ | ||
Replicas: common.PointerTo(int32(1)), | ||
}, | ||
}, | ||
b: &appsv1.Deployment{ | ||
Spec: appsv1.DeploymentSpec{ | ||
Replicas: common.PointerTo(int32(1)), | ||
}, | ||
}, | ||
shouldBeEqual: true, | ||
}, | ||
{ | ||
name: "different init container resources", | ||
a: &appsv1.Deployment{ | ||
Spec: appsv1.DeploymentSpec{ | ||
Template: corev1.PodTemplateSpec{ | ||
Spec: corev1.PodSpec{ | ||
InitContainers: []corev1.Container{ | ||
{ | ||
Resources: corev1.ResourceRequirements{ | ||
Limits: corev1.ResourceList{ | ||
"cpu": requireQuantity(t, "111m"), | ||
"memory": requireQuantity(t, "111Mi"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
b: &appsv1.Deployment{ | ||
Spec: appsv1.DeploymentSpec{ | ||
Template: corev1.PodTemplateSpec{ | ||
Spec: corev1.PodSpec{ | ||
InitContainers: []corev1.Container{ | ||
{ | ||
Resources: corev1.ResourceRequirements{ | ||
Limits: corev1.ResourceList{ | ||
"cpu": requireQuantity(t, "222m"), | ||
"memory": requireQuantity(t, "111Mi"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
shouldBeEqual: false, | ||
}, | ||
{ | ||
name: "same init container resources", | ||
a: &appsv1.Deployment{ | ||
Spec: appsv1.DeploymentSpec{ | ||
Template: corev1.PodTemplateSpec{ | ||
Spec: corev1.PodSpec{ | ||
InitContainers: []corev1.Container{ | ||
{ | ||
Resources: corev1.ResourceRequirements{ | ||
Limits: corev1.ResourceList{ | ||
"cpu": requireQuantity(t, "111m"), | ||
"memory": requireQuantity(t, "111Mi"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
b: &appsv1.Deployment{ | ||
Spec: appsv1.DeploymentSpec{ | ||
Template: corev1.PodTemplateSpec{ | ||
Spec: corev1.PodSpec{ | ||
InitContainers: []corev1.Container{ | ||
{ | ||
Resources: corev1.ResourceRequirements{ | ||
Limits: corev1.ResourceList{ | ||
"cpu": requireQuantity(t, "111m"), | ||
"memory": requireQuantity(t, "111Mi"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
shouldBeEqual: true, | ||
}, | ||
{ | ||
name: "different container ports", | ||
a: &appsv1.Deployment{ | ||
Spec: appsv1.DeploymentSpec{ | ||
Template: corev1.PodTemplateSpec{ | ||
Spec: corev1.PodSpec{ | ||
Containers: []corev1.Container{ | ||
{ | ||
Ports: []corev1.ContainerPort{ | ||
{ContainerPort: 7070}, | ||
{ContainerPort: 9090}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
b: &appsv1.Deployment{ | ||
Spec: appsv1.DeploymentSpec{ | ||
Template: corev1.PodTemplateSpec{ | ||
Spec: corev1.PodSpec{ | ||
Containers: []corev1.Container{ | ||
{ | ||
Ports: []corev1.ContainerPort{ | ||
{ContainerPort: 8080}, | ||
{ContainerPort: 9090}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
shouldBeEqual: false, | ||
}, | ||
{ | ||
name: "same container ports", | ||
a: &appsv1.Deployment{ | ||
Spec: appsv1.DeploymentSpec{ | ||
Template: corev1.PodTemplateSpec{ | ||
Spec: corev1.PodSpec{ | ||
Containers: []corev1.Container{ | ||
{ | ||
Ports: []corev1.ContainerPort{ | ||
{ContainerPort: 8080}, | ||
{ContainerPort: 9090}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
b: &appsv1.Deployment{ | ||
Spec: appsv1.DeploymentSpec{ | ||
Template: corev1.PodTemplateSpec{ | ||
Spec: corev1.PodSpec{ | ||
Containers: []corev1.Container{ | ||
{ | ||
Ports: []corev1.ContainerPort{ | ||
{ContainerPort: 8080}, | ||
{ContainerPort: 9090}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
shouldBeEqual: true, | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
if testCase.shouldBeEqual { | ||
assert.True(t, compareDeployments(testCase.a, testCase.b), "expected deployments to be equal but they were not") | ||
} else { | ||
assert.False(t, compareDeployments(testCase.a, testCase.b), "expected deployments to be different but they were not") | ||
} | ||
}) | ||
} | ||
} |