Skip to content

Commit

Permalink
Merge pull request #137 from spangenberg/ebs
Browse files Browse the repository at this point in the history
Add e2e tests for EBS support
  • Loading branch information
openshift-merge-robot authored Feb 4, 2019
2 parents 7be33d2 + 17bc2b5 commit ee2a7ec
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 2 deletions.
25 changes: 25 additions & 0 deletions pkg/actuators/machine/awsclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,28 @@ func (client *AwsClientWrapper) GetAvailabilityZone(machine *machinev1beta1.Mach
}
return *instance.Placement.AvailabilityZone, nil
}

// GetVolumes gets volumes attached to instance
func (client *AwsClientWrapper) GetVolumes(machine *machinev1beta1.Machine) (map[string]map[string]interface{}, error) {
instance, err := getRunningInstance(machine, client.client)
if err != nil {
return nil, err
}
if instance.BlockDeviceMappings == nil {
return nil, err
}
volumes := make(map[string]map[string]interface{}, len(instance.BlockDeviceMappings))
for _, blockDeviceMapping := range instance.BlockDeviceMappings {
volume, err := getVolume(client.client, *blockDeviceMapping.Ebs.VolumeId)
if err != nil {
return volumes, err
}
volumes[*blockDeviceMapping.DeviceName] = map[string]interface{}{
"id": *volume.VolumeId,
"iops": *volume.Iops,
"size": *volume.Size,
"type": *volume.VolumeType,
}
}
return volumes, nil
}
16 changes: 16 additions & 0 deletions pkg/actuators/machine/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,22 @@ func getInstances(machine *machinev1.Machine, client awsclient.Client, instanceS
return instances, nil
}

func getVolume(client awsclient.Client, volumeID string) (*ec2.Volume, error) {
request := &ec2.DescribeVolumesInput{
VolumeIds: []*string{&volumeID},
}
result, err := client.DescribeVolumes(request)
if err != nil {
return &ec2.Volume{}, err
}

if len(result.Volumes) != 1 {
return &ec2.Volume{}, fmt.Errorf("unable to get volume ID: %q", volumeID)
}

return result.Volumes[0], nil
}

