-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement events manager container
- set up `talos.events.sink` kernel args. - build and run additional container to receive talos events. - log all events in the adapter. Signed-off-by: Artem Chernyshev <[email protected]>
- Loading branch information
Showing
7 changed files
with
248 additions
and
0 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
93 changes: 93 additions & 0 deletions
93
app/sidero-controller-manager/cmd/events-manager/adapter.go
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,93 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"go.uber.org/zap" | ||
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"github.com/talos-systems/siderolink/pkg/events" | ||
|
||
"github.com/talos-systems/talos/pkg/machinery/api/machine" | ||
) | ||
|
||
// Adapter implents gRPC API. | ||
type Adapter struct { | ||
Sink *events.Sink | ||
|
||
logger *zap.Logger | ||
metalClient runtimeclient.Client | ||
} | ||
|
||
// NewAdapter initializes new server. | ||
func NewAdapter(metalClient runtimeclient.Client, logger *zap.Logger) *Adapter { | ||
return &Adapter{ | ||
logger: logger, | ||
metalClient: metalClient, | ||
} | ||
} | ||
|
||
// HandleEvent implements events.Adapter. | ||
func (a *Adapter) HandleEvent(ctx context.Context, event events.Event) error { | ||
logger := a.logger.With( | ||
zap.String("node", event.Node), | ||
zap.String("id", event.ID), | ||
zap.String("type", event.TypeURL), | ||
) | ||
|
||
fields := []zap.Field{} | ||
message := "incoming event" | ||
|
||
var err error | ||
|
||
switch event := event.Payload.(type) { | ||
case *machine.AddressEvent: | ||
fields = append(fields, zap.String("hostname", event.GetHostname()), zap.String("addresses", strings.Join(event.GetAddresses(), ","))) | ||
case *machine.ConfigValidationErrorEvent: | ||
fields = append(fields, zap.Error(fmt.Errorf(event.GetError()))) | ||
case *machine.ConfigLoadErrorEvent: | ||
fields = append(fields, zap.Error(fmt.Errorf(event.GetError()))) | ||
case *machine.PhaseEvent: | ||
fields = append(fields, zap.String("phase", event.GetPhase()), zap.String("action", event.GetAction().String())) | ||
case *machine.TaskEvent: | ||
fields = append(fields, zap.String("task", event.GetTask()), zap.String("action", event.GetAction().String())) | ||
case *machine.ServiceStateEvent: | ||
message = "service " + event.GetMessage() | ||
fields = append(fields, zap.String("service", event.GetService()), zap.String("action", event.GetAction().String())) | ||
case *machine.SequenceEvent: | ||
fields = append(fields, zap.String("sequence", event.GetSequence()), zap.String("action", event.GetAction().String())) | ||
|
||
if event.GetError() != nil { | ||
err = fmt.Errorf(event.GetError().GetMessage()) | ||
} | ||
|
||
if event.GetSequence() == "install" && | ||
event.GetAction() == machine.SequenceEvent_STOP { | ||
if event.GetError() != nil { | ||
message = "failed to install Talos" | ||
|
||
break | ||
} | ||
|
||
message = "successfully installed Talos" | ||
} | ||
} | ||
|
||
if err != nil { | ||
fields = append(fields, zap.Error(err)) | ||
|
||
logger.Error(message, fields...) | ||
|
||
return nil | ||
} | ||
|
||
logger.Info(message, fields...) | ||
|
||
return nil | ||
} |
32 changes: 32 additions & 0 deletions
32
app/sidero-controller-manager/cmd/events-manager/kubernetes.go
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,32 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package main | ||
|
||
import ( | ||
"k8s.io/apimachinery/pkg/runtime" | ||
clientgoscheme "k8s.io/client-go/kubernetes/scheme" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
sidero "github.com/talos-systems/sidero/app/caps-controller-manager/api/v1alpha3" | ||
) | ||
|
||
func getMetalClient() (runtimeclient.Client, error) { | ||
kubeconfig := ctrl.GetConfigOrDie() | ||
|
||
scheme := runtime.NewScheme() | ||
|
||
if err := clientgoscheme.AddToScheme(scheme); err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := sidero.AddToScheme(scheme); err != nil { | ||
return nil, err | ||
} | ||
|
||
client, err := runtimeclient.New(kubeconfig, runtimeclient.Options{Scheme: scheme}) | ||
|
||
return client, err | ||
} |
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,90 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"flag" | ||
"fmt" | ||
"net" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
|
||
"go.uber.org/zap" | ||
"golang.org/x/sync/errgroup" | ||
"google.golang.org/grpc" | ||
|
||
"github.com/talos-systems/sidero/app/sidero-controller-manager/internal/siderolink" | ||
"github.com/talos-systems/siderolink/api/events" | ||
sink "github.com/talos-systems/siderolink/pkg/events" | ||
) | ||
|
||
func main() { | ||
flag.Parse() | ||
|
||
if err := run(); err != nil { | ||
fmt.Fprintf(os.Stderr, "error: %s", err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func run() error { | ||
logger, err := zap.NewProduction() | ||
if err != nil { | ||
return fmt.Errorf("failed to initialize logger: %w", err) | ||
} | ||
|
||
zap.ReplaceGlobals(logger) | ||
zap.RedirectStdLog(logger) | ||
|
||
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) | ||
defer cancel() | ||
|
||
eg, ctx := errgroup.WithContext(ctx) | ||
|
||
address := fmt.Sprintf(":%d", siderolink.EventsSinkPort) | ||
|
||
lis, err := net.Listen("tcp", address) | ||
if err != nil { | ||
return fmt.Errorf("error listening for gRPC API: %w", err) | ||
} | ||
|
||
s := grpc.NewServer() | ||
|
||
client, err := getMetalClient() | ||
if err != nil { | ||
return fmt.Errorf("error getting metal client: %w", err) | ||
} | ||
|
||
srv := sink.NewSink( | ||
NewAdapter(client, | ||
logger.With(zap.String("component", "sink")), | ||
), | ||
) | ||
|
||
events.RegisterEventSinkServiceServer(s, srv) | ||
|
||
eg.Go(func() error { | ||
logger.Info("started gRPC event sink", zap.String("address", address)) | ||
|
||
return s.Serve(lis) | ||
}) | ||
|
||
eg.Go(func() error { | ||
<-ctx.Done() | ||
|
||
s.Stop() | ||
|
||
return nil | ||
}) | ||
|
||
if err := eg.Wait(); err != nil && !errors.Is(err, grpc.ErrServerStopped) && errors.Is(err, context.Canceled) { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
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