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

Add additional options to control storage endpoints on import #84

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,11 @@
after the import process has completed. Possible values are: `true` to
leave it in the bucket, `false` to remove it. Default is `false`.

- `storage_endpoint` (string) - StorageEndpoint custom Yandex Object Storage endpoint to upload image, Default `storage.yandexcloud.net`.

- `storage_endpoint_autoresolve` (bool) - StorageEndpointAutoresolve auto resolve storage endpoint via YC Public API ListEndpoints call. Option has
precedence over 'storage_endpoint' option.

- `storage_region` (string) - StorageRegion custom Yandex Object region. Default `ru-central1`

<!-- End of code generated from the comments of the Config struct in post-processor/yandex-import/post-processor.go; -->
44 changes: 43 additions & 1 deletion post-processor/yandex-import/post-processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ package yandeximport
import (
"context"
"fmt"
"log"
"strings"

"github.com/hashicorp/hcl/v2/hcldec"
"github.com/hashicorp/packer-plugin-sdk/common"
Expand All @@ -17,9 +19,15 @@ import (
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
"github.com/hashicorp/packer-plugin-yandex/builder/yandex"
yandexexport "github.com/hashicorp/packer-plugin-yandex/post-processor/yandex-export"
"github.com/yandex-cloud/go-genproto/yandex/cloud/endpoint"
"github.com/yandex-cloud/go-genproto/yandex/cloud/iam/v1/awscompatibility"
)

const (
defaultStorageEndpoint = "storage.yandexcloud.net"
defaultStorageRegion = "ru-central1"
)

type Config struct {
common.PackerConfig `mapstructure:",squash"`
yandex.AccessConfig `mapstructure:",squash"`
Expand All @@ -42,6 +50,14 @@ type Config struct {
// leave it in the bucket, `false` to remove it. Default is `false`.
SkipClean bool `mapstructure:"skip_clean" required:"false"`

// StorageEndpoint custom Yandex Object Storage endpoint to upload image, Default `storage.yandexcloud.net`.
StorageEndpoint string `mapstructure:"storage_endpoint" required:"false"`
// StorageEndpointAutoresolve auto resolve storage endpoint via YC Public API ListEndpoints call. Option has
// precedence over 'storage_endpoint' option.
Copy link
Contributor

Choose a reason for hiding this comment

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

If we reject using both at the same time through a check in Configure (which I think would make sense), this comment should be amended to state that this and the storage_endpoint options are mutually exclusive.

StorageEndpointAutoresolve bool `mapstructure:"storage_endpoint_autoresolve" required:"false"`
// StorageRegion custom Yandex Object region. Default `ru-central1`
StorageRegion string `mapstructure:"storage_region" required:"false"`

ctx interpolate.Context
}

Expand Down Expand Up @@ -124,7 +140,33 @@ func (p *PostProcessor) PostProcess(ctx context.Context, ui packersdk.Ui, artifa
return nil, false, false, err
}

storageClient, err := newYCStorageClient("", respWithKey.GetAccessKey().GetKeyId(), respWithKey.GetSecret())
if p.config.StorageEndpoint == "" {
p.config.StorageEndpoint = defaultStorageEndpoint
}

if p.config.StorageRegion == "" {
p.config.StorageRegion = defaultStorageRegion
}

if p.config.StorageEndpointAutoresolve {
Copy link
Contributor

Choose a reason for hiding this comment

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

We should probably also check during Configure that this is not set alongside StorageEndpoint, the two look mutually exclusive, so we can error if both are set in the same config.

ui.Say("Resolving storage endpoint...")
response, err := client.SDK().ApiEndpoint().ApiEndpoint().Get(ctx, &endpoint.GetApiEndpointRequest{
ApiEndpointId: "storage",
})

if err != nil {
return nil, false, false, err
}
// The API request returns a string potentially containing colon followed by a port number.
// This causes confusion later at Compute API call, which does not understand URI with port specification.
// While clearly a bug on Compute side, this is the only workaround we have to be able to operate if
// the url is resolved automatically.
p.config.StorageEndpoint, _, _ = strings.Cut(response.Address, ":")
}

log.Printf("[DEBUG] Using storage endpoint: '%s'", p.config.StorageEndpoint)

storageClient, err := newYCStorageClient(p.config.StorageEndpoint, respWithKey.GetAccessKey().GetKeyId(), respWithKey.GetSecret())
if err != nil {
return nil, false, false, fmt.Errorf("error create object storage client: %s", err)
}
Expand Down
106 changes: 56 additions & 50 deletions post-processor/yandex-import/post-processor.hcl2spec.go

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

5 changes: 1 addition & 4 deletions post-processor/yandex-import/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ import (
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
)

const defaultS3Region = "ru-central1"
const defaultStorageEndpoint = "storage.yandexcloud.net"

func newYCStorageClient(storageEndpoint, accessKey, secretKey string) (*s3.S3, error) {
var creds *credentials.Credentials

Expand All @@ -28,7 +25,7 @@ func newYCStorageClient(storageEndpoint, accessKey, secretKey string) (*s3.S3, e

s3Config := &aws.Config{
Endpoint: aws.String(storageEndpoint),
Region: aws.String(defaultS3Region),
Region: aws.String(defaultStorageRegion),
}

switch {
Expand Down