Skip to content
Merged
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
121 changes: 107 additions & 14 deletions api/serverless/openapi.yaml

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions api/serverless/overlay.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,14 @@ actions:
# this action once every deployed API sends servingWorkers.
- target: $.components.schemas.AppRuntime.required[?(@ == 'servingWorkers')]
remove: true
# The same for an app's regions: required, they would print as null from an
# API that does not send them. Remove this action once every deployed API
# sends regions.
- target: $.components.schemas.App.required[?(@ == 'regions')]
remove: true
# The same for the Shared Volume settings' usage: required, it would decode
# as zero stored bytes from an API that does not send it, and 'volumes
# usage' would print that zero as the organization's usage. Remove this
# action once every deployed API sends usage.
- target: $.components.schemas.VolumeSettings.required[?(@ == 'usage')]
remove: true
262 changes: 241 additions & 21 deletions internal/api/serverless/gen/client.gen.go

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions internal/api/serverless/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ type VolumeFile = gen.VolumeFile
// file list it was read from.
type VolumeFilePage = gen.VolumeFilePage

// VolumeSettings are the organization's Shared Volume access and effective
// limits.
// VolumeSettings are the organization's Shared Volume access, effective
// limits and stored-byte usage.
type VolumeSettings = gen.VolumeSettings

// GetVolumeReferencesParams are the query parameters for getVolumeReferences.
Expand Down Expand Up @@ -251,9 +251,9 @@ func (c *Client) ListVolumeFiles(ctx context.Context, name string, params ListVo
return resp.JSON200, nil
}

