Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15,137 changes: 7,844 additions & 7,293 deletions doc/index.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion generate/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ go-mocks:
--tmpfs /.cache:uid=$$(id -u),gid=$$(id -g) \
-w /work \
-v ${PWD}:/work \
vektra/mockery:v3.6.3
vektra/mockery:v3.6.4
3 changes: 3 additions & 0 deletions generate/go_client.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
package client

import (
"context"

"connectrpc.com/connect"
compress "github.com/klauspost/connect-compress/v2"

Expand All @@ -15,6 +17,7 @@ type (
{{ range $name, $api := . -}}
{{ $name | title }}() {{ $name | title }}
{{ end }}
Ping(context.Context, *PingConfig)
}
client struct {
config *DialConfig
Expand Down
11 changes: 6 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
module github.com/metal-stack/api

go 1.25
go 1.26

require (
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.11-20260209202127-80ab13bee0bf.1
buf.build/go/protovalidate v1.1.0
buf.build/go/protovalidate v1.1.3
connectrpc.com/connect v1.19.1
github.com/bufbuild/protocompile v0.14.1
github.com/go-task/slim-sprig/v3 v3.0.0
github.com/golang-jwt/jwt/v5 v5.3.1
github.com/google/go-cmp v0.7.0
github.com/google/uuid v1.6.0
github.com/klauspost/connect-compress/v2 v2.1.1
github.com/stretchr/testify v1.11.1
google.golang.org/protobuf v1.36.11
Expand All @@ -25,10 +26,10 @@ require (
github.com/minio/minlz v1.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.3 // indirect
golang.org/x/exp v0.0.0-20260209203927-2842357ff358 // indirect
golang.org/x/exp v0.0.0-20260218203240-3dfff04db8fa // indirect
golang.org/x/text v0.34.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20260209200024-4cfbd4190f57 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20260209200024-4cfbd4190f57 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20260217215200-42d3e9bedb6d // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20260217215200-42d3e9bedb6d // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
18 changes: 10 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.11-20260209202127-80ab13bee0bf.1 h1:PMmTMyvHScV9Mn8wc6ASge9uRcHy0jtqPd+fM35LmsQ=
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.11-20260209202127-80ab13bee0bf.1/go.mod h1:tvtbpgaVXZX4g6Pn+AnzFycuRK3MOz5HJfEGeEllXYM=
buf.build/go/protovalidate v1.1.0 h1:pQqEQRpOo4SqS60qkvmhLTTQU9JwzEvdyiqAtXa5SeY=
buf.build/go/protovalidate v1.1.0/go.mod h1:bGZcPiAQDC3ErCHK3t74jSoJDFOs2JH3d7LWuTEIdss=
buf.build/go/protovalidate v1.1.3 h1:m2GVEgQWd7rk+vIoAZ+f0ygGjvQTuqPQapBBdcpWVPE=
buf.build/go/protovalidate v1.1.3/go.mod h1:9XIuohWz+kj+9JVn3WQneHA5LZP50mjvneZMnbLkiIE=
cel.dev/expr v0.25.1 h1:1KrZg61W6TWSxuNZ37Xy49ps13NUovb66QLprthtwi4=
cel.dev/expr v0.25.1/go.mod h1:hrXvqGP6G6gyx8UAHSHJ5RGk//1Oj5nXQ2NI02Nrsg4=
connectrpc.com/connect v1.19.1 h1:R5M57z05+90EfEvCY1b7hBxDVOUl45PrtXtAV2fOC14=
Expand All @@ -23,6 +23,8 @@ github.com/google/cel-go v0.27.0 h1:e7ih85+4qVrBuqQWTW4FKSqZYokVuc3HnhH5keboFTo=
github.com/google/cel-go v0.27.0/go.mod h1:tTJ11FWqnhw5KKpnWpvW9CJC3Y9GK4EIS0WXnBbebzw=
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/klauspost/compress v1.18.4 h1:RPhnKRAQ4Fh8zU2FY/6ZFDwTVTxgJ/EMydqSTzE9a2c=
github.com/klauspost/compress v1.18.4/go.mod h1:R0h/fSBs8DE4ENlcrlib3PsXS61voFxhIs2DeRhCvJ4=
github.com/klauspost/connect-compress/v2 v2.1.1 h1:ycZNp4rWOZBodVE2Ls5AzK4aHkyK+GteEfzRZgKNs+c=
Expand All @@ -49,14 +51,14 @@ github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc=
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
golang.org/x/exp v0.0.0-20260209203927-2842357ff358 h1:kpfSV7uLwKJbFSEgNhWzGSL47NDSF/5pYYQw1V0ub6c=
golang.org/x/exp v0.0.0-20260209203927-2842357ff358/go.mod h1:R3t0oliuryB5eenPWl3rrQxwnNM3WTwnsRZZiXLAAW8=
golang.org/x/exp v0.0.0-20260218203240-3dfff04db8fa h1:Zt3DZoOFFYkKhDT3v7Lm9FDMEV06GpzjG2jrqW+QTE0=
golang.org/x/exp v0.0.0-20260218203240-3dfff04db8fa/go.mod h1:K79w1Vqn7PoiZn+TkNpx3BUWUQksGO3JcVX6qIjytmA=
golang.org/x/text v0.34.0 h1:oL/Qq0Kdaqxa1KbNeMKwQq0reLCCaFtqu2eNuSeNHbk=
golang.org/x/text v0.34.0/go.mod h1:homfLqTYRFyVYemLBFl5GgL/DWEiH5wcsQ5gSh1yziA=
google.golang.org/genproto/googleapis/api v0.0.0-20260209200024-4cfbd4190f57 h1:JLQynH/LBHfCTSbDWl+py8C+Rg/k1OVH3xfcaiANuF0=
google.golang.org/genproto/googleapis/api v0.0.0-20260209200024-4cfbd4190f57/go.mod h1:kSJwQxqmFXeo79zOmbrALdflXQeAYcUbgS7PbpMknCY=
google.golang.org/genproto/googleapis/rpc v0.0.0-20260209200024-4cfbd4190f57 h1:mWPCjDEyshlQYzBpMNHaEof6UX1PmHcaUODUywQ0uac=
google.golang.org/genproto/googleapis/rpc v0.0.0-20260209200024-4cfbd4190f57/go.mod h1:j9x/tPzZkyxcgEFkiKEEGxfvyumM01BEtsW8xzOahRQ=
google.golang.org/genproto/googleapis/api v0.0.0-20260217215200-42d3e9bedb6d h1:EocjzKLywydp5uZ5tJ79iP6Q0UjDnyiHkGRWxuPBP8s=
google.golang.org/genproto/googleapis/api v0.0.0-20260217215200-42d3e9bedb6d/go.mod h1:48U2I+QQUYhsFrg2SY6r+nJzeOtjey7j//WBESw+qyQ=
google.golang.org/genproto/googleapis/rpc v0.0.0-20260217215200-42d3e9bedb6d h1:t/LOSXPJ9R0B6fnZNyALBRfZBH0Uy0gT+uR+SJ6syqQ=
google.golang.org/genproto/googleapis/rpc v0.0.0-20260217215200-42d3e9bedb6d/go.mod h1:4Hqkh8ycfw05ld/3BWL7rJOSfebL2Q+DVDeRgYgxUU8=
google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE=
google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
34 changes: 30 additions & 4 deletions go/client/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

90 changes: 90 additions & 0 deletions go/client/ping.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package client

import (
"context"
"os"
"time"

"github.com/google/uuid"
v2 "github.com/metal-stack/api/go/metalstack/api/v2"
infra "github.com/metal-stack/api/go/metalstack/infra/v2"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/timestamppb"
)

var (
minInterval = 5 * time.Second
maxInterval = time.Hour
defaultInterval = 5 * time.Minute
)

// PingConfig is used to configure ping
type PingConfig struct {
// ComponentType should be set to the type of the microservice
ComponentType v2.ComponentType
// Identifier helps to identify multiple instances of a microservice
// Usually the hostname or the podname could be used for that
// If omitted, hostname is used
Identifier *string
// StartedAt contains the starttime when this go process was started
StartedAt time.Time
// Interval at which the ping should happen
// If not specified, or shorter than 1min, or longer than 1h, it defaults to 5min
Interval time.Duration
// Version contains all version details about this microservice
// You should use https://github.com/metal-stack/v to get all information.
Version v2.Version
}

// Ping should be called from every microservice which talks to the metal-apiserver
// It will stay and ping at config.Interval in the background until context is canceled
func (c *client) Ping(ctx context.Context, config *PingConfig) {
if config == nil {
return
}

if config.Interval < minInterval || config.Interval > maxInterval {
config.Interval = defaultInterval
}

var identifier string
if config.Identifier == nil {
hostname, err := os.Hostname()
if err != nil {
suffix := uuid.NewString()
identifier = "unknown-" + suffix
} else {
identifier = hostname
}
} else {
identifier = *config.Identifier
}

req := &infra.ComponentServicePingRequest{
Type: config.ComponentType,
Identifier: identifier,
StartedAt: timestamppb.New(config.StartedAt),
Interval: durationpb.New(config.Interval),
Version: &config.Version,
}

c.config.Log.Debug("ping", "config", config)

ticker := time.NewTicker(config.Interval)
go func() {
for {
select {
case <-ctx.Done():
ticker.Stop()
return
case <-ticker.C:
pingCtx, cancel := context.WithTimeout(ctx, 2*time.Second)
defer cancel()
_, err := c.Infrav2().Component().Ping(pingCtx, req)
if err != nil {
c.config.Log.Error("ping", "error", err)
}
}
}
}()
}
82 changes: 82 additions & 0 deletions go/client/ping_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package client_test

import (
"context"
"log/slog"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/metal-stack/api/go/client"
apiv2 "github.com/metal-stack/api/go/metalstack/api/v2"
infra "github.com/metal-stack/api/go/metalstack/infra/v2"
"github.com/metal-stack/api/go/metalstack/infra/v2/infrav2connect"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/testing/protocmp"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/timestamppb"
)

func Test_Ping(t *testing.T) {
var (
mux = http.NewServeMux()
log = slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}))
cs = &mockComponentService{log: log}
)

mux.Handle(infrav2connect.NewComponentServiceHandler(cs))
server := httptest.NewTLSServer(mux)
server.EnableHTTP2 = true
defer server.Close()

ctx := t.Context()

tokenString, err := generateToken(2 * time.Second)
require.NoError(t, err)

c, err := client.New(&client.DialConfig{
BaseURL: server.URL,
Token: tokenString,
Transport: server.Client().Transport,
Log: log,
})
require.NoError(t, err)

start := time.Now()
config := &client.PingConfig{
ComponentType: apiv2.ComponentType_COMPONENT_TYPE_METAL_CORE,
Identifier: new("server01"),
StartedAt: start,
Interval: 5 * time.Second,
}

want := []*infra.ComponentServicePingRequest{
{Type: apiv2.ComponentType_COMPONENT_TYPE_METAL_CORE, Identifier: "server01", StartedAt: timestamppb.New(start), Interval: durationpb.New(5 * time.Second), Version: &apiv2.Version{}},
{Type: apiv2.ComponentType_COMPONENT_TYPE_METAL_CORE, Identifier: "server01", StartedAt: timestamppb.New(start), Interval: durationpb.New(5 * time.Second), Version: &apiv2.Version{}},
}

c.Ping(ctx, config)
time.Sleep(3 * config.Interval)
if diff := cmp.Diff(
cs.pings, want,
protocmp.Transform(),
cmpopts.IgnoreUnexported(),
); diff != "" {
t.Errorf("%v, want %v diff: %s", cs.pings, want, diff)
}
}

type mockComponentService struct {
log *slog.Logger
pings []*infra.ComponentServicePingRequest
}

func (m *mockComponentService) Ping(_ context.Context, req *infra.ComponentServicePingRequest) (*infra.ComponentServicePingResponse, error) {
m.log.Debug("ping", "req", req)
m.pings = append(m.pings, req)
return nil, nil
}
Loading
Loading