This repository has been archived by the owner on Oct 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Bin Liu <[email protected]>
- Loading branch information
Showing
7 changed files
with
119 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package config | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/go-logr/logr" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
const ( | ||
defaultConfigurationFile = "/etc/addons-manager/addons-manager.conf" | ||
) | ||
|
||
type Options struct { | ||
// The path of configuration file | ||
ConfigFile string | ||
Config *ControllerConfig | ||
Log logr.Logger | ||
} | ||
|
||
type ControllerConfig struct { | ||
AntreaNsxEnabled bool `yaml:"antreaNsxEnabled,omitempty"` | ||
} | ||
|
||
func NewOptions(Log logr.Logger) *Options { | ||
return &Options{ | ||
Config: new(ControllerConfig), | ||
Log: Log, | ||
} | ||
} | ||
|
||
func (o *Options) Complete(configFile string) error { | ||
o.setDefaults() | ||
if configFile != "" { | ||
_, err := os.Stat(configFile) | ||
if err != nil { | ||
o.Log.Info("configFile does not exist, will use default settings") | ||
return nil | ||
} | ||
o.ConfigFile = configFile | ||
} | ||
if len(o.ConfigFile) > 0 { | ||
o.Log.Info("config file is", o.ConfigFile) | ||
err := o.loadConfigFromFile(o.ConfigFile) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (o *Options) setDefaults() { | ||
if o.ConfigFile == "" { | ||
o.ConfigFile = defaultConfigurationFile | ||
} | ||
} | ||
|
||
func (o *Options) loadConfigFromFile(file string) error { | ||
data, err := os.ReadFile(file) | ||
if err != nil { | ||
o.Log.Error(err, "failed to read file", file) | ||
return err | ||
} | ||
|
||
o.Log.Info("read config from file", file, string(data)) | ||
err = yaml.UnmarshalStrict(data, o.Config) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.