|
| 1 | +/* |
| 2 | + * Copyright 2026 NVIDIA CORPORATION |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package nscale |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "encoding/json" |
| 11 | + "fmt" |
| 12 | + "net/http" |
| 13 | + "strconv" |
| 14 | + |
| 15 | + "github.com/NVIDIA/topograph/internal/config" |
| 16 | + "github.com/NVIDIA/topograph/internal/httperr" |
| 17 | + "github.com/NVIDIA/topograph/internal/httpreq" |
| 18 | + "github.com/NVIDIA/topograph/pkg/providers" |
| 19 | + "github.com/NVIDIA/topograph/pkg/topology" |
| 20 | +) |
| 21 | + |
| 22 | +const ( |
| 23 | + NAME = "nscale" |
| 24 | + |
| 25 | + urlTopologyPath = "/v1/topology" |
| 26 | +) |
| 27 | + |
| 28 | +type baseProvider struct { |
| 29 | + params *ProviderParams |
| 30 | + client Client |
| 31 | +} |
| 32 | + |
| 33 | +type ProviderParams struct { |
| 34 | + BaseURL string `mapstructure:"baseUrl"` |
| 35 | + Region string `mapstructure:"region"` |
| 36 | + TrimTiers int `mapstructure:"trimTiers"` |
| 37 | +} |
| 38 | + |
| 39 | +type Credentials struct { |
| 40 | + Org string `mapstructure:"org"` |
| 41 | + Token string `mapstructure:"token"` |
| 42 | +} |
| 43 | + |
| 44 | +type Client interface { |
| 45 | + Topology(context.Context, string, int, int) ([]InstanceTopology, error) |
| 46 | +} |
| 47 | + |
| 48 | +// nscaleClient is a Topology API client. |
| 49 | +type nscaleClient struct { |
| 50 | + baseURL string |
| 51 | + org string |
| 52 | + token string |
| 53 | +} |
| 54 | + |
| 55 | +// InstanceTopology represents the topology of a single instance. |
| 56 | +type InstanceTopology struct { |
| 57 | + ID string `json:"instance_id"` |
| 58 | + NetworkPath []string `json:"network_node_path"` |
| 59 | + BlockID *string `json:"block_id,omitempty"` |
| 60 | +} |
| 61 | + |
| 62 | +func (c *nscaleClient) Topology(ctx context.Context, region string, pageSize, offset int) ([]InstanceTopology, error) { |
| 63 | + headers := map[string]string{ |
| 64 | + "Authorization": "Bearer " + c.token, |
| 65 | + "X-Organization": c.org, |
| 66 | + "X-Region": region, |
| 67 | + } |
| 68 | + query := map[string]string{ |
| 69 | + "limit": strconv.Itoa(pageSize), |
| 70 | + "offset": strconv.Itoa(offset), |
| 71 | + } |
| 72 | + f := httpreq.GetRequestFunc(ctx, http.MethodGet, headers, query, nil, c.baseURL, urlTopologyPath) |
| 73 | + |
| 74 | + body, httpErr := httpreq.DoRequestWithRetries(f, false) |
| 75 | + if httpErr != nil { |
| 76 | + return nil, httpErr |
| 77 | + } |
| 78 | + |
| 79 | + resp := []InstanceTopology{} |
| 80 | + if err := json.Unmarshal(body, &resp); err != nil { |
| 81 | + return nil, httperr.NewError(http.StatusBadGateway, err.Error()) |
| 82 | + } |
| 83 | + |
| 84 | + return resp, nil |
| 85 | +} |
| 86 | + |
| 87 | +type Provider struct { |
| 88 | + baseProvider |
| 89 | +} |
| 90 | + |
| 91 | +func NamedLoader() (string, providers.Loader) { |
| 92 | + return NAME, Loader |
| 93 | +} |
| 94 | + |
| 95 | +func Loader(ctx context.Context, config providers.Config) (providers.Provider, *httperr.Error) { |
| 96 | + params, err := getParams(config.Params) |
| 97 | + if err != nil { |
| 98 | + return nil, httperr.NewError(http.StatusBadRequest, err.Error()) |
| 99 | + } |
| 100 | + |
| 101 | + creds, err := getCreds(config.Creds) |
| 102 | + if err != nil { |
| 103 | + return nil, httperr.NewError(http.StatusBadRequest, err.Error()) |
| 104 | + } |
| 105 | + |
| 106 | + return &Provider{ |
| 107 | + baseProvider: baseProvider{ |
| 108 | + client: &nscaleClient{ |
| 109 | + baseURL: params.BaseURL, |
| 110 | + org: creds.Org, |
| 111 | + token: creds.Token, |
| 112 | + }, |
| 113 | + params: params, |
| 114 | + }, |
| 115 | + }, nil |
| 116 | +} |
| 117 | + |
| 118 | +func getParams(params map[string]any) (*ProviderParams, error) { |
| 119 | + p := &ProviderParams{} |
| 120 | + if err := config.Decode(params, p); err != nil { |
| 121 | + return nil, fmt.Errorf("failed to decode params: %v", err) |
| 122 | + } |
| 123 | + if len(p.BaseURL) == 0 { |
| 124 | + return nil, fmt.Errorf("missing 'baseUrl'") |
| 125 | + } |
| 126 | + |
| 127 | + return p, nil |
| 128 | +} |
| 129 | + |
| 130 | +func getCreds(creds map[string]any) (*Credentials, error) { |
| 131 | + c := &Credentials{} |
| 132 | + if err := config.Decode(creds, c); err != nil { |
| 133 | + return nil, fmt.Errorf("failed to decode creds: %v", err) |
| 134 | + } |
| 135 | + if len(c.Org) == 0 { |
| 136 | + return nil, fmt.Errorf("missing 'org'") |
| 137 | + } |
| 138 | + if len(c.Token) == 0 { |
| 139 | + return nil, fmt.Errorf("missing 'token'") |
| 140 | + } |
| 141 | + |
| 142 | + return c, nil |
| 143 | +} |
| 144 | + |
| 145 | +func (p *baseProvider) GenerateTopologyConfig(ctx context.Context, pageSize *int, instances []topology.ComputeInstances) (*topology.Graph, *httperr.Error) { |
| 146 | + topo, err := p.generateInstanceTopology(ctx, pageSize, instances) |
| 147 | + if err != nil { |
| 148 | + return nil, err |
| 149 | + } |
| 150 | + |
| 151 | + return topo.ToThreeTierGraph(NAME, instances, p.params.TrimTiers, false), nil |
| 152 | +} |
| 153 | + |
| 154 | +// Instances2NodeMap implements slurm.instanceMapper |
| 155 | +func (p *Provider) Instances2NodeMap(ctx context.Context, nodes []string) (map[string]string, error) { |
| 156 | + i2n := make(map[string]string) |
| 157 | + for _, node := range nodes { |
| 158 | + i2n[node] = node |
| 159 | + } |
| 160 | + |
| 161 | + return i2n, nil |
| 162 | +} |
| 163 | + |
| 164 | +// GetInstancesRegions implements slurm.instanceMapper |
| 165 | +func (p *Provider) GetInstancesRegions(ctx context.Context, nodes []string) (map[string]string, error) { |
| 166 | + res := make(map[string]string) |
| 167 | + for _, node := range nodes { |
| 168 | + res[node] = p.params.Region |
| 169 | + } |
| 170 | + |
| 171 | + return res, nil |
| 172 | +} |
0 commit comments