clean up client setup
Some checks failed
Gitea/etcdmtr/pipeline/head There was a failure building this commit

This commit is contained in:
dave 2024-02-16 12:20:38 -08:00
parent cba6219694
commit e5282fe0ca

View File

@ -10,7 +10,6 @@ import (
"io/ioutil"
"log"
"os"
"strings"
"time"
)
@ -22,19 +21,50 @@ type EtcdMtrArgs struct {
Insecure bool `short:"i" long:"insecure" description:"Do not verify against CA"`
}
func getClient(clientCert *tls.Certificate, caCert *x509.CertPool, endpoints []string) (*clientv3.Client, error) {
type Client struct {
tlsConf *tls.Config
endpoints []string
}
func NewClient(endpoints []string, clientCertPath string, clientKeyPath string, caCertPath string, insecure bool) (*Client, error) {
clientCert, err := tls.LoadX509KeyPair(clientCertPath, clientKeyPath)
if err != nil {
panic(err)
}
caContent, err := ioutil.ReadFile(caCertPath)
if err != nil {
panic(err)
}
roots := x509.NewCertPool()
ok := roots.AppendCertsFromPEM(caContent)
if !ok {
panic("failed to parse root certificate")
}
tlsConf := &tls.Config{
Certificates: []tls.Certificate{
*clientCert,
clientCert,
},
RootCAs: caCert,
RootCAs: roots,
//InsecureSkipVerify: true,
}
return &Client{
tlsConf: tlsConf,
endpoints: endpoints,
}, nil
}
func (c *Client) GetEtcd() (*clientv3.Client, error) {
return c.GetEtcdWithEndpoints(c.endpoints)
}
func (c *Client) GetEtcdWithEndpoints(endpoints []string) (*clientv3.Client, error) {
return clientv3.New(clientv3.Config{
Endpoints: endpoints,
DialTimeout: 5 * time.Second,
TLS: tlsConf,
TLS: c.tlsConf,
})
}
@ -66,43 +96,23 @@ func main() {
}
}
//client, err := getClient(positionals, args.ClientKey, args.ClientCert, args.ServerCA, args.Insecure)
//endpoints, ck, cc, ca, insecure
os.Exit(0)
cert, err := tls.LoadX509KeyPair(positionals[2], positionals[3])
client, err := NewClient(positionals, args.ClientCert, args.ClientKey, args.ServerCA, args.Insecure)
if err != nil {
panic(err)
}
caContent, err := ioutil.ReadFile(positionals[1])
etcd, err := client.GetEtcd()
if err != nil {
panic(err)
}
roots := x509.NewCertPool()
ok := roots.AppendCertsFromPEM(caContent)
if !ok {
panic("failed to parse root certificate")
}
endpoints := strings.Split(positionals[0], ",")
cli, err := getClient(&cert, roots, endpoints)
if err != nil {
panic(err)
}
defer cli.Close()
ctx, _ := context.WithTimeout(context.TODO(), 5*time.Second)
err = cli.Sync(ctx)
err = etcd.Sync(ctx)
if err != nil {
panic(err)
}
members, err := cli.Cluster.MemberList(ctx)
members, err := etcd.Cluster.MemberList(ctx)
if err != nil {
panic(err)
}
@ -112,7 +122,7 @@ func main() {
fmt.Printf("found member list of: %d\n", numMembers)
for _, member := range members.Members {
c2, err := getClient(&cert, roots, []string{member.ClientURLs[0]})
c2, err := client.GetEtcdWithEndpoints([]string{member.ClientURLs[0]})
if err != nil {
panic(err)
}