/
all_connectors_flavor.go
125 lines (101 loc) 路 4.09 KB
/
all_connectors_flavor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright (c) 2017 Cisco and/or its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package connectors
import (
"github.com/ligato/cn-infra/core"
"github.com/ligato/cn-infra/datasync/kvdbsync"
"github.com/ligato/cn-infra/datasync/resync"
"github.com/ligato/cn-infra/db/keyval/etcdv3"
"github.com/ligato/cn-infra/db/keyval/redis"
"github.com/ligato/cn-infra/db/sql/cassandra"
"github.com/ligato/cn-infra/flavors/local"
"github.com/ligato/cn-infra/messaging/kafka"
)
// NewAgent returns a new instance of the Agent with plugins.
// It is an alias for core.NewAgent() to implicit use of the FlavorLocal.
func NewAgent(opts ...core.Option) *core.Agent {
return core.NewAgent(&AllConnectorsFlavor{}, opts...)
}
// WithPlugins for adding custom plugins to SFC Controller
// <listPlugins> is a callback that uses flavor input to
// inject dependencies for custom plugins that are in output
//
// Example:
//
// NewAgent(connectors.WithPlugins(func(flavor) {
// return []*core.NamedPlugin{{"my-plugin", &MyPlugin{DependencyXY: &flavor.ETCD}}}
// }))
func WithPlugins(listPlugins func(local *AllConnectorsFlavor) []*core.NamedPlugin) core.WithPluginsOpt {
return &withPluginsOpt{listPlugins}
}
// AllConnectorsFlavor is a combination of all plugins that allow
// connectivity to external database/messaging...
// Effectively it is combination of ETCD, Kafka, Redis, Cassandra
// plugins.
//
// User/admin can enable those plugins/connectors by providing
// configs (at least endpoints) for them.
type AllConnectorsFlavor struct {
*local.FlavorLocal
ETCD etcdv3.Plugin
ETCDDataSync kvdbsync.Plugin
Kafka kafka.Plugin
Redis redis.Plugin
RedisDataSync kvdbsync.Plugin
Cassandra cassandra.Plugin
ResyncOrch resync.Plugin // the order is important because of AfterInit()
injected bool
}
// Inject initializes flavor references/dependencies.
func (f *AllConnectorsFlavor) Inject() bool {
if f.injected {
return false
}
f.injected = true
if f.FlavorLocal == nil {
f.FlavorLocal = &local.FlavorLocal{}
}
f.FlavorLocal.Inject()
f.ETCD.Deps.PluginInfraDeps = *f.InfraDeps("etcdv3", local.WithConf())
InjectKVDBSync(&f.ETCDDataSync, &f.ETCD, f.ETCD.PluginName, f.FlavorLocal, &f.ResyncOrch)
f.Redis.Deps.PluginInfraDeps = *f.InfraDeps("redis", local.WithConf())
InjectKVDBSync(&f.RedisDataSync, &f.Redis, f.Redis.PluginName, f.FlavorLocal, &f.ResyncOrch)
f.Kafka.Deps.PluginInfraDeps = *f.InfraDeps("kafka", local.WithConf())
f.Cassandra.Deps.PluginInfraDeps = *f.InfraDeps("cassandra", local.WithConf())
f.ResyncOrch.PluginLogDeps = *f.LogDeps("resync-orch")
return true
}
// Plugins combines all Plugins in flavor to the list
func (f *AllConnectorsFlavor) Plugins() []*core.NamedPlugin {
f.Inject()
return core.ListPluginsInFlavor(f)
}
// withPluginsOpt is return value of connectors.WithPlugins() utility
// to easily define new plugins for the agent based on LocalFlavor.
type withPluginsOpt struct {
callback func(local *AllConnectorsFlavor) []*core.NamedPlugin
}
// OptionMarkerCore is just for marking implementation that it implements this interface
func (opt *withPluginsOpt) OptionMarkerCore() {}
// Plugins methods is here to implement core.WithPluginsOpt go interface
// <flavor> is a callback that uses flavor input for dependency injection
// for custom plugins (returned as NamedPlugin)
func (opt *withPluginsOpt) Plugins(flavors ...core.Flavor) []*core.NamedPlugin {
for _, flavor := range flavors {
if f, ok := flavor.(*AllConnectorsFlavor); ok {
return opt.callback(f)
}
}
panic("wrong usage of connectors.WithPlugin() for other than AllConnectorsFlavor")
}