compute/cmd/mgr/main.go

116 lines
2.5 KiB
Go

package main
import (
"crypto/tls"
"crypto/x509"
"fmt"
"log"
"net"
"os"
"time"
raft_hashicorp "github.com/hashicorp/raft"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/reflection"
"deevirt.fr/compute/cmd/mgr/domain"
"deevirt.fr/compute/cmd/mgr/node"
"deevirt.fr/compute/pkg/config"
pb "deevirt.fr/compute/pkg/proto"
"deevirt.fr/compute/pkg/raft"
)
func createGRPCServer(conf *config.Config) *grpc.Server {
if conf.Manager.TlsKey != "" {
cert, err := tls.LoadX509KeyPair(conf.Manager.TlsCert, conf.Manager.TlsKey)
if err != nil {
log.Fatalf("Erreur chargement du certificat: %v", err)
}
// Charger la CA (facultatif, pour la vérification des clients)
caCert, err := os.ReadFile(conf.Manager.TlsCert)
if err != nil {
log.Fatalf("Erreur chargement CA: %v", err)
}
certPool := x509.NewCertPool()
certPool.AppendCertsFromPEM(caCert)
// Créer les credentials TLS
creds := credentials.NewTLS(&tls.Config{
Certificates: []tls.Certificate{cert},
ClientCAs: certPool,
ClientAuth: tls.RequireAndVerifyClientCert, // Authentification mutuelle (mTLS),
})
return grpc.NewServer(grpc.Creds(creds))
}
return grpc.NewServer()
}
func main() {
logger, _ := zap.NewProduction()
// Récupération de la configuration deevirt
conf, err := config.New()
if err != nil {
log.Fatalf("failed load configuration: %v", err)
}
sock, err := net.Listen("tcp", fmt.Sprintf(":%d", 4480))
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
r := raft.New(conf)
s, tm, err := r.Open()
if err != nil {
log.Fatalf("failed to start raft: %v", err)
}
// Observer pour surveiller les changements d'état
stateCh := make(chan raft_hashicorp.Observation, 1) // Canal de type raft.Observation
s.Raft.RegisterObserver(raft_hashicorp.NewObserver(stateCh, true, nil))
nodes := &RaftNode{
NodeID: conf.NodeID,
Conf: conf,
Store: s,
StateCh: stateCh,
isReady: false,
}
go nodes.WatchStateChanges()
for {
if nodes.isReady {
break
}
time.Sleep(100 * time.Millisecond)
}
println("On démarre le serveur GRPC")
server := createGRPCServer(conf)
pb.RegisterNodeServer(server, &node.Node{
Config: conf,
Store: r,
})
pb.RegisterDomainServer(server, &domain.Domain{
Config: conf,
Store: r,
Logger: logger,
})
tm.Register(server)
//leaderhealth.Setup(r, s, []string{"Example"})
raft.Register(server, r.Raft)
reflection.Register(server)
if err := server.Serve(sock); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}