Skip to content

Commit 5ad9955

Browse files
committed
Resolve IP address to hostname during startup
Previously, the code was resolving each time the CSRs were checked. Changed error logging to improve clarity.
1 parent 1f2e11b commit 5ad9955

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

pkg/agent/monitoraddnodes.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type addNodeStatusHistory struct {
3333

3434
type addNodeMonitor struct {
3535
nodeIPAddress string
36+
hostnames []string
3637
cluster *Cluster
3738
status addNodeStatusHistory
3839
}
@@ -54,6 +55,12 @@ func newAddNodeMonitor(nodeIP string, cluster *Cluster) (*addNodeMonitor, error)
5455
NodeIsReady: false,
5556
},
5657
}
58+
hostnames, err := net.LookupAddr(nodeIP)
59+
if err != nil {
60+
logrus.Infof("Cannot resolve IP address %v to a hostname. Skipping checks for pending CSRs.", nodeIP)
61+
} else {
62+
mon.hostnames = hostnames
63+
}
5764
return &mon, nil
5865
}
5966

@@ -202,19 +209,18 @@ func (mon *addNodeMonitor) clusterHasSecondCSRPending() bool {
202209
}
203210

204211
func (mon *addNodeMonitor) getCSRsPendingApproval(signerName string) []certificatesv1.CertificateSigningRequest {
205-
hostnames, err := net.LookupAddr(mon.nodeIPAddress)
206-
if err != nil {
207-
logrus.Debugf("error looking up hostnames from IP address: %v", err)
212+
if mon.hostnames == nil {
208213
return []certificatesv1.CertificateSigningRequest{}
209214
}
210215

211216
csrs, err := mon.cluster.API.Kube.ListCSRs()
212217
if err != nil {
213218
logrus.Debugf("error calling listCSRs(): %v", err)
219+
logrus.Infof("Cannot retrieve CSRs from Kube API. Skipping checks for pending CSRs")
214220
return []certificatesv1.CertificateSigningRequest{}
215221
}
216222

217-
return filterCSRsMatchingHostname(signerName, csrs, hostnames)
223+
return filterCSRsMatchingHostname(signerName, csrs, mon.hostnames)
218224
}
219225

220226
func filterCSRsMatchingHostname(signerName string, csrs *certificatesv1.CertificateSigningRequestList, hostnames []string) []certificatesv1.CertificateSigningRequest {
@@ -236,6 +242,10 @@ func filterCSRsMatchingHostname(signerName string, csrs *certificatesv1.Certific
236242
return matchedCSRs
237243
}
238244

245+
// containsHostname checks if the searchString contains one of the node's
246+
// hostnames. Only the first element of the hostname is checked.
247+
// For example if the hostname is "extraworker-0.ostest.test.metalkube.org",
248+
// "extraworker-0" is used to check if it exists in the searchString.
239249
func containsHostname(searchString string, hostnames []string) bool {
240250
for _, hostname := range hostnames {
241251
parts := strings.Split(hostname, ".")

0 commit comments

Comments
 (0)