-
Notifications
You must be signed in to change notification settings - Fork 149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add action and state stores migrations #4305
Changes from all commits
2584db6
3e8c546
dc43804
5558612
4bb54c7
c768a55
42441b9
9b9001a
73eaf64
af354cc
92dc2b4
41bfe12
6d8b347
a06bacc
5f458fe
f73bd81
7424ddb
fdb5aa2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package migrations | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"time" | ||
|
||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
type StateStore struct { | ||
Action Action `yaml:"action"` | ||
AckToken string `yaml:"ack_token"` | ||
ActionQueue []ActionQueue `yaml:"action_queue"` | ||
} | ||
|
||
// Action is a struct to read an Action Policy Change from its old | ||
// YAML format. | ||
type Action struct { | ||
ActionID string `yaml:"action_id"` | ||
Type string `yaml:"action_type"` | ||
StartTime time.Time `yaml:"start_time,omitempty"` | ||
SourceURI string `yaml:"source_uri,omitempty"` | ||
RetryAttempt int `yaml:"retry_attempt,omitempty"` | ||
Policy map[string]interface{} `yaml:"policy,omitempty"` | ||
IsDetected bool `yaml:"is_detected,omitempty"` | ||
} | ||
|
||
// ActionQueue is a struct to read the action queue from its old YAML format. | ||
type ActionQueue struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This queue is quite peculiar: it's the same as |
||
ActionID string `yaml:"action_id"` | ||
Type string `yaml:"type"` | ||
StartTime time.Time `yaml:"start_time,omitempty"` | ||
ExpirationTime time.Time `yaml:"expiration,omitempty"` | ||
Version string `yaml:"version,omitempty"` | ||
SourceURI string `yaml:"source_uri,omitempty"` | ||
RetryAttempt int `yaml:"retry_attempt,omitempty"` | ||
Policy map[string]interface{} `yaml:"policy,omitempty"` | ||
IsDetected bool `yaml:"is_detected,omitempty"` | ||
} | ||
|
||
type loader interface { | ||
Load() (io.ReadCloser, error) | ||
} | ||
|
||
// LoadActionStore loads an action store from loader. | ||
func LoadActionStore(loader loader) (*Action, error) { | ||
return LoadStore[Action](loader) | ||
} | ||
|
||
// LoadYAMLStateStore loads the old YAML state store from loader. | ||
func LoadYAMLStateStore(loader loader) (*StateStore, error) { | ||
return LoadStore[StateStore](loader) | ||
} | ||
|
||
// LoadStore loads a YAML file. | ||
func LoadStore[Store any](loader loader) (store *Store, err error) { | ||
// Store is a generic type, this might be needed. | ||
store = new(Store) | ||
|
||
reader, err := loader.Load() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to load action store: %w", err) | ||
} | ||
defer func() { | ||
err2 := reader.Close() | ||
if err != nil { | ||
err = errors.Join(err, | ||
fmt.Errorf("migration storeLoad failed to close reader: %w", err2)) | ||
} | ||
}() | ||
|
||
data, err := io.ReadAll(reader) | ||
if err != nil { | ||
return nil, err | ||
} | ||
buff := bytes.NewReader(data) | ||
|
||
dec := yaml.NewDecoder(buff) | ||
err = dec.Decode(&store) | ||
if errors.Is(err, io.EOF) { | ||
return nil, nil | ||
} | ||
if err != nil { | ||
return nil, fmt.Errorf("could not YAML unmarshal action from action store: %w", err) | ||
} | ||
|
||
return store, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Action has quite a broad meaning in the rest of the agent.
This looks like a very specific action store: it represents a Policy Action.
What about calling this
PolicyActionStore
or something more specific?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is also a private
type actionQueue []fleetapi.ScheduledAction
in state_store.go that makes following this even more confusion.Why isn't this the general
fleetapi.Action
? Or one of the concrete Fleet action API implementations?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This package is only for action and state store migrations. It cannot be imported outside the
strore
package. That's why I thought it would not be confusing, at this level there is only one meaning forAction
.because it's a code for migration, the
fleetapi
models do not work, they changed. There was a whole PR just to fix the schema: #4240There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also it was copied from the old store code. I kept it as it was, even thought it has properties to read different actions, as far as I can tell, there should only be 'Policy Change' actions saved