Skip to content

Commit b894397

Browse files
sumo-drosiekMikołaj Świątek
andauthored
feat(operator): add support for configuration file (#470)
* feat(operator): add support for configuration file Signed-off-by: Dominik Rosiek <[email protected]> * Update operator/config.go Co-authored-by: Mikołaj Świątek <[email protected]> * fix: syntax Signed-off-by: Dominik Rosiek <[email protected]> * chore: lint Signed-off-by: Dominik Rosiek <[email protected]> --------- Signed-off-by: Dominik Rosiek <[email protected]> Co-authored-by: Mikołaj Świątek <[email protected]>
1 parent 8c0a8fb commit b894397

File tree

6 files changed

+153
-6
lines changed

6 files changed

+153
-6
lines changed

operator/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ COPY . .
1111
RUN go mod download
1212

1313
# Build
14-
RUN CGO_ENABLED=0 GOOS=linux GO111MODULE=on go build -a -o manager main.go
14+
RUN CGO_ENABLED=0 GOOS=linux GO111MODULE=on go build -a -o manager main.go config.go
1515

1616
# Use distroless as minimal base image to package the manager binary
1717
# Refer to https://github.com/GoogleContainerTools/distroless for more details

operator/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ clean:
4646

4747
# Build manager binary
4848
manager: generate fmt vet
49-
go build -o bin/manager main.go
49+
go build -o bin/manager main.go ./config.go
5050

5151
# Run against the configured Kubernetes cluster in ~/.kube/config
5252
run: generate fmt vet manifests
53-
go run ./main.go
53+
go run ./main.go ./config.go
5454

5555
# Install CRDs into a cluster
5656
install: manifests kustomize

operator/config.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package main
2+
3+
import (
4+
"os"
5+
6+
"gopkg.in/yaml.v3"
7+
)
8+
9+
type Config struct {
10+
Sidecar SidecarConfig `yaml:"sidecar,omitempty"`
11+
}
12+
13+
type SidecarConfig struct {
14+
Image string `yaml:"image,omitempty"`
15+
}
16+
17+
func ReadConfig(configPath string) (Config, error) {
18+
// Set default values
19+
config := Config{
20+
Sidecar: SidecarConfig{
21+
Image: "sumologic/tailing-sidecar:latest",
22+
},
23+
}
24+
25+
content, err := os.ReadFile(configPath)
26+
if err != nil {
27+
return config, err
28+
}
29+
err = yaml.Unmarshal(content, &config)
30+
if err != nil {
31+
return config, err
32+
}
33+
return config, err
34+
}
35+
36+
func (c *Config) Validate() error {
37+
return nil
38+
}

operator/config_test.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package main
2+
3+
import (
4+
"io/ioutil"
5+
"os"
6+
"testing"
7+
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestReadConfig(t *testing.T) {
12+
testCases := []struct {
13+
name string
14+
content string
15+
expected Config
16+
expectedError error
17+
}{
18+
{
19+
name: "empty file",
20+
content: ``,
21+
expected: Config{
22+
Sidecar: SidecarConfig{
23+
Image: "sumologic/tailing-sidecar:latest",
24+
},
25+
},
26+
expectedError: nil,
27+
},
28+
{
29+
name: "defaults",
30+
content: `
31+
sidecar:
32+
commands:
33+
- test
34+
- command`,
35+
expected: Config{
36+
Sidecar: SidecarConfig{
37+
Image: "sumologic/tailing-sidecar:latest",
38+
},
39+
},
40+
expectedError: nil,
41+
},
42+
{
43+
name: "overwrite defaults",
44+
content: `
45+
sidecar:
46+
image: my-new-image`,
47+
expected: Config{
48+
Sidecar: SidecarConfig{
49+
Image: "my-new-image",
50+
},
51+
},
52+
expectedError: nil,
53+
},
54+
}
55+
56+
for _, tt := range testCases {
57+
t.Run(tt.name, func(t *testing.T) {
58+
file, err := ioutil.TempFile(".", "prefix")
59+
require.NoError(t, err)
60+
defer os.Remove(file.Name())
61+
62+
_, err = file.WriteString(tt.content)
63+
require.NoError(t, err)
64+
config, err := ReadConfig(file.Name())
65+
66+
if tt.expectedError != nil {
67+
require.Error(t, tt.expectedError, err)
68+
return
69+
}
70+
71+
require.NoError(t, err)
72+
require.Equal(t, tt.expected, config)
73+
})
74+
}
75+
}
76+
77+
func TestReadConfigInvalidFile(t *testing.T) {
78+
_, err := ReadConfig("non-existing-file")
79+
require.Error(t, err)
80+
require.EqualError(t, err, "open non-existing-file: no such file or directory")
81+
}

operator/go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@ require (
77
github.com/nsf/jsondiff v0.0.0-20200515183724-f29ed568f4ce
88
github.com/onsi/ginkgo v1.16.5
99
github.com/onsi/gomega v1.27.6
10+
github.com/stretchr/testify v1.8.1
1011
gomodules.xyz/jsonpatch/v2 v2.2.0
12+
gopkg.in/yaml.v3 v3.0.1
1113
k8s.io/api v0.26.3
1214
k8s.io/apimachinery v0.27.1
1315
k8s.io/client-go v0.26.3
1416
sigs.k8s.io/controller-runtime v0.14.6
1517
)
1618

19+
require github.com/pmezard/go-difflib v1.0.0 // indirect
20+
1721
require (
1822
github.com/beorn7/perks v1.0.1 // indirect
1923
github.com/cespare/xxhash/v2 v2.1.2 // indirect
@@ -63,7 +67,6 @@ require (
6367
gopkg.in/inf.v0 v0.9.1 // indirect
6468
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
6569
gopkg.in/yaml.v2 v2.4.0 // indirect
66-
gopkg.in/yaml.v3 v3.0.1 // indirect
6770
k8s.io/apiextensions-apiserver v0.26.1 // indirect
6871
k8s.io/component-base v0.26.1 // indirect
6972
k8s.io/klog/v2 v2.90.1 // indirect

operator/main.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,40 @@ func main() {
5050
var metricsAddr string
5151
var enableLeaderElection bool
5252
var tailingSidecarImage string
53+
var configPath string
54+
var config Config
55+
var err error
56+
5357
flag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
5458
flag.BoolVar(&enableLeaderElection, "enable-leader-election", false,
5559
"Enable leader election for controller manager. "+
5660
"Enabling this will ensure there is only one active controller manager.")
57-
flag.StringVar(&tailingSidecarImage, "tailing-sidecar-image", "sumologic/tailing-sidecar:latest", "tailing sidecar image")
61+
flag.StringVar(&tailingSidecarImage, "tailing-sidecar-image", "", "tailing sidecar image")
62+
flag.StringVar(&configPath, "config", "", "Path to the configuration file")
5863
flag.Parse()
5964

6065
ctrl.SetLogger(zap.New(zap.UseDevMode(true)))
6166

67+
if configPath != "" {
68+
config, err = ReadConfig(configPath)
69+
70+
if err != nil {
71+
setupLog.Error(err, "unable to read configuration", "configPath", configPath)
72+
os.Exit(1)
73+
}
74+
} else {
75+
config = Config{}
76+
}
77+
78+
if err := config.Validate(); err != nil {
79+
setupLog.Error(err, "configuration error", "configPath", configPath)
80+
os.Exit(1)
81+
}
82+
83+
if tailingSidecarImage != "" {
84+
config.Sidecar.Image = tailingSidecarImage
85+
}
86+
6287
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
6388
Scheme: scheme,
6489
MetricsBindAddress: metricsAddr,
@@ -84,7 +109,7 @@ func main() {
84109
mgr.GetWebhookServer().Register("/add-tailing-sidecars-v1-pod", &webhook.Admission{
85110
Handler: &handler.PodExtender{
86111
Client: mgr.GetClient(),
87-
TailingSidecarImage: tailingSidecarImage,
112+
TailingSidecarImage: config.Sidecar.Image,
88113
},
89114
})
90115

0 commit comments

Comments
 (0)