Skip to content
This repository was archived by the owner on Nov 5, 2021. It is now read-only.

Commit afddefa

Browse files
committed
Ignore CLOUDPROBER_PORT if set by Kubernetes.
Kubernetes automatically sets the <SERVICE>_PORT environment variable to service URL - "tcp://<service-ip>:<service-port>". This conflicts with cloudprober's port environment variable CLOUDPROBER_PORT if someone runs cloudprober with service name cloudprober. To solve this, we used to parse CLOUDPROBER_PORT if it started with "tcp://". But this doesn't really work well, if someone wants to expose some other port from cloudprober, e.g. RDS. To avoid this confusion, we should just ignore CLOUDPROBER_PORT if it has been set by Kubernetes. Context: #476 PiperOrigin-RevId: 333552093
1 parent f5fe722 commit afddefa

2 files changed

Lines changed: 89 additions & 51 deletions

File tree

cloudprober.go

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"fmt"
2828
"net"
2929
"net/http"
30-
"net/url"
3130
"os"
3231
"strconv"
3332
"strings"
@@ -83,37 +82,37 @@ func getServerHost(c *configpb.ProberConfig) string {
8382
return serverHost
8483
}
8584

86-
func parsePort(portStr string) (int64, error) {
85+
func getDefaultServerPort(c *configpb.ProberConfig, l *logger.Logger) (int, error) {
86+
if c.GetPort() != 0 {
87+
return int(c.GetPort()), nil
88+
}
89+
90+
// If ServerPortEnvVar is defined, it will override the default
91+
// server port.
92+
portStr := os.Getenv(ServerPortEnvVar)
93+
if portStr == "" {
94+
return DefaultServerPort, nil
95+
}
96+
8797
if strings.HasPrefix(portStr, "tcp://") {
88-
u, err := url.Parse(portStr)
89-
if err != nil {
90-
return 0, err
91-
}
92-
if u.Port() == "" {
93-
return 0, fmt.Errorf("no port specified in URL %s", portStr)
94-
}
95-
// u.Port() returns port as a string, thus it
96-
// will be converted to int64 at the end.
97-
portStr = u.Port()
98+
l.Warningf("%s environment variable likely set by Kubernetes (to %s), ignoring it", ServerPortEnvVar, portStr)
99+
return DefaultServerPort, nil
98100
}
99-
return strconv.ParseInt(portStr, 10, 32)
101+
102+
port, err := strconv.ParseInt(portStr, 10, 32)
103+
if err != nil {
104+
return 0, fmt.Errorf("failed to parse default port from the env var: %s=%s", ServerPortEnvVar, portStr)
105+
}
106+
107+
return int(port), nil
100108
}
101109

102-
func initDefaultServer(c *configpb.ProberConfig) (net.Listener, error) {
110+
func initDefaultServer(c *configpb.ProberConfig, l *logger.Logger) (net.Listener, error) {
103111
serverHost := getServerHost(c)
104-
serverPort := int(c.GetPort())
105-
if serverPort == 0 {
106-
serverPort = DefaultServerPort
107-
108-
// If ServerPortEnvVar is defined, it will override the default
109-
// server port.
110-
if portStr := os.Getenv(ServerPortEnvVar); portStr != "" {
111-
port, err := parsePort(portStr)
112-
if err != nil {
113-
return nil, fmt.Errorf("failed to parse default port from the env var: %s=%s", ServerPortEnvVar, portStr)
114-
}
115-
serverPort = int(port)
116-
}
112+
serverPort, err := getDefaultServerPort(c, l)
113+
114+
if err != nil {
115+
return nil, err
117116
}
118117

119118
ln, err := net.Listen("tcp", fmt.Sprintf("%s:%d", serverHost, serverPort))
@@ -161,7 +160,7 @@ func InitFromConfig(configFile string) error {
161160

162161
// Start default HTTP server. It's used for profile handlers and
163162
// prometheus exporter.
164-
ln, err := initDefaultServer(cfg)
163+
ln, err := initDefaultServer(cfg, l)
165164
if err != nil {
166165
return err
167166
}

cloudprober_test.go

Lines changed: 62 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2019 Google Inc.
1+
// Copyright 2019-2020 Google Inc.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -15,33 +15,72 @@
1515
package cloudprober
1616

1717
import (
18-
"strings"
18+
"os"
1919
"testing"
20+
21+
configpb "github.com/google/cloudprober/config/proto"
22+
"google.golang.org/protobuf/proto"
2023
)
2124

22-
func TestParsePort(t *testing.T) {
23-
// test if it parses just a number in string format
24-
port, _ := parsePort("1234")
25-
expectedPort := int64(1234)
26-
if port != expectedPort {
27-
t.Errorf("parsePort(\"%d\") = %d; want %d", expectedPort, port, expectedPort)
25+
func TestGetDefaultServerPort(t *testing.T) {
26+
tests := []struct {
27+
desc string
28+
configPort int32
29+
envVar string
30+
wantPort int
31+
wantErr bool
32+
}{
33+
{
34+
desc: "use port from config",
35+
configPort: 9316,
36+
envVar: "3141",
37+
wantPort: 9316,
38+
},
39+
{
40+
desc: "use default port",
41+
configPort: 0,
42+
envVar: "",
43+
wantPort: DefaultServerPort,
44+
},
45+
{
46+
desc: "use port from env",
47+
configPort: 0,
48+
envVar: "3141",
49+
wantPort: 3141,
50+
},
51+
{
52+
desc: "ignore kubernetes port",
53+
configPort: 0,
54+
envVar: "tcp://100.101.102.103:3141",
55+
wantPort: 9313,
56+
},
57+
{
58+
desc: "error due to bad env var",
59+
configPort: 0,
60+
envVar: "a3141",
61+
wantErr: true,
62+
},
2863
}
2964

30-
// test if it parses full URL
31-
testStr := "tcp://10.1.1.4:9313"
32-
port, _ = parsePort(testStr)
33-
expectedPort = int64(9313)
34-
if port != expectedPort {
35-
t.Errorf("parsePort(\"%s\") = %d; want %d", testStr, port, expectedPort)
36-
}
65+
for _, test := range tests {
66+
t.Run(test.desc, func(t *testing.T) {
67+
os.Setenv(ServerPortEnvVar, test.envVar)
68+
port, err := getDefaultServerPort(&configpb.ProberConfig{
69+
Port: proto.Int32(test.configPort),
70+
}, nil)
3771

38-
// test if it detects absent port in URL
39-
testStr = "tcp://10.1.1.4"
40-
_, err := parsePort(testStr)
41-
errStr := "no port specified in URL"
42-
if err != nil && !strings.Contains(err.Error(), errStr) {
43-
t.Errorf("parsePort(\"%s\") doesn't return \"%s\" error, however found error: %s", testStr, "no port specified in URL", err.Error())
44-
} else if err == nil {
45-
t.Errorf("parsePort(\"%s\") should return \"%s\" error, however no errors found", testStr, "no port specified in URL")
72+
if err != nil {
73+
if !test.wantErr {
74+
t.Errorf("Got unexpected error: %v", err)
75+
} else {
76+
return
77+
}
78+
}
79+
80+
if port != test.wantPort {
81+
t.Errorf("got port: %d, want port: %d", port, test.wantPort)
82+
}
83+
})
4684
}
85+
4786
}

0 commit comments

Comments
 (0)