Skip to content

Commit 7cb2f32

Browse files
authored
Merge pull request #627 from hjensas/OSPRH-16335
Add operator package
2 parents 3cb2ae8 + efc6a77 commit 7cb2f32

File tree

2 files changed

+202
-0
lines changed

2 files changed

+202
-0
lines changed

modules/common/operator/options.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
Copyright 2025 Red Hat
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package operator
18+
19+
import (
20+
"fmt"
21+
"os"
22+
"strconv"
23+
"time"
24+
25+
"github.com/go-logr/logr"
26+
ctrl "sigs.k8s.io/controller-runtime"
27+
)
28+
29+
// SetManagerOptions - Get options from environment, validate and set controller manager options.
30+
func SetManagerOptions(options *ctrl.Options, setupLog logr.Logger) error {
31+
leaseDuration, err := getEnvInDuration("LEASE_DURATION")
32+
if err != nil {
33+
return err
34+
} else if leaseDuration != 0 {
35+
setupLog.Info("manager configured with lease duration", "seconds", int(leaseDuration.Seconds()))
36+
options.LeaseDuration = &leaseDuration
37+
}
38+
39+
renewDeadline, err := getEnvInDuration("RENEW_DEADLINE")
40+
if err != nil {
41+
return err
42+
} else if renewDeadline != 0 {
43+
setupLog.Info("manager configured with renew deadline", "seconds", int(renewDeadline.Seconds()))
44+
options.RenewDeadline = &renewDeadline
45+
}
46+
47+
retryPeriod, err := getEnvInDuration("RETRY_PERIOD")
48+
if err != nil {
49+
return err
50+
} else if retryPeriod != 0 {
51+
setupLog.Info("manager configured with retry period", "seconds", int(retryPeriod.Seconds()))
52+
options.RetryPeriod = &retryPeriod
53+
}
54+
55+
return nil
56+
}
57+
58+
func getEnvInDuration(envName string) (time.Duration, error) {
59+
var durationInt int64
60+
if durationStr := os.Getenv(envName); durationStr != "" {
61+
var err error
62+
if durationInt, err = strconv.ParseInt(durationStr, 10, 64); err != nil {
63+
return 0, fmt.Errorf("unable to parse provided '%s', err: '%w'", envName, err)
64+
}
65+
}
66+
return time.Duration(durationInt) * time.Second, nil
67+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
2+
Copyright 2025 Red Hat
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package operator
18+
19+
import (
20+
"strconv"
21+
"testing"
22+
"time"
23+
24+
"github.com/go-logr/logr"
25+
ctrl "sigs.k8s.io/controller-runtime"
26+
)
27+
28+
func TestGetEnvInDuration(t *testing.T) {
29+
tests := []struct {
30+
name string
31+
envName string
32+
envValue string
33+
wantErr bool
34+
expect time.Duration
35+
}{
36+
{
37+
name: "Test valid",
38+
envName: "VALID",
39+
envValue: "30",
40+
wantErr: false,
41+
expect: time.Duration(30 * time.Second),
42+
},
43+
{
44+
name: "Test invvalid",
45+
envName: "INVALID",
46+
envValue: "3x0",
47+
wantErr: true,
48+
expect: time.Duration(60 * time.Second),
49+
},
50+
}
51+
for _, tt := range tests {
52+
t.Run(tt.name, func(t *testing.T) {
53+
t.Setenv(tt.envName, tt.envValue)
54+
res, err := getEnvInDuration(tt.envName)
55+
if tt.wantErr {
56+
if err == nil {
57+
t.Errorf("getEnvInDuration() expected error but got none")
58+
}
59+
return
60+
} else if err != nil {
61+
t.Errorf("getEnvInDuration() unexpected error: %v", err)
62+
return
63+
}
64+
if res != tt.expect {
65+
t.Errorf("getEnvInDuration() got = %v, want %v", res, tt.expect)
66+
}
67+
})
68+
}
69+
}
70+
71+
func TestSetManagerOptions(t *testing.T) {
72+
var durationInt int64
73+
var expectedValue time.Duration
74+
setupLog := logr.New(nil)
75+
76+
tests := []struct {
77+
name string
78+
leaseDuration string
79+
renewDeadline string
80+
retryPeriod string
81+
wantErr bool
82+
}{
83+
{
84+
name: "Test SetOperatorOptions valid values",
85+
leaseDuration: "137",
86+
renewDeadline: "107",
87+
retryPeriod: "26",
88+
wantErr: false,
89+
},
90+
{
91+
name: "Test SetOperatorOptions invalid values",
92+
leaseDuration: "foo",
93+
renewDeadline: "bar",
94+
retryPeriod: "INVALID",
95+
wantErr: true,
96+
},
97+
}
98+
99+
for _, tt := range tests {
100+
t.Run(tt.name, func(t *testing.T) {
101+
t.Setenv("LEASE_DURATION", tt.leaseDuration)
102+
t.Setenv("RENEW_DEADLINE", tt.renewDeadline)
103+
t.Setenv("RETRY_PERIOD", tt.retryPeriod)
104+
options := ctrl.Options{}
105+
err := SetManagerOptions(&options, setupLog)
106+
if tt.wantErr {
107+
if err == nil {
108+
t.Errorf("SetOperatorOptions() expected error but got none")
109+
}
110+
return
111+
} else if err != nil {
112+
t.Errorf("SetOperatorOptions() unexpected error: %v", err)
113+
return
114+
}
115+
116+
durationInt, _ = strconv.ParseInt(tt.leaseDuration, 10, 64)
117+
expectedValue = time.Duration(durationInt) * time.Second
118+
if *options.LeaseDuration != expectedValue {
119+
t.Errorf("SetOperatorOptions() got = %v, want %v", options.LeaseDuration, expectedValue)
120+
}
121+
122+
durationInt, _ = strconv.ParseInt(tt.renewDeadline, 10, 64)
123+
expectedValue = time.Duration(durationInt) * time.Second
124+
if *options.RenewDeadline != expectedValue {
125+
t.Errorf("SetOperatorOptions() got = %v, want %v", options.RenewDeadline, expectedValue)
126+
}
127+
128+
durationInt, _ = strconv.ParseInt(tt.retryPeriod, 10, 64)
129+
expectedValue = time.Duration(durationInt) * time.Second
130+
if *options.RetryPeriod != expectedValue {
131+
t.Errorf("SetOperatorOptions() got = %v, want %v", options.RetryPeriod, expectedValue)
132+
}
133+
})
134+
}
135+
}

0 commit comments

Comments
 (0)