Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.

Commit 3113b14

Browse files
author
David Chung
authored
Introduce types.Any to be used in place of json.RawMessage (#363)
Signed-off-by: David Chung <[email protected]>
1 parent a7d5dab commit 3113b14

File tree

20 files changed

+255
-148
lines changed

20 files changed

+255
-148
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
InfraKit
22
========
33

4-
[![Circle
5-
CI](https://circleci.com/gh/docker/infrakit.png?style=shield&circle-token=50d2063f283f98b7d94746416c979af3102275b5)](https://circleci.com/gh/docker/infrakit)
4+
[![Circle CI](https://circleci.com/gh/docker/infrakit.png?style=shield&circle-token=50d2063f283f98b7d94746416c979af3102275b5)](https://circleci.com/gh/docker/infrakit)
5+
[![Go Report Card](https://goreportcard.com/badge/github.com/docker/infrakit)](https://goreportcard.com/report/github.com/docker/infrakit)
66
[![codecov.io](https://codecov.io/github/docker/infrakit/coverage.svg?branch=master&token=z08ZKeIJfA)](https://codecov.io/github/docker/infrakit?branch=master)
77

88
_InfraKit_ is a toolkit for creating and managing declarative, self-healing infrastructure.

cmd/cli/plugin.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/docker/infrakit/pkg/launch/os"
1010
"github.com/docker/infrakit/pkg/plugin"
1111
"github.com/docker/infrakit/pkg/template"
12+
"github.com/docker/infrakit/pkg/types"
1213
"github.com/spf13/cobra"
1314
)
1415

@@ -63,10 +64,10 @@ func pluginCommand(plugins func() discovery.Plugins) *cobra.Command {
6364
return err
6465
}
6566

66-
configs := launch.Config([]byte(view))
67+
configs := types.AnyString(view)
6768

6869
parsedRules := []launch.Rule{}
69-
err = configs.Unmarshal(&parsedRules)
70+
err = configs.Decode(&parsedRules)
7071
if err != nil {
7172
return err
7273
}
@@ -107,11 +108,11 @@ func pluginCommand(plugins func() discovery.Plugins) *cobra.Command {
107108
name := pluginToStart
108109
ch <- launch.StartPlugin{
109110
Plugin: plugin.Name(name),
110-
Started: func(config *launch.Config) {
111+
Started: func(config *types.Any) {
111112
fmt.Println(name, "started.")
112113
wait.Done()
113114
},
114-
Error: func(config *launch.Config, err error) {
115+
Error: func(config *types.Any, err error) {
115116
fmt.Println("Error starting", name, "err=", err)
116117
wait.Done()
117118
},

docs/plugins/group.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Group plugin API
22

3-
<!-- SOURCE-CHECKSUM pkg/spi/group/* 4bc86b2ae0893db92f880ab4bb2479b5def55746 -->
3+
<!-- SOURCE-CHECKSUM pkg/spi/group/* 98638b90e25c24c9c750b61d8a288ee332977214 -->
44

55
## API
66

pkg/discovery/dir_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ func TestDirDiscovery(t *testing.T) {
6868

6969
blockWhileFileExists(path2)
7070

71-
p, err = discover.Find(plugin.Name(name1))
71+
_, err = discover.Find(plugin.Name(name1))
7272
require.Error(t, err)
7373

74-
p, err = discover.Find(plugin.Name(name2))
74+
_, err = discover.Find(plugin.Name(name2))
7575
require.Error(t, err)
7676

7777
list, err := discover.List()

pkg/example/flavor/combo/flavor.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,7 @@ func (f flavorCombo) Prepare(
154154
return inst, err
155155
}
156156

157-
var props json.RawMessage
158-
if pluginSpec.Properties != nil {
159-
props = *pluginSpec.Properties
160-
}
161-
162-
flavorOutput, err := plugin.Prepare(props, clone, allocation)
157+
flavorOutput, err := plugin.Prepare(types.RawMessage(pluginSpec.Properties), clone, allocation)
163158
if err != nil {
164159
return inst, err
165160
}

pkg/launch/config.go

Lines changed: 0 additions & 45 deletions
This file was deleted.

pkg/launch/exec.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package launch
22

3+
import (
4+
"github.com/docker/infrakit/pkg/types"
5+
)
6+
37
// Exec is a service that is able to start plugins based on different
48
// mechanisms from running local binary to pulling and running docker containers or engine plugins
59
type Exec interface {
@@ -14,5 +18,5 @@ type Exec interface {
1418
// status of the plugin.
1519
// The client can receive and block on the returned channel
1620
// and add optional timeout in its own select statement.
17-
Exec(name string, config *Config) (<-chan error, error)
21+
Exec(name string, config *types.Any) (<-chan error, error)
1822
}

pkg/launch/monitor.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@ import (
66

77
log "github.com/Sirupsen/logrus"
88
"github.com/docker/infrakit/pkg/plugin"
9+
"github.com/docker/infrakit/pkg/types"
910
)
1011

11-
var errNoConfig = errors.New("no-counfig")
12+
var errNoConfig = errors.New("no-config")
1213

1314
// ExecRule encapsulates what's required to exec a plugin
1415
type ExecRule struct {
1516
// Exec is the name of the exec to use to start the plugin
1617
Exec string
1718
// Properties is the properties for the executor
18-
Properties *Config
19+
Properties *types.Any
1920
}
2021

2122
// Rule provides the instructions on starting the plugin
@@ -58,17 +59,17 @@ func NewMonitor(l Exec, rules []Rule) *Monitor {
5859
// StartPlugin is the command to start a plugin
5960
type StartPlugin struct {
6061
Plugin plugin.Name
61-
Started func(*Config)
62-
Error func(*Config, error)
62+
Started func(*types.Any)
63+
Error func(*types.Any, error)
6364
}
6465

65-
func (s StartPlugin) reportError(config *Config, e error) {
66+
func (s StartPlugin) reportError(config *types.Any, e error) {
6667
if s.Error != nil {
6768
go s.Error(config, e)
6869
}
6970
}
7071

71-
func (s StartPlugin) reportSuccess(config *Config) {
72+
func (s StartPlugin) reportSuccess(config *types.Any) {
7273
if s.Started != nil {
7374
go s.Started(config)
7475
}
@@ -111,7 +112,7 @@ func (m *Monitor) Start() (chan<- StartPlugin, error) {
111112
continue loop
112113
}
113114

114-
configCopy := &Config{}
115+
configCopy := types.AnyBytes(nil)
115116
if r.Launch.Properties != nil {
116117
*configCopy = *r.Launch.Properties
117118
}

pkg/launch/monitor_test.go

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package launch
33
import (
44
"testing"
55

6+
"github.com/docker/infrakit/pkg/types"
67
"github.com/stretchr/testify/require"
78
)
89

@@ -14,16 +15,16 @@ type testConfig struct {
1415
type testLauncher struct {
1516
name string
1617
t *testing.T
17-
callback func(*Config)
18+
callback func(*types.Any)
1819
}
1920

2021
func (l *testLauncher) Name() string {
2122
return l.name
2223
}
2324

24-
func (l *testLauncher) Exec(name string, config *Config) (<-chan error, error) {
25+
func (l *testLauncher) Exec(name string, config *types.Any) (<-chan error, error) {
2526
rule := testConfig{}
26-
err := config.Unmarshal(&rule)
27+
err := config.Decode(&rule)
2728
if err != nil {
2829
return nil, err
2930
}
@@ -47,7 +48,7 @@ func TestMonitorLoopNoRules(t *testing.T) {
4748

4849
input <- StartPlugin{
4950
Plugin: "test",
50-
Error: func(config *Config, e error) {
51+
Error: func(config *types.Any, e error) {
5152
errChan <- e
5253
},
5354
}
@@ -60,28 +61,23 @@ func TestMonitorLoopNoRules(t *testing.T) {
6061

6162
func TestMonitorLoopValidRule(t *testing.T) {
6263

63-
raw := &Config{}
6464
config := &testConfig{
6565
Cmd: "hello",
6666
Args: []string{"world", "hello"},
6767
}
6868

69-
rawErr := raw.Marshal(config)
70-
require.NoError(t, rawErr)
71-
require.True(t, len([]byte(*raw)) > 0)
72-
73-
var receivedArgs *Config
69+
var receivedArgs *types.Any
7470
rule := Rule{
7571
Plugin: "hello",
7672
Launch: ExecRule{
7773
Exec: "test",
78-
Properties: raw,
74+
Properties: types.AnyValueMust(config),
7975
},
8076
}
8177
monitor := NewMonitor(&testLauncher{
8278
name: "test",
8379
t: t,
84-
callback: func(c *Config) {
80+
callback: func(c *types.Any) {
8581
receivedArgs = c
8682
},
8783
}, []Rule{rule})
@@ -93,45 +89,38 @@ func TestMonitorLoopValidRule(t *testing.T) {
9389
started := make(chan interface{})
9490
input <- StartPlugin{
9591
Plugin: "hello",
96-
Started: func(config *Config) {
92+
Started: func(config *types.Any) {
9793
close(started)
9894
},
9995
}
10096

10197
<-started
10298

103-
expected := &Config{}
104-
err = expected.Marshal(config)
105-
require.NoError(t, err)
106-
99+
expected := types.AnyValueMust(config)
107100
require.Equal(t, *expected, *receivedArgs)
108101

109102
monitor.Stop()
110103
}
111104

112105
func TestMonitorLoopRuleLookupBehavior(t *testing.T) {
113-
raw := &Config{}
106+
114107
config := &testConfig{
115108
Cmd: "hello",
116109
Args: []string{"world", "hello"},
117110
}
118111

119-
rawErr := raw.Marshal(config)
120-
require.NoError(t, rawErr)
121-
require.True(t, len([]byte(*raw)) > 0)
122-
123-
var receivedArgs *Config
112+
var receivedArgs *types.Any
124113
rule := Rule{
125114
Plugin: "hello",
126115
Launch: ExecRule{
127116
Exec: "test",
128-
Properties: raw,
117+
Properties: types.AnyValueMust(config),
129118
},
130119
}
131120
monitor := NewMonitor(&testLauncher{
132121
name: "test",
133122
t: t,
134-
callback: func(c *Config) {
123+
callback: func(c *types.Any) {
135124
receivedArgs = c
136125
},
137126
}, []Rule{rule})
@@ -143,17 +132,14 @@ func TestMonitorLoopRuleLookupBehavior(t *testing.T) {
143132
started := make(chan interface{})
144133
input <- StartPlugin{
145134
Plugin: "hello",
146-
Started: func(config *Config) {
135+
Started: func(config *types.Any) {
147136
close(started)
148137
},
149138
}
150139

151140
<-started
152141

153-
expected := &Config{}
154-
err = expected.Marshal(config)
155-
require.NoError(t, err)
156-
142+
expected := types.AnyValueMust(config)
157143
require.Equal(t, *expected, *receivedArgs)
158144

159145
monitor.Stop()

pkg/launch/noop.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package launch
22

33
import (
44
log "github.com/Sirupsen/logrus"
5+
"github.com/docker/infrakit/pkg/types"
56
)
67

78
type noOp int
@@ -17,7 +18,7 @@ func (n noOp) Name() string {
1718
}
1819

1920
// Launch starts the plugin given the name
20-
func (n noOp) Exec(name string, config *Config) (<-chan error, error) {
21+
func (n noOp) Exec(name string, config *types.Any) (<-chan error, error) {
2122
log.Infoln("NO-OP Exec: not automatically starting plugin", name, "args=", config)
2223

2324
starting := make(chan error)

0 commit comments

Comments
 (0)