Skip to content

Commit e6f344b

Browse files
committed
Kops - try computeMetadata for external IP before using ifconfig.co
This logic was removed from kops-e2e-runner.sh because it wasnt falling back properly after the computeMetadata requests would fail due to workload identity being enabled on the cluster. Now that we can use a ServiceAccount on our prow jobs that will enable access to the computeMetadata URLs, we can try to use it first before ifconfig.co. We've also been getting occasionally blocked from ifconfig.co, presumably due to high volume, so this will help with test flakes. The prow jobs are not yet configured with a service account but this logic can be added independently of that.
1 parent ed946ab commit e6f344b

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

kubetest/kops.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"io/ioutil"
2727
"log"
2828
"math/rand"
29+
"net"
2930
"os"
3031
"os/exec"
3132
"os/user"
@@ -47,6 +48,8 @@ import (
4748
// kopsAWSMasterSize is the default ec2 instance type for kops on aws
4849
const kopsAWSMasterSize = "c5.large"
4950

51+
const externalIPURL = "http://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/0/access-configs/0/external-ip"
52+
5053
var (
5154

5255
// kops specific flags.
@@ -399,8 +402,12 @@ func (k kops) Up() error {
399402
}
400403
if k.adminAccess == "" {
401404
var b bytes.Buffer
402-
if err := httpRead("https://v4.ifconfig.co", &b); err != nil {
403-
return err
405+
err := httpReadWithHeaders(externalIPURL, map[string]string{"Metadata-Flavor": "Google"}, &b)
406+
if err != nil || net.ParseIP(strings.TrimSpace(b.String())) == nil {
407+
b.Reset()
408+
if err := httpRead("https://v4.ifconfig.co", &b); err != nil {
409+
return err
410+
}
404411
}
405412
externalIP := strings.TrimSpace(b.String()) + "/32"
406413
log.Printf("Using external IP for admin access: %v", externalIP)

kubetest/util.go

+26
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,32 @@ func init() {
3737
httpTransport.RegisterProtocol("file", http.NewFileTransport(http.Dir("/")))
3838
}
3939

40+
// Essentially curl url | writer including request headers
41+
func httpReadWithHeaders(url string, headers map[string]string, writer io.Writer) error {
42+
log.Printf("curl %s", url)
43+
c := &http.Client{Transport: httpTransport}
44+
req, err := http.NewRequest("GET", url, nil)
45+
if err != nil {
46+
return err
47+
}
48+
for k, v := range headers {
49+
req.Header.Add(k, v)
50+
}
51+
r, err := c.Do(req)
52+
if err != nil {
53+
return err
54+
}
55+
defer r.Body.Close()
56+
if r.StatusCode >= 400 {
57+
return fmt.Errorf("%v returned %d", url, r.StatusCode)
58+
}
59+
_, err = io.Copy(writer, r.Body)
60+
if err != nil {
61+
return err
62+
}
63+
return nil
64+
}
65+
4066
// Essentially curl url | writer
4167
func httpRead(url string, writer io.Writer) error {
4268
log.Printf("curl %s", url)

0 commit comments

Comments
 (0)