Skip to content
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
2 changes: 1 addition & 1 deletion gateway/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ require (
golang.org/x/crypto v0.58.0 // indirect
golang.org/x/net v0.62.0 // indirect
golang.org/x/sync v0.24.0 // indirect
golang.org/x/sys v0.49.0 // indirect
golang.org/x/sys v0.49.0
golang.org/x/text v0.43.0 // indirect
golang.org/x/time v0.16.0 // indirect
)
Expand Down
15 changes: 11 additions & 4 deletions gateway/internal/buckets/buckets.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
// (README.md "Why bucket-per-volume"), which only the
// operator can provision, so CreateBucket creates the S3Bucket and waits for
// its volume to be mounted under the gateway's root, and DeleteBucket deletes
// the S3Bucket and waits for it to be gone. Every other call goes to the
// posix backend unchanged.
// the S3Bucket and waits for it to be gone. PutObject, CopyObject, and
// CompleteMultipartUpload go to the posix backend with their errors
// reclassified (nospace.go). Every other call goes to the posix backend
// unchanged.

package buckets

Expand Down Expand Up @@ -76,18 +78,23 @@ type Settings struct {
PollInterval time.Duration
}

// Backend is the posix backend with CreateBucket and DeleteBucket replaced.
// Backend is the posix backend with CreateBucket and DeleteBucket replaced
// and the write errors of a full volume corrected.
type Backend struct {
backend.Backend
client client.Client
settings Settings

// statfs reports a path's available and total bytes. A field so a test
// can stand in for a full volume.
statfs func(path string) (avail, total uint64, err error)
}

// New wraps be. The client needs get, create, and delete on S3Buckets in
// settings.Namespace, which the operator grants the gateway's
// ServiceAccount.
func New(be backend.Backend, cli client.Client, settings Settings) *Backend {
return &Backend{Backend: be, client: cli, settings: settings}
return &Backend{Backend: be, client: cli, settings: settings, statfs: statfs}
}

// CreateBucket creates the bucket's S3Bucket, waits until the operator
Expand Down
117 changes: 117 additions & 0 deletions gateway/internal/buckets/nospace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Reclassifies the ExistingObjectIsDirectory error versitygw's posix backend
// returns for any failed write, so a full bucket volume answers 507
// InsufficientStorage. It lives beside Backend because the gateway's own
// wrapper of the posix backend is the only place the error can be corrected
// without patching versitygw.
//
// versitygw v1.9.0 maps every error from linking a finished object into place
// to ExistingObjectIsDirectory (backend/posix/posix.go, PutObjectWithPostFunc
// and CompleteMultipartUploadWithCopy). On a pNFS mount a full volume surfaces
// exactly there: write() only fills the client's page cache, and the server's
// ENOSPC comes back from the fsync and close inside the link. The errno is
// discarded, so the cause is re-derived from the filesystem instead.

package buckets

import (
"context"
"errors"
"os"
"path/filepath"
"strings"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/versity/versitygw/s3err"
"github.com/versity/versitygw/s3response"
"golang.org/x/sys/unix"
)

// lowSpaceDivisor sets the free-space floor for a write whose size is not
// known up front: a volume with less than 1/lowSpaceDivisor of its capacity
// left counts as full.
const lowSpaceDivisor = 100

// PutObject is the posix backend's PutObject with a full volume reported as
// InsufficientStorage.
func (b *Backend) PutObject(ctx context.Context, input s3response.PutObjectInput) (s3response.PutObjectOutput, error) {
out, err := b.Backend.PutObject(ctx, input)
return out, b.classifyWriteErr(err, aws.ToString(input.Bucket), aws.ToString(input.Key), aws.ToInt64(input.ContentLength))
}

