Skip to content

Commit 1b62ef7

Browse files
author
Abby Deng
committed
use DNS for ICAT datacenters
1 parent 02b4b2c commit 1b62ef7

3 files changed

Lines changed: 69 additions & 19 deletions

File tree

internal/crusoe/nfs_flag.go

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,31 @@ import (
99
"net/http"
1010
)
1111

12-
const nfsFlagRouteTemplate = "%s/projects/%s/storage/nfs/is-using-nfs"
12+
const (
13+
nfsFlagRouteTemplate = "%s/projects/%s/storage/nfs/is-using-nfs"
14+
vastUseSecondaryClusterFlagRouteTemplate = "%s/projects/%s/storage/nfs/vast-use-secondary-cluster"
15+
)
1316

1417
var (
15-
errCreateNfsFlagRequest = errors.New("failed to create NFS flag request")
16-
errGetNfsFlag = errors.New("failed to get NFS flag")
17-
errReadNfsResponse = errors.New("failed to read NFS flag response")
18-
errUnmarshalNfsFlag = errors.New("failed to unmarshal NFS flag response")
18+
errCreateFlagRequest = errors.New("failed to create flag request")
19+
errGetFlag = errors.New("failed to get flag")
20+
errReadFlagResponse = errors.New("failed to read flag response")
21+
errUnmarshalFlag = errors.New("failed to unmarshal flag response")
1922
)
2023

2124
type NfsFlagResponse struct {
2225
Status bool `json:"status"`
2326
}
2427

