Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
bbd68f3a
Unverified
Commit
bbd68f3a
authored
Jan 02, 2024
by
Pierre
Committed by
GitHub
Jan 02, 2024
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Rebase & Squash (#9070)
Signed-off-by:
Yodo
<
pierre@azmed.co
>
parent
c7a8eef9
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
57 additions
and
8 deletions
+57
-8
go.mod
go.mod
+1
-0
go.sum
go.sum
+2
-0
servers.go
pkg/agent/loadbalancer/servers.go
+54
-8
No files found.
go.mod
View file @
bbd68f3a
...
...
@@ -117,6 +117,7 @@ require (
github.com/lib/pq v1.10.2
github.com/mattn/go-sqlite3 v1.14.17
github.com/minio/minio-go/v7 v7.0.33
github.com/mwitkow/go-http-dialer v0.0.0-20161116154839-378f744fb2b8
github.com/natefinch/lumberjack v2.0.0+incompatible
github.com/onsi/ginkgo/v2 v2.13.2
github.com/onsi/gomega v1.30.0
...
...
go.sum
View file @
bbd68f3a
...
...
@@ -1121,6 +1121,8 @@ github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/mwitkow/go-http-dialer v0.0.0-20161116154839-378f744fb2b8 h1:BhQQWYKJwXPtAhm12d4gQU4LKS9Yov22yOrDc2QA7ho=
github.com/mwitkow/go-http-dialer v0.0.0-20161116154839-378f744fb2b8/go.mod h1:ntWhh7pzdiiRKBMxUB5iG+Q2gmZBxGxpX1KyK6N8kX8=
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f h1:y5//uYreIhSUg3J1GEMiLbxo1LJaP8RfCpH6pymGZus=
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw=
github.com/natefinch/lumberjack v2.0.0+incompatible h1:4QJd3OLAMgj7ph+yZTuX13Ld4UpgHp07nNdFX7mqFfM=
...
...
pkg/agent/loadbalancer/servers.go
View file @
bbd68f3a
...
...
@@ -3,14 +3,47 @@ package loadbalancer
import
(
"context"
"errors"
"fmt"
"math/rand"
"net"
"net/http"
"net/url"
"os"
"strconv"
"github.com/k3s-io/k3s/pkg/version"
http_dialer
"github.com/mwitkow/go-http-dialer"
"golang.org/x/net/proxy"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/util/sets"
)
var
defaultDialer
=
&
net
.
Dialer
{}
var
defaultDialer
proxy
.
Dialer
=
&
net
.
Dialer
{}
func
init
()
{
// Check if env variable for proxy is set
address
:=
os
.
Getenv
(
version
.
ProgramUpper
+
"_URL"
)
if
useProxy
,
_
:=
strconv
.
ParseBool
(
os
.
Getenv
(
version
.
ProgramUpper
+
"_AGENT_HTTP_PROXY_ALLOWED"
));
!
useProxy
{
return
}
req
,
err
:=
http
.
NewRequest
(
"GET"
,
"https://"
+
address
,
nil
)
if
err
!=
nil
{
logrus
.
Errorf
(
"Error creating request for address %s: %v"
,
address
,
err
)
}
proxyURL
,
err
:=
http
.
ProxyFromEnvironment
(
req
)
if
err
!=
nil
{
logrus
.
Errorf
(
"Error getting the proxy for address %s: %v"
,
address
,
err
)
}
if
dialer
,
err
:=
proxyDialer
(
proxyURL
);
err
!=
nil
{
logrus
.
Errorf
(
"Error creating the proxyDialer for %s: %v"
,
address
,
err
)
}
else
{
defaultDialer
=
dialer
}
}
func
(
lb
*
LoadBalancer
)
setServers
(
serverAddresses
[]
string
)
bool
{
serverAddresses
,
hasOriginalServer
:=
sortServers
(
serverAddresses
,
lb
.
defaultServerAddress
)
...
...
@@ -84,20 +117,33 @@ func (lb *LoadBalancer) nextServer(failedServer string) (string, error) {
return
lb
.
currentServerAddress
,
nil
}
// dialContext dials a new connection, and adds its wrapped connection to the map
// dialContext dials a new connection
using the environment's proxy settings
, and adds its wrapped connection to the map
func
(
s
*
server
)
dialContext
(
ctx
context
.
Context
,
network
,
address
string
)
(
net
.
Conn
,
error
)
{
conn
,
err
:=
defaultDialer
.
Dial
Context
(
ctx
,
network
,
address
)
conn
,
err
:=
defaultDialer
.
Dial
(
network
,
address
)
if
err
!=
nil
{
return
nil
,
err
}
// don't lock until adding the connection to the map, otherwise we may block
//
while waiting for the dial to time out
//
Wrap the connection and add it to the server's connection map
s
.
mutex
.
Lock
()
defer
s
.
mutex
.
Unlock
()
conn
=
&
serverConn
{
server
:
s
,
Conn
:
conn
}
s
.
connections
[
conn
]
=
struct
{}{}
return
conn
,
nil
wrappedConn
:=
&
serverConn
{
server
:
s
,
Conn
:
conn
}
s
.
connections
[
wrappedConn
]
=
struct
{}{}
return
wrappedConn
,
nil
}
// proxyDialer creates a new proxy.Dialer that routes connections through the specified proxy.
func
proxyDialer
(
proxyURL
*
url
.
URL
)
(
proxy
.
Dialer
,
error
)
{
if
proxyURL
.
Scheme
==
"http"
||
proxyURL
.
Scheme
==
"https"
{
// Create a new HTTP proxy dialer
httpProxyDialer
:=
http_dialer
.
New
(
proxyURL
)
return
httpProxyDialer
,
nil
}
else
if
proxyURL
.
Scheme
==
"socks5"
{
// For SOCKS5 proxies, use the proxy package's FromURL
return
proxy
.
FromURL
(
proxyURL
,
proxy
.
Direct
)
}
return
nil
,
fmt
.
Errorf
(
"unsupported proxy scheme: %s"
,
proxyURL
.
Scheme
)
}
// closeAll closes all connections to the server, and removes their entries from the map
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment