Skip to content

Commit

Permalink
Greatly clarify misleading metadata logging
Browse files Browse the repository at this point in the history
Signed-off-by: Connor Catlett <[email protected]>
  • Loading branch information
ConnorJC3 committed May 24, 2024
1 parent bce9949 commit a18de96
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 26 deletions.
26 changes: 13 additions & 13 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,24 +147,24 @@ func main() {
var md metadata.MetadataService
var metadataErr error

if region == "" {
klog.V(5).InfoS("[Debug] Retrieving region from metadata service")
md, metadataErr = metadata.NewMetadataService(cfg, region)
if metadataErr != nil {
klog.ErrorS(metadataErr, "Could not determine region from any metadata service. The region can be manually supplied via the AWS_REGION environment variable.")
panic(metadataErr)
if region != "" {
klog.InfoS("Region provided via AWS_REGION environment variable", "region", region)
if options.Mode != driver.ControllerMode {
klog.InfoS("Node service requires metadata even if AWS_REGION provided, initializing metadata")
md, metadataErr = metadata.NewMetadataService(cfg, region)
}
} else {
klog.InfoS("No region provided via AWS_REGION environment variable, initializing metadata")
md, metadataErr = metadata.NewMetadataService(cfg, region)
region = md.GetRegion()
}

if md == nil {
if options.Mode == driver.NodeMode || options.Mode == driver.AllMode {
md, metadataErr = metadata.NewMetadataService(cfg, region)
if metadataErr != nil {
klog.ErrorS(metadataErr, "failed to initialize metadata service")
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
}
if metadataErr != nil {
klog.ErrorS(metadataErr, "Failed to initialize metadata when it is required")
if options.Mode == driver.ControllerMode {
klog.InfoS("The region can be manually supplied via the AWS_REGION environment variable")
}
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
}

cloud, err := cloud.NewCloud(region, options.AwsSdkDebugLog, options.UserAgentExtra, options.Batching)
Expand Down
25 changes: 17 additions & 8 deletions pkg/cloud/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,19 @@ var _ MetadataService = &Metadata{}
func NewMetadataService(cfg MetadataServiceConfig, region string) (MetadataService, error) {
metadata, err := retrieveEC2Metadata(cfg.EC2MetadataClient, region)
if err == nil {
klog.InfoS("ec2 metadata is available")
return metadata, nil
klog.InfoS("Retrieved metadata from IMDS")
return metadata.overrideRegion(region), nil
}
klog.ErrorS(err, "Retrieving IMDS metadata failed, falling back to Kubernetes metadata")

klog.InfoS("failed to retrieve instance data from ec2 metadata; retrieving instance data from kubernetes api", "err", err)
metadata, err = retrieveK8sMetadata(cfg.K8sAPIClient)
if err == nil {
klog.InfoS("kubernetes api is available")
return metadata, nil
klog.InfoS("Retrieved metadata from Kubernetes")
return metadata.overrideRegion(region), nil
}
klog.ErrorS(err, "Retrieving Kubernetes metadata failed")

return nil, fmt.Errorf("error getting instance data from ec2 metadata or kubernetes api")
return nil, fmt.Errorf("IMDS metadata and Kubernetes metadata are both unavailable")
}

func retrieveEC2Metadata(ec2MetadataClient EC2MetadataClient, region string) (*Metadata, error) {
Expand All @@ -67,7 +68,7 @@ func retrieveEC2Metadata(ec2MetadataClient EC2MetadataClient, region string) (*M

svc, err := ec2MetadataClient()
if err != nil {
klog.ErrorS(err, "Failed to initialize EC2 Metadata client")
klog.ErrorS(err, "failed to initialize EC2 Metadata client")
return nil, err
}
return EC2MetadataInstanceInfo(svc, region)
Expand All @@ -76,13 +77,21 @@ func retrieveEC2Metadata(ec2MetadataClient EC2MetadataClient, region string) (*M
func retrieveK8sMetadata(k8sAPIClient KubernetesAPIClient) (*Metadata, error) {
clientset, err := k8sAPIClient()
if err != nil {
klog.InfoS("error creating kubernetes api client", "err", err)
klog.ErrorS(err, "error creating kubernetes api client")
return nil, err
}

return KubernetesAPIInstanceInfo(clientset)
}

// Override the region on a Metadata object if it is non-empty
func (m *Metadata) overrideRegion(region string) *Metadata {
if region != "" {
m.Region = region
}
return m
}

// GetInstanceID returns the instance identification.
func (m *Metadata) GetInstanceID() string {
return m.InstanceID
Expand Down
2 changes: 1 addition & 1 deletion pkg/cloud/metadata/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func TestNewMetadataService(t *testing.T) {
region: "us-west-2",
ec2MetadataError: errors.New("EC2 metadata error"),
k8sAPIError: errors.New("K8s API error"),
expectedError: errors.New("error getting instance data from ec2 metadata or kubernetes api"),
expectedError: errors.New("IMDS metadata and Kubernetes metadata are both unavailable"),
},
}

Expand Down
4 changes: 0 additions & 4 deletions pkg/driver/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,6 @@ type NodeService struct {

// NewNodeService creates a new node service
func NewNodeService(o *Options, md metadata.MetadataService, m mounter.Mounter, k kubernetes.Interface) *NodeService {
klog.V(5).InfoS("[Debug] Retrieving node info from metadata service")
region := os.Getenv("AWS_REGION")
klog.InfoS("regionFromSession Node service", "region", region)

if k != nil {
// Remove taint from node to indicate driver startup success
// This is done at the last possible moment to prevent race conditions or false positive removals
Expand Down

0 comments on commit a18de96

Please sign in to comment.