Skip to content

Commit

Permalink
Merge pull request #186 from sososo0/main
Browse files Browse the repository at this point in the history
PR: Add HTTPS handling and logging for LoxiLB based on --loxiURL co…
  • Loading branch information
UltraInstinct14 authored Oct 13, 2024
2 parents 61fd7f6 + 09cc8d0 commit 85496b9
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions pkg/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package api

import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"net"
"net/http"
Expand Down Expand Up @@ -30,14 +32,18 @@ type LoxiClient struct {
// apiServer is string. what format? http://10.0.0.1 or 10.0.0.1
func NewLoxiClient(apiServer string, aliveCh chan *LoxiClient, deadCh chan struct{}, peerOnly bool, noRole bool) (*LoxiClient, error) {

client := &http.Client{}

base, err := url.Parse(apiServer)
if err != nil {
fmt.Printf("failed to parse url %s. err: %s", apiServer, err.Error())
return nil, err
}

client, err := CreateHTTPClient(base)
if err != nil {
fmt.Printf("failed to create HTTP client: %v", err.Error())
return nil, err
}

restClient, err := NewRESTClient(base, "netlox", "v1", client)
if err != nil {
fmt.Printf("failed to call NewRESTClient. err: %s", err.Error())
Expand Down Expand Up @@ -70,6 +76,33 @@ func NewLoxiClient(apiServer string, aliveCh chan *LoxiClient, deadCh chan struc
return lc, nil
}

func CreateHTTPClient(baseURL *url.URL) (*http.Client, error) {

client := &http.Client{}

if baseURL.Scheme == "https" {

rootCAs, err := x509.SystemCertPool()
if err != nil || rootCAs == nil {
baseURL.Scheme = "http"
klog.Infof("HTTPS not supported: %s", baseURL)
return client, nil
}

tlsConfig := &tls.Config{
RootCAs: rootCAs,
InsecureSkipVerify: false,
}

transport := &http.Transport{
TLSClientConfig: tlsConfig,
}
client.Transport = transport
}

return client, nil
}

func (l *LoxiClient) StartLoxiHealthCheckChan(aliveCh chan *LoxiClient, deadCh chan struct{}) {
l.IsAlive = false

Expand Down

0 comments on commit 85496b9

Please sign in to comment.