albatros/pkg/cluster.go
2023-04-17 11:25:26 +02:00

46 lines
1.1 KiB
Go

package pkg
import (
"log"
consul "github.com/hashicorp/consul/api"
nomad "github.com/hashicorp/nomad/api"
)
type Cluster struct {
Nomad *nomad.Client
Consul *consul.Client
}
func NewCluster(conf* Config) Cluster {
cluster := Cluster{}
// Init Nomad
nomadConfig := nomad.DefaultConfig()
nomadConfig.Namespace = "ci"
nomadConfig.Address = conf.NomadAddr
nomadConfig.TLSConfig.CACert = conf.NomadCACert
nomadConfig.TLSConfig.ClientCert = conf.NomadClientCert
nomadConfig.TLSConfig.ClientKey = conf.NomadClientKey
ncli, err := nomad.NewClient(nomadConfig)
if err != nil {
log.Fatal("Unable to connect to Nomad, check your config and setup")
}
cluster.Nomad = ncli
// Init Consul
consulConfig := consul.DefaultConfig()
consulConfig.Address = conf.ConsulAddr
consulConfig.TLSConfig.CAFile = conf.ConsulCACert
consulConfig.TLSConfig.CertFile = conf.ConsulClientCert
consulConfig.TLSConfig.KeyFile = conf.ConsulClientKey
ccli, err := consul.NewClient(consulConfig)
if err != nil {
log.Fatal("Unable to connect to Consul, check your config and setup")
}
cluster.Consul = ccli
return cluster
}