-
Notifications
You must be signed in to change notification settings - Fork 25
/
provider.go
183 lines (170 loc) · 5.97 KB
/
provider.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
package controlplane
import (
"context"
"encoding/json"
"go.uber.org/fx"
"go.uber.org/multierr"
"sigs.k8s.io/yaml"
languagev1 "github.com/fluxninja/aperture/v2/api/gen/proto/go/aperture/policy/language/v1"
"github.com/fluxninja/aperture/v2/pkg/config"
etcdclient "github.com/fluxninja/aperture/v2/pkg/etcd/client"
etcdnotifier "github.com/fluxninja/aperture/v2/pkg/etcd/notifier"
etcdwatcher "github.com/fluxninja/aperture/v2/pkg/etcd/watcher"
"github.com/fluxninja/aperture/v2/pkg/log"
"github.com/fluxninja/aperture/v2/pkg/notifiers"
"github.com/fluxninja/aperture/v2/pkg/policies/controlplane/crwatcher"
"github.com/fluxninja/aperture/v2/pkg/policies/controlplane/iface"
"github.com/fluxninja/aperture/v2/pkg/policies/paths"
)
const (
policiesTrackerFxTag = "PoliciesTracker"
policiesDynamicConfigTrackerFxTag = "PoliciesDynamicConfigTracker"
policiesAPITrackerFxTag = "PoliciesAPITracker"
policiesAPIDynamicConfigTrackerFxTag = "PoliciesAPIDynamicConfigTracker"
)
// swagger:operation POST /policies common-configuration PoliciesConfig
// ---
// x-fn-config-env: true
// parameters:
// - name: promql_jobs_scheduler
// in: body
// schema:
// "$ref": "#/definitions/JobGroupConfig"
// - name: cr_watcher
// in: body
// schema:
// "$ref": "#/definitions/CRWatcherConfig"
// Module - Controller can be initialized by passing options from Module() to fx app.
func Module() fx.Option {
return fx.Options(
fx.Provide(
providePolicyValidator,
fx.Annotate(
provideTrackers,
fx.ResultTags(
config.NameTag(policiesTrackerFxTag),
config.NameTag(policiesDynamicConfigTrackerFxTag),
),
),
),
// Create a new watcher for CRs
crwatcher.Constructor{
Name: policiesTrackerFxTag,
DynamicConfigName: policiesDynamicConfigTrackerFxTag,
}.Annotate(),
// Create a new watcher for policies API
etcdwatcher.Constructor{
Name: policiesAPITrackerFxTag,
EtcdPath: paths.PoliciesAPIConfigPath,
}.Annotate(),
// Create a new watcher for policies API dynamic config
etcdwatcher.Constructor{
Name: policiesAPIDynamicConfigTrackerFxTag,
EtcdPath: paths.PoliciesAPIDynamicConfigPath,
}.Annotate(),
fx.Invoke(
fx.Annotate(
setupPoliciesNotifier,
fx.ParamTags(
config.NameTag(policiesTrackerFxTag),
config.NameTag(policiesDynamicConfigTrackerFxTag),
config.NameTag(policiesAPITrackerFxTag),
config.NameTag(policiesAPIDynamicConfigTrackerFxTag),
),
),
),
// Policy factory
policyFactoryModule(),
)
}
// provideTrackers provides new trackers for policies and policies dynamic config.
func provideTrackers(lifecycle fx.Lifecycle) (notifiers.Trackers, notifiers.Trackers, error) {
policyTrackers := notifiers.NewDefaultTrackers()
policyDynamicConfigTrackers := notifiers.NewDefaultTrackers()
lifecycle.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
return multierr.Combine(
policyTrackers.Start(),
policyDynamicConfigTrackers.Start(),
)
},
OnStop: func(ctx context.Context) error {
return multierr.Combine(
policyTrackers.Stop(),
policyDynamicConfigTrackers.Stop(),
)
},
})
return policyTrackers, policyDynamicConfigTrackers, nil
}
// Sync policies config directory with etcd.
func setupPoliciesNotifier(
policyTrackers notifiers.Trackers,
policyDynamicConfigTrackers notifiers.Trackers,
policyAPIWatcher notifiers.Watcher,
policyAPIDynamicConfigWatcher notifiers.Watcher,
scopedKV *etcdclient.SessionScopedKV,
lifecycle fx.Lifecycle,
) {
wrapPolicy := func(key notifiers.Key, bytes []byte, etype notifiers.EventType) (notifiers.Key, []byte, error) {
var dat []byte
if bytes == nil {
return key, dat, nil
}
switch etype {
case notifiers.Write:
policyMessage := &iface.PolicyMessage{
Policy: &languagev1.Policy{},
}
unmarshalErr := json.Unmarshal(bytes, policyMessage)
if unmarshalErr != nil {
log.Warn().Err(unmarshalErr).Msg("Failed to unmarshal policy")
return key, nil, unmarshalErr
}
wrapper, wrapErr := hashAndPolicyWrap(policyMessage.Policy, string(key))
if wrapErr != nil {
log.Warn().Err(wrapErr).Msg("Failed to wrap message in config properties")
return key, nil, wrapErr
}
var marshalWrapErr error
jsonDat, marshalWrapErr := json.Marshal(wrapper)
if marshalWrapErr != nil {
log.Warn().Err(marshalWrapErr).Msgf("Failed to marshal config wrapper for proto message %+v", &wrapper)
return key, nil, marshalWrapErr
}
// convert to yaml
dat, marshalWrapErr = yaml.JSONToYAML(jsonDat)
if marshalWrapErr != nil {
log.Warn().Err(marshalWrapErr).Msgf("Failed to marshal config wrapper for proto message %+v", &wrapper)
return key, nil, marshalWrapErr
}
}
return key, dat, nil
}
policyEtcdNotifier := etcdnotifier.NewPrefixToEtcdNotifier(paths.PoliciesConfigPath, &scopedKV.KVWrapper)
// content transform callback to wrap policy in config properties wrapper
policyEtcdNotifier.SetTransformFunc(wrapPolicy)
policyDynamicConfigEtcdNotifier := etcdnotifier.NewPrefixToEtcdNotifier(paths.PoliciesDynamicConfigPath, &scopedKV.KVWrapper)
lifecycle.Append(fx.Hook{
OnStart: func(_ context.Context) error {
return multierr.Combine(
policyEtcdNotifier.Start(),
policyTrackers.AddPrefixNotifier(policyEtcdNotifier),
policyAPIWatcher.AddPrefixNotifier(policyEtcdNotifier),
policyDynamicConfigEtcdNotifier.Start(),
policyDynamicConfigTrackers.AddPrefixNotifier(policyDynamicConfigEtcdNotifier),
policyAPIDynamicConfigWatcher.AddPrefixNotifier(policyDynamicConfigEtcdNotifier),
)
},
OnStop: func(_ context.Context) error {
return multierr.Combine(
policyTrackers.RemovePrefixNotifier(policyEtcdNotifier),
policyAPIWatcher.RemovePrefixNotifier(policyEtcdNotifier),
policyEtcdNotifier.Stop(),
policyDynamicConfigTrackers.RemovePrefixNotifier(policyDynamicConfigEtcdNotifier),
policyAPIDynamicConfigWatcher.RemovePrefixNotifier(policyDynamicConfigEtcdNotifier),
policyDynamicConfigEtcdNotifier.Stop(),
)
},
})
}