Skip to content

Commit 0b7ddd7

Browse files
authored
chore: pkg-new cloudutils.TryDiscoverPublicIP (#2301)
* chore: pkg-new cloudutils.TryDiscoverPublicIP * f
1 parent fe92697 commit 0b7ddd7

File tree

9 files changed

+99
-34
lines changed

9 files changed

+99
-34
lines changed

cmd/installer/cli/api.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
apilogger "github.com/replicatedhq/embedded-cluster/api/pkg/logger"
1919
apitypes "github.com/replicatedhq/embedded-cluster/api/types"
2020
ecv1beta1 "github.com/replicatedhq/embedded-cluster/kinds/apis/v1beta1"
21+
"github.com/replicatedhq/embedded-cluster/pkg-new/cloudutils"
2122
"github.com/replicatedhq/embedded-cluster/pkg-new/tlsutils"
2223
"github.com/replicatedhq/embedded-cluster/pkg/metrics"
2324
"github.com/replicatedhq/embedded-cluster/pkg/release"
@@ -213,7 +214,7 @@ func getManagerURL(hostname string, port int) string {
213214
if hostname != "" {
214215
return fmt.Sprintf("https://%s:%v", hostname, port)
215216
}
216-
ipaddr := runtimeconfig.TryDiscoverPublicIP()
217+
ipaddr := cloudutils.TryDiscoverPublicIP()
217218
if ipaddr == "" {
218219
if addr := os.Getenv("EC_PUBLIC_ADDRESS"); addr != "" {
219220
ipaddr = addr

cmd/installer/cli/install.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
apitypes "github.com/replicatedhq/embedded-cluster/api/types"
1717
"github.com/replicatedhq/embedded-cluster/cmd/installer/kotscli"
1818
ecv1beta1 "github.com/replicatedhq/embedded-cluster/kinds/apis/v1beta1"
19+
"github.com/replicatedhq/embedded-cluster/pkg-new/cloudutils"
1920
newconfig "github.com/replicatedhq/embedded-cluster/pkg-new/config"
2021
"github.com/replicatedhq/embedded-cluster/pkg-new/hostutils"
2122
"github.com/replicatedhq/embedded-cluster/pkg-new/k0s"
@@ -1078,7 +1079,7 @@ func getAdminConsoleURL(hostname string, networkInterface string, port int) stri
10781079
if hostname != "" {
10791080
return fmt.Sprintf("http://%s:%v", hostname, port)
10801081
}
1081-
ipaddr := runtimeconfig.TryDiscoverPublicIP()
1082+
ipaddr := cloudutils.TryDiscoverPublicIP()
10821083
if ipaddr == "" {
10831084
var err error
10841085
ipaddr, err = netutils.FirstValidAddress(networkInterface)

pkg-new/cloudutils/cloudutils.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package cloudutils
2+
3+
import "github.com/sirupsen/logrus"
4+
5+
type CloudUtils struct {
6+
logger logrus.FieldLogger
7+
}
8+
9+
type CloudUtilsOption func(*CloudUtils)
10+
11+
func WithLogger(logger logrus.FieldLogger) CloudUtilsOption {
12+
return func(c *CloudUtils) {
13+
c.logger = logger
14+
}
15+
}
16+
17+
func New(opts ...CloudUtilsOption) *CloudUtils {
18+
c := &CloudUtils{}
19+
for _, opt := range opts {
20+
opt(c)
21+
}
22+
23+
if c.logger == nil {
24+
c.logger = logrus.StandardLogger()
25+
}
26+
27+
return c
28+
}

pkg-new/cloudutils/interface.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package cloudutils
2+
3+
var _c Interface
4+
5+
func init() {
6+
Set(New())
7+
}
8+
9+
func Set(c Interface) {
10+
_c = c
11+
}
12+
13+
type Interface interface {
14+
TryDiscoverPublicIP() string
15+
}
16+
17+
func TryDiscoverPublicIP() string {
18+
return _c.TryDiscoverPublicIP()
19+
}

pkg-new/cloudutils/mock.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package cloudutils
2+
3+
import "github.com/stretchr/testify/mock"
4+
5+
var _ Interface = (*MockCloudUtils)(nil)
6+
7+
// MockCloudUtils is a mock implementation of the CloudUtilsInterface
8+
type MockCloudUtils struct {
9+
mock.Mock
10+
}
11+
12+
func (m *MockCloudUtils) TryDiscoverPublicIP() string {
13+
args := m.Called()
14+
return args.String(0)
15+
}

pkg/runtimeconfig/cloudprovider.go renamed to pkg-new/cloudutils/publicip.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package runtimeconfig
1+
package cloudutils
22

33
import (
44
"encoding/json"
@@ -19,42 +19,41 @@ func init() {
1919
noProxyTransport.Proxy = nil // no proxy
2020
}
2121

22-
// TryDiscoverPublicIP tries to discover the public IP of the node by querying
23-
// a list of known providers. If the public IP cannot be discovered, an empty
24-
// string is returned.
25-
func TryDiscoverPublicIP() string {
22+
// TryDiscoverPublicIP tries to discover the public IP of the node by querying a list of known
23+
// providers. If the public IP cannot be discovered, an empty string is returned.
24+
func (c *CloudUtils) TryDiscoverPublicIP() string {
2625
if !shouldUseMetadataService() {
27-
logrus.Debug("No cloud provider metadata service found, skipping public IP discovery")
26+
c.logger.Debug("No cloud provider metadata service found, skipping public IP discovery")
2827
return ""
2928
}
3029

3130
publicIP := tryDiscoverPublicIPAWSIMDSv2()
3231
if publicIP != "" {
33-
logrus.Debugf("Found public IP %s using AWS IMDSv2", publicIP)
32+
c.logger.Debugf("Found public IP %s using AWS IMDSv2", publicIP)
3433
return publicIP
3534
}
3635

3736
publicIP = tryDiscoverPublicIPAWSIMDSv1()
3837
if publicIP != "" {
39-
logrus.Debugf("Found public IP %s using AWS IMDSv1", publicIP)
38+
c.logger.Debugf("Found public IP %s using AWS IMDSv1", publicIP)
4039
return publicIP
4140
}
4241

4342
publicIP = tryDiscoverPublicIPGCE()
4443
if publicIP != "" {
45-
logrus.Debugf("Found public IP %s using GCE", publicIP)
44+
c.logger.Debugf("Found public IP %s using GCE", publicIP)
4645
return publicIP
4746
}
4847

4948
publicIP = tryDiscoverPublicIPAzureStandardSKU()
5049
if publicIP != "" {
51-
logrus.Debugf("Found public IP %s using Azure Standard SKU", publicIP)
50+
c.logger.Debugf("Found public IP %s using Azure Standard SKU", publicIP)
5251
return publicIP
5352
}
5453

5554
publicIP = tryDiscoverPublicIPAzure()
5655
if publicIP != "" {
57-
logrus.Debugf("Found public IP %s using Azure", publicIP)
56+
c.logger.Debugf("Found public IP %s using Azure", publicIP)
5857
return publicIP
5958
}
6059

pkg/runtimeconfig/cloudprovider_test.go renamed to pkg-new/cloudutils/publicip_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package runtimeconfig
1+
package cloudutils
22

33
import "testing"
44

pkg/netutils/ips.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ import (
55
"net"
66
"strings"
77

8-
"github.com/replicatedhq/embedded-cluster/pkg/runtimeconfig"
8+
"github.com/replicatedhq/embedded-cluster/pkg-new/cloudutils"
99
)
1010

1111
// Dependency injection variables for testing
1212
var (
1313
networkInterfaceProvider = DefaultNetworkInterfaceProvider
14-
tryDiscoverPublicIP = runtimeconfig.TryDiscoverPublicIP
1514
)
1615

1716
// adapted from https://github.com/k0sproject/k0s/blob/v1.30.4%2Bk0s.0/internal/pkg/iface/iface.go#L61
@@ -142,7 +141,7 @@ func ListAllValidIPAddresses() ([]net.IP, error) {
142141
}
143142

144143
// try discovering the public IP if we're running in a cloud provider
145-
publicIP := tryDiscoverPublicIP()
144+
publicIP := cloudutils.TryDiscoverPublicIP()
146145
if publicIP != "" {
147146
ipAddresses = append(ipAddresses, net.ParseIP(publicIP))
148147
}

pkg/netutils/ips_test.go

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"net"
55
"testing"
66

7+
"github.com/replicatedhq/embedded-cluster/pkg-new/cloudutils"
78
"github.com/stretchr/testify/assert"
89
"github.com/stretchr/testify/require"
910
)
@@ -468,16 +469,15 @@ func TestListValidNetworkInterfaces(t *testing.T) {
468469
func TestListAllValidIPAddresses(t *testing.T) {
469470
// Save original provider
470471
originalProvider := networkInterfaceProvider
471-
originalTryDiscoverPublicIP := tryDiscoverPublicIP
472472
defer func() {
473473
networkInterfaceProvider = originalProvider
474-
tryDiscoverPublicIP = originalTryDiscoverPublicIP
474+
cloudutils.Set(cloudutils.New())
475475
}()
476476

477477
tests := []struct {
478478
name string
479479
mockProvider NetworkInterfaceProvider
480-
mockPublicIPFunc func() string
480+
setupMockCloudUtils func(m *cloudutils.MockCloudUtils)
481481
expectedIPs []string
482482
expectedError bool
483483
expectedErrorContains string
@@ -503,8 +503,8 @@ func TestListAllValidIPAddresses(t *testing.T) {
503503
},
504504
},
505505
},
506-
mockPublicIPFunc: func() string {
507-
return ""
506+
setupMockCloudUtils: func(m *cloudutils.MockCloudUtils) {
507+
m.On("TryDiscoverPublicIP").Once().Return("")
508508
},
509509
expectedIPs: []string{"192.168.1.100", "192.168.1.101", "10.0.0.50"},
510510
expectedError: false,
@@ -524,8 +524,8 @@ func TestListAllValidIPAddresses(t *testing.T) {
524524
},
525525
},
526526
},
527-
mockPublicIPFunc: func() string {
528-
return ""
527+
setupMockCloudUtils: func(m *cloudutils.MockCloudUtils) {
528+
m.On("TryDiscoverPublicIP").Once().Return("")
529529
},
530530
expectedIPs: []string{"192.168.1.100"},
531531
expectedError: false,
@@ -541,8 +541,8 @@ func TestListAllValidIPAddresses(t *testing.T) {
541541
},
542542
},
543543
},
544-
mockPublicIPFunc: func() string {
545-
return ""
544+
setupMockCloudUtils: func(m *cloudutils.MockCloudUtils) {
545+
m.On("TryDiscoverPublicIP").Once().Return("")
546546
},
547547
expectedIPs: []string{},
548548
expectedError: false,
@@ -560,8 +560,8 @@ func TestListAllValidIPAddresses(t *testing.T) {
560560
},
561561
},
562562
},
563-
mockPublicIPFunc: func() string {
564-
return ""
563+
setupMockCloudUtils: func(m *cloudutils.MockCloudUtils) {
564+
m.On("TryDiscoverPublicIP").Once().Return("")
565565
},
566566
expectedIPs: []string{},
567567
expectedError: false,
@@ -571,9 +571,7 @@ func TestListAllValidIPAddresses(t *testing.T) {
571571
mockProvider: &mockNetworkInterfaceProvider{
572572
err: assert.AnError,
573573
},
574-
mockPublicIPFunc: func() string {
575-
return ""
576-
},
574+
setupMockCloudUtils: func(m *cloudutils.MockCloudUtils) {},
577575
expectedIPs: nil,
578576
expectedError: true,
579577
expectedErrorContains: "list valid network interfaces",
@@ -591,8 +589,8 @@ func TestListAllValidIPAddresses(t *testing.T) {
591589
},
592590
},
593591
},
594-
mockPublicIPFunc: func() string {
595-
return "203.0.113.45"
592+
setupMockCloudUtils: func(m *cloudutils.MockCloudUtils) {
593+
m.On("TryDiscoverPublicIP").Once().Return("203.0.113.45")
596594
},
597595
expectedIPs: []string{"192.168.1.100", "203.0.113.45"},
598596
expectedError: false,
@@ -602,10 +600,15 @@ func TestListAllValidIPAddresses(t *testing.T) {
602600
for _, tt := range tests {
603601
t.Run(tt.name, func(t *testing.T) {
604602
networkInterfaceProvider = tt.mockProvider
605-
tryDiscoverPublicIP = tt.mockPublicIPFunc
603+
604+
mockCloudUtils := &cloudutils.MockCloudUtils{}
605+
cloudutils.Set(mockCloudUtils)
606+
tt.setupMockCloudUtils(mockCloudUtils)
606607

607608
result, err := ListAllValidIPAddresses()
608609

610+
mockCloudUtils.AssertExpectations(t)
611+
609612
if tt.expectedError {
610613
require.Error(t, err)
611614
assert.Contains(t, err.Error(), tt.expectedErrorContains)

0 commit comments

Comments
 (0)