109 lines
2.5 KiB
Go
109 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/cmd/mgr/worker"
|
|
|
|
pb "deevirt.fr/compute/pkg/api/proto"
|
|
"deevirt.fr/compute/pkg/config"
|
|
"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 := &worker.RaftNode{
|
|
Bootstrap: false,
|
|
Store: s,
|
|
NodeID: conf.NodeID,
|
|
StateCh: stateCh,
|
|
}
|
|
|
|
go nodes.WatchStateChanges()
|
|
|
|
// On temporise 5 secondes, le temps de laisser la reprise des logs
|
|
time.Sleep(5 * time.Second)
|
|
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)
|
|
}
|
|
}
|