// CopyObject is the posix backend's CopyObject with a full volume reported as
// InsufficientStorage. The posix backend's CopyObject calls its own PutObject,
// not this Backend's, so it needs the same correction.
func (b *Backend) CopyObject(ctx context.Context, input s3response.CopyObjectInput) (s3response.CopyObjectOutput, error) {
out, err := b.Backend.CopyObject(ctx, input)
return out, b.classifyWriteErr(err, aws.ToString(input.Bucket), aws.ToString(input.Key), 0)
}

// CompleteMultipartUpload is the posix backend's CompleteMultipartUpload with
// a full volume reported as InsufficientStorage.
func (b *Backend) CompleteMultipartUpload(
ctx context.Context, input *s3.CompleteMultipartUploadInput,
) (s3response.CompleteMultipartUploadResult, string, error) {
out, version, err := b.Backend.CompleteMultipartUpload(ctx, input)
return out, version, b.classifyWriteErr(err, aws.ToString(input.Bucket), aws.ToString(input.Key), 0)
}

// classifyWriteErr returns err unchanged unless it is ExistingObjectIsDirectory
// and the key is not blocked by the filesystem layout. Then it is
// InsufficientStorage when the bucket's volume has less than size bytes, or
// less than 1/lowSpaceDivisor of its capacity, available, and InternalError
// otherwise. The free space is read after the failed write's temporary file
// is removed, so a write that did not fit still finds less than its own size
// free.
func (b *Backend) classifyWriteErr(err error, bucket, key string, size int64) error {
var apiErr s3err.APIError
if !errors.As(err, &apiErr) || apiErr.Code != s3err.GetAPIError(s3err.ErrExistingObjectIsDirectory).Code {
return err
}
bucketDir := filepath.Join(b.settings.RootDir, bucket)
if keyBlocked(bucketDir, key) {
return err
}
avail, total, serr := b.statfs(bucketDir)
if serr != nil {
return internalError("storing the object failed, and the bucket's free space could not be read: " + serr.Error())
}
floor := total / lowSpaceDivisor
if size > 0 && uint64(size) > floor {
floor = uint64(size)
}
if avail < floor {
return s3err.GetAPIError(s3err.ErrNoSpaceLeftOnDevice)
}
return internalError("storing the object failed for a reason the storage backend did not report")
}

// keyBlocked reports whether the layout under bucketDir really prevents key
// from being stored as a file: key itself is a directory, or one of its
// parents exists as something other than a directory.
func keyBlocked(bucketDir, key string) bool {
path := bucketDir
parts := strings.Split(strings.Trim(key, "/"), "/")
for i, part := range parts {
path = filepath.Join(path, part)
fi, err := os.Lstat(path)
if err != nil {
return false
}
last := i == len(parts)-1
if last && fi.IsDir() || !last && !fi.IsDir() {
return true
}
}
return false
}

// statfs is the Backend.statfs default: the filesystem holding path, as the
// unprivileged caller sees it.
func statfs(path string) (avail, total uint64, err error) {
var st unix.Statfs_t
if err := unix.Statfs(path, &st); err != nil {
return 0, 0, err
}
return st.Bavail * uint64(st.Bsize), st.Blocks * uint64(st.Bsize), nil
}
137 changes: 137 additions & 0 deletions gateway/internal/buckets/nospace_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// The writes whose ExistingObjectIsDirectory error Backend reclassifies, over
// a fake inner backend that fails every write the way versitygw's posix
// backend does when the bucket's volume is full, and a stubbed statfs.

package buckets

import (
"context"
"os"
"path/filepath"
"testing"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/versity/versitygw/backend"
"github.com/versity/versitygw/s3err"
"github.com/versity/versitygw/s3response"
)

// fullVolume fails every write with ExistingObjectIsDirectory, which is what
// versitygw v1.9.0's posix backend returns for any error from linking the
// finished object into place, ENOSPC included.
type fullVolume struct {
backend.BackendUnsupported
}

func (fullVolume) PutObject(context.Context, s3response.PutObjectInput) (s3response.PutObjectOutput, error) {
return s3response.PutObjectOutput{}, s3err.GetAPIError(s3err.ErrExistingObjectIsDirectory)
}

