Skip to content

Commit

Permalink
Remote logging support (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
majst01 authored Feb 5, 2024
1 parent 21b590f commit dd779c5
Show file tree
Hide file tree
Showing 9 changed files with 2,032 additions and 81 deletions.
31 changes: 2 additions & 29 deletions cmd/metal-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@ import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/json"
"errors"
"fmt"
"io"
"log/slog"
"net/http"
"time"

v1 "github.com/metal-stack/metal-api/pkg/api/v1"
metalgo "github.com/metal-stack/metal-go"
pixiecore "github.com/metal-stack/pixie/api"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/keepalive"
Expand All @@ -28,30 +23,8 @@ type MetalAPIClient struct {

// NewMetalAPIClient fetches the address,hmac and certificates from pixie needed to communicate with metal-api,
// and returns a new client that can be used to invoke all provided grpc and rest endpoints.
func NewMetalAPIClient(log *slog.Logger, pixieURL string) (*MetalAPIClient, error) {
certClient := http.Client{
Timeout: 5 * time.Second,
}
ctx, httpcancel := context.WithTimeout(context.Background(), 5*time.Second)
defer httpcancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, pixieURL, nil)
if err != nil {
return nil, err
}
resp, err := certClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

js, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var metalConfig pixiecore.MetalConfig
if err := json.Unmarshal(js, &metalConfig); err != nil {
return nil, fmt.Errorf("unable to unmarshal grpcConfig:%w", err)
}
func NewMetalAPIClient(log *slog.Logger, spec *Specification) (*MetalAPIClient, error) {
metalConfig := spec.MetalConfig

clientCert, err := tls.X509KeyPair([]byte(metalConfig.Cert), []byte(metalConfig.Key))
if err != nil {
Expand Down
39 changes: 39 additions & 0 deletions cmd/pixie-client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package cmd

import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"time"

pixiecore "github.com/metal-stack/pixie/api"
)

func fetchMetalConfig(pixieURL string) (*pixiecore.MetalConfig, error) {
certClient := http.Client{
Timeout: 5 * time.Second,
}
ctx, httpcancel := context.WithTimeout(context.Background(), 5*time.Second)
defer httpcancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, pixieURL, nil)
if err != nil {
return nil, err
}
resp, err := certClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

js, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var metalConfig pixiecore.MetalConfig
if err := json.Unmarshal(js, &metalConfig); err != nil {
return nil, fmt.Errorf("unable to unmarshal grpcConfig:%w", err)
}
return &metalConfig, nil
}
78 changes: 78 additions & 0 deletions cmd/remote-logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package cmd

import (
"context"
"encoding/json"
"fmt"
"log/slog"

"github.com/grafana/loki-client-go/loki"
"github.com/metal-stack/pixie/api"
promconfig "github.com/prometheus/common/config"
slogloki "github.com/samber/slog-loki/v3"
slogmulti "github.com/samber/slog-multi"
)

func AddRemoteHandler(spec *Specification, handler slog.Handler) (slog.Handler, error) {
metalConfig := spec.MetalConfig
if metalConfig.Logging == nil || metalConfig.Logging.Endpoint == "" {
return handler, nil
}
if metalConfig.Logging.Type != api.LogTypeLoki {
slog.New(handler).Error("unsupported remote logging type, ignoring", "type", metalConfig.Logging.Type)
return handler, nil
}
httpClient := promconfig.DefaultHTTPClientConfig
if metalConfig.Logging.BasicAuth != nil {
httpClient.BasicAuth = &promconfig.BasicAuth{
Username: metalConfig.Logging.BasicAuth.User,
Password: promconfig.Secret(metalConfig.Logging.BasicAuth.Password),
}
}
if metalConfig.Logging.CertificateAuth != nil {
httpClient.TLSConfig = promconfig.TLSConfig{
Cert: metalConfig.Logging.CertificateAuth.Cert,
Key: promconfig.Secret(metalConfig.Logging.CertificateAuth.Key),
InsecureSkipVerify: metalConfig.Logging.CertificateAuth.InsecureSkipVerify,
}
}

config, err := loki.NewDefaultConfig(metalConfig.Logging.Endpoint)
if err != nil {
return nil, fmt.Errorf("unable to create loki default config %w", err)
}
// config.EncodeJson = true
config.Client = httpClient
client, err := loki.New(config)
if err != nil {
return nil, fmt.Errorf("unable to create loki client %w", err)
}

lokiHandler := slogloki.Option{
Level: slog.LevelDebug,
Client: client}.NewLokiHandler().WithAttrs(
[]slog.Attr{
{Key: "component", Value: slog.StringValue("metal-hammer")},
{Key: "machineID", Value: slog.StringValue(spec.MachineUUID)},
},
)
mdw := slogmulti.NewHandleInlineMiddleware(jsonFormattingMiddleware)
combinedHandler := slogmulti.Fanout(slogmulti.Pipe(mdw).Handler(lokiHandler), handler)
return combinedHandler, nil
}

func jsonFormattingMiddleware(ctx context.Context, record slog.Record, next func(context.Context, slog.Record) error) error {
attrs := map[string]string{"msg": record.Message, "level": record.Level.String(), "time": record.Time.String()}

record.Attrs(func(attr slog.Attr) bool {
attrs[attr.Key] = attr.Value.String()
return true
})

r, err := json.Marshal(attrs)
if err != nil {
return fmt.Errorf("unable to marshal log attributes %w", err)
}
record = slog.NewRecord(record.Time, record.Level, string(r), record.PC)
return next(ctx, record)
}
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type Hammer struct {
// Run orchestrates the whole register/wipe/format/burn and reboot process
func Run(log *slog.Logger, spec *Specification, hal hal.InBand) (*event.EventEmitter, error) {
log.Info("metal-hammer run", "firmware", kernel.Firmware(), "bios", hal.Board().BIOS.String())
metalAPIClient, err := NewMetalAPIClient(log, spec.PixieAPIUrl)
metalAPIClient, err := NewMetalAPIClient(log, spec)
if err != nil {
log.Error("failed to fetch GRPC certificates", "error", err)
return nil, err
Expand Down
11 changes: 11 additions & 0 deletions cmd/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"

"github.com/metal-stack/metal-hammer/pkg/kernel"
pixiecore "github.com/metal-stack/pixie/api"
)

// Specification defines configuration items of the application
Expand All @@ -26,6 +27,8 @@ type Specification struct {
MachineUUID string
// IP of this instance
IP string
// MetalConfig is fetched from pixiecore to get the certs for the metal-api and logging config
MetalConfig *pixiecore.MetalConfig

log *slog.Logger
}
Expand All @@ -50,6 +53,14 @@ func NewSpec(log *slog.Logger) *Specification {
spec.PixieAPIUrl = url
}

metalConfig, err := fetchMetalConfig(spec.PixieAPIUrl)
if err != nil {
log.Error("unable to fetch configuration from pixiecore", "error", err)
os.Exit(1)
}

spec.MetalConfig = metalConfig

if bgp, ok := envmap["BGP"]; ok {
enabled, err := strconv.ParseBool(bgp)
if err == nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (c *MetalAPIClient) WaitForAllocation(e *event.EventEmitter, machineID stri
break
}

c.log.Info("wait for allocation...", "machineID", machineID)
c.log.Info("wait for allocation...")
}
}
}
55 changes: 40 additions & 15 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,28 @@ go 1.21

require (
github.com/beevik/ntp v1.3.1
github.com/cheggaaa/pb/v3 v3.1.4
github.com/cheggaaa/pb/v3 v3.1.5
github.com/google/gopacket v1.1.19
github.com/google/uuid v1.5.0
github.com/google/uuid v1.6.0
github.com/grafana/loki-client-go v0.0.0-20230116142646-e7494d0ef70c
github.com/jaypipes/ghw v0.12.0
github.com/metal-stack/go-hal v0.5.0
github.com/metal-stack/go-lldpd v0.4.6
github.com/metal-stack/metal-api v0.26.2
github.com/metal-stack/metal-go v0.26.2
github.com/metal-stack/pixie v0.3.0
github.com/metal-stack/metal-api v0.26.3
github.com/metal-stack/metal-go v0.26.3
github.com/metal-stack/pixie v0.3.1-0.20240201124704-d6767c4e0abd
github.com/metal-stack/v v1.0.3
// archiver must stay in version v2.1.0, see replace below
github.com/mholt/archiver v3.1.1+incompatible
github.com/pierrec/lz4/v4 v4.1.21
github.com/prometheus/common v0.46.0
github.com/samber/slog-loki/v3 v3.2.0
github.com/samber/slog-multi v1.0.2
github.com/u-root/u-root v0.12.0
github.com/vishvananda/netlink v1.2.1-beta.2
golang.org/x/sync v0.6.0
golang.org/x/sys v0.16.0
google.golang.org/grpc v1.60.1
google.golang.org/grpc v1.61.0
google.golang.org/protobuf v1.32.0
gopkg.in/yaml.v3 v3.0.1
)
Expand All @@ -34,15 +38,21 @@ require (
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be // indirect
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
github.com/avast/retry-go/v4 v4.5.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/coreos/go-oidc/v3 v3.9.0 // indirect
github.com/creack/pty v1.1.21 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/dennwc/varint v1.0.0 // indirect
github.com/dsnet/compress v0.0.1 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/frankban/quicktest v1.14.6 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/gliderlabs/ssh v0.3.6 // indirect
github.com/go-jose/go-jose/v3 v3.0.1 // indirect
github.com/go-kit/kit v0.13.0 // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
Expand All @@ -51,18 +61,22 @@ require (
github.com/go-openapi/jsonpointer v0.20.2 // indirect
github.com/go-openapi/jsonreference v0.20.4 // indirect
github.com/go-openapi/loads v0.21.5 // indirect
github.com/go-openapi/runtime v0.26.2 // indirect
github.com/go-openapi/runtime v0.27.1 // indirect
github.com/go-openapi/spec v0.20.14 // indirect
github.com/go-openapi/strfmt v0.22.0 // indirect
github.com/go-openapi/swag v0.22.7 // indirect
github.com/go-openapi/validate v0.22.6 // indirect
github.com/go-openapi/swag v0.22.9 // indirect
github.com/go-openapi/validate v0.23.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/gorilla/mux v1.8.1 // indirect
github.com/grafana/regexp v0.0.0-20221122212121-6b5c0a4cb7fd // indirect
github.com/jaypipes/pcidb v1.0.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/lestrrat-go/blackmagic v1.0.2 // indirect
github.com/lestrrat-go/httpcc v1.0.1 // indirect
github.com/lestrrat-go/httprc v1.0.4 // indirect
Expand All @@ -75,32 +89,43 @@ require (
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mdlayher/ethernet v0.0.0-20220221185849-529eae5b6118 // indirect
github.com/mdlayher/lldp v0.0.0-20150915211757-afd9f83164c5 // indirect
github.com/metal-stack/metal-lib v0.14.2 // indirect
github.com/metal-stack/metal-lib v0.14.4 // indirect
github.com/metal-stack/security v0.7.1 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect
github.com/nwaples/rardecode v1.1.3 // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pierrec/lz4 v2.6.1+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/prometheus/client_golang v1.18.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/prometheus/prometheus v0.49.1 // indirect
github.com/rivo/uniseg v0.4.6 // indirect
github.com/samber/lo v1.39.0 // indirect
github.com/samber/slog-common v0.15.0 // indirect
github.com/segmentio/asm v1.2.0 // indirect
github.com/sethvargo/go-password v0.2.0 // indirect
github.com/stmcginnis/gofish v0.15.0 // indirect
github.com/ulikunitz/xz v0.5.11 // indirect
github.com/vishvananda/netns v0.0.4 // indirect
github.com/vmware/goipmi v0.0.0-20181114221114-2333cd82d702 // indirect
go.mongodb.org/mongo-driver v1.13.1 // indirect
go.opentelemetry.io/otel v1.21.0 // indirect
go.opentelemetry.io/otel/metric v1.21.0 // indirect
go.opentelemetry.io/otel/trace v1.21.0 // indirect
go.opentelemetry.io/otel v1.22.0 // indirect
go.opentelemetry.io/otel/metric v1.22.0 // indirect
go.opentelemetry.io/otel/trace v1.22.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/oauth2 v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240108191215-35c7eff3a6b1 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240125205218-1f4bbc51befe // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
howett.net/plist v1.0.1 // indirect
)
Loading

0 comments on commit dd779c5

Please sign in to comment.