-
Notifications
You must be signed in to change notification settings - Fork 23
/
sampler.go
187 lines (166 loc) · 6.37 KB
/
sampler.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
package sampler
import (
"context"
"path"
"go.uber.org/fx"
"google.golang.org/protobuf/proto"
policylangv1 "github.com/fluxninja/aperture/api/v2/gen/proto/go/aperture/policy/language/v1"
policysyncv1 "github.com/fluxninja/aperture/api/v2/gen/proto/go/aperture/policy/sync/v1"
"github.com/fluxninja/aperture/v2/pkg/config"
etcdclient "github.com/fluxninja/aperture/v2/pkg/etcd/client"
"github.com/fluxninja/aperture/v2/pkg/notifiers"
"github.com/fluxninja/aperture/v2/pkg/policies/controlplane/iface"
"github.com/fluxninja/aperture/v2/pkg/policies/controlplane/runtime"
"github.com/fluxninja/aperture/v2/pkg/policies/flowcontrol/selectors"
"github.com/fluxninja/aperture/v2/pkg/policies/paths"
)
type samplerSync struct {
policyReadAPI iface.Policy
SamplerProto *policylangv1.Sampler
decision *policysyncv1.SamplerDecision
etcdClient *etcdclient.Client
componentID string
configEtcdPaths []string
decisionEtcdPaths []string
passThroughLabelValues []string
}
// Name implements runtime.Component.
func (*samplerSync) Name() string { return "Sampler" }
// Type implements runtime.Component.
func (*samplerSync) Type() runtime.ComponentType { return runtime.ComponentTypeSink }
// ShortDescription implements runtime.Component.
func (samplerSync *samplerSync) ShortDescription() string {
return iface.GetSelectorsShortDescription(samplerSync.SamplerProto.Parameters.GetSelectors())
}
// IsActuator implements runtime.Component.
func (*samplerSync) IsActuator() bool { return true }
// NewSamplerAndOptions creates fx options for Sampler and also returns agent group name associated with it.
func NewSamplerAndOptions(
samplerProto *policylangv1.Sampler,
componentID runtime.ComponentID,
policyReadAPI iface.Policy,
) (runtime.Component, fx.Option, error) {
s := samplerProto.Parameters.GetSelectors()
agentGroups := selectors.UniqueAgentGroups(s)
var configEtcdPaths, decisionEtcdPaths []string
for _, agentGroup := range agentGroups {
etcdKey := paths.AgentComponentKey(agentGroup, policyReadAPI.GetPolicyName(), componentID.String())
configEtcdPath := path.Join(paths.SamplerConfigPath, etcdKey)
configEtcdPaths = append(configEtcdPaths, configEtcdPath)
decisionEtcdPath := path.Join(paths.SamplerDecisionsPath, etcdKey)
decisionEtcdPaths = append(decisionEtcdPaths, decisionEtcdPath)
}
samplerSync := &samplerSync{
SamplerProto: samplerProto,
decision: &policysyncv1.SamplerDecision{},
policyReadAPI: policyReadAPI,
configEtcdPaths: configEtcdPaths,
decisionEtcdPaths: decisionEtcdPaths,
componentID: componentID.String(),
}
return samplerSync, fx.Options(
fx.Invoke(
samplerSync.setupSync,
),
), nil
}
func (samplerSync *samplerSync) setupSync(etcdClient *etcdclient.Client, lifecycle fx.Lifecycle) error {
logger := samplerSync.policyReadAPI.GetStatusRegistry().GetLogger()
samplerSync.etcdClient = etcdClient
lifecycle.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
wrapper := &policysyncv1.SamplerWrapper{
Sampler: samplerSync.SamplerProto,
CommonAttributes: &policysyncv1.CommonAttributes{
PolicyName: samplerSync.policyReadAPI.GetPolicyName(),
PolicyHash: samplerSync.policyReadAPI.GetPolicyHash(),
ComponentId: samplerSync.componentID,
},
}
dat, err := proto.Marshal(wrapper)
if err != nil {
logger.Error().Err(err).Msg("failed to marshal sampler config")
return err
}
for _, configEtcdPath := range samplerSync.configEtcdPaths {
etcdClient.Put(configEtcdPath, string(dat))
}
return nil
},
OnStop: func(ctx context.Context) error {
deleteEtcdPath := func(paths []string) {
for _, path := range paths {
etcdClient.Delete(path)
}
}
deleteEtcdPath(samplerSync.configEtcdPaths)
deleteEtcdPath(samplerSync.decisionEtcdPaths)
return nil
},
})
return nil
}
// Execute implements runtime.Component.Execute.
func (samplerSync *samplerSync) Execute(inPortReadings runtime.PortToReading, circuitAPI runtime.CircuitAPI) (runtime.PortToReading, error) {
acceptPercentage, ok := inPortReadings["accept_percentage"]
if !ok {
return nil, nil
}
if len(acceptPercentage) == 0 {
return nil, nil
}
acceptPercentageReading := inPortReadings.ReadSingleReadingPort("accept_percentage")
var acceptPercentageValue float64
if !acceptPercentageReading.Valid() {
if samplerSync.SamplerProto.Parameters.RampMode {
acceptPercentageValue = 0
} else {
acceptPercentageValue = 100 // default to 100%
}
} else {
acceptPercentageValue = acceptPercentageReading.Value()
}
return nil, samplerSync.publishAcceptPercentage(acceptPercentageValue)
}
func (samplerSync *samplerSync) publishAcceptPercentage(acceptPercentageValue float64) error {
logger := samplerSync.policyReadAPI.GetStatusRegistry().GetLogger()
// Publish decision
logger.Debug().Float64("flux", acceptPercentageValue).Msg("publishing flux sampler decision")
wrapper := &policysyncv1.SamplerDecisionWrapper{
SamplerDecision: &policysyncv1.SamplerDecision{
AcceptPercentage: acceptPercentageValue,
PassThroughLabelValues: samplerSync.passThroughLabelValues,
},
CommonAttributes: &policysyncv1.CommonAttributes{
PolicyName: samplerSync.policyReadAPI.GetPolicyName(),
PolicyHash: samplerSync.policyReadAPI.GetPolicyHash(),
ComponentId: samplerSync.componentID,
},
}
dat, err := proto.Marshal(wrapper)
if err != nil {
logger.Error().Err(err).Msg("failed to marshal flux sampler decision")
return err
}
for _, decisionEtcdPath := range samplerSync.decisionEtcdPaths {
samplerSync.etcdClient.Put(decisionEtcdPath, string(dat))
}
return nil
}
// DynamicConfigUpdate handles overrides.
func (samplerSync *samplerSync) DynamicConfigUpdate(event notifiers.Event, unmarshaller config.Unmarshaller) {
logger := samplerSync.policyReadAPI.GetStatusRegistry().GetLogger()
key := samplerSync.SamplerProto.GetPassThroughLabelValuesConfigKey()
passThroughLabelValues := []string{}
if !unmarshaller.IsSet(key) {
samplerSync.passThroughLabelValues = samplerSync.SamplerProto.GetPassThroughLabelValues()
return
}
err := unmarshaller.UnmarshalKey(key, passThroughLabelValues)
if err != nil {
logger.Error().Err(err).Msg("failed to unmarshal dynamic config")
samplerSync.passThroughLabelValues = samplerSync.SamplerProto.GetPassThroughLabelValues()
return
}
samplerSync.passThroughLabelValues = passThroughLabelValues
}