func (fullVolume) CopyObject(context.Context, s3response.CopyObjectInput) (s3response.CopyObjectOutput, error) {
return s3response.CopyObjectOutput{}, s3err.GetAPIError(s3err.ErrExistingObjectIsDirectory)
}

func (fullVolume) CompleteMultipartUpload(context.Context, *s3.CompleteMultipartUploadInput) (s3response.CompleteMultipartUploadResult, string, error) {
return s3response.CompleteMultipartUploadResult{}, "", s3err.GetAPIError(s3err.ErrExistingObjectIsDirectory)
}

// newFullVolumeBackend is a Backend over fullVolume whose bucket volume
// reports avail of total bytes free.
func newFullVolumeBackend(t *testing.T, avail, total uint64) (*Backend, string) {
t.Helper()
root := t.TempDir()
if err := os.Mkdir(filepath.Join(root, "reports"), 0o755); err != nil {
t.Fatalf("creating the bucket directory: %v", err)
}
b := New(fullVolume{}, nil, Settings{RootDir: root})
b.statfs = func(string) (uint64, uint64, error) { return avail, total, nil }
return b, root
}

// The three writes, each called against bucket "reports" and key.
var writes = map[string]func(*Backend, string) error{
"PutObject": func(b *Backend, key string) error {
_, err := b.PutObject(context.Background(), s3response.PutObjectInput{
Bucket: aws.String("reports"), Key: aws.String(key), ContentLength: aws.Int64(64 << 20),
})
return err
},
"CopyObject": func(b *Backend, key string) error {
_, err := b.CopyObject(context.Background(), s3response.CopyObjectInput{
Bucket: aws.String("reports"), Key: aws.String(key), CopySource: aws.String("reports/source"),
})
return err
},
"CompleteMultipartUpload": func(b *Backend, key string) error {
_, _, err := b.CompleteMultipartUpload(context.Background(), &s3.CompleteMultipartUploadInput{
Bucket: aws.String("reports"), Key: aws.String(key), UploadId: aws.String("upload"),
})
return err
},
}

// Regression: 2026-11-23-s3-full-bucket-reports-directory. A write into a
// full bucket volume answered 409 ExistingObjectIsDirectory, which no S3
// client treats as out of space, so clients neither stopped nor alerted (CI
// run 45100042373, TC-S3-003). A full volume must answer 507
// InsufficientStorage.
func TestWriteToAFullVolumeIsInsufficientStorage(t *testing.T) {
for name, write := range writes {
t.Run(name, func(t *testing.T) {
b, _ := newFullVolumeBackend(t, 1<<20, 1<<30)
code, status := s3Code(t, write(b, "fill/00014"))
if code != "InsufficientStorage" || status != 507 {
t.Errorf("full volume answered %d %s, want 507 InsufficientStorage", status, code)
}
})
}
}

// A key that really is a directory keeps versitygw's own 409, whatever the
// free space.
func TestWriteOverADirectoryStaysExistingObjectIsDirectory(t *testing.T) {
for name, write := range writes {
t.Run(name, func(t *testing.T) {
b, root := newFullVolumeBackend(t, 1<<20, 1<<30)
if err := os.MkdirAll(filepath.Join(root, "reports", "fill", "00014"), 0o755); err != nil {
t.Fatalf("creating the directory: %v", err)
}
code, status := s3Code(t, write(b, "fill/00014"))
if code != "ExistingObjectIsDirectory" || status != 409 {
t.Errorf("write over a directory answered %d %s, want 409 ExistingObjectIsDirectory", status, code)
}
})
}
}

