mirror of https://github.com/kubernetes/kops.git
Migrate some packages to aws-sdk-go-v2
This commit is contained in:
parent
26b0df58ae
commit
0290a7e8d7
|
@ -22,7 +22,7 @@ import (
|
|||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws/arn"
|
||||
"github.com/aws/aws-sdk-go-v2/aws/arn"
|
||||
"github.com/aws/aws-sdk-go/service/ec2"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
|
|
|
@ -22,7 +22,7 @@ import (
|
|||
|
||||
"k8s.io/kops/pkg/nodeidentity/aws"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws/arn"
|
||||
"github.com/aws/aws-sdk-go-v2/aws/arn"
|
||||
"github.com/aws/aws-sdk-go/service/ec2"
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
"k8s.io/apimachinery/pkg/util/validation/field"
|
||||
|
|
|
@ -19,8 +19,8 @@ package awsmodel
|
|||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/aws/arn"
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/aws/arn"
|
||||
"github.com/aws/aws-sdk-go/service/eventbridge"
|
||||
"k8s.io/kops/pkg/apis/kops"
|
||||
"k8s.io/kops/pkg/model"
|
||||
|
|
|
@ -31,8 +31,8 @@ import (
|
|||
"github.com/aws/aws-sdk-go/service/sqs/sqsiface"
|
||||
"golang.org/x/sync/errgroup"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/aws/arn"
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/aws/arn"
|
||||
"github.com/aws/aws-sdk-go/aws/awserr"
|
||||
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
|
||||
"github.com/aws/aws-sdk-go/aws/request"
|
||||
|
|
|
@ -22,8 +22,8 @@ import (
|
|||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/aws/arn"
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/aws/arn"
|
||||
"github.com/aws/aws-sdk-go/aws/awserr"
|
||||
"github.com/aws/aws-sdk-go/aws/endpoints"
|
||||
"github.com/aws/aws-sdk-go/aws/session"
|
||||
|
|
|
@ -31,12 +31,12 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
awsconfig "github.com/aws/aws-sdk-go-v2/config"
|
||||
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
|
||||
"github.com/aws/aws-sdk-go-v2/service/kms"
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/aws/awserr"
|
||||
"github.com/aws/aws-sdk-go/aws/ec2metadata"
|
||||
"github.com/aws/aws-sdk-go/aws/session"
|
||||
"github.com/aws/aws-sdk-go/service/autoscaling"
|
||||
"github.com/aws/aws-sdk-go/service/kms"
|
||||
"go.uber.org/multierr"
|
||||
"k8s.io/klog/v2"
|
||||
"k8s.io/kops/nodeup/pkg/model"
|
||||
|
@ -254,7 +254,7 @@ func (c *NodeUpCommand) Run(out io.Writer) error {
|
|||
}
|
||||
}
|
||||
|
||||
modelContext.MachineType, err = getMachineType()
|
||||
modelContext.MachineType, err = getMachineType(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get machine type: %w", err)
|
||||
}
|
||||
|
@ -381,19 +381,26 @@ func (c *NodeUpCommand) Run(out io.Writer) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func getMachineType() (string, error) {
|
||||
config := aws.NewConfig()
|
||||
config = config.WithCredentialsChainVerboseErrors(true)
|
||||
func getMachineType(ctx context.Context) (string, error) {
|
||||
config, err := awsconfig.LoadDefaultConfig(ctx)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to load AWS config: %w", err)
|
||||
}
|
||||
|
||||
sess := session.Must(session.NewSession(config))
|
||||
metadata := ec2metadata.New(sess)
|
||||
metadata := imds.NewFromConfig(config)
|
||||
|
||||
// Get the actual instance type by querying the EC2 instance metadata service.
|
||||
instanceTypeName, err := metadata.GetMetadata("instance-type")
|
||||
result, err := metadata.GetMetadata(ctx, &imds.GetMetadataInput{
|
||||
Path: "instance-type",
|
||||
})
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to get instance metadata type: %w", err)
|
||||
}
|
||||
return instanceTypeName, err
|
||||
instanceTypeName, err := io.ReadAll(result.Content)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to read instance metadata response: %w", err)
|
||||
}
|
||||
return string(instanceTypeName), err
|
||||
}
|
||||
|
||||
func completeWarmingLifecycleAction(ctx context.Context, cloud awsup.AWSCloud, modelContext *model.NodeupModelContext) error {
|
||||
|
@ -579,14 +586,15 @@ func getRegion(ctx context.Context, bootConfig *nodeup.BootConfig) (string, erro
|
|||
func seedRNG(ctx context.Context, bootConfig *nodeup.BootConfig, region string) error {
|
||||
switch bootConfig.CloudProvider {
|
||||
case api.CloudProviderAWS:
|
||||
config := aws.NewConfig().WithCredentialsChainVerboseErrors(true).WithRegion(region)
|
||||
sess, err := session.NewSession(config)
|
||||
cfg, err := awsconfig.LoadDefaultConfig(context.TODO(),
|
||||
awsconfig.WithRegion(region),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
random, err := kms.New(sess, config).GenerateRandom(&kms.GenerateRandomInput{
|
||||
NumberOfBytes: aws.Int64(64),
|
||||
random, err := kms.NewFromConfig(cfg).GenerateRandom(ctx, &kms.GenerateRandomInput{
|
||||
NumberOfBytes: aws.Int32(64),
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("generating random seed: %v", err)
|
||||
|
|
|
@ -28,8 +28,8 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
|
||||
"github.com/aws/aws-sdk-go/aws/ec2metadata"
|
||||
"github.com/aws/aws-sdk-go/aws/session"
|
||||
awsconfig "github.com/aws/aws-sdk-go-v2/config"
|
||||
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
|
||||
"github.com/gophercloud/gophercloud"
|
||||
"google.golang.org/api/option"
|
||||
storage "google.golang.org/api/storage/v1"
|
||||
|
@ -217,17 +217,21 @@ func (c *VFSContext) BuildVfsPath(p string) (Path, error) {
|
|||
|
||||
// readAWSMetadata reads the specified path from the AWS EC2 metadata service
|
||||
func (c *VFSContext) readAWSMetadata(ctx context.Context, path string) ([]byte, error) {
|
||||
awsSession, err := session.NewSession()
|
||||
config, err := awsconfig.LoadDefaultConfig(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error building AWS session: %v", err)
|
||||
return nil, fmt.Errorf("failed to load AWS config: %w", err)
|
||||
}
|
||||
client := ec2metadata.New(awsSession)
|
||||
|
||||
client := imds.NewFromConfig(config)
|
||||
|
||||
if strings.HasPrefix(path, "/meta-data/") {
|
||||
s, err := client.GetMetadataWithContext(ctx, strings.TrimPrefix(path, "/meta-data/"))
|
||||
s, err := client.GetMetadata(ctx, &imds.GetMetadataInput{
|
||||
Path: strings.TrimPrefix(path, "/meta-data/"),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error reading from AWS metadata service: %v", err)
|
||||
}
|
||||
return []byte(s), nil
|
||||
return io.ReadAll(s.Content)
|
||||
}
|
||||
// There are others (e.g. user-data), but as we don't use them yet let's not expose them
|
||||
return nil, fmt.Errorf("unhandled aws metadata path %q", path)
|
||||
|
|
|
@ -27,9 +27,10 @@ import (
|
|||
"sync"
|
||||
"time"
|
||||
|
||||
awsconfig "github.com/aws/aws-sdk-go-v2/config"
|
||||
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/aws/credentials"
|
||||
"github.com/aws/aws-sdk-go/aws/ec2metadata"
|
||||
"github.com/aws/aws-sdk-go/aws/session"
|
||||
"github.com/aws/aws-sdk-go/service/ec2"
|
||||
"github.com/aws/aws-sdk-go/service/s3"
|
||||
|
@ -362,24 +363,23 @@ func getRegionFromMetadata(ctx context.Context) (string, error) {
|
|||
|
||||
// Use an even shorter timeout, to minimize impact when not running on EC2
|
||||
// Note that we still retry a few times, this works out a little under a 1s delay
|
||||
shortTimeout := &aws.Config{
|
||||
HTTPClient: &http.Client{
|
||||
Timeout: 100 * time.Millisecond,
|
||||
},
|
||||
shortTimeout := &http.Client{
|
||||
Timeout: 100 * time.Millisecond,
|
||||
}
|
||||
|
||||
metadataSession, err := session.NewSession(shortTimeout)
|
||||
config, err := awsconfig.LoadDefaultConfig(ctx, awsconfig.WithHTTPClient(shortTimeout))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("building AWS metadata session: %w", err)
|
||||
return "", fmt.Errorf("failed to load AWS config: %w", err)
|
||||
}
|
||||
|
||||
metadata := ec2metadata.New(metadataSession)
|
||||
metadataRegion, err := metadata.RegionWithContext(ctx)
|
||||
client := imds.NewFromConfig(config)
|
||||
|
||||
metadataRegion, err := client.GetRegion(ctx, &imds.GetRegionInput{})
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("getting AWS region from metadata: %w", err)
|
||||
}
|
||||
|
||||
return metadataRegion, nil
|
||||
return metadataRegion.Region, nil
|
||||
}
|
||||
|
||||
func VFSPath(url string) (string, error) {
|
||||
|
|
Loading…
Reference in New Issue