Skip to content

Commit 0cd0585

Browse files
mikeeeantontroshin
andauthored
fix: arguments accept units (#1490)
* fix: arguments accept units `max-body-size` and `read-buffer-size` now accept units as defined in the docs. Fixes #1489 Signed-off-by: Mike Nguyen <[email protected]> * chore: gofumpt Signed-off-by: Mike Nguyen <[email protected]> * refactor: modify logic to comply with vetting Signed-off-by: Mike Nguyen <[email protected]> * chore: gofumpt -w . Signed-off-by: Mike Nguyen <[email protected]> * refactor: set defaults `max-body-size` is defaulted to 4Mi `request-buffer-size` is defaulted to 4Ki This is inline with the runtime. Signed-off-by: Mike Nguyen <[email protected]> * fix: set defaults in run and annotate Signed-off-by: Mike Nguyen <[email protected]> * chore: gofumpt Signed-off-by: Mike Nguyen <[email protected]> * refactor: exit with error rather than panic Co-authored-by: Anton Troshin <[email protected]> Signed-off-by: Mike Nguyen <[email protected]> --------- Signed-off-by: Mike Nguyen <[email protected]> Co-authored-by: Anton Troshin <[email protected]>
1 parent a968b18 commit 0cd0585

File tree

4 files changed

+120
-30
lines changed

4 files changed

+120
-30
lines changed

cmd/annotate.go

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ import (
2121
"net/url"
2222
"os"
2323
"path/filepath"
24+
"strconv"
25+
26+
"github.com/dapr/dapr/pkg/runtime"
27+
28+
"k8s.io/apimachinery/pkg/api/resource"
2429

2530
"github.com/spf13/cobra"
2631

@@ -60,8 +65,8 @@ var (
6065
annotateReadinessProbeThreshold int
6166
annotateDaprImage string
6267
annotateAppSSL bool
63-
annotateMaxRequestBodySize int
64-
annotateReadBufferSize int
68+
annotateMaxRequestBodySize string
69+
annotateReadBufferSize string
6570
annotateHTTPStreamRequestBody bool
6671
annotateGracefulShutdownSeconds int
6772
annotateEnableAPILogging bool
@@ -318,12 +323,23 @@ func getOptionsFromFlags() kubernetes.AnnotateOptions {
318323
if annotateAppSSL {
319324
o = append(o, kubernetes.WithAppSSL())
320325
}
321-
if annotateMaxRequestBodySize != -1 {
322-
o = append(o, kubernetes.WithMaxRequestBodySize(annotateMaxRequestBodySize))
326+
if annotateMaxRequestBodySize != "-1" {
327+
if q, err := resource.ParseQuantity(annotateMaxRequestBodySize); err != nil {
328+
print.FailureStatusEvent(os.Stderr, "error parsing value of max-body-size: %s, error: %s", annotateMaxRequestBodySize, err.Error())
329+
os.Exit(1)
330+
} else {
331+
o = append(o, kubernetes.WithMaxRequestBodySize(int(q.Value())))
332+
}
323333
}
324-
if annotateReadBufferSize != -1 {
325-
o = append(o, kubernetes.WithReadBufferSize(annotateReadBufferSize))
334+
if annotateReadBufferSize != "-1" {
335+
if q, err := resource.ParseQuantity(annotateReadBufferSize); err != nil {
336+
print.FailureStatusEvent(os.Stderr, "error parsing value of read-buffer-size: %s, error: %s", annotateMaxRequestBodySize, err.Error())
337+
os.Exit(1)
338+
} else {
339+
o = append(o, kubernetes.WithReadBufferSize(int(q.Value())))
340+
}
326341
}
342+
327343
if annotateHTTPStreamRequestBody {
328344
o = append(o, kubernetes.WithHTTPStreamRequestBody())
329345
}
@@ -385,8 +401,8 @@ func init() {
385401
AnnotateCmd.Flags().StringVar(&annotateDaprImage, "dapr-image", "", "The image to use for the dapr sidecar container")
386402
AnnotateCmd.Flags().BoolVar(&annotateAppSSL, "app-ssl", false, "Enable SSL for the app")
387403
AnnotateCmd.Flags().MarkDeprecated("app-ssl", "This flag is deprecated and will be removed in a future release. Use \"app-protocol\" flag with https or grpcs instead")
388-
AnnotateCmd.Flags().IntVar(&annotateMaxRequestBodySize, "max-body-size", -1, "The maximum request body size to use")
389-
AnnotateCmd.Flags().IntVar(&annotateReadBufferSize, "read-buffer-size", -1, "The maximum size of HTTP header read buffer in kilobytes")
404+
AnnotateCmd.Flags().StringVar(&annotateMaxRequestBodySize, "max-body-size", strconv.Itoa(runtime.DefaultMaxRequestBodySize>>20)+"Mi", "The maximum request body size to use")
405+
AnnotateCmd.Flags().StringVar(&annotateReadBufferSize, "read-buffer-size", strconv.Itoa(runtime.DefaultReadBufferSize>>10)+"Ki", "The maximum size of HTTP header read buffer in kilobytes")
390406
AnnotateCmd.Flags().BoolVar(&annotateHTTPStreamRequestBody, "http-stream-request-body", false, "Enable streaming request body for HTTP")
391407
AnnotateCmd.Flags().IntVar(&annotateGracefulShutdownSeconds, "graceful-shutdown-seconds", -1, "The number of seconds to wait for the app to shutdown")
392408
AnnotateCmd.Flags().BoolVar(&annotateEnableAPILogging, "enable-api-logging", false, "Enable API logging for the Dapr sidecar")

cmd/run.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import (
2929
"github.com/spf13/cobra"
3030
"github.com/spf13/viper"
3131

32+
daprRuntime "github.com/dapr/dapr/pkg/runtime"
33+
3234
"github.com/dapr/cli/pkg/kubernetes"
3335
"github.com/dapr/cli/pkg/metadata"
3436
"github.com/dapr/cli/pkg/print"
@@ -55,8 +57,8 @@ var (
5557
resourcesPaths []string
5658
appSSL bool
5759
metricsPort int
58-
maxRequestBodySize int
59-
readBufferSize int
60+
maxRequestBodySize string
61+
readBufferSize string
6062
unixDomainSocket string
6163
enableAppHealth bool
6264
appHealthPath string
@@ -473,8 +475,8 @@ func init() {
473475
RunCmd.Flags().MarkDeprecated("app-ssl", "This flag is deprecated and will be removed in the future releases. Use \"app-protocol\" flag with https or grpcs values instead")
474476
RunCmd.Flags().IntVarP(&metricsPort, "metrics-port", "M", -1, "The port of metrics on dapr")
475477
RunCmd.Flags().BoolP("help", "h", false, "Print this help message")
476-
RunCmd.Flags().IntVarP(&maxRequestBodySize, "max-body-size", "", -1, "Max size of request body in MB")
477-
RunCmd.Flags().IntVarP(&readBufferSize, "read-buffer-size", "", -1, "HTTP header read buffer in KB")
478+
RunCmd.Flags().StringVarP(&maxRequestBodySize, "max-body-size", "", strconv.Itoa(daprRuntime.DefaultMaxRequestBodySize>>20)+"Mi", "Max size of request body in MB")
479+
RunCmd.Flags().StringVarP(&readBufferSize, "read-buffer-size", "", strconv.Itoa(daprRuntime.DefaultReadBufferSize>>10)+"Ki", "HTTP header read buffer in KB")
478480
RunCmd.Flags().StringVarP(&unixDomainSocket, "unix-domain-socket", "u", "", "Path to a unix domain socket dir. If specified, Dapr API servers will use Unix Domain Sockets")
479481
RunCmd.Flags().BoolVar(&enableAppHealth, "enable-app-health-check", false, "Enable health checks for the application using the protocol defined with app-protocol")
480482
RunCmd.Flags().StringVar(&appHealthPath, "app-health-check-path", "", "Path used for health checks; HTTP only")

pkg/runexec/runexec_test.go

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"strings"
2121
"testing"
2222

23+
"github.com/stretchr/testify/require"
24+
2325
"github.com/stretchr/testify/assert"
2426

2527
"github.com/dapr/cli/pkg/standalone"
@@ -180,8 +182,8 @@ func TestRun(t *testing.T) {
180182
AppProtocol: "http",
181183
ComponentsPath: componentsDir,
182184
AppSSL: true,
183-
MaxRequestBodySize: -1,
184-
HTTPReadBufferSize: -1,
185+
MaxRequestBodySize: "-1",
186+
HTTPReadBufferSize: "-1",
185187
EnableAPILogging: true,
186188
APIListenAddresses: "127.0.0.1",
187189
}
@@ -294,8 +296,8 @@ func TestRun(t *testing.T) {
294296
basicConfig.ProfilePort = 0
295297
basicConfig.EnableProfiling = true
296298
basicConfig.MaxConcurrency = 0
297-
basicConfig.MaxRequestBodySize = 0
298-
basicConfig.HTTPReadBufferSize = 0
299+
basicConfig.MaxRequestBodySize = ""
300+
basicConfig.HTTPReadBufferSize = ""
299301
basicConfig.AppProtocol = ""
300302

301303
basicConfig.SetDefaultFromSchema()
@@ -307,8 +309,8 @@ func TestRun(t *testing.T) {
307309
assert.Equal(t, -1, basicConfig.ProfilePort)
308310
assert.True(t, basicConfig.EnableProfiling)
309311
assert.Equal(t, -1, basicConfig.MaxConcurrency)
310-
assert.Equal(t, -1, basicConfig.MaxRequestBodySize)
311-
assert.Equal(t, -1, basicConfig.HTTPReadBufferSize)
312+
assert.Equal(t, "4Mi", basicConfig.MaxRequestBodySize)
313+
assert.Equal(t, "4Ki", basicConfig.HTTPReadBufferSize)
312314
assert.Equal(t, "http", basicConfig.AppProtocol)
313315

314316
// Test after Validate gets called.
@@ -322,8 +324,46 @@ func TestRun(t *testing.T) {
322324
assert.Positive(t, basicConfig.ProfilePort)
323325
assert.True(t, basicConfig.EnableProfiling)
324326
assert.Equal(t, -1, basicConfig.MaxConcurrency)
325-
assert.Equal(t, -1, basicConfig.MaxRequestBodySize)
326-
assert.Equal(t, -1, basicConfig.HTTPReadBufferSize)
327+
assert.Equal(t, "4Mi", basicConfig.MaxRequestBodySize)
328+
assert.Equal(t, "4Ki", basicConfig.HTTPReadBufferSize)
327329
assert.Equal(t, "http", basicConfig.AppProtocol)
328330
})
331+
332+
t.Run("run with max body size without units", func(t *testing.T) {
333+
basicConfig.MaxRequestBodySize = "4000000"
334+
335+
output, err := NewOutput(basicConfig)
336+
require.NoError(t, err)
337+
assertArgumentEqual(t, "max-body-size", "4M", output.DaprCMD.Args)
338+
})
339+
340+
t.Run("run with max body size with units", func(t *testing.T) {
341+
basicConfig.MaxRequestBodySize = "4Mi"
342+
343+
output, err := NewOutput(basicConfig)
344+
require.NoError(t, err)
345+
assertArgumentEqual(t, "max-body-size", "4Mi", output.DaprCMD.Args)
346+
347+
basicConfig.MaxRequestBodySize = "5M"
348+
349+
output, err = NewOutput(basicConfig)
350+
require.NoError(t, err)
351+
assertArgumentEqual(t, "max-body-size", "5M", output.DaprCMD.Args)
352+
})
353+
354+
t.Run("run with read buffer size set without units", func(t *testing.T) {
355+
basicConfig.HTTPReadBufferSize = "16001"
356+
357+
output, err := NewOutput(basicConfig)
358+
require.NoError(t, err)
359+
assertArgumentEqual(t, "read-buffer-size", "16001", output.DaprCMD.Args)
360+
})
361+
362+
t.Run("run with read buffer size set with units", func(t *testing.T) {
363+
basicConfig.HTTPReadBufferSize = "4Ki"
364+
365+
output, err := NewOutput(basicConfig)
366+
require.NoError(t, err)
367+
assertArgumentEqual(t, "read-buffer-size", "4Ki", output.DaprCMD.Args)
368+
})
329369
}

pkg/standalone/run.go

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import (
2424
"strconv"
2525
"strings"
2626

27+
"k8s.io/apimachinery/pkg/api/resource"
28+
2729
"github.com/Pallinder/sillyname-go"
2830
"github.com/phayes/freeport"
2931
"gopkg.in/yaml.v2"
@@ -79,8 +81,8 @@ type SharedRunConfig struct {
7981
ResourcesPaths []string `arg:"resources-path" yaml:"resourcesPaths"`
8082
// Speicifcally omitted from annotations as appSSL is deprecated.
8183
AppSSL bool `arg:"app-ssl" yaml:"appSSL"`
82-
MaxRequestBodySize int `arg:"max-body-size" annotation:"dapr.io/max-body-size" yaml:"maxBodySize" default:"-1"`
83-
HTTPReadBufferSize int `arg:"read-buffer-size" annotation:"dapr.io/read-buffer-size" yaml:"readBufferSize" default:"-1"`
84+
MaxRequestBodySize string `arg:"max-body-size" annotation:"dapr.io/max-body-size" yaml:"maxBodySize" default:"4Mi"`
85+
HTTPReadBufferSize string `arg:"read-buffer-size" annotation:"dapr.io/read-buffer-size" yaml:"readBufferSize" default:"4Ki"`
8486
EnableAppHealth bool `arg:"enable-app-health-check" annotation:"dapr.io/enable-app-health-check" yaml:"enableAppHealthCheck"`
8587
AppHealthPath string `arg:"app-health-check-path" annotation:"dapr.io/app-health-check-path" yaml:"appHealthCheckPath"`
8688
AppHealthInterval int `arg:"app-health-probe-interval" annotation:"dapr.io/app-health-probe-interval" ifneq:"0" yaml:"appHealthProbeInterval"`
@@ -226,12 +228,27 @@ func (config *RunConfig) Validate() error {
226228
if config.MaxConcurrency < 1 {
227229
config.MaxConcurrency = -1
228230
}
229-
if config.MaxRequestBodySize < 0 {
230-
config.MaxRequestBodySize = -1
231+
232+
qBody, err := resource.ParseQuantity(config.MaxRequestBodySize)
233+
if err != nil {
234+
return fmt.Errorf("invalid max request body size: %w", err)
235+
}
236+
237+
if qBody.Value() < 0 {
238+
config.MaxRequestBodySize = "-1"
239+
} else {
240+
config.MaxRequestBodySize = qBody.String()
241+
}
242+
243+
qBuffer, err := resource.ParseQuantity(config.HTTPReadBufferSize)
244+
if err != nil {
245+
return fmt.Errorf("invalid http read buffer size: %w", err)
231246
}
232247

233-
if config.HTTPReadBufferSize < 0 {
234-
config.HTTPReadBufferSize = -1
248+
if qBuffer.Value() < 0 {
249+
config.HTTPReadBufferSize = "-1"
250+
} else {
251+
config.HTTPReadBufferSize = qBuffer.String()
235252
}
236253

237254
err = config.validatePlacementHostAddr()
@@ -265,12 +282,27 @@ func (config *RunConfig) ValidateK8s() error {
265282
if config.MaxConcurrency < 1 {
266283
config.MaxConcurrency = -1
267284
}
268-
if config.MaxRequestBodySize < 0 {
269-
config.MaxRequestBodySize = -1
285+
286+
qBody, err := resource.ParseQuantity(config.MaxRequestBodySize)
287+
if err != nil {
288+
return fmt.Errorf("invalid max request body size: %w", err)
289+
}
290+
291+
if qBody.Value() < 0 {
292+
config.MaxRequestBodySize = "-1"
293+
} else {
294+
config.MaxRequestBodySize = qBody.String()
295+
}
296+
297+
qBuffer, err := resource.ParseQuantity(config.HTTPReadBufferSize)
298+
if err != nil {
299+
return fmt.Errorf("invalid http read buffer size: %w", err)
270300
}
271301

272-
if config.HTTPReadBufferSize < 0 {
273-
config.HTTPReadBufferSize = -1
302+
if qBuffer.Value() < 0 {
303+
config.HTTPReadBufferSize = "-1"
304+
} else {
305+
config.HTTPReadBufferSize = qBuffer.String()
274306
}
275307

276308
return nil

0 commit comments

Comments
 (0)