// A key whose parent is an object keeps versitygw's own 409 too: the key
// cannot be created as a file below another file.
func TestWriteBelowAnObjectStaysExistingObjectIsDirectory(t *testing.T) {
for name, write := range writes {
t.Run(name, func(t *testing.T) {
b, root := newFullVolumeBackend(t, 1<<20, 1<<30)
if err := os.WriteFile(filepath.Join(root, "reports", "fill"), nil, 0o644); err != nil {
t.Fatalf("creating the object: %v", err)
}
code, status := s3Code(t, write(b, "fill/00014"))
if code != "ExistingObjectIsDirectory" || status != 409 {
t.Errorf("write below an object answered %d %s, want 409 ExistingObjectIsDirectory", status, code)
}
})
}
}

// With neither a directory in the way nor the volume full, the cause is
// unknown, which is an InternalError rather than a claim about the key.
func TestWriteFailureWithSpaceLeftIsInternalError(t *testing.T) {
for name, write := range writes {
t.Run(name, func(t *testing.T) {
b, _ := newFullVolumeBackend(t, 900<<20, 1<<30)
code, status := s3Code(t, write(b, "fill/00014"))
if code != "InternalError" || status != 500 {
t.Errorf("unexplained write failure answered %d %s, want 500 InternalError", status, code)
}
})
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ spec:
# migration cannot be carried across and is drained instead.
- --legacy-volume-migration
{{- end }}
{{- if .Values.operatorMetrics.enabled }}
- --metrics-bind-address=:{{ .Values.operatorMetrics.port }}
{{- end }}
{{- if .Values.metricsAPI.enabled }}
- --metrics-api-bind-port={{ .Values.metricsAPI.port }}
- --prometheus-url={{ .Values.metricsAPI.prometheusURL }}
Expand Down Expand Up @@ -197,6 +200,11 @@ spec:
containerPort: {{ .Values.metricsAPI.port }}
protocol: TCP
{{- end }}
{{- if .Values.operatorMetrics.enabled }}
- name: metrics
containerPort: {{ .Values.operatorMetrics.port }}
protocol: TCP
{{- end }}
livenessProbe:
httpGet:
path: /healthz
Expand Down
11 changes: 11 additions & 0 deletions helm-charts/charts/simplyblock-operator/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,17 @@ metricsAPI:
# -- Prometheus the capacity samples are read from. Empty serves provisioned sizes only.
prometheusURL: http://simplyblock-prometheus:9090

operatorMetrics:
# The operator's controller-runtime metrics, including the ObjectStore and
# S3Bucket families. Served per pod, on the container port named "metrics",
# over HTTPS behind Kubernetes authentication and authorization: a scraper
# presents a ServiceAccount token bound to the
# simplyblock-operator-metrics-reader ClusterRole.
# -- Serves the operator's own metrics. `false` closes the port.
enabled: false
# -- Container port of the operator's metrics endpoint.
port: 8443

mongodb-kubernetes:
# -- Name of the MongoDB operator.
name: "simplyblock-mongodb"
Expand Down
1 change: 1 addition & 0 deletions operator/api/v1alpha2/s3bucket_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type S3BucketSpec struct {
// reconciles to a PVC expansion. A decrease is rejected, the same
// restriction the PVC it wraps already carries.
// +kubebuilder:validation:Required
// +kubebuilder:validation:XValidation:rule="self >= oldSelf",message="capacityBytes cannot decrease"
CapacityBytes int64 `json:"capacityBytes"`

// StorageClassName names the StorageClass the backing PVC is created
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ spec:
restriction the PVC it wraps already carries.
format: int64
type: integer
x-kubernetes-validations:
- message: capacityBytes cannot decrease
rule: self >= oldSelf
objectStoreRef:
description: |-
ObjectStoreRef names the ObjectStore whose gateway Deployment serves
Expand Down
1 change: 1 addition & 0 deletions operator/config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ rules:
- controlplaneops/finalizers
- controlplanes/finalizers
- nfsexports/finalizers
- objectstores/finalizers
- operatorops/finalizers
- persistentvolumeops/finalizers
- replicationops/finalizers
Expand Down
Loading
Loading