feat: add storage:set, storage:exec, storage:wait, storage:report --global
storage:set is the in-place editor for an existing entry; size grows are passed through to a helm upgrade and Kubernetes-incompatible swaps (access-mode, storage-class) are rejected up front. storage:exec spawns a throwaway container that mounts the entry: docker run on docker-local entries, kubectl run with a Pod-spec override on k3s, falling back to bash then sh when no command is supplied. storage:wait blocks until a k3s entry's PVC reports Bound via the storage-status trigger, with a docker-local fast path that just checks the host directory. storage:report --global enumerates every registered entry and the apps that mount it, in text or JSON form.
This commit is contained in:
5 files changed
+300
-3
No files matched your search
@@ -179,6 +179,235 @@ func CommandInfo(name string, format string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// CommandSetInput captures the flags accepted by storage:set.
|
||||
type CommandSetInput struct {
|
||||
Name string
|
||||
Size string
|
||||
AccessMode string
|
||||
StorageClass string
|
||||
Namespace string
|
||||
Chown string
|
||||
ReclaimPolicy string
|
||||
Annotations map[string]string
|
||||
Labels map[string]string
|
||||
}
|
||||
|
||||
// CommandSet edits an existing entry's mutable fields and re-fires the
|
||||
// scheduler-side helm release. Refuses changes Kubernetes can't apply
|
||||
// in place (access-mode swap, storage-class swap, size shrink).
|
||||
func CommandSet(input CommandSetInput) error {
|
||||
if !EntryExists(input.Name) {
|
||||
return fmt.Errorf("storage entry %q does not exist", input.Name)
|
||||
}
|
||||
entry, err := LoadEntry(input.Name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if input.AccessMode != "" && input.AccessMode != entry.AccessMode {
|
||||
return fmt.Errorf("storage:set cannot change access-mode in place; recreate the entry")
|
||||
}
|
||||
if input.StorageClass != "" && input.StorageClass != entry.StorageClass {
|
||||
return fmt.Errorf("storage:set cannot change storage-class-name in place; recreate the entry")
|
||||
}
|
||||
if input.Size != "" {
|
||||
entry.Size = input.Size
|
||||
}
|
||||
if input.Namespace != "" {
|
||||
entry.Namespace = input.Namespace
|
||||
}
|
||||
if input.Chown != "" {
|
||||
entry.Chown = input.Chown
|
||||
}
|
||||
if input.ReclaimPolicy != "" {
|
||||
entry.ReclaimPolicy = input.ReclaimPolicy
|
||||
}
|
||||
if input.Annotations != nil {
|
||||
entry.Annotations = input.Annotations
|
||||
}
|
||||
if input.Labels != nil {
|
||||
entry.Labels = input.Labels
|
||||
}
|
||||
|
||||
if err := entry.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := SaveEntry(entry); err != nil {
|
||||
return err
|
||||
}
|
||||
if entry.Scheduler == SchedulerK3s {
|
||||
if err := callSchedulerCreateTrigger(entry); err != nil {
|
||||
return fmt.Errorf("scheduler refused storage:set for %q: %w", entry.Name, err)
|
||||
}
|
||||
}
|
||||
common.LogInfo1(fmt.Sprintf("Storage entry %s updated", entry.Name))
|
||||
return nil
|
||||
}
|
||||
|
||||
// CommandExec spawns a temporary container/Pod that mounts the entry
|
||||
// and runs the given command (or an interactive shell when args is
|
||||
// empty). It execs into the underlying CLI (docker or kubectl) so
|
||||
// stdin/stdout/stderr stream naturally.
|
||||
func CommandExec(name string, image string, args []string) error {
|
||||
if !EntryExists(name) {
|
||||
return fmt.Errorf("storage entry %q does not exist", name)
|
||||
}
|
||||
entry, err := LoadEntry(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if image == "" {
|
||||
image = "alpine:3"
|
||||
}
|
||||
|
||||
cmd := args
|
||||
if len(cmd) == 0 {
|
||||
cmd = []string{"sh", "-c", "command -v bash >/dev/null 2>&1 && exec bash || exec sh"}
|
||||
}
|
||||
|
||||
if entry.Scheduler == SchedulerDockerLocal {
|
||||
dockerArgs := []string{"run", "--rm", "-it", "-v", fmt.Sprintf("%s:/data", entry.HostPath), image}
|
||||
dockerArgs = append(dockerArgs, cmd...)
|
||||
return execStream(common.DockerBin(), dockerArgs)
|
||||
}
|
||||
|
||||
// k3s: render a Pod-spec override that mounts the PVC, then kubectl run.
|
||||
namespace := entry.Namespace
|
||||
if namespace == "" {
|
||||
namespace = "default"
|
||||
}
|
||||
overrides := map[string]any{
|
||||
"apiVersion": "v1",
|
||||
"spec": map[string]any{
|
||||
"containers": []map[string]any{
|
||||
{
|
||||
"name": "exec",
|
||||
"image": image,
|
||||
"stdin": true,
|
||||
"tty": true,
|
||||
"volumeMounts": []map[string]any{{"name": "data", "mountPath": "/data"}},
|
||||
},
|
||||
},
|
||||
"volumes": []map[string]any{
|
||||
{"name": "data", "persistentVolumeClaim": map[string]any{"claimName": entry.Name}},
|
||||
},
|
||||
},
|
||||
}
|
||||
overrideJSON, err := json.Marshal(overrides)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
kubectlArgs := []string{
|
||||
"run", "dokku-storage-exec-" + entry.Name,
|
||||
"--rm", "-it",
|
||||
"--namespace=" + namespace,
|
||||
"--image=" + image,
|
||||
"--restart=Never",
|
||||
"--overrides=" + string(overrideJSON),
|
||||
"--",
|
||||
}
|
||||
kubectlArgs = append(kubectlArgs, cmd...)
|
||||
return execStream("kubectl", kubectlArgs)
|
||||
}
|
||||
|
||||
func execStream(bin string, args []string) error {
|
||||
result, err := common.CallExecCommand(common.ExecCommandInput{
|
||||
Command: bin,
|
||||
Args: args,
|
||||
StreamStdio: true,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if result.ExitCode != 0 {
|
||||
return fmt.Errorf("%s exited with %d", bin, result.ExitCode)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CommandWait blocks until a k3s storage entry's PVC is bound.
|
||||
func CommandWait(name string) error {
|
||||
if !EntryExists(name) {
|
||||
return fmt.Errorf("storage entry %q does not exist", name)
|
||||
}
|
||||
entry, err := LoadEntry(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if entry.Scheduler != SchedulerK3s {
|
||||
// Docker-local entries are immediately "ready" once the directory exists.
|
||||
if entry.HostPath != "" {
|
||||
info, statErr := os.Stat(entry.HostPath)
|
||||
if statErr != nil || !info.IsDir() {
|
||||
return fmt.Errorf("storage entry %q host path %s is not present", name, entry.HostPath)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
data, err := json.Marshal(entry)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
results, err := common.CallPlugnTrigger(common.PlugnTriggerInput{
|
||||
Trigger: "storage-status",
|
||||
Args: []string{entry.Name},
|
||||
Stdin: strings.NewReader(string(data)),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
status := strings.TrimSpace(results.StdoutContents())
|
||||
if status != "Bound" {
|
||||
return fmt.Errorf("storage entry %q is %s, not Bound", name, status)
|
||||
}
|
||||
common.LogInfo1(fmt.Sprintf("Storage entry %s is bound", name))
|
||||
return nil
|
||||
}
|
||||
|
||||
// CommandReportGlobal prints a global storage report listing every entry
|
||||
// and the apps that mount it. Output is text by default; pass
|
||||
// format="json" for machine-readable output.
|
||||
func CommandReportGlobal(format string) error {
|
||||
entries, err := ListEntries()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
type entryWithUse struct {
|
||||
Entry *Entry `json:"entry"`
|
||||
MountedBy []string `json:"mounted_by"`
|
||||
}
|
||||
rows := []entryWithUse{}
|
||||
for _, entry := range entries {
|
||||
using, err := AppsUsingEntry(entry.Name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rows = append(rows, entryWithUse{Entry: entry, MountedBy: using})
|
||||
}
|
||||
if format == "json" {
|
||||
data, err := json.MarshalIndent(rows, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(string(data))
|
||||
return nil
|
||||
}
|
||||
if len(rows) == 0 {
|
||||
common.LogInfo1Quiet("No storage entries registered")
|
||||
return nil
|
||||
}
|
||||
common.LogInfo1Quiet("Storage report (global):")
|
||||
for _, row := range rows {
|
||||
mountedBy := "—"
|
||||
if len(row.MountedBy) > 0 {
|
||||
mountedBy = strings.Join(row.MountedBy, ", ")
|
||||
}
|
||||
common.LogVerbose(fmt.Sprintf("%s\t%s\tmounted by: %s", row.Entry.Name, row.Entry.Scheduler, mountedBy))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CommandListEntries lists registered entries (distinct from the legacy
|
||||
// per-app storage:list which lists colon-form mounts).
|
||||
func CommandListEntries(scheduler string, format string) error {
|
||||
|
||||
Reference in new issue
Block a user