Add new annotations to annotate command (#1020)

* Add new annotations

Signed-off-by: Shubham Sharma <shubhash@microsoft.com>

* Fix lint

Signed-off-by: Shubham Sharma <shubhash@microsoft.com>
This commit is contained in:
Shubham Sharma 2022-07-05 22:01:24 +05:30 committed by GitHub
parent 076d7b6d19
commit abb267057c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 222 additions and 103 deletions

View File

@ -62,8 +62,15 @@ var (
annotateDaprImage string
annotateAppSSL bool
annotateMaxRequestBodySize int
annotateReadBufferSize int
annotateHTTPStreamRequestBody bool
annotateGracefulShutdownSeconds int
annotateEnableAPILogging bool
annotateUnixDomainSocketPath string
annotateVolumeMountsReadOnly string
annotateVolumeMountsReadWrite string
annotateDisableBuiltinK8sSecretStore bool
annotatePlacementHostAddress string
)
var AnnotateCmd = &cobra.Command{
@ -316,12 +323,33 @@ func getOptionsFromFlags() kubernetes.AnnotateOptions {
if annotateMaxRequestBodySize != -1 {
o = append(o, kubernetes.WithMaxRequestBodySize(annotateMaxRequestBodySize))
}
if annotateReadBufferSize != -1 {
o = append(o, kubernetes.WithReadBufferSize(annotateReadBufferSize))
}
if annotateHTTPStreamRequestBody {
o = append(o, kubernetes.WithHTTPStreamRequestBody())
}
if annotateGracefulShutdownSeconds != -1 {
o = append(o, kubernetes.WithGracefulShutdownSeconds(annotateGracefulShutdownSeconds))
}
if annotateEnableAPILogging {
o = append(o, kubernetes.WithEnableAPILogging())
}
if annotateUnixDomainSocketPath != "" {
o = append(o, kubernetes.WithUnixDomainSocketPath(annotateUnixDomainSocketPath))
}
if annotateVolumeMountsReadOnly != "" {
o = append(o, kubernetes.WithVolumeMountsReadOnly(annotateVolumeMountsReadOnly))
}
if annotateVolumeMountsReadWrite != "" {
o = append(o, kubernetes.WithVolumeMountsReadWrite(annotateVolumeMountsReadWrite))
}
if annotateDisableBuiltinK8sSecretStore {
o = append(o, kubernetes.WithDisableBuiltinK8sSecretStore())
}
if annotatePlacementHostAddress != "" {
o = append(o, kubernetes.WithPlacementHostAddress(annotatePlacementHostAddress))
}
return kubernetes.NewAnnotateOptions(o...)
}
@ -359,7 +387,14 @@ func init() {
AnnotateCmd.Flags().StringVar(&annotateDaprImage, "dapr-image", "", "The image to use for the dapr sidecar container")
AnnotateCmd.Flags().BoolVar(&annotateAppSSL, "app-ssl", false, "Enable SSL for the app")
AnnotateCmd.Flags().IntVar(&annotateMaxRequestBodySize, "max-request-body-size", -1, "The maximum request body size to use")
AnnotateCmd.Flags().IntVar(&annotateReadBufferSize, "http-read-buffer-size", -1, "The maximum size of HTTP header read buffer in kilobytes")
AnnotateCmd.Flags().BoolVar(&annotateHTTPStreamRequestBody, "http-stream-request-body", false, "Enable streaming request body for HTTP")
AnnotateCmd.Flags().IntVar(&annotateGracefulShutdownSeconds, "graceful-shutdown-seconds", -1, "The number of seconds to wait for the app to shutdown")
AnnotateCmd.Flags().BoolVar(&annotateEnableAPILogging, "enable-api-logging", false, "Enable API logging for the Dapr sidecar")
AnnotateCmd.Flags().StringVar(&annotateUnixDomainSocketPath, "unix-domain-socket-path", "", "Linux domain socket path to use for communicating with the Dapr sidecar")
AnnotateCmd.Flags().StringVar(&annotateVolumeMountsReadOnly, "volume-mounts", "", "List of pod volumes to be mounted to the sidecar container in read-only mode")
AnnotateCmd.Flags().StringVar(&annotateVolumeMountsReadWrite, "volume-mounts-rw", "", "List of pod volumes to be mounted to the sidecar container in read-write mode")
AnnotateCmd.Flags().BoolVar(&annotateDisableBuiltinK8sSecretStore, "disable-builtin-k8s-secret-store", false, "Disable the built-in k8s secret store")
AnnotateCmd.Flags().StringVar(&annotatePlacementHostAddress, "placement-host-address", "", "Comma separated list of addresses for Dapr actor placement servers")
RootCmd.AddCommand(AnnotateCmd)
}

View File

@ -59,6 +59,12 @@ const (
daprReadBufferSizeKey = "dapr.io/http-read-buffer-size"
daprHTTPStreamRequestBodyKey = "dapr.io/http-stream-request-body"
daprGracefulShutdownSecondsKey = "dapr.io/graceful-shutdown-seconds"
daprEnableAPILoggingKey = "dapr.io/enable-api-logging"
daprUnixDomainSocketPathKey = "dapr.io/unix-domain-socket-path"
daprVolumeMountsReadOnlyKey = "dapr.io/volume-mounts"
daprVolumeMountsReadWriteKey = "dapr.io/volume-mounts-rw"
daprDisableBuiltinK8sSecretStoreKey = "dapr.io/disable-builtin-k8s-secret-store" /* #nosec */
daprPlacementHostAddressKey = "dapr.io/placement-host-address"
// K8s kinds.
pod = "pod"
@ -490,6 +496,24 @@ func getDaprAnnotations(config *AnnotateOptions) map[string]string {
if config.gracefulShutdownSeconds != nil {
annotations[daprGracefulShutdownSecondsKey] = strconv.FormatInt(int64(*config.gracefulShutdownSeconds), 10)
}
if config.enableAPILogging != nil {
annotations[daprEnableAPILoggingKey] = strconv.FormatBool(*config.enableAPILogging)
}
if config.unixDomainSocketPath != nil {
annotations[daprUnixDomainSocketPathKey] = *config.unixDomainSocketPath
}
if config.volumeMountsReadOnly != nil {
annotations[daprVolumeMountsReadOnlyKey] = *config.volumeMountsReadOnly
}
if config.volumeMountsReadWrite != nil {
annotations[daprVolumeMountsReadWriteKey] = *config.volumeMountsReadWrite
}
if config.disableBuiltinK8sSecretStore != nil {
annotations[daprDisableBuiltinK8sSecretStoreKey] = strconv.FormatBool(*config.disableBuiltinK8sSecretStore)
}
if config.placementHostAddress != nil {
annotations[daprPlacementHostAddressKey] = *config.placementHostAddress
}
return annotations
}

View File

@ -36,6 +36,12 @@ type AnnotateOptions struct {
readBufferSize *int
httpStreamRequestBody *bool
gracefulShutdownSeconds *int
enableAPILogging *bool
unixDomainSocketPath *string
volumeMountsReadOnly *string
volumeMountsReadWrite *string
disableBuiltinK8sSecretStore *bool
placementHostAddress *string
}
type AnnoteOption func(*AnnotateOptions)
@ -257,3 +263,41 @@ func WithGracefulShutdownSeconds(gracefulShutdownSeconds int) AnnoteOption {
config.gracefulShutdownSeconds = &gracefulShutdownSeconds
}
}
func WithEnableAPILogging() AnnoteOption {
return func(config *AnnotateOptions) {
enabled := true
config.enableAPILogging = &enabled
}
}
func WithUnixDomainSocketPath(unixDomainSocketPath string) AnnoteOption {
return func(config *AnnotateOptions) {
config.unixDomainSocketPath = &unixDomainSocketPath
}
}
func WithVolumeMountsReadOnly(volumeMountsReadOnly string) AnnoteOption {
return func(config *AnnotateOptions) {
config.volumeMountsReadOnly = &volumeMountsReadOnly
}
}
func WithVolumeMountsReadWrite(volumeMountsReadWrite string) AnnoteOption {
return func(config *AnnotateOptions) {
config.volumeMountsReadWrite = &volumeMountsReadWrite
}
}
func WithDisableBuiltinK8sSecretStore() AnnoteOption {
return func(config *AnnotateOptions) {
enabled := true
config.disableBuiltinK8sSecretStore = &enabled
}
}
func WithPlacementHostAddress(placementHostAddress string) AnnoteOption {
return func(config *AnnotateOptions) {
config.placementHostAddress = &placementHostAddress
}
}

View File

@ -372,6 +372,10 @@ func TestGetDaprAnnotations(t *testing.T) {
readinessProbeTimeout := 60
logLevel := "debug"
gracefulShutdownSeconds := 10
unixDomainSocketPath := "/tmp/dapr.sock"
volumeMountsReadOnly := "vm1:/tmp/path1,vm2:/tmp/path2"
volumeMountsReadWrite := "vm3:/tmp/path3"
placementHostAddress := "127.0.0.1:50057,127.0.0.1:50058"
opts := NewAnnotateOptions(
WithAppID(appID),
@ -408,6 +412,12 @@ func TestGetDaprAnnotations(t *testing.T) {
WithLogLevel(logLevel),
WithHTTPStreamRequestBody(),
WithGracefulShutdownSeconds(gracefulShutdownSeconds),
WithEnableAPILogging(),
WithUnixDomainSocketPath(unixDomainSocketPath),
WithVolumeMountsReadOnly(volumeMountsReadOnly),
WithVolumeMountsReadWrite(volumeMountsReadWrite),
WithDisableBuiltinK8sSecretStore(),
WithPlacementHostAddress(placementHostAddress),
)
annotations := getDaprAnnotations(&opts)
@ -447,5 +457,11 @@ func TestGetDaprAnnotations(t *testing.T) {
assert.Equal(t, fmt.Sprintf("%d", readBufferSize), annotations[daprReadBufferSizeKey])
assert.Equal(t, "true", annotations[daprHTTPStreamRequestBodyKey])
assert.Equal(t, fmt.Sprintf("%d", gracefulShutdownSeconds), annotations[daprGracefulShutdownSecondsKey])
assert.Equal(t, "true", annotations[daprEnableAPILoggingKey])
assert.Equal(t, unixDomainSocketPath, annotations[daprUnixDomainSocketPathKey])
assert.Equal(t, volumeMountsReadOnly, annotations[daprVolumeMountsReadOnlyKey])
assert.Equal(t, volumeMountsReadWrite, annotations[daprVolumeMountsReadWriteKey])
assert.Equal(t, "true", annotations[daprDisableBuiltinK8sSecretStoreKey])
assert.Equal(t, placementHostAddress, annotations[daprPlacementHostAddressKey])
})
}