Skip to content
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

Take default support instead of rely on existence of config #211

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ type Serialized struct {
Impersonate string `json:"impersonate"`
}

func (s *Serialized) ToController() (*Controller, error) {
cfg := Controller{
Report: s.Report,
StoragePath: s.StoragePath,
Endpoint: s.Endpoint,
ReportEndpoint: s.PullReport.Endpoint,
Impersonate: s.Impersonate,
func (s *Serialized) ToController(cfg *Controller) (*Controller, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to send the cfg attribute? We can always create a new Controller as before or not? Otherwise it looks good.

Copy link
Contributor Author

@martinkunc martinkunc Oct 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is called by Load - so the steps are:
Set default to support.Controller (in code)
Try to load config to support.Controller

And unfortunatelly previously loading was resetting whole support.Controller with what was set in config file, so all the defaults had to be set there. That was ignoring what was already set in support.Controller and creating a new one.

Now, there is an option to not to pass existing Controller and create failback to previous behaviour, but we use the way which passes in the existing support.Controller default values set before.
And there is an option to override the defaults from config. For this there are if len(...)>0 blocks below - if it is set in config file, use it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh I see. Sorry. I should have read the description.

if cfg == nil {
cfg = &Controller{}
}
cfg.Report = s.Report
cfg.StoragePath = s.StoragePath
cfg.Endpoint = s.Endpoint
cfg.Impersonate = s.Impersonate

if len(s.Interval) > 0 {
d, err := time.ParseDuration(s.Interval)
if err != nil {
Expand All @@ -40,6 +41,10 @@ func (s *Serialized) ToController() (*Controller, error) {
return nil, fmt.Errorf("interval must be a non-negative duration")
}

if len(s.PullReport.Endpoint) > 0 {
cfg.ReportEndpoint = s.PullReport.Endpoint
}

if len(s.PullReport.Delay) > 0 {
d, err := time.ParseDuration(s.PullReport.Delay)
if err != nil {
Expand Down Expand Up @@ -79,7 +84,7 @@ func (s *Serialized) ToController() (*Controller, error) {
if len(cfg.StoragePath) == 0 {
return nil, fmt.Errorf("storagePath must point to a directory where snapshots can be stored")
}
return &cfg, nil
return cfg, nil
}

// Controller defines the standard config for this operator.
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (s *Support) LoadConfig(obj map[string]interface{}) error {
return fmt.Errorf("unable to load config: %v", err)
}

controller, err := cfg.ToController()
controller, err := cfg.ToController(&s.Controller)
if err != nil {
return err
}
Expand Down