-
Notifications
You must be signed in to change notification settings - Fork 25
/
scale-actuator.go
140 lines (123 loc) · 4.65 KB
/
scale-actuator.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
package podscaler
import (
"context"
"path"
clientv3 "go.etcd.io/etcd/client/v3"
"go.uber.org/fx"
"go.uber.org/multierr"
"google.golang.org/protobuf/proto"
policyprivatev1 "github.com/fluxninja/aperture/v2/api/gen/proto/go/aperture/policy/private/v1"
policysyncv1 "github.com/fluxninja/aperture/v2/api/gen/proto/go/aperture/policy/sync/v1"
"github.com/fluxninja/aperture/v2/pkg/config"
etcdclient "github.com/fluxninja/aperture/v2/pkg/etcd/client"
etcdwriter "github.com/fluxninja/aperture/v2/pkg/etcd/writer"
"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/paths"
)
// ScaleActuator struct.
type ScaleActuator struct {
policyReadAPI iface.Policy
decisionWriter *etcdwriter.Writer
scaleActuatorProto *policyprivatev1.PodScaleActuator
decisionsEtcdPath string
agentGroupName string
podScalerComponentID string
}
// Name implements runtime.Component.
func (*ScaleActuator) Name() string { return "ScaleActuator" }
// Type implements runtime.Component.
func (*ScaleActuator) Type() runtime.ComponentType { return runtime.ComponentTypeSink }
// ShortDescription implements runtime.Component.
func (sa *ScaleActuator) ShortDescription() string { return sa.agentGroupName }
// IsActuator implements runtime.Component.
func (*ScaleActuator) IsActuator() bool { return true }
// NewScaleActuatorAndOptions creates scale actuator and its fx options.
func NewScaleActuatorAndOptions(
scaleActuatorProto *policyprivatev1.PodScaleActuator,
_ runtime.ComponentID,
policyReadAPI iface.Policy,
) (runtime.Component, fx.Option, error) {
agentGroup := scaleActuatorProto.GetAgentGroup()
podScalerComponentID := scaleActuatorProto.GetPodScalerComponentId()
etcdKey := paths.AgentComponentKey(agentGroup, policyReadAPI.GetPolicyName(), podScalerComponentID)
decisionsEtcdPath := path.Join(paths.PodScalerDecisionsPath, etcdKey)
sa := &ScaleActuator{
policyReadAPI: policyReadAPI,
agentGroupName: agentGroup,
podScalerComponentID: podScalerComponentID,
decisionsEtcdPath: decisionsEtcdPath,
scaleActuatorProto: scaleActuatorProto,
}
return sa, fx.Options(
fx.Invoke(sa.setupWriter),
), nil
}
func (sa *ScaleActuator) setupWriter(scopedKV *etcdclient.SessionScopedKV, lifecycle fx.Lifecycle) error {
logger := sa.policyReadAPI.GetStatusRegistry().GetLogger()
lifecycle.Append(fx.Hook{
OnStart: func(context.Context) error {
sa.decisionWriter = etcdwriter.NewWriter(&scopedKV.KVWrapper)
return nil
},
OnStop: func(ctx context.Context) error {
var merr, err error
sa.decisionWriter.Close()
_, err = scopedKV.Delete(clientv3.WithRequireLeader(ctx), sa.decisionsEtcdPath)
if err != nil {
logger.Error().Err(err).Msg("Failed to delete scale decisions")
merr = multierr.Append(merr, err)
}
return merr
},
})
return nil
}
// Execute implements runtime.Component.Execute.
func (sa *ScaleActuator) Execute(inPortReadings runtime.PortToReading, circuitAPI runtime.CircuitAPI) (runtime.PortToReading, error) {
// Get the decision from the port
replicasReading := inPortReadings.ReadSingleReadingPort("replicas")
var replicasValue float64
if replicasReading.Valid() {
if replicasReading.Value() <= 0 {
replicasValue = 0
} else {
replicasValue = replicasReading.Value()
}
return nil, sa.publishDecision(replicasValue)
}
sa.publishDefaultDecision()
return nil, nil
}
// DynamicConfigUpdate implements runtime.Component.DynamicConfigUpdate.
func (sa *ScaleActuator) DynamicConfigUpdate(event notifiers.Event, unmarshaller config.Unmarshaller) {
}
func (sa *ScaleActuator) publishDefaultDecision() {
// delete the decision
sa.decisionWriter.Delete(sa.decisionsEtcdPath)
}
func (sa *ScaleActuator) publishDecision(desiredReplicas float64) error {
logger := sa.policyReadAPI.GetStatusRegistry().GetLogger()
// Save desired replicas in decision message
decision := &policysyncv1.ScaleDecision{
DesiredReplicas: int32(desiredReplicas),
}
// Publish decision
logger.Autosample().Debug().Float64("desiredReplicas", desiredReplicas).Msg("Publish scale decision")
wrapper := &policysyncv1.ScaleDecisionWrapper{
ScaleDecision: decision,
CommonAttributes: &policysyncv1.CommonAttributes{
PolicyName: sa.policyReadAPI.GetPolicyName(),
PolicyHash: sa.policyReadAPI.GetPolicyHash(),
ComponentId: sa.podScalerComponentID,
},
}
dat, err := proto.Marshal(wrapper)
if err != nil {
logger.Error().Err(err).Msg("Failed to marshal policy decision")
return err
}
sa.decisionWriter.Write(sa.decisionsEtcdPath, dat)
return nil
}