// terminateInstances terminates all provided instances with a single EC2 request.
func terminateInstances(client awsclient.Client, instances []*ec2.Instance) error {
instanceIDs := []*string{}
Expand Down
5 changes: 5 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type Client interface {
RunInstances(*ec2.RunInstancesInput) (*ec2.Reservation, error)
DescribeInstances(*ec2.DescribeInstancesInput) (*ec2.DescribeInstancesOutput, error)
TerminateInstances(*ec2.TerminateInstancesInput) (*ec2.TerminateInstancesOutput, error)
DescribeVolumes(*ec2.DescribeVolumesInput) (*ec2.DescribeVolumesOutput, error)

RegisterInstancesWithLoadBalancer(*elb.RegisterInstancesWithLoadBalancerInput) (*elb.RegisterInstancesWithLoadBalancerOutput, error)
ELBv2DescribeLoadBalancers(*elbv2.DescribeLoadBalancersInput) (*elbv2.DescribeLoadBalancersOutput, error)
Expand Down Expand Up @@ -101,6 +102,10 @@ func (c *awsClient) TerminateInstances(input *ec2.TerminateInstancesInput) (*ec2
return c.ec2Client.TerminateInstances(input)
}

func (c *awsClient) DescribeVolumes(input *ec2.DescribeVolumesInput) (*ec2.DescribeVolumesOutput, error) {
return c.ec2Client.DescribeVolumes(input)
}

func (c *awsClient) RegisterInstancesWithLoadBalancer(input *elb.RegisterInstancesWithLoadBalancerInput) (*elb.RegisterInstancesWithLoadBalancerOutput, error) {
return c.elbClient.RegisterInstancesWithLoadBalancer(input)
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/client/fake/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ func (c *awsClient) TerminateInstances(input *ec2.TerminateInstancesInput) (*ec2
return &ec2.TerminateInstancesOutput{}, nil
}

func (c *awsClient) DescribeVolumes(input *ec2.DescribeVolumesInput) (*ec2.DescribeVolumesOutput, error) {
// Feel free to extend the returned values
return &ec2.DescribeVolumesOutput{}, nil
}

func (c *awsClient) RegisterInstancesWithLoadBalancer(input *elb.RegisterInstancesWithLoadBalancerInput) (*elb.RegisterInstancesWithLoadBalancerOutput, error) {
// Feel free to extend the returned values
return &elb.RegisterInstancesWithLoadBalancerOutput{}, nil
Expand Down
13 changes: 13 additions & 0 deletions pkg/client/mock/client_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions test/machines/machines_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,37 @@ var _ = framework.SigKubeDescribe("Machines", func() {
f.DeleteMachineAndWait(testMachine, acw)
})

It("Can create EBS volumes", func() {
testMachineProviderSpec, err := utils.TestingMachineProviderSpecWithEBS(awsCredSecret.Name, cluster.Name)
Expect(err).NotTo(HaveOccurred())
testMachine := manifests.TestingMachine(cluster.Name, cluster.Namespace, testMachineProviderSpec)
f.CreateMachineAndWait(testMachine, acw)
machinesToDelete.AddMachine(testMachine, f, acw)

volumes, err := acw.GetVolumes(testMachine)
Expect(err).NotTo(HaveOccurred())

By("Checking EBS volume mount", func() {
Expect(volumes).To(HaveKey("/dev/sda1"))
})

By("Checking EBS volume size", func() {
Expect(volumes["/dev/sda1"]["size"].(int64)).To(Equal(int64(142)))
})

By("Checking EBS volume type", func() {
Expect(volumes["/dev/sda1"]["type"].(string)).To(Equal("gp2"))
})

By("Checking only root volume get's modified", func() {
for dev, volume := range volumes {
if dev != "/dev/sda1" {
Expect(volume["size"].(int64)).ToNot(Equal(int64(142)))
}
}
})
})

It("Can deploy compute nodes through machineset", func() {
// Any controller linking kubernetes node with its machine
// needs to run inside the cluster where the node is registered.
Expand Down
30 changes: 28 additions & 2 deletions test/utils/manifests.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ func GenerateAwsCredentialsSecretFromEnv(secretName, namespace string) *apiv1.Se
}
}

func TestingMachineProviderSpec(awsCredentialsSecretName string, clusterID string) (machinev1beta1.ProviderSpec, error) {
func testingAWSMachineProviderSpec(awsCredentialsSecretName string, clusterID string) *providerconfigv1.AWSMachineProviderConfig {
publicIP := true
machinePc := &providerconfigv1.AWSMachineProviderConfig{
return &providerconfigv1.AWSMachineProviderConfig{
AMI: providerconfigv1.AWSResourceReference{
Filters: []providerconfigv1.Filter{
{
Expand Down Expand Up @@ -86,7 +86,33 @@ func TestingMachineProviderSpec(awsCredentialsSecretName string, clusterID strin
},
PublicIP: &publicIP,
}
}

func TestingMachineProviderSpec(awsCredentialsSecretName string, clusterID string) (machinev1beta1.ProviderSpec, error) {
machinePc := testingAWSMachineProviderSpec(awsCredentialsSecretName, clusterID)
codec, err := providerconfigv1.NewCodec()
if err != nil {
return machinev1beta1.ProviderSpec{}, fmt.Errorf("failed creating codec: %v", err)
}
config, err := codec.EncodeProviderSpec(machinePc)
if err != nil {
return machinev1beta1.ProviderSpec{}, fmt.Errorf("EncodeToProviderConfig failed: %v", err)
}
return *config, nil
}

func TestingMachineProviderSpecWithEBS(awsCredentialsSecretName string, clusterID string) (machinev1beta1.ProviderSpec, error) {
volumeSize := int64(142)
volumeType := "gp2"
machinePc := testingAWSMachineProviderSpec(awsCredentialsSecretName, clusterID)
machinePc.BlockDevices = []providerconfigv1.BlockDeviceMappingSpec{
{
EBS: &providerconfigv1.EBSBlockDeviceSpec{
VolumeSize: &volumeSize,
VolumeType: &volumeType,
},
},
}
codec, err := providerconfigv1.NewCodec()
if err != nil {
return machinev1beta1.ProviderSpec{}, fmt.Errorf("failed creating codec: %v", err)
Expand Down

0 comments on commit ee2a7ec

Please sign in to comment.