Skip to content

Commit

Permalink
adjust features pkg and don't send featuresFlag if nil
Browse files Browse the repository at this point in the history
  • Loading branch information
AndersonQ committed Mar 1, 2023
1 parent f2bde85 commit beca755
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 28 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,4 @@ replace (
// Exclude this version because the version has an invalid checksum.
exclude github.com/docker/distribution v2.8.0+incompatible

replace github.com/elastic/elastic-agent-client/v7 => github.com/AndersonQ/elastic-agent-client/v7 v7.0.0-20230227212100-3ecfad4b2b6a
replace github.com/elastic/elastic-agent-client/v7 => github.com/AndersonQ/elastic-agent-client/v7 v7.0.0-20230228215808-7bea0d6b73be
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RX
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/AlecAivazis/survey/v2 v2.3.2/go.mod h1:TH2kPCDU3Kqq7pLbnCWwZXDBjnhZtmsCle5EiYDJ2fg=
github.com/AndersonQ/elastic-agent-client/v7 v7.0.0-20230227212100-3ecfad4b2b6a h1:4KXiwYssK4WHhpI6hyb8gACtCH3ijwxcKTboizgdoL8=
github.com/AndersonQ/elastic-agent-client/v7 v7.0.0-20230227212100-3ecfad4b2b6a/go.mod h1:UlVOHSDvTA4Mfdhdg+BE7aqOsUR3ab+x2Zl0aHlBXs4=
github.com/AndersonQ/elastic-agent-client/v7 v7.0.0-20230228215808-7bea0d6b73be h1:ELXZzC/Inb0C7/4CJ9K6chVxSeUZNSTxPQSIJmbVnuQ=
github.com/AndersonQ/elastic-agent-client/v7 v7.0.0-20230228215808-7bea0d6b73be/go.mod h1:UlVOHSDvTA4Mfdhdg+BE7aqOsUR3ab+x2Zl0aHlBXs4=
github.com/Azure/azure-sdk-for-go v16.2.1+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc=
github.com/Azure/azure-sdk-for-go v56.3.0+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc=
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78/go.mod h1:LmzpDX56iTiv29bbRTIsUNlaFfuhWRQBWjQdVyAevI8=
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/agent/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func New(
return nil, fmt.Errorf("failed to load configuration: %w", err)
}

if _, err := features.Apply(rawConfig); err != nil {
if err := features.Apply(rawConfig); err != nil {
return nil, fmt.Errorf("could not parse and apply feature falgs config: %w", err)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/agent/application/coordinator/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ func (c *Coordinator) processConfig(ctx context.Context, cfg *config.Config) (er
}
}

if _, err := features.Apply(cfg); err != nil {
if err := features.Apply(cfg); err != nil {
return fmt.Errorf("could not update feature flags config: %w", err)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/component/runtime/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ func (m *Manager) Subscribe(ctx context.Context, componentID string) *Subscripti
func (m *Manager) SubscribeAll(ctx context.Context) *SubscriptionAll {
sub := newSubscriptionAll(ctx, m)

// add latest states
// add the latest states
m.mx.RLock()
latest := make([]ComponentComponentState, 0, len(m.current))
for _, comp := range m.current {
Expand Down
9 changes: 1 addition & 8 deletions pkg/component/runtime/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ func TestManager_FakeInput_Features(t *testing.T) {
comp.Features = &proto.Features{
Fqdn: &proto.FQDNFeature{Enabled: true},
}

err := m.Update([]component.Component{comp})
if err != nil {
subscriptionErrCh <- fmt.Errorf("[case %d]: failed to update component: %w",
Expand Down Expand Up @@ -446,14 +447,6 @@ func TestManager_FakeInput_Features(t *testing.T) {
}
assert.True(t, ff.FQDN)

// Change something, but feature flags
comp.Units[0].LogLevel = client.UnitLogLevelInfo
comp.Units[0].Config = component.MustExpectedConfig(map[string]interface{}{
"type": "fake",
"state": int(client.UnitStateConfiguring),
"message": "Fake Healthy",
})

doneCh <- struct{}{}
return
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/component/runtime/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ func (s *ComponentState) syncExpected(comp *component.Component) bool {
}
}

if !gproto.Equal(s.expectedFeatures, comp.Features) {
if s.expectedFeatures != nil &&
!gproto.Equal(s.expectedFeatures, comp.Features) {
changed = true
s.expectedFeaturesIdx++
s.expectedFeatures = comp.Features
Expand Down
28 changes: 16 additions & 12 deletions pkg/features/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ type Flags struct {
// Parse receives a policy, parses and returns it.
// policy can be a *config.Config, config.Config or anything config.NewConfigFrom
// can work with. If policy is nil, Parse is a no-op.
func Parse(policy any) (Flags, error) {
func Parse(policy any) (*Flags, error) {
if policy == nil {
logp.L().Debug("feature flags policy is nil, nothing to do")
return Flags{}, nil
return nil, nil
}

var c *config.Config
Expand All @@ -43,14 +43,14 @@ func Parse(policy any) (Flags, error) {
var err error
c, err = config.NewConfigFrom(policy)
if err != nil {
return Flags{}, fmt.Errorf("could not get a config from type %T: %w",
return nil, fmt.Errorf("could not get a config from type %T: %w",
policy, err)
}
}

if c == nil {
logp.L().Debug("feature flags config is nil, nothing to do")
return Flags{}, nil
return nil, nil
}

type cfg struct {
Expand All @@ -63,26 +63,30 @@ func Parse(policy any) (Flags, error) {

parsedFlags := cfg{}
if err := c.Unpack(&parsedFlags); err != nil {
return Flags{}, fmt.Errorf("could not umpack features config: %w", err)
return nil, fmt.Errorf("could not umpack features config: %w", err)
}

return Flags{FQDN: parsedFlags.Agent.Features.FQDN.Enabled()}, nil
return &Flags{FQDN: parsedFlags.Agent.Features.FQDN.Enabled()}, nil
}

// Apply receives a config and applies it. If policy is nil, Apply is a no-op.
func Apply(c *config.Config) (Flags, error) {
// Apply receives a config and applies it. If c is nil, Apply is a no-op.
func Apply(c *config.Config) error {
if c == nil {
logp.L().Debug("feature flags config is nil, nothing to do")
return Flags{}, nil
return nil
}

var err error

mu.Lock()
defer mu.Unlock()
current, err = Parse(c) // Updating global state
parsed, err := Parse(c) // Updating global state
if err != nil {
return fmt.Errorf("could not apply feature flag config: %w", err)
}

return current, err
current = *parsed
return err
}

// FQDN reports if FQDN should be used instead of hostname for host.name.
Expand All @@ -100,7 +104,7 @@ func Current() Flags {
return current
}

func (f Flags) AsProto() *proto.Features {
func (f *Flags) AsProto() *proto.Features {
mu.Lock()
defer mu.Unlock()
return &proto.Features{
Expand Down
2 changes: 1 addition & 1 deletion pkg/features/features_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ agent:
t.Fatalf("could not parse config YAML: %v", err)
}

_, err = Apply(c)
err = Apply(c)
if err != nil {
t.Fatalf("Apply failed: %v", err)
}
Expand Down

0 comments on commit beca755

Please sign in to comment.