// GetVolumeSettings returns the organization's Shared Volume access and
// effective limits. Settings belong to the organization, so the request
// carries no environment.
// GetVolumeSettings returns the organization's Shared Volume access,
// effective limits and stored-byte usage. Settings belong to the
// organization, so the request carries no environment.
func (c *Client) GetVolumeSettings(ctx context.Context) (*VolumeSettings, error) {
if c.apiKey == "" {
return nil, transport.ErrNoAPIKey
Expand Down
41 changes: 41 additions & 0 deletions internal/cmd/serverless/display_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,47 @@ func TestWorkerResult_NilOptionalFields(t *testing.T) {
}
}

// appRegionsCase is one app response for TestAppResultKeepsOmittedRegionsOmitted.
type appRegionsCase struct {
name string
// regions is the regions member appended to the app's JSON, or empty to
// omit it.
regions string
// want is what the output holds exactly when regions is sent.
want string
}

// An API that does not send an app's regions yet must not print them as null in
// JSON and YAML; one that sends them keeps them.
func TestAppResultKeepsOmittedRegionsOmitted(t *testing.T) {
for _, tc := range []appRegionsCase{
{
name: "omitted",
regions: ``,
want: `"regions"`,
},
{
name: "sent",
regions: `,"regions":["global"]`,
want: `"regions":["global"]`,
},
} {
t.Run(tc.name, func(t *testing.T) {
var app serverlessapi.App
if err := json.Unmarshal([]byte(`{"appId":"`+testAppID+`"`+tc.regions+`}`), &app); err != nil {
t.Fatalf("decode: %v", err)
}
out, err := json.Marshal(appResult(app))
if err != nil {
t.Fatalf("encode: %v", err)
}
if got, sent := strings.Contains(string(out), tc.want), tc.regions != ""; got != sent {
t.Fatalf("output = %s, want %s printed: %v", out, tc.want, sent)
}
})
}
}

// An API that does not send servingWorkers yet must not print a zero count in
// JSON and YAML; one that sends zero keeps it.
func TestAppResultKeepsAnOmittedServingWorkersOmitted(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/serverless/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func newVolumesCmd(logger *log.Logger) *cobra.Command {
newVolumesFilesCmd(logger),
newVolumesPutCmd(logger),
newVolumesSettingsCmd(logger),
newVolumesUsageCmd(logger),
)
return cmd
}
Expand Down
35 changes: 34 additions & 1 deletion internal/cmd/serverless/volumes_display.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const (
colDeleted = "Deleted"
colLostRollback = "Lost rollback"
colAvailable = "Enabled"
colStored = "Stored"
colFree = "Free"
colMaxStored = "Max stored"
colMaxFileSize = "Max file size"
colMaxFiles = "Max files per Volume"
Expand Down Expand Up @@ -219,6 +221,8 @@ func (r volumeDeletedResult) Rows() [][]any {
}

// volumeSettingsResult is the organization's Shared Volume access and limits.
// JSON and YAML print the API's settings without the usage, which 'volumes
// usage' shows.
type volumeSettingsResult serverlessapi.VolumeSettings

func (r volumeSettingsResult) Headers() []string {
Expand All @@ -238,5 +242,34 @@ func (r volumeSettingsResult) Rows() [][]any {
}

func (r volumeSettingsResult) MarshalJSON() ([]byte, error) {
return json.Marshal(serverlessapi.VolumeSettings(r))
settings := serverlessapi.VolumeSettings(r)
settings.Usage = nil
return json.Marshal(settings)
}

// volumeUsageResult is the organization's Shared Volume storage against its
// limit.
type volumeUsageResult struct {
StoredBytes int64 `json:"storedBytes"`
MaxStoredBytes int64 `json:"maxStoredBytes"`
}

func (r volumeUsageResult) Headers() []string {
return []string{colField, colValue}
}

// Rows add the free bytes, so a table reader near the limit sees the headroom
// that the two rounded sizes hide. JSON and YAML keep the exact bytes instead.
func (r volumeUsageResult) Rows() [][]any {
return [][]any{
{colStored, humanBytes(r.StoredBytes)},
Comment thread
emmmile marked this conversation as resolved.
Resolved
{colMaxStored, humanBytes(r.MaxStoredBytes)},
{colFree, humanBytes(r.freeBytes())},
}
}

// freeBytes is the limit minus the stored bytes, or zero when the stored bytes
// reach or pass the limit (the API reads the two separately).
func (r volumeUsageResult) freeBytes() int64 {
return max(r.MaxStoredBytes-r.StoredBytes, 0)
}
70 changes: 68 additions & 2 deletions internal/cmd/serverless/volumes_manage.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,25 @@ const (

var errNothingToUpdateOnVolume = errors.New("nothing to update; pass --description <text> or --clear description")

// errVolumesNotEnabled makes 'volumes settings' exit non-zero, after printing
// the settings, for an organization without Shared Volumes.
// errVolumeUsageMissing is returned when the settings the API answered with
// carry no usage for an organization with Shared Volumes, so 'volumes usage'
// never prints zero stored bytes for it.
var errVolumeUsageMissing = errors.New("the API did not report the organization's stored bytes")
Comment thread
emmmile marked this conversation as resolved.
Resolved
Comment thread
emmmile marked this conversation as resolved.
Resolved

// volumeUsageNote says why the Volume sizes 'volumes list' shows do not add up
// to Stored, and why an upload that fits in Free can still be refused. The
// usage table prints it on stderr, and the usage help holds it, so each rule is
// written once.
const volumeUsageNote = `A file counts once however many Volumes, apps or environments use it, so
adding up the sizes 'volumes list' shows can give more than Stored. That list
also covers one environment, and Stored covers all of them.

An upload can be refused even when it fits in Free: content that upload
sessions hold, open or not yet cleaned up, counts against the limit too.`

// errVolumesNotEnabled makes 'volumes settings' and 'volumes usage' exit
// non-zero, after printing what they can, for an organization without Shared
// Volumes.
var errVolumesNotEnabled = errors.New("this organization does not have Shared Volumes enabled")

// volumesHelpLong is the help of the volumes group.
Expand Down Expand Up @@ -495,6 +512,55 @@ func newVolumesSettingsCmd(logger *log.Logger) *cobra.Command {
}
}

func newVolumesUsageCmd(logger *log.Logger) *cobra.Command {
return &cobra.Command{
Use: "usage",
Short: "Show the organization's Shared Volume storage against its limit",
Long: `Show the bytes the organization's Volumes store (Stored), against its
stored-byte limit (Max stored). The table adds what is left (Free); JSON and
YAML carry the exact bytes instead.

` + volumeUsageNote + `
Comment thread
emmmile marked this conversation as resolved.
Resolved

Like 'volumes settings', it exits non-zero when Shared Volumes are not enabled
for the organization, after printing the usage when the API reports it.`,
Example: ` runware serverless volumes usage
runware serverless volumes usage --format json`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
spin := cmdutil.NewSpinner("Fetching Shared Volume usage...")
spin.Start()

settings, err := newServerlessClient(cmd, logger).GetVolumeSettings(cmd.Context())
spin.Stop()
if err != nil {
return err
}
if settings.Usage == nil {
if !settings.Available {
return errVolumesNotEnabled
}
return errVolumeUsageMissing
}

format := cmdutil.FormatFor(cmd)
if err := output.Print(format, volumeUsageResult{
StoredBytes: settings.Usage.StoredBytes,
MaxStoredBytes: settings.Limits.MaxStoredBytes,
}); err != nil {
return err
}
if format == output.FormatTable {
_, _ = fmt.Fprintln(cmd.ErrOrStderr(), volumeUsageNote)
}
if !settings.Available {
return errVolumesNotEnabled
}
return nil
},
}
}

// volumeNameArg takes one argument, a Volume name the API accepts.
func volumeNameArg(cmd *cobra.Command, args []string) error {
if err := cobra.ExactArgs(1)(cmd, args); err != nil {
Expand Down
Loading
Loading