Skip to content

Commit 5d9dc50

Browse files
author
Wei Fu
committed
refactor: change for the reviewer
1. s/optionsForContainerio/logOptionsForContainerio/g 2. make the convertKVStringsToMap into pkg/utils 3. fix typo Signed-off-by: Wei Fu <[email protected]>
1 parent cf0e2e7 commit 5d9dc50

6 files changed

Lines changed: 72 additions & 23 deletions

File tree

apis/opts/log_options.go

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package opts
22

33
import (
4-
"errors"
54
"fmt"
6-
"strings"
5+
6+
"github.com/alibaba/pouch/pkg/utils"
77
)
88

99
// ParseLogOptions parses [key=value] slice-type log options into map.
1010
func ParseLogOptions(driver string, logOpts []string) (map[string]string, error) {
11-
opts, err := convertKVStringsToMap(logOpts)
11+
opts, err := utils.ConvertKVStringsToMap(logOpts)
1212
if err != nil {
1313
return nil, err
1414
}
@@ -18,19 +18,3 @@ func ParseLogOptions(driver string, logOpts []string) (map[string]string, error)
1818
}
1919
return opts, nil
2020
}
21-
22-
// convertKVStringsToMap converts ["key=value"] into {"key":"value"}
23-
//
24-
// TODO(fuwei): make it common in the opts.ParseXXX().
25-
func convertKVStringsToMap(values []string) (map[string]string, error) {
26-
kvs := make(map[string]string, len(values))
27-
28-
for _, value := range values {
29-
terms := strings.SplitN(value, "=", 2)
30-
if len(terms) != 2 {
31-
return nil, errors.New("the format must be key=value")
32-
}
33-
kvs[terms[0]] = terms[1]
34-
}
35-
return kvs, nil
36-
}

daemon/logger/syslog/validate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var (
2525
// ErrInvalidSyslogFormat represents the invalid format.
2626
ErrInvalidSyslogFormat = errors.New("invalid syslog format")
2727

28-
// ErrFailedToLoadX509KeyPair is to used to indicate that it's failed to load x590 key pair.
28+
// ErrFailedToLoadX509KeyPair is to used to indicate that it's failed to load x509 key pair.
2929
ErrFailedToLoadX509KeyPair = errors.New("fail to load x509 key pair")
3030

3131
fmtErrInvalidAddressFormat = "syslog-address must be in form proto://address, but got %v"

daemon/mgr/container.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,7 +1475,7 @@ func (mgr *ContainerManager) openContainerIO(c *Container) (*containerio.IO, err
14751475
containerio.WithStdin(c.Config.OpenStdin),
14761476
}
14771477

1478-
options = append(options, optionsForContainerio(c)...)
1478+
options = append(options, logOptionsForContainerio(c)...)
14791479

14801480
io := containerio.NewIO(containerio.NewOption(options...))
14811481
mgr.IOs.Put(c.ID, io)
@@ -1612,7 +1612,7 @@ func (mgr *ContainerManager) openAttachIO(c *Container, attach *AttachConfig) (*
16121612
containerio.WithID(c.ID),
16131613
containerio.WithLoggerInfo(logInfo),
16141614
}
1615-
options = append(options, optionsForContainerio(c)...)
1615+
options = append(options, logOptionsForContainerio(c)...)
16161616

16171617
if attach != nil {
16181618
options = append(options, attachConfigToOptions(attach)...)

daemon/mgr/container_logger.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/sirupsen/logrus"
99
)
1010

11-
func optionsForContainerio(c *Container) []func(*containerio.Option) {
11+
func logOptionsForContainerio(c *Container) []func(*containerio.Option) {
1212
optFuncs := make([]func(*containerio.Option), 0, 1)
1313

1414
cfg := c.HostConfig.LogConfig

pkg/utils/utils.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,3 +336,17 @@ func SetOOMScore(pid, score int) error {
336336
f.Close()
337337
return err
338338
}
339+
340+
// ConvertKVStringsToMap converts ["key=value"] into {"key":"value"}
341+
func ConvertKVStringsToMap(values []string) (map[string]string, error) {
342+
kvs := make(map[string]string, len(values))
343+
344+
for _, value := range values {
345+
terms := strings.SplitN(value, "=", 2)
346+
if len(terms) != 2 {
347+
return nil, errors.New("the format must be key=value")
348+
}
349+
kvs[terms[0]] = terms[1]
350+
}
351+
return kvs, nil
352+
}

pkg/utils/utils_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,3 +430,54 @@ func TestParseTimestamp(t *testing.T) {
430430
}
431431
}
432432
}
433+
434+
func TestConvertKVStringsToMap(t *testing.T) {
435+
type tCases struct {
436+
input []string
437+
expected map[string]string
438+
hasError bool
439+
}
440+
441+
for idx, tc := range []tCases{
442+
{
443+
input: nil,
444+
expected: map[string]string{},
445+
hasError: false,
446+
}, {
447+
input: []string{"withoutValue"},
448+
expected: nil,
449+
hasError: true,
450+
}, {
451+
input: []string{"key=value"},
452+
expected: map[string]string{
453+
"key": "value",
454+
},
455+
hasError: false,
456+
}, {
457+
input: []string{"key=key=value"},
458+
expected: map[string]string{
459+
"key": "key=value",
460+
},
461+
hasError: false,
462+
}, {
463+
input: []string{"test=1", "flag=oops", "test=2"},
464+
expected: map[string]string{
465+
"test": "2",
466+
"flag": "oops",
467+
},
468+
hasError: false,
469+
},
470+
} {
471+
got, err := ConvertKVStringsToMap(tc.input)
472+
if err == nil && tc.hasError {
473+
t.Fatalf("[%d case] should have error here, but got nothing", idx)
474+
}
475+
if err != nil && !tc.hasError {
476+
t.Fatalf("[%d case] should have no error here, but got error(%v)", idx, err)
477+
}
478+
479+
if !reflect.DeepEqual(got, tc.expected) {
480+
t.Fatalf("[%d case] should have (%v), but got (%v)", idx, tc.expected, got)
481+
}
482+
}
483+
}

0 commit comments

Comments
 (0)