Skip to content

Commit

Permalink
OKE-27386: Remove IMDS V1 Fallback Logic
Browse files Browse the repository at this point in the history
  • Loading branch information
l-technicore authored and YashwantGohokar committed Oct 2, 2023
1 parent 7cf894d commit e43e445
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 53 deletions.
19 changes: 4 additions & 15 deletions pkg/oci/instance/metadata/instance_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"encoding/json"
"go.uber.org/zap"
"net/http"
"strings"
"time"

"github.com/pkg/errors"
Expand Down Expand Up @@ -69,27 +68,17 @@ func (m *metadataGetter) Get() (*InstanceMetadata, error) {
func (m *metadataGetter) executeRequest(req *http.Request) (*InstanceMetadata, error) {
req.Header.Add("Authorization", "Bearer Oracle")
resp, err := m.client.Do(req)
if err != nil || resp.StatusCode != http.StatusOK {
zap.S().With(zap.Error(err)).Warn("Failed to get instance metadata with endpoint v2. Falling back to v1.")
if resp != nil {
v2resp := resp
defer v2resp.Body.Close()
}
v1Req := *req
v1Path := strings.Replace(req.URL.Path, "/opc/v2", "/opc/v1", 1)
v1Req.URL.Path = v1Path
resp, err = m.client.Do(&v1Req)
if err != nil {
return nil, errors.Wrap(err, "Failed to get instance metadata with v1 endpoint after falling back from v2 endpoint")
}
if err != nil {
zap.S().With(zap.Error(err)).Warn("Failed to get instance metadata with v2 endpoint.")
return nil, errors.Wrap(err, "Failed to get instance metadata with v2 endpoint")
}

zap.S().Infof("Metadata endpoint %s returned response successfully", req.URL.Path)

if resp != nil {
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, errors.Errorf("metadata endpoint v1 returned status %d; expected 200 OK", resp.StatusCode)
return nil, errors.Errorf("metadata endpoint v2 returned status %d; expected 200 OK", resp.StatusCode)
}
}
md := &InstanceMetadata{}
Expand Down
40 changes: 2 additions & 38 deletions pkg/oci/instance/metadata/instance_metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,6 @@ func TestGetMetadata(t *testing.T) {
expected Result
handlerFunc http.HandlerFunc
}{
{
name: "metadata v1 response returned successfully",
endpoint: "opc/v1/instance",
expected: Result{
metadata: &InstanceMetadata{
CompartmentID: "ocid1.compartment.oc1..abc",
Region: "phx",
CanonicalRegionName: "us-phoenix-1",
},
err: "",
},
handlerFunc: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, exampleResponse)
}),
},
{
name: "metadata v2 response returned successfully",
endpoint: "opc/v2/instance",
Expand All @@ -82,36 +67,15 @@ func TestGetMetadata(t *testing.T) {
}),
},
{
name: "metadata v1 and v2 response returned error",
name: "metadata v2 response returned error",
endpoint: "opc/v2/instance",
expected: Result{
metadata: nil,
err: fmt.Sprintf("metadata endpoint v1 returned status %d; expected 200 OK", 404),
},
handlerFunc: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.Contains(r.URL.Path, "opc/v2") {
w.WriteHeader(404)
} else if strings.Contains(r.URL.Path, "opc/v1") {
w.WriteHeader(404)
}
}),
},
{
name: "metadata v2 response returned error but v1 success",
endpoint: "opc/v2/instance",
expected: Result{
metadata: &InstanceMetadata{
CompartmentID: "ocid1.compartment.oc1..abc",
Region: "phx",
CanonicalRegionName: "us-phoenix-1",
},
err: "",
err: fmt.Sprintf("metadata endpoint v2 returned status %d; expected 200 OK", 404),
},
handlerFunc: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.Contains(r.URL.Path, "opc/v2") {
w.WriteHeader(404)
} else if strings.Contains(r.URL.Path, "opc/v1") {
fmt.Fprint(w, exampleResponse)
}
}),
},
Expand Down

0 comments on commit e43e445

Please sign in to comment.