albatros/pkg/cluster.go
2023-05-03 10:52:34 +02:00

45 lines
1.1 KiB
Go

package pkg
import (
consul "github.com/hashicorp/consul/api"
nomad "github.com/hashicorp/nomad/api"
"log"
)
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
}