Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new: Add support for Images Gen. 2 #526

Merged

Conversation

lgarber-akamai
Copy link
Contributor

@lgarber-akamai lgarber-akamai commented Jun 13, 2024

📝 Description

This pull request adds support for the Images Gen. 2 project, including the following changes:

  • New tags field in Image, ImageCreateOptions, ImageUpdateOptions, and ImageCreateUploadOptions structs
  • New total_size field in Image struct
  • New regions field in Image struct
  • New Client{}.ReplicateImage(...) endpoint method (/images/{image_id}/regions)
  • New ImageStatus constants relating to image replication

✔️ How to Test

The following test steps assume you have pulled down this PR locally and pointed your local environment to an API instance where Images Gen. 2 is available:

export LINODE_URL=...
export LINODE_TOKEN=...

Integration Testing

make ARGS="-run TestImage_CloudInit" fixtures
make ARGS="-run TestImage_CreateUpload" fixtures

# Not currently ready for testing
make ARGS="-run TestImage_Replicate" fixtures

Unit Testing

make testunit

Manual Testing

The following test steps are expected to be run in a linodego sandbox environment (e.g. dx-devenv).

Testing Manual Images

  1. Run the following:
package main

import (
	"context"
	"github.com/davecgh/go-spew/spew"
	"github.com/linode/linodego"
	"log"
	"os"
	"slices"
)

func main() {
	ctx := context.Background()

	client := linodego.NewClient(nil)
	client.SetToken(os.Getenv("LINODE_TOKEN"))

	// Create an instance for imaging
	booted := false

	inst, err := client.CreateInstance(ctx, linodego.InstanceCreateOptions{
		Region:   "us-east",
		Type:     "g6-nanode-1",
		Label:    "linodego-manual-test",
		RootPass: "f00b4r!!!!123123!!!!11",
		Image:    "linode/alpine3.19",
		Booted:   &booted,
	})
	if err != nil {
		log.Fatal(err)
	}

	if _, err := client.WaitForInstanceStatus(ctx, inst.ID, linodego.InstanceOffline, 240); err != nil {
		log.Fatal(err)
	}

	disks, err := client.ListInstanceDisks(ctx, inst.ID, nil)
	if err != nil {
		log.Fatal(err)
	}

	diskIdx := slices.IndexFunc(disks, func(disk linodego.InstanceDisk) bool {
		return disk.Filesystem == linodego.FilesystemExt4
	})

	// Image the instance
	image, err := client.CreateImage(ctx, linodego.ImageCreateOptions{
		DiskID: disks[diskIdx].ID,
		Label:  "linodego-test-image",
		Tags:   &[]string{"very", "cool"},
	})
	if err != nil {
		log.Fatal(err)
	}

	image, err = client.WaitForImageStatus(ctx, image.ID, linodego.ImageStatusAvailable, 240)
	if err != nil {
		log.Fatal(err)
	}

	spew.Dump(image)

	// NOTE: This section is not currently testable
	//image, err = client.ReplicateImage(
	//	ctx,
	//	image.ID,
	//	linodego.ImageReplicateOptions{
	//		Regions: []string{
	//			"us-southeast",
	//		},
	//	},
	//)
	//if err != nil {
	//	log.Fatal(err)
	//}
	//
	//spew.Dump(image)
}
  1. Ensure the output looks similar to the following:
(*linodego.Image)(0xc00018a000)({
 ID: (string) (len=16) "private/12345",
 Label: (string) (len=19) "linodego-test-image",
 TotalSize: (int) 358,
 Regions: ([]linodego.ImageRegion) {
 },
 Tags: ([]string) (len=2 cap=2) {
  (string) (len=4) "cool",
  (string) (len=4) "very"
 },
 ...
})

Testing Uploaded Images

  1. Run the following:
package main

import (
	"context"
	"github.com/davecgh/go-spew/spew"
	"github.com/linode/linodego"
	"log"
	"os"
)

// Minimal gzipped image for testing purposes
var minimalImageData = []byte{
	0x1f, 0x8b, 0x08, 0x08, 0xbd, 0x5c, 0x91, 0x60,
	0x00, 0x03, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x69, 0x6d, 0x67, 0x00, 0x03, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
}

func main() {
	ctx := context.Background()

	client := linodego.NewClient(nil)
	client.SetToken(os.Getenv("LINODE_TOKEN"))

	// Create & upload an image
	image, _, err := client.CreateImageUpload(ctx, linodego.ImageCreateUploadOptions{
		Region: "us-east",
		Label:  "linodego-upload-test",
		Tags:   &[]string{"very", "cool"},
	})
	if err != nil {
		log.Fatal(err)
	}

	image, err = client.WaitForImageStatus(ctx, image.ID, linodego.ImageStatusPendingUpload, 240)
	if err != nil {
		log.Fatal(err)
	}

	spew.Dump(image)
}
  1. Ensure the output looks similar to the following:
(*linodego.Image)(0xc00029a4b0)({
 ID: (string) (len=16) "private/1234",
 TotalSize: (int) 0,
 Regions: ([]linodego.ImageRegion) {
 },
 Tags: ([]string) (len=2 cap=2) {
  (string) (len=4) "cool",
  (string) (len=4) "very"
 },
 ...
})

@lgarber-akamai lgarber-akamai added the new-feature for new features in the changelog. label Jun 13, 2024
oops
@@ -21,3 +21,5 @@
vendor/**/
.env
coverage.txt

go.work.sum
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a part of this PR, I dropped the Go workspace sum file from the source tree because it was constantly triggering unexpected diffs and seems to be machine-dependent. If anyone feels I should revert this or break it out into a separate PR, definitely let me know 🙂

https://go.dev/ref/mod#go-work-file

@lgarber-akamai lgarber-akamai marked this pull request as ready for review June 25, 2024 16:39
@lgarber-akamai lgarber-akamai requested a review from a team as a code owner June 25, 2024 16:39
@lgarber-akamai lgarber-akamai requested review from zliang-akamai and yec-akamai and removed request for a team June 25, 2024 16:39
@linode linode deleted a comment from yec-akamai Jun 27, 2024
@lgarber-akamai
Copy link
Contributor Author

@yec-akamai Thanks for the feedback! I split up the ImageStatus and ImageRegionStatus types/consts and they should now match the API implementations 👍

Copy link
Member

@zliang-akamai zliang-akamai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested, and looks good to me!

Copy link
Contributor

@yec-akamai yec-akamai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work, things work as expected!

@lgarber-akamai lgarber-akamai merged commit 30ab53b into linode:proj/images-gen2 Jun 28, 2024
4 checks passed
yec-akamai added a commit that referenced this pull request Aug 9, 2024
* new: Add support for Images Gen. 2 (#526)

* WIP
oops

* Add unit test case

* Drop go.work.sum from source tree

* oops

* Add tags to ImageUploadOptions

* Separate ImageStatus from ImageRegionStatus

* Fix tests; add WaitForImageRegionStatus

* image replication test with upload image; use getRegionsWithCaps when available

* modified replication tests to use uploaded image; use random regions when available

* fix fixtures

* clean up

* sanitize fixture

* revert ipv6 pool fixture change

---------

Co-authored-by: Lena Garber <114949949+lgarber-akamai@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new-feature for new features in the changelog.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants