Skip to content

Commit 276e0d3

Browse files
authored
Merge pull request #3418 from ntnn/fix-bind-address
Fix `--bind-address`
2 parents 7814220 + 2ac44e8 commit 276e0d3

File tree

15 files changed

+121
-47
lines changed

15 files changed

+121
-47
lines changed

cmd/sharded-test-server/cache.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import (
3939
kcptestingserver "github.com/kcp-dev/kcp/sdk/testing/server"
4040
)
4141

42-
func startCacheServer(ctx context.Context, logDirPath, workingDir string, syntheticDelay time.Duration) (<-chan error, string, error) {
42+
func startCacheServer(ctx context.Context, logDirPath, workingDir, hostIP string, syntheticDelay time.Duration) (<-chan error, string, error) {
4343
cyan := color.New(color.BgHiCyan, color.FgHiWhite).SprintFunc()
4444
inverse := color.New(color.BgHiWhite, color.FgHiCyan).SprintFunc()
4545
out := lineprefix.New(
@@ -56,6 +56,7 @@ func startCacheServer(ctx context.Context, logDirPath, workingDir string, synthe
5656
commandLine = append(
5757
commandLine,
5858
fmt.Sprintf("--root-directory=%s", cacheWorkingDir),
59+
"--bind-address="+hostIP,
5960
"--embedded-etcd-client-port=8010",
6061
"--embedded-etcd-peer-port=8011",
6162
fmt.Sprintf("--secure-port=%d", cachePort),

cmd/sharded-test-server/frontproxy.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ func startFrontProxy(
132132

133133
// run front-proxy command
134134
commandLine := append(kcptestingserver.Command("kcp-front-proxy", "front-proxy"),
135+
"--bind-address="+hostIP,
135136
fmt.Sprintf("--mapping-file=%s", filepath.Join(workDirPath, ".kcp-front-proxy/mapping.yaml")),
136137
fmt.Sprintf("--root-directory=%s", filepath.Join(workDirPath, ".kcp-front-proxy")),
137138
fmt.Sprintf("--root-kubeconfig=%s", filepath.Join(workDirPath, ".kcp/root.kubeconfig")),

cmd/sharded-test-server/main.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828

2929
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3030
"k8s.io/apimachinery/pkg/types"
31-
machineryutilnet "k8s.io/apimachinery/pkg/util/net"
3231
"k8s.io/apimachinery/pkg/util/sets"
3332
"k8s.io/apimachinery/pkg/util/wait"
3433
kuser "k8s.io/apiserver/pkg/authentication/user"
@@ -192,17 +191,13 @@ func start(proxyFlags, shardFlags []string, logDirPath, workDirPath string, numb
192191
return fmt.Errorf("failed to create service-account-signing-ca: %w", err)
193192
}
194193

195-
// find external IP to put into certs as valid IPs
196-
hostIP, err := machineryutilnet.ResolveBindAddress(net.IPv4(0, 0, 0, 0))
197-
if err != nil {
198-
return err
199-
}
194+
hostIP := net.IPv4(127, 0, 0, 1)
200195

201196
standaloneVW := sets.New[string](shardFlags...).Has("--run-virtual-workspaces=false")
202197

203198
cacheServerErrCh := make(chan indexErrTuple)
204199
cacheServerConfigPath := ""
205-
cacheServerCh, configPath, err := startCacheServer(ctx, logDirPath, workDirPath, cacheSyntheticDelay)
200+
cacheServerCh, configPath, err := startCacheServer(ctx, logDirPath, workDirPath, hostIP.String(), cacheSyntheticDelay)
206201
if err != nil {
207202
return fmt.Errorf("error starting the cache server: %w", err)
208203
}

cmd/sharded-test-server/virtual.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,15 @@ func newVirtualWorkspace(ctx context.Context, index int, servingCA *crypto.CA, h
142142
return nil, err
143143
}
144144

145-
var args []string
146-
args = append(args,
145+
args := []string{
147146
fmt.Sprintf("--kubeconfig=%s", kubeconfigPath),
148147
fmt.Sprintf("--shard-external-url=https://%s:%d", hostIP, 6443),
149148
fmt.Sprintf("--cache-kubeconfig=%s", cacheServerConfigPath),
150149
fmt.Sprintf("--authentication-kubeconfig=%s", authenticationKubeconfigPath),
151150
fmt.Sprintf("--client-ca-file=%s", clientCAFilePath),
152151
fmt.Sprintf("--tls-private-key-file=%s", servingKeyFile),
153152
fmt.Sprintf("--tls-cert-file=%s", servingCertFile),
153+
fmt.Sprintf("--bind-address=%s", hostIP),
154154
fmt.Sprintf("--secure-port=%s", virtualWorkspacePort(index)),
155155
"--audit-log-maxsize=1024",
156156
"--audit-log-mode=batch",
@@ -161,7 +161,7 @@ func newVirtualWorkspace(ctx context.Context, index int, servingCA *crypto.CA, h
161161
"--audit-log-batch-throttle-enable=true",
162162
"--audit-log-batch-throttle-qps=10",
163163
fmt.Sprintf("--audit-policy-file=%s", auditPolicyFile),
164-
)
164+
}
165165

166166
return &VirtualWorkspace{
167167
index: index,

cmd/test-server/kcp/shard.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ func (s *Shard) Start(ctx context.Context, quiet bool) error {
9999
commandLine = append(commandLine, s.args...)
100100
commandLine = append(commandLine,
101101
"--root-directory", s.runtimeDir,
102+
"--bind-address=127.0.0.1",
102103
"--token-auth-file", framework.DefaultTokenAuthFile,
103104
"--audit-log-maxsize", "1024",
104105
"--audit-log-mode=batch",

pkg/server/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ type CompletedConfig struct {
155155
// Complete fills in any fields not set that are required to have valid data. It's mutating the receiver.
156156
func (c *Config) Complete() (CompletedConfig, error) {
157157
miniAggregator := c.MiniAggregator.Complete()
158+
158159
return CompletedConfig{&completedConfig{
159160
Options: c.Options,
160161

pkg/server/options/options.go

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,22 @@ func (o *Options) Complete(ctx context.Context, rootDir string) (*CompletedOptio
287287
}
288288
}
289289

290+
// ExternalAddress is the address used e.g. when generating
291+
// kubeconfigs. It defaults to the default interface, usually the
292+
// first non-loopback interface, e.g. 192.168.0.1.
293+
// BindAddress is the address the server binds to, it defaults to
294+
// 0.0.0.0 or ::.
295+
//
296+
// If BindAddress is set to a specific address, e.g. the loopback
297+
// 127.0.0.1 the ExternalAddress is invalid and all URLs generated
298+
// from it will not work.
299+
//
300+
// To prevent this ExternalAddress is set to the value of
301+
// BindAddress if it wasn't set to a specific address.
302+
if o.GenericControlPlane.GenericServerRunOptions.ExternalHost == "" && !o.GenericControlPlane.SecureServing.BindAddress.IsUnspecified() {
303+
o.GenericControlPlane.GenericServerRunOptions.ExternalHost = o.GenericControlPlane.SecureServing.BindAddress.String()
304+
}
305+
290306
if o.Extra.ExperimentalBindFreePort {
291307
listener, _, err := genericapiserveroptions.CreateListener("tcp", fmt.Sprintf("%s:0", o.GenericControlPlane.SecureServing.BindAddress), net.ListenConfig{})
292308
if err != nil {
@@ -311,7 +327,34 @@ func (o *Options) Complete(ctx context.Context, rootDir string) (*CompletedOptio
311327
o.GenericControlPlane.ServiceAccountSigningKeyFile = o.Controllers.SAController.ServiceAccountKeyFile
312328
}
313329

314-
completedGenericOptions, err := o.GenericControlPlane.Complete(ctx, nil, nil)
330+
// o.GenericControlPlane.Complete creates self-signed certificates
331+
// with the advertise address by default. This can cause spurious
332+
// errors if the server binds on multiple interfaces.
333+
possibleIPs := []net.IP{
334+
o.GenericControlPlane.GenericServerRunOptions.AdvertiseAddress,
335+
o.GenericControlPlane.SecureServing.BindAddress,
336+
o.GenericControlPlane.SecureServing.ExternalAddress,
337+
}
338+
if o.GenericControlPlane.SecureServing.Listener != nil {
339+
host, _, err := net.SplitHostPort(o.GenericControlPlane.SecureServing.Listener.Addr().String())
340+
if err != nil {
341+
return nil, err
342+
}
343+
possibleIPs = append(possibleIPs, net.ParseIP(host))
344+
}
345+
346+
alternateIPs := []net.IP{}
347+
alternateDNS := []string{}
348+
349+
for _, ip := range possibleIPs {
350+
if ip == nil || ip.IsUnspecified() {
351+
continue
352+
}
353+
alternateIPs = append(alternateIPs, ip)
354+
alternateDNS = append(alternateDNS, ip.String())
355+
}
356+
357+
completedGenericOptions, err := o.GenericControlPlane.Complete(ctx, alternateDNS, alternateIPs)
315358
if err != nil {
316359
return nil, err
317360
}

sdk/testing/config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ type SharedKcpOption func()
2828

2929
var (
3030
sharedConfig = kcptestingserver.Config{
31-
Name: "shared",
31+
Name: "shared",
32+
BindAddress: "127.0.0.1",
3233
}
3334
externalConfig = struct {
3435
kubeconfigPath string

sdk/testing/kcp.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ var fs embed.FS
3535
func PrivateKcpServer(t TestingT, options ...kcptestingserver.Option) kcptestingserver.RunningServer {
3636
t.Helper()
3737

38-
cfg := &kcptestingserver.Config{Name: "main"}
38+
cfg := &kcptestingserver.Config{
39+
Name: "main",
40+
BindAddress: "127.0.0.1",
41+
}
3942
for _, opt := range options {
4043
opt(cfg)
4144
}

sdk/testing/server/config.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type Config struct {
2727
ArtifactDir string
2828
DataDir string
2929
ClientCADir string
30+
BindAddress string
3031

3132
LogToConsole bool
3233
RunInProcess bool
@@ -84,3 +85,10 @@ func WithLogToConsole() Option {
8485
cfg.LogToConsole = true
8586
}
8687
}
88+
89+
// WithBindAddress sets the kcp server to log to console.
90+
func WithBindAddress(addr string) Option {
91+
return func(cfg *Config) {
92+
cfg.BindAddress = addr
93+
}
94+
}

0 commit comments

Comments
 (0)