Skip to content

Use metadata golang package instead of raw HTTP #2123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 7 additions & 40 deletions pkg/mount-manager/safe-mounter-v1_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,10 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"

diskapi "github.com/kubernetes-csi/csi-proxy/client/api/disk/v1"
diskclient "github.com/kubernetes-csi/csi-proxy/client/groups/disk/v1"
Expand All @@ -38,20 +35,11 @@ import (
volumeapi "github.com/kubernetes-csi/csi-proxy/client/api/volume/v1"
volumeclient "github.com/kubernetes-csi/csi-proxy/client/groups/volume/v1"

"cloud.google.com/go/compute/metadata"
"k8s.io/klog/v2"
mount "k8s.io/mount-utils"
)

// GoogleCloudDisk represents a disk from Google Cloud metadata
type GoogleCloudDisk struct {
DeviceName string `json:"deviceName"`
Index int `json:"index"`
Interface string `json:"interface"`
Mode string `json:"mode"`
NvmeNamespaceIdentifier uint64 `json:"nvmeNamespaceIdentifier"`
Type string `json:"type"`
}

// CSIProxyMounterV1 is the mounter implementation that uses the v1 API
type CSIProxyMounterV1 struct {
FsClient *fsclient.Client
Expand Down Expand Up @@ -197,7 +185,7 @@ func (mounter *CSIProxyMounterV1) Unmount(target string) error {

func (mounter *CSIProxyMounterV1) GetDiskNumber(deviceName string, partition string, volumeKey string) (string, error) {
// First, get Google Cloud metadata to find the nvmeNamespaceIdentifier for this device
googleDisks, err := mounter.getGoogleCloudDisks()
googleDisks, err := attachedDisks()
if err != nil {
klog.V(4).Infof("Failed to get Google Cloud metadata, falling back to legacy method: %v", err)
return mounter.getDiskNumberLegacy(deviceName)
Expand Down Expand Up @@ -349,35 +337,14 @@ func (mounter *CSIProxyMounterV1) convertEUIToDecimal(euiValue string) (uint64,
}

// Helper function to get Google Cloud metadata
func (mounter *CSIProxyMounterV1) getGoogleCloudDisks() ([]GoogleCloudDisk, error) {
client := &http.Client{
Timeout: 10 * time.Second,
}

req, err := http.NewRequest("GET", "http://metadata.google.internal/computeMetadata/v1/instance/disks/?recursive=true", nil)
if err != nil {
return nil, fmt.Errorf("failed to create request: %v", err)
}

req.Header.Set("Metadata-Flavor", "Google")

resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to call metadata service: %v", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("metadata service returned status %d", resp.StatusCode)
}

body, err := io.ReadAll(resp.Body)
func (mounter *CSIProxyMounterV1) getGoogleCloudDisks() ([]googleCloudDisk, error) {
disksResp, err := metadata.GetWithContext(context.Background(), "instance/disks/?recursive=true")
if err != nil {
return nil, fmt.Errorf("failed to read response body: %v", err)
return nil, fmt.Errorf("failed to get disks using metadata package: %v", err)
}

var disks []GoogleCloudDisk
if err := json.Unmarshal(body, &disks); err != nil {
var disks []googleCloudDisk
if err := json.Unmarshal([]byte(disksResp), &disks); err != nil {
return nil, fmt.Errorf("failed to parse JSON response: %v", err)
}

Expand Down
33 changes: 5 additions & 28 deletions pkg/mount-manager/safe-mounter-v1beta_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"

"cloud.google.com/go/compute/metadata"
diskapi "github.com/kubernetes-csi/csi-proxy/client/api/disk/v1beta2"
diskclient "github.com/kubernetes-csi/csi-proxy/client/groups/disk/v1beta2"

Expand Down Expand Up @@ -202,7 +200,7 @@ func (mounter *CSIProxyMounterV1Beta) Unmount(target string) error {

func (mounter *CSIProxyMounterV1Beta) GetDiskNumber(deviceName string, partition string, volumeKey string) (string, error) {
// First, get Google Cloud metadata to find the nvmeNamespaceIdentifier for this device
googleDisks, err := mounter.getGoogleCloudDisks()
googleDisks, err := attachedDisks()
if err != nil {
klog.V(4).Infof("Failed to get Google Cloud metadata, falling back to legacy method: %v", err)
return mounter.getDiskNumberLegacy(deviceName)
Expand Down Expand Up @@ -358,34 +356,13 @@ func (mounter *CSIProxyMounterV1Beta) convertEUIToDecimal(euiValue string) (uint

// Helper function to get Google Cloud metadata
func (mounter *CSIProxyMounterV1Beta) getGoogleCloudDisks() ([]GoogleCloudDiskBeta, error) {
client := &http.Client{
Timeout: 10 * time.Second,
}

req, err := http.NewRequest("GET", "http://metadata.google.internal/computeMetadata/v1/instance/disks/?recursive=true", nil)
if err != nil {
return nil, fmt.Errorf("failed to create request: %v", err)
}

req.Header.Set("Metadata-Flavor", "Google")

resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to call metadata service: %v", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("metadata service returned status %d", resp.StatusCode)
}

body, err := io.ReadAll(resp.Body)
disksResponse, err := metadata.GetWithContext(context.Background(), "instance/disks/?recursive=true")
if err != nil {
return nil, fmt.Errorf("failed to read response body: %v", err)
return nil, fmt.Errorf("failed to get disks using metadata package: %v", err)
}

var disks []GoogleCloudDiskBeta
if err := json.Unmarshal(body, &disks); err != nil {
if err := json.Unmarshal([]byte(disksResponse), &disks); err != nil {
return nil, fmt.Errorf("failed to parse JSON response: %v", err)
}

Expand Down
30 changes: 30 additions & 0 deletions pkg/mount-manager/safe-mounter_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ limitations under the License.
package mountmanager

import (
"context"
"encoding/json"
"fmt"
"time"

"cloud.google.com/go/compute/metadata"
"k8s.io/klog/v2"
"k8s.io/mount-utils"
utilexec "k8s.io/utils/exec"
Expand Down Expand Up @@ -82,3 +86,29 @@ func NewSafeMounter(int, time.Duration) (*mount.SafeFormatAndMount, error) {
klog.V(4).Infof("failed to connect to csi-proxy v1beta with error=%v", err.Error())
return nil, err
}

type googleCloudDisk struct {
DeviceName string `json:"deviceName"`
Index int `json:"index"`
Interface string `json:"interface"`
Mode string `json:"mode"`
NvmeNamespaceIdentifier uint64 `json:"nvmeNamespaceIdentifier"`
Type string `json:"type"`
}

// attachedDisks returns the list of disks attached to the instance from which
// the metadata server is called.
func attachedDisks() ([]googleCloudDisk, error) {
disksResp, err := metadata.GetWithContext(context.Background(), "instance/disks/?recursive=true")
if err != nil {
return nil, fmt.Errorf("failed to get disks using metadata package: %v", err)
}

var disks []googleCloudDisk
if err := json.Unmarshal([]byte(disksResp), &disks); err != nil {
return nil, fmt.Errorf("failed to parse JSON response: %v", err)
}

klog.V(4).Infof("Retrieved %d disks from Google Cloud metadata", len(disks))
return disks, nil
}