-
Notifications
You must be signed in to change notification settings - Fork 23
/
policy-factory.go
194 lines (176 loc) · 5.72 KB
/
policy-factory.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package controlplane
import (
"context"
"sync"
"go.uber.org/fx"
policylangv1 "github.com/fluxninja/aperture/api/gen/proto/go/aperture/policy/language/v1"
policysyncv1 "github.com/fluxninja/aperture/api/gen/proto/go/aperture/policy/sync/v1"
"github.com/fluxninja/aperture/pkg/config"
etcdclient "github.com/fluxninja/aperture/pkg/etcd/client"
"github.com/fluxninja/aperture/pkg/jobs"
"github.com/fluxninja/aperture/pkg/net/grpcgateway"
"github.com/fluxninja/aperture/pkg/notifiers"
"github.com/fluxninja/aperture/pkg/policies/common"
"github.com/fluxninja/aperture/pkg/policies/controlplane/iface"
"github.com/fluxninja/aperture/pkg/prometheus"
"github.com/fluxninja/aperture/pkg/status"
)
// policyFactoryModule module for policy factory.
func policyFactoryModule() fx.Option {
return fx.Options(
fx.Provide(
fx.Annotate(
providePolicyFactory,
fx.ParamTags(
config.NameTag(policiesFxTag),
config.NameTag(policiesDynamicConfigFxTag),
common.FxOptionsFuncTag,
),
),
),
grpcgateway.RegisterHandler{Handler: policylangv1.RegisterPolicyServiceHandlerFromEndpoint}.Annotate(),
fx.Invoke(RegisterPolicyService),
prometheus.Module(),
policyModule(),
)
}
// PolicyFactory factory for policies.
type PolicyFactory struct {
lock sync.RWMutex
circuitJobGroup *jobs.JobGroup
etcdClient *etcdclient.Client
registry status.Registry
dynamicConfigWatcher notifiers.Watcher
policyTracker map[string]*policysyncv1.PolicyWrapper
}
// Main fx app.
func providePolicyFactory(
etcdWatcher notifiers.Watcher,
dynamicConfigWatcher notifiers.Watcher,
fxOptionsFuncs []notifiers.FxOptionsFunc,
etcdClient *etcdclient.Client,
lifecycle fx.Lifecycle,
registry status.Registry,
) (*PolicyFactory, error) {
policiesStatusRegistry := registry.Child(iface.PoliciesRoot)
logger := policiesStatusRegistry.GetLogger()
circuitJobGroup, err := jobs.NewJobGroup(policiesStatusRegistry.Child("circuit_jobs"), 0, jobs.RescheduleMode, nil)
if err != nil {
logger.Error().Err(err).Msg("Failed to create job group")
return nil, err
}
factory := &PolicyFactory{
registry: policiesStatusRegistry,
circuitJobGroup: circuitJobGroup,
etcdClient: etcdClient,
dynamicConfigWatcher: dynamicConfigWatcher,
policyTracker: make(map[string]*policysyncv1.PolicyWrapper),
}
optionsFunc := []notifiers.FxOptionsFunc{factory.provideControllerPolicyFxOptions}
if len(fxOptionsFuncs) > 0 {
optionsFunc = append(optionsFunc, fxOptionsFuncs...)
}
fxDriver := ¬ifiers.FxDriver{
FxOptionsFuncs: optionsFunc,
UnmarshalPrefixNotifier: notifiers.UnmarshalPrefixNotifier{
GetUnmarshallerFunc: config.KoanfUnmarshallerConstructor{}.NewKoanfUnmarshaller,
},
StatusRegistry: policiesStatusRegistry,
}
lifecycle.Append(fx.Hook{
OnStart: func(context.Context) error {
err := factory.circuitJobGroup.Start()
if err != nil {
return err
}
return nil
},
OnStop: func(context.Context) error {
defer policiesStatusRegistry.Detach()
err := factory.circuitJobGroup.Stop()
if err != nil {
return err
}
return nil
},
})
notifiers.NotifierLifecycle(lifecycle, etcdWatcher, fxDriver)
return factory, nil
}
// provideControllerPolicyFxOptions Per policy fx app in controller.
func (factory *PolicyFactory) provideControllerPolicyFxOptions(
key notifiers.Key,
unmarshaller config.Unmarshaller,
registry status.Registry,
) (fx.Option, error) {
policyMessage := &policylangv1.Policy{}
err := unmarshaller.Unmarshal(policyMessage)
if err != nil {
registry.SetStatus(status.NewStatus(nil, err))
registry.GetLogger().Error().Err(err).Msg("Failed to unmarshal policy")
return fx.Options(), err
}
wrapperMessage, err := hashAndPolicyWrap(policyMessage, string(key))
if err != nil {
registry.SetStatus(status.NewStatus(nil, err))
registry.GetLogger().Error().Err(err).Msg("Failed to wrap message in config properties")
return fx.Options(), err
}
policyFxOptions, err := newPolicyOptions(
wrapperMessage,
registry,
)
if err != nil {
registry.SetStatus(status.NewStatus(nil, err))
registry.GetLogger().Warn().Err(err).Msg("Failed to create policy options")
return fx.Options(), err
}
return fx.Options(
fx.Supply(
fx.Annotate(factory.dynamicConfigWatcher, fx.As(new(notifiers.Watcher))),
factory.circuitJobGroup,
factory.etcdClient,
wrapperMessage,
),
policyFxOptions,
fx.Invoke(factory.trackPolicy),
), nil
}
func (factory *PolicyFactory) trackPolicy(wrapperMessage *policysyncv1.PolicyWrapper, lifecycle fx.Lifecycle) {
lifecycle.Append(fx.Hook{
OnStart: func(context.Context) error {
factory.lock.Lock()
defer factory.lock.Unlock()
factory.policyTracker[wrapperMessage.GetCommonAttributes().GetPolicyName()] = wrapperMessage
return nil
},
OnStop: func(context.Context) error {
factory.lock.Lock()
defer factory.lock.Unlock()
delete(factory.policyTracker, wrapperMessage.GetCommonAttributes().GetPolicyName())
return nil
},
})
}
// GetPolicyWrappers returns all policy wrappers.
func (factory *PolicyFactory) GetPolicyWrappers() map[string]*policysyncv1.PolicyWrapper {
factory.lock.RLock()
defer factory.lock.RUnlock()
// deepcopy wrappers
policyWrappers := make(map[string]*policysyncv1.PolicyWrapper)
for k, v := range factory.policyTracker {
policyWrappers[k] = v.DeepCopy()
}
return policyWrappers
}
// GetPolicies returns all policies.
func (factory *PolicyFactory) GetPolicies() *policylangv1.Policies {
policyWrappers := factory.GetPolicyWrappers()
policies := make(map[string]*policylangv1.Policy)
for _, v := range policyWrappers {
policies[v.GetCommonAttributes().GetPolicyName()] = v.GetPolicy().DeepCopy()
}
return &policylangv1.Policies{
Policies: policies,
}
}