-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(agent): implements
/cloud/metadata
agent endpoint.
This extends agent server with `/cloud/metadata` endpoint which returns instance details such as `cloud_provider` and `instance_type`.
- Loading branch information
1 parent
c2b8fbd
commit 969ee86
Showing
7 changed files
with
138 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright (C) 2024 ScyllaDB | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"sync" | ||
|
||
"github.com/go-chi/render" | ||
"github.com/pkg/errors" | ||
"github.com/scylladb/go-log" | ||
"github.com/scylladb/scylla-manager/v3/pkg/cloudmeta" | ||
"github.com/scylladb/scylla-manager/v3/swagger/gen/agent/models" | ||
) | ||
|
||
func newMetadataHandler(logger log.Logger) http.HandlerFunc { | ||
var ( | ||
m sync.Mutex | ||
loaded bool | ||
metadata cloudmeta.InstanceMetadata | ||
) | ||
|
||
// Caches only successful result of GetInstanceMetadata. | ||
lazyGetMetadata := func(ctx context.Context) (cloudmeta.InstanceMetadata, error) { | ||
m.Lock() | ||
defer m.Unlock() | ||
if loaded { | ||
return metadata, nil | ||
} | ||
|
||
metaSvc, err := cloudmeta.NewCloudMeta(logger) | ||
if err != nil { | ||
return cloudmeta.InstanceMetadata{}, errors.Wrap(err, "NewCloudMeta") | ||
} | ||
|
||
metadata, err = metaSvc.GetInstanceMetadata(ctx) | ||
if err != nil { | ||
return cloudmeta.InstanceMetadata{}, err | ||
} | ||
|
||
loaded = true | ||
return metadata, nil | ||
} | ||
|
||
return func(w http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
instanceMeta, err := lazyGetMetadata(ctx) | ||
if err != nil { | ||
// Metadata may not be available for several reasons: | ||
// 1. running on-premise 2. disabled 3. smth went wrong with metadata server. | ||
// As we cannot distinguish between these cases, we can only log err. | ||
logger.Error(ctx, "GetInstanceMetadata", "err", err) | ||
render.Respond(w, r, models.InstanceMetadata{}) | ||
return | ||
} | ||
render.Respond(w, r, models.InstanceMetadata{ | ||
CloudProvider: string(instanceMeta.CloudProvider), | ||
InstanceType: instanceMeta.InstanceType, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters