Skip to content

Instantly share code, notes, and snippets.

@Jeffwan
Last active March 13, 2020 08:12
Show Gist options
  • Save Jeffwan/e05ebbdd88822061a9ffd95220a6d8e4 to your computer and use it in GitHub Desktop.
Save Jeffwan/e05ebbdd88822061a9ffd95220a6d8e4 to your computer and use it in GitHub Desktop.
buildNodeTemplateTest
// AddCustomResourcesToNode adds Custom Resource to given node. Given resource will override existing resource of node.
func AddCustomResourcesToNode(node *apiv1.Node, resourceList apiv1.ResourceList) {
for resourceName, value := range resourceList {
node.Status.Capacity[apiv1.ResourceName(resourceName)] = value
node.Status.Allocatable[apiv1.ResourceName(resourceName)] = value
}
}
// AddLabelsToNode adds Labels to given node. Given labels will override existing label of node.
func AddLabelsToNode(node *apiv1.Node, labels map[string]string) {
for key, value := range labels {
node.Labels[key] = value
}
}
// AddTaintsToNode adds taints to given node.
func AddTaintsToNode(node *apiv1.Node, taints []apiv1.Taint) {
node.Spec.Taints = append(node.Spec.Taints, taints...)
}
func removeNodeLabelByKey(node *apiv1.Node, key string) {
delete(node.Labels, key)
}
func TestBuildNodeFromTemplate(t *testing.T) {
type buildNodeFromTemplateTestCase struct {
asg *asg
tags []*autoscaling.TagDescription
asgTemplate *asgTemplate
expectedNode *apiv1.Node
expectedError error
}
awsManager := &AwsManager{}
asg := &asg{AwsRef: AwsRef{Name:"test-auto-scaling-group"}}
c5Instance := &InstanceType{
InstanceType: "c5.xlarge",
VCPU: 4,
MemoryMb: 8192,
GPU: 0,
}
asgTemplateRegion := "us-west-2"
asgTemplateZone := "us-west-2"
nodeName := "testNode"
// Add genericLabels for all node object then we can compare node.Labels directly
genericLabels := buildGenericLabels(&asgTemplate{
InstanceType: c5Instance,
Region: asgTemplateRegion,
Zone: asgTemplateZone,
Tags: nil,
}, nodeName)
// Node with custom resource
ephemeralStorageKey := "ephemeral-storage"
ephemeralStorageValue := int64(20)
vpcIPKey := "vpc.amazonaws.com/PrivateIPv4Address"
vpcIPValue := int64(30)
nodeWithCustomResource := test.BuildTestNode(nodeName, c5Instance.VCPU, c5Instance.MemoryMb)
test.AddLabelsToNode(nodeWithCustomResource, genericLabels)
test.AddCustomResourcesToNode(nodeWithCustomResource, apiv1.ResourceList{
apiv1.ResourceName(ephemeralStorageKey): *resource.NewQuantity(ephemeralStorageValue, resource.DecimalSI),
apiv1.ResourceName(vpcIPKey): *resource.NewQuantity(vpcIPValue, resource.DecimalSI),
})
// Nod with labels
nodeWithLabels := test.BuildTestNode(nodeName, c5Instance.VCPU, c5Instance.MemoryMb)
test.AddLabelsToNode(nodeWithLabels, genericLabels)
GPULabelValue := "nvidia-telsa-v100"
test.AddLabelsToNode(nodeWithLabels, map[string]string{GPULabel: GPULabelValue})
// Node with taints
nodeWithTaints := test.BuildTestNode(nodeName, c5Instance.VCPU, c5Instance.MemoryMb)
test.AddLabelsToNode(nodeWithTaints, genericLabels)
gpuTaint := apiv1.Taint{
Key: "nvidia.com/gpu",
Value: "present",
Effect: "NoSchedule",
}
test.AddTaintsToNode(nodeWithTaints, []apiv1.Taint{gpuTaint})
resourceTags := []*autoscaling.TagDescription{
{
Key: aws.String(fmt.Sprintf("k8s.io/cluster-autoscaler/node-template/resources/%s", ephemeralStorageKey)),
Value: aws.String(strconv.FormatInt(ephemeralStorageValue, 10)),
},
{
Key: aws.String(fmt.Sprintf("k8s.io/cluster-autoscaler/node-template/resources/%s", vpcIPKey)),
Value: aws.String(strconv.FormatInt(vpcIPValue, 10)),
},
}
labelTags := []*autoscaling.TagDescription{
{
Key: aws.String(fmt.Sprintf("k8s.io/cluster-autoscaler/node-template/label/%s", GPULabel)),
Value: aws.String(GPULabelValue),
},
}
taintTags := []*autoscaling.TagDescription{
{
Key: aws.String(fmt.Sprintf("k8s.io/cluster-autoscaler/node-template/taint/%s", gpuTaint.Key)),
Value: aws.String(fmt.Sprintf("%s:%s", gpuTaint.Value, gpuTaint.Effect)),
},
}
testcases := []buildNodeFromTemplateTestCase{
{
asg: asg,
asgTemplate: &asgTemplate{
InstanceType: c5Instance,
Tags: resourceTags,
Region: asgTemplateRegion,
Zone: asgTemplateZone,
},
expectedNode: nodeWithCustomResource,
expectedError: nil,
},
{
asg: asg,
asgTemplate: &asgTemplate{
InstanceType: c5Instance,
Tags: labelTags,
Region: asgTemplateRegion,
Zone: asgTemplateZone,
},
expectedNode: nodeWithLabels,
expectedError: nil,
},
{
asg: asg,
asgTemplate: &asgTemplate{
InstanceType: c5Instance,
Tags: taintTags,
Region: asgTemplateRegion,
Zone: asgTemplateZone,
},
expectedNode: nodeWithTaints,
expectedError: nil,
},
}
for _, tc := range testcases {
observedNode, observedErr := awsManager.buildNodeFromTemplate(tc.asg, tc.asgTemplate)
// buildNodeFromTemplate use random string for name and we want to exclude that label before comparision
removeNodeLabelByKey(observedNode, apiv1.LabelHostname)
removeNodeLabelByKey(tc.expectedNode, apiv1.LabelHostname)
assert.Equal(t, tc.expectedNode.Labels, observedNode.Labels, "incorrect node label template")
assert.Equal(t, tc.expectedNode.Status.Capacity, observedNode.Status.Capacity, "incorrect node capacity resource template")
assert.Equal(t, tc.expectedNode.Status.Allocatable, observedNode.Status.Allocatable, "incorrect node allocatable resource template")
assert.Equal(t, tc.expectedNode.Spec.Taints, observedNode, "incorrect node taint template")
assert.Equal(t, tc.expectedError, observedErr, "incorrect error")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment