-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(operator): add support for configuration file
Signed-off-by: Dominik Rosiek <[email protected]>
- Loading branch information
Dominik Rosiek
committed
May 17, 2023
1 parent
82f8a68
commit 869ce1d
Showing
6 changed files
with
157 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
|
||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
type Config struct { | ||
Sidecar SidecarConfig `yaml:"sidecar,omitempty"` | ||
} | ||
|
||
type SidecarConfig struct { | ||
Image string `yaml:"image,omitempty"` | ||
} | ||
|
||
func ReadConfig(configPath string) (Config, error) { | ||
// Set default values | ||
config := Config{ | ||
Sidecar: SidecarConfig{ | ||
Image: "sumologic/tailing-sidecar:latest", | ||
}, | ||
} | ||
|
||
f, err := os.Open(configPath) | ||
if err != nil { | ||
return config, err | ||
} | ||
defer f.Close() | ||
|
||
decoder := yaml.NewDecoder(f) | ||
err = decoder.Decode(&config) | ||
|
||
if err != nil && err.Error() == "EOF" { | ||
return config, nil | ||
} | ||
return config, err | ||
} | ||
|
||
func (c *Config) Validate() error { | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package main | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestReadConfig(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
content string | ||
expected Config | ||
expectedError error | ||
}{ | ||
{ | ||
name: "empty file", | ||
content: ``, | ||
expected: Config{ | ||
Sidecar: SidecarConfig{ | ||
Image: "sumologic/tailing-sidecar:latest", | ||
}, | ||
}, | ||
expectedError: nil, | ||
}, | ||
{ | ||
name: "defaults", | ||
content: ` | ||
sidecar: | ||
commands: | ||
- test | ||
- command`, | ||
expected: Config{ | ||
Sidecar: SidecarConfig{ | ||
Image: "sumologic/tailing-sidecar:latest", | ||
}, | ||
}, | ||
expectedError: nil, | ||
}, | ||
{ | ||
name: "overwrite defaults", | ||
content: ` | ||
sidecar: | ||
image: my-new-image`, | ||
expected: Config{ | ||
Sidecar: SidecarConfig{ | ||
Image: "my-new-image", | ||
}, | ||
}, | ||
expectedError: nil, | ||
}, | ||
} | ||
|
||
for _, tt := range testCases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
file, err := ioutil.TempFile(".", "prefix") | ||
require.NoError(t, err) | ||
defer os.Remove(file.Name()) | ||
|
||
_, err = file.WriteString(tt.content) | ||
require.NoError(t, err) | ||
config, err := ReadConfig(file.Name()) | ||
|
||
if tt.expectedError != nil { | ||
require.Error(t, tt.expectedError, err) | ||
return | ||
} | ||
|
||
require.NoError(t, err) | ||
require.Equal(t, tt.expected, config) | ||
}) | ||
} | ||
} | ||
|
||
func TestReadConfigInvalidFile(t *testing.T) { | ||
_, err := ReadConfig("non-existing-file") | ||
require.Error(t, err) | ||
require.EqualError(t, err, "open non-existing-file: no such file or directory") | ||
} |
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