Files
teleport/lib/cloud/imds.go
T
rosstimothy 8410d1e4fb Refactor imds/gcp to no longer depend on lib/cloud/gcp (#43122)
The imds pacakges are inteneded to be very lightweight
and consumable by client tools without any additional
cloud specific dependencies. By inverting dependencies
such that lib/cloud/gcp depends on lib/cloud/imds/gcp
client tools no longer transatively import the very
large `cloud.google.com/go/compute/apiv1`.

The depguard rules were also updated to prevent any
accidental regressions.
2024-06-18 00:02:18 +00:00

67 lines
2.0 KiB
Go

/*
* Teleport
* Copyright (C) 2023 Gravitational, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cloud
import (
"context"
"time"
"github.com/gravitational/trace"
"github.com/gravitational/teleport/lib/cloud/imds"
)
// discoverInstanceMetadataTimeout is the maximum amount of time allowed
// to discover an instance metadata service. The timeout is short to
// minimize Teleport's startup time when it isn't running on any cloud
// instance. Checking for instance metadata typically takes less than 30ms.
const discoverInstanceMetadataTimeout = 500 * time.Millisecond
// DiscoverInstanceMetadata checks which cloud instance type Teleport is
// running on, if any.
func DiscoverInstanceMetadata(ctx context.Context, providers []func(ctx context.Context) (imds.Client, error)) (imds.Client, error) {
ctx, cancel := context.WithTimeout(ctx, discoverInstanceMetadataTimeout)
defer cancel()
c := make(chan imds.Client)
clients := make([]imds.Client, 0, len(providers))
for _, constructor := range providers {
im, err := constructor(ctx)
if err == nil {
clients = append(clients, im)
}
}
for _, client := range clients {
client := client
go func() {
if client.IsAvailable(ctx) {
c <- client
}
}()
}
select {
case client := <-c:
return client, nil
case <-ctx.Done():
return nil, trace.NotFound("no instance metadata service found")
}
}