fix: only clear the requested resource type
Running `resource:limit-clear` or `resource:reserve-clear` without `--process-type` deleted every resource property for the app, so clearing limits also removed reservations and vice versa. Both commands now clear only their own resource type across all process types.
This commit is contained in:
6 files changed
+154
-14
No files matched your search
@@ -2,6 +2,7 @@ package resource
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/dokku/dokku/plugins/common"
|
||||
)
|
||||
@@ -36,6 +37,17 @@ func formatResourceValueForDocker(key string, value string) (string, error) {
|
||||
return value, nil
|
||||
}
|
||||
|
||||
// isResourceTypeKey returns true if the property key is a `<process>.<resource-type>.<resource>` key
|
||||
// for the given resource type. The process type may contain dots, so the key is matched from the right
|
||||
func isResourceTypeKey(key string, resourceType string) bool {
|
||||
parts := strings.Split(key, ".")
|
||||
if len(parts) < 3 {
|
||||
return false
|
||||
}
|
||||
|
||||
return parts[len(parts)-2] == resourceType
|
||||
}
|
||||
|
||||
func clearByResourceType(appName string, processType string, resourceType string) {
|
||||
noun := "limits"
|
||||
if resourceType == "reserve" {
|
||||
@@ -54,6 +66,9 @@ func clearByResourceType(appName string, processType string, resourceType string
|
||||
return
|
||||
}
|
||||
for key := range resources {
|
||||
if !isResourceTypeKey(key, resourceType) {
|
||||
continue
|
||||
}
|
||||
common.PropertyDelete("resource", appName, key)
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -53,3 +53,30 @@ func TestFormatResourceValueForDocker(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsResourceTypeKey(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
key string
|
||||
resourceType string
|
||||
expected bool
|
||||
}{
|
||||
{name: "default limit key", key: "_default_.limit.memory", resourceType: "limit", expected: true},
|
||||
{name: "process limit key", key: "web.limit.cpu", resourceType: "limit", expected: true},
|
||||
{name: "process reserve key", key: "web.reserve.memory", resourceType: "reserve", expected: true},
|
||||
{name: "dotted process type", key: "foo.bar.limit.memory", resourceType: "limit", expected: true},
|
||||
{name: "reserve key does not match limit", key: "_default_.reserve.memory", resourceType: "limit", expected: false},
|
||||
{name: "limit key does not match reserve", key: "web.limit.memory", resourceType: "reserve", expected: false},
|
||||
{name: "key without process type", key: "limit.memory", resourceType: "limit", expected: false},
|
||||
{name: "key with a single part", key: "limit", resourceType: "limit", expected: false},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
value := isResourceTypeKey(testCase.key, testCase.resourceType)
|
||||
if value != testCase.expected {
|
||||
t.Fatalf("expected %v for %s/%s, got %v", testCase.expected, testCase.key, testCase.resourceType, value)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -19,10 +19,10 @@ Additional commands:`
|
||||
|
||||
helpContent = `
|
||||
resource:limit [--process-type <process-type>] [RESOURCE_OPTS...] <app>, Limit resources for a given app/process-type combination
|
||||
resource:limit-clear [--process-type <process-type>] <app>, Limit resources for a given app/process-type combination
|
||||
resource:limit-clear [--process-type <process-type>] <app>, Clear resource limits for a given app/process-type combination
|
||||
resource:report [<app>] [<flag>], Displays a resource report for one or more apps
|
||||
resource:reserve [--process-type <process-type>] [RESOURCE_OPTS...] <app>, Reserve resources for a given app/process-type combination
|
||||
resource:reserve-clear [--process-type <process-type>] <app>, Reserve resources for a given app/process-type combination`
|
||||
resource:reserve-clear [--process-type <process-type>] <app>, Clear resource reservations for a given app/process-type combination`
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
Reference in new issue
Block a user