25-
// GetNFSFlag returns true if the project has NFS enabled.
26-
func GetNFSFlag(ctx context.Context, crusoeHTTPClient *http.Client, apiEndpoint, projectID string) (bool, error) {
27-
nfsFlagRoute := fmt.Sprintf(nfsFlagRouteTemplate, apiEndpoint, projectID)
28-
req, err := http.NewRequestWithContext(ctx, http.MethodGet, nfsFlagRoute, http.NoBody)
28+
// getFlag is a helper function to fetch a boolean flag from the API.
29+
func getFlag(ctx context.Context, crusoeHTTPClient *http.Client, flagRoute string) (bool, error) {
30+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, flagRoute, http.NoBody)
2931
if err != nil {
30-
return false, fmt.Errorf("%w: %w", errCreateNfsFlagRequest, err)
32+
return false, fmt.Errorf("%w: %w", errCreateFlagRequest, err)
3133
}
3234
resp, err := crusoeHTTPClient.Do(req)
3335
if err != nil {
34-
return false, fmt.Errorf("%w: %w", errGetNfsFlag, err)
36+
return false, fmt.Errorf("%w: %w", errGetFlag, err)
3537
}
3638

3739
if resp != nil && resp.Body != nil {
@@ -40,15 +42,33 @@ func GetNFSFlag(ctx context.Context, crusoeHTTPClient *http.Client, apiEndpoint,
4042

4143
bodyBytes, err := io.ReadAll(resp.Body)
4244
if err != nil {
43-
return false, fmt.Errorf("%w: %w", errReadNfsResponse, err)
45+
return false, fmt.Errorf("%w: %w", errReadFlagResponse, err)
4446
}
4547

46-
var nfsFlag NfsFlagResponse
48+
var flagResponse NfsFlagResponse
4749

48-
unmarshalErr := json.Unmarshal(bodyBytes, &nfsFlag)
50+
unmarshalErr := json.Unmarshal(bodyBytes, &flagResponse)
4951
if unmarshalErr != nil {
50-
return false, fmt.Errorf("%w: %w", errUnmarshalNfsFlag, unmarshalErr)
52+
return false, fmt.Errorf("%w: %w", errUnmarshalFlag, unmarshalErr)
5153
}
5254

53-
return nfsFlag.Status, nil
55+
return flagResponse.Status, nil
56+
}
57+
58+
// GetNFSFlag returns true if the project has NFS enabled.
59+
func GetNFSFlag(ctx context.Context, crusoeHTTPClient *http.Client, apiEndpoint, projectID string) (bool, error) {
60+
nfsFlagRoute := fmt.Sprintf(nfsFlagRouteTemplate, apiEndpoint, projectID)
61+
62+
return getFlag(ctx, crusoeHTTPClient, nfsFlagRoute)
63+
}
64+
65+
// GetVastUseSecondaryClusterFlag returns true if the project has the vast-use-secondary-cluster flag enabled.
66+
func GetVastUseSecondaryClusterFlag(
67+
ctx context.Context,
68+
crusoeHTTPClient *http.Client,
69+
apiEndpoint, projectID string,
70+
) (bool, error) {
71+
flagRoute := fmt.Sprintf(vastUseSecondaryClusterFlagRouteTemplate, apiEndpoint, projectID)
72+
73+
return getFlag(ctx, crusoeHTTPClient, flagRoute)
5474
}

internal/node/fs/fs_node.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ import (
1616
"k8s.io/mount-utils"
1717
)
1818

19+
const (
20+
crusoeCloudDNSNFSHost = "nfs.crusoecloudcompute.com"
21+
icatLocation = "icat-m"
22+
dnsRemotePorts = "dns"
23+
)
24+
1925
type Node struct {
2026
csi.UnimplementedNodeServer
2127
CrusoeClient *crusoeapi.APIClient
@@ -65,14 +71,32 @@ func (d *Node) NodePublishVolume(ctx context.Context, request *csi.NodePublishVo
6571
}
6672
klog.Infof("NFS enabled: %v", nfsEnabled)
6773

74+
vastUseSecondaryCluster, err := crusoe.GetVastUseSecondaryClusterFlag(
75+
ctx, d.CrusoeHTTPClient, d.CrusoeAPIEndpoint, d.HostInstance.ProjectId,
76+
)
77+
if err != nil {
78+
klog.Errorf("failed to fetch vast-use-secondary-cluster flag: %s", err)
79+
80+
return nil, status.Errorf(codes.Internal, "failed to fetch vast-use-secondary-cluster flag: %s", err)
81+
}
82+
klog.Infof("vast-use-secondary-cluster enabled: %v", vastUseSecondaryCluster)
83+
6884
var mountOpts []string
6985

7086
if request.GetReadonly() {
7187
// Read-only volumes cannot be written to in any way
7288
mountOpts = append(mountOpts, node.ReadOnlyMountOption)
7389
}
7490

75-
err = nodePublishVolume(d.Mounter, d.Resizer, mountOpts, nfsEnabled, d.NFSRemotePorts, d.NFSHost, request)
91+
// Use DNS for ICAT locations instead of IP-based NFSHost if feature flag is enabled
92+
nfsHost := d.NFSHost
93+
nfsRemotePorts := d.NFSRemotePorts
94+
if vastUseSecondaryCluster && d.HostInstance.Location == icatLocation {
95+
nfsHost = crusoeCloudDNSNFSHost
96+
nfsRemotePorts = dnsRemotePorts
97+
}
98+
99+
err = nodePublishVolume(d.Mounter, d.Resizer, mountOpts, nfsEnabled, nfsRemotePorts, nfsHost, request)
76100
if err != nil {
77101
klog.Errorf("failed to publish volume %s: %s", request.GetVolumeId(), err.Error())
78102

internal/node/fs/util.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,19 @@ const (
1717
)
1818

1919
func getNFSMountOpts(nfsRemotePorts string) []string {
20-
return []string{
20+
opts := []string{
2121
"vers=3",
2222
"nconnect=16",
2323
"spread_reads",
2424
"spread_writes",
25-
fmt.Sprintf("remoteports=%s", nfsRemotePorts),
2625
}
26+
27+
// Only add remoteports if specified
28+
if nfsRemotePorts != "" {
29+
opts = append(opts, fmt.Sprintf("remoteports=%s", nfsRemotePorts))
30+
}
31+
32+
return opts
2733
}
2834

2935
func supportsFS(instance *crusoeapi.InstanceV1Alpha5) bool {

0 commit comments

Comments
 (0)