-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* enabling external informer * preparing integration tests * Fixed tests * almost-working tests with external cache * specifying cache port in integration tests * updating beyla-k8s-cache library
- Loading branch information
Showing
24 changed files
with
1,258 additions
and
203 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package kube | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log/slog" | ||
"time" | ||
|
||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/credentials/insecure" | ||
|
||
"github.com/grafana/beyla-k8s-cache/pkg/informer" | ||
"github.com/grafana/beyla-k8s-cache/pkg/meta" | ||
) | ||
|
||
func cslog() *slog.Logger { | ||
return slog.With("component", "kube.CacheSvcClient") | ||
} | ||
|
||
type cacheSvcClient struct { | ||
meta.BaseNotifier | ||
address string | ||
log *slog.Logger | ||
|
||
waitForSubscription chan struct{} | ||
} | ||
|
||
func (sc *cacheSvcClient) Start(ctx context.Context) { | ||
sc.log = cslog() | ||
sc.waitForSubscription = make(chan struct{}) | ||
go func() { | ||
select { | ||
case <-ctx.Done(): | ||
sc.log.Debug("context done, stopping client") | ||
return | ||
case <-sc.waitForSubscription: | ||
sc.log.Debug("subscriptor attached, start connection to K8s cache service") | ||
} | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
sc.log.Debug("context done, stopping client") | ||
return | ||
default: | ||
// TODO: reconnection should include a timestamp | ||
// with the last received event, to avoid unnecessarily | ||
// receiving the whole metadata snapshot on each reconnection | ||
err := sc.connect(ctx) | ||
sc.log.Info("K8s cache service connection lost. Reconnecting...", "error", err) | ||
// TODO: exponential backoff | ||
time.Sleep(5 * time.Second) | ||
} | ||
} | ||
}() | ||
} | ||
|
||
func (sc *cacheSvcClient) connect(ctx context.Context) error { | ||
// Set up a connection to the server. | ||
conn, err := grpc.NewClient(sc.address, | ||
// TODO: allow configuring the transport credentials | ||
grpc.WithTransportCredentials(insecure.NewCredentials())) | ||
if err != nil { | ||
return fmt.Errorf("did not connect: %w", err) | ||
} | ||
defer conn.Close() | ||
|
||
client := informer.NewEventStreamServiceClient(conn) | ||
|
||
// Subscribe to the event stream. | ||
stream, err := client.Subscribe(ctx, &informer.SubscribeMessage{}) | ||
if err != nil { | ||
return fmt.Errorf("could not subscribe: %w", err) | ||
} | ||
|
||
// Receive and print messages. | ||
for { | ||
event, err := stream.Recv() | ||
if err != nil { | ||
return fmt.Errorf("error receiving message: %w", err) | ||
} | ||
sc.BaseNotifier.Notify(event) | ||
} | ||
} | ||
|
||
func (sc *cacheSvcClient) Subscribe(observer meta.Observer) { | ||
sc.BaseNotifier.Subscribe(observer) | ||
close(sc.waitForSubscription) | ||
} |
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
Oops, something went wrong.