mirror of
https://github.com/edgelesssys/constellation.git
synced 2025-01-11 07:29:29 -05:00
deubgd: add instance metadata to collected logs
Signed-off-by: Paul Meyer <49727155+katexochen@users.noreply.github.com>
This commit is contained in:
parent
568f288f0d
commit
e5e5d8eaae
@ -59,13 +59,7 @@ func main() {
|
||||
|
||||
csp := os.Getenv("CONSTEL_CSP")
|
||||
|
||||
infoMap := info.NewMap()
|
||||
infoMap.RegisterOnReceiveTrigger(
|
||||
logcollector.NewStartTrigger(ctx, wg, platform.FromString(csp), log.Named("logcollector")),
|
||||
)
|
||||
|
||||
download := deploy.New(log.Named("download"), &net.Dialer{}, serviceManager, streamer, infoMap)
|
||||
var fetcher metadata.Fetcher
|
||||
var fetcher *cloudprovider.Fetcher
|
||||
switch platform.FromString(csp) {
|
||||
case platform.AWS:
|
||||
meta, err := awscloud.New(ctx)
|
||||
@ -94,8 +88,16 @@ func main() {
|
||||
|
||||
default:
|
||||
log.Errorf("Unknown / unimplemented cloud provider CONSTEL_CSP=%v. Using fallback", csp)
|
||||
fetcher = fallback.Fetcher{}
|
||||
fetcher = fallback.NewFallbackFetcher()
|
||||
}
|
||||
|
||||
infoMap := info.NewMap()
|
||||
infoMap.RegisterOnReceiveTrigger(
|
||||
logcollector.NewStartTrigger(ctx, wg, platform.FromString(csp), fetcher, log.Named("logcollector")),
|
||||
)
|
||||
|
||||
download := deploy.New(log.Named("download"), &net.Dialer{}, serviceManager, streamer, infoMap)
|
||||
|
||||
sched := metadata.NewScheduler(log.Named("scheduler"), fetcher, download)
|
||||
serv := server.New(log.Named("server"), serviceManager, streamer, infoMap)
|
||||
if err := deploy.DefaultServiceUnit(ctx, serviceManager); err != nil {
|
||||
|
@ -18,6 +18,7 @@ import (
|
||||
|
||||
"github.com/edgelesssys/constellation/v2/debugd/internal/debugd/info"
|
||||
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
|
||||
"github.com/edgelesssys/constellation/v2/internal/cloud/metadata"
|
||||
"github.com/edgelesssys/constellation/v2/internal/logger"
|
||||
"github.com/edgelesssys/constellation/v2/internal/versions"
|
||||
)
|
||||
@ -32,7 +33,7 @@ const (
|
||||
//
|
||||
// This requires podman to be installed.
|
||||
func NewStartTrigger(ctx context.Context, wg *sync.WaitGroup, provider cloudprovider.Provider,
|
||||
logger *logger.Logger,
|
||||
metadata providerMetadata, logger *logger.Logger,
|
||||
) func(*info.Map) {
|
||||
return func(infoMap *info.Map) {
|
||||
wg.Add(1)
|
||||
@ -83,7 +84,7 @@ func NewStartTrigger(ctx context.Context, wg *sync.WaitGroup, provider cloudprov
|
||||
return
|
||||
}
|
||||
infoMapM = filterInfoMap(infoMapM)
|
||||
infoMapM["provider"] = provider.String()
|
||||
setCloudMetadata(ctx, infoMapM, provider, metadata)
|
||||
|
||||
logger.Infof("Writing logstash pipeline")
|
||||
pipelineConf := logstashConfInput{
|
||||
@ -246,6 +247,28 @@ func filterInfoMap(in map[string]string) map[string]string {
|
||||
return out
|
||||
}
|
||||
|
||||
func setCloudMetadata(ctx context.Context, m map[string]string, provider cloudprovider.Provider, metadata providerMetadata) {
|
||||
m["provider"] = provider.String()
|
||||
|
||||
self, err := metadata.Self(ctx)
|
||||
if err != nil {
|
||||
m["name"] = "unknown"
|
||||
m["role"] = "unknown"
|
||||
m["vpcip"] = "unknown"
|
||||
} else {
|
||||
m["name"] = self.Name
|
||||
m["role"] = self.Role.String()
|
||||
m["vpcip"] = self.VPCIP
|
||||
}
|
||||
|
||||
uid, err := metadata.UID(ctx)
|
||||
if err != nil {
|
||||
m["uid"] = "unknown"
|
||||
} else {
|
||||
m["uid"] = uid
|
||||
}
|
||||
}
|
||||
|
||||
func newCmdLogger(logger *logger.Logger) io.Writer {
|
||||
return &cmdLogger{logger: logger}
|
||||
}
|
||||
@ -258,3 +281,10 @@ func (c *cmdLogger) Write(p []byte) (n int, err error) {
|
||||
c.logger.Infof("%s", p)
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
type providerMetadata interface {
|
||||
// Self retrieves the current instance.
|
||||
Self(ctx context.Context) (metadata.InstanceMetadata, error)
|
||||
// UID returns the UID of the current instance.
|
||||
UID(ctx context.Context) (string, error)
|
||||
}
|
||||
|
@ -22,6 +22,8 @@ type providerMetadata interface {
|
||||
Self(ctx context.Context) (metadata.InstanceMetadata, error)
|
||||
// GetLoadBalancerEndpoint returns the endpoint of the load balancer.
|
||||
GetLoadBalancerEndpoint(ctx context.Context) (string, error)
|
||||
// UID returns the UID of the current instance.
|
||||
UID(ctx context.Context) (string, error)
|
||||
}
|
||||
|
||||
// Fetcher checks the metadata service to search for instances that were set up for debugging.
|
||||
@ -46,6 +48,20 @@ func (f *Fetcher) Role(ctx context.Context) (role.Role, error) {
|
||||
return self.Role, nil
|
||||
}
|
||||
|
||||
// UID returns node UID via meta data API.
|
||||
func (f *Fetcher) UID(ctx context.Context) (string, error) {
|
||||
uid, err := f.metaAPI.UID(ctx)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("retrieving UID from cloud provider metadata: %w", err)
|
||||
}
|
||||
return uid, nil
|
||||
}
|
||||
|
||||
// Self returns the current instance via meta data API.
|
||||
func (f *Fetcher) Self(ctx context.Context) (metadata.InstanceMetadata, error) {
|
||||
return f.metaAPI.Self(ctx)
|
||||
}
|
||||
|
||||
// DiscoverDebugdIPs will query the metadata of all instances and return any ips of instances already set up for debugging.
|
||||
func (f *Fetcher) DiscoverDebugdIPs(ctx context.Context) ([]string, error) {
|
||||
self, err := f.metaAPI.Self(ctx)
|
||||
|
@ -172,6 +172,8 @@ type stubMetadata struct {
|
||||
selfErr error
|
||||
getLBEndpointRes string
|
||||
getLBEndpointErr error
|
||||
uid string
|
||||
uidErr error
|
||||
}
|
||||
|
||||
func (m *stubMetadata) List(ctx context.Context) ([]metadata.InstanceMetadata, error) {
|
||||
@ -185,3 +187,7 @@ func (m *stubMetadata) Self(ctx context.Context) (metadata.InstanceMetadata, err
|
||||
func (m *stubMetadata) GetLoadBalancerEndpoint(ctx context.Context) (string, error) {
|
||||
return m.getLBEndpointRes, m.getLBEndpointErr
|
||||
}
|
||||
|
||||
func (m *stubMetadata) UID(context.Context) (string, error) {
|
||||
return m.uid, m.uidErr
|
||||
}
|
||||
|
@ -9,23 +9,33 @@ package fallback
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/edgelesssys/constellation/v2/internal/role"
|
||||
"github.com/edgelesssys/constellation/v2/debugd/internal/debugd/metadata/cloudprovider"
|
||||
"github.com/edgelesssys/constellation/v2/internal/cloud/metadata"
|
||||
)
|
||||
|
||||
// Fetcher implements metadata.Fetcher interface but does not actually fetch cloud provider metadata.
|
||||
type Fetcher struct{}
|
||||
|
||||
// Role for fallback fetcher does not try to fetch role.
|
||||
func (f Fetcher) Role(_ context.Context) (role.Role, error) {
|
||||
return role.Unknown, nil
|
||||
// NewFallbackFetcher returns a cloudprovider.Fetcher with a fake metadata backend.
|
||||
func NewFallbackFetcher() *cloudprovider.Fetcher {
|
||||
return cloudprovider.New(&fallbackMetadata{})
|
||||
}
|
||||
|
||||
// DiscoverDebugdIPs for fallback fetcher does not try to discover debugd IPs.
|
||||
func (f Fetcher) DiscoverDebugdIPs(ctx context.Context) ([]string, error) {
|
||||
type fallbackMetadata struct{}
|
||||
|
||||
// List retrieves all instances belonging to the current constellation.
|
||||
func (fallbackMetadata) List(context.Context) ([]metadata.InstanceMetadata, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// DiscoverLoadbalancerIP for fallback fetcher does not try to discover loadbalancer IP.
|
||||
func (f Fetcher) DiscoverLoadbalancerIP(ctx context.Context) (string, error) {
|
||||
// Self retrieves the current instance.
|
||||
func (fallbackMetadata) Self(context.Context) (metadata.InstanceMetadata, error) {
|
||||
return metadata.InstanceMetadata{}, nil
|
||||
}
|
||||
|
||||
// GetLoadBalancerEndpoint returns the endpoint of the load balancer.
|
||||
func (fallbackMetadata) GetLoadBalancerEndpoint(context.Context) (string, error) {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
// UID returns the UID of the current instance.
|
||||
func (fallbackMetadata) UID(context.Context) (string, error) {
|
||||
return "", nil
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/edgelesssys/constellation/v2/internal/role"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"go.uber.org/goleak"
|
||||
)
|
||||
@ -21,9 +22,20 @@ func TestMain(m *testing.M) {
|
||||
func TestDiscoverDebugdIPs(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
fetcher := Fetcher{}
|
||||
fetcher := NewFallbackFetcher()
|
||||
ips, err := fetcher.DiscoverDebugdIPs(context.Background())
|
||||
|
||||
assert.NoError(err)
|
||||
assert.Empty(ips)
|
||||
|
||||
rol, err := fetcher.Role(context.Background())
|
||||
assert.NoError(err)
|
||||
assert.Equal(rol, role.Unknown)
|
||||
|
||||
uid, err := fetcher.UID(context.Background())
|
||||
assert.NoError(err)
|
||||
assert.Empty(uid)
|
||||
|
||||
self, err := fetcher.Self(context.Background())
|
||||
assert.NoError(err)
|
||||
assert.Empty(self)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user