Skip to content

Commit 61744ff

Browse files
tedhtchangyhwang
andauthored
Refactor some lmesreconcile methods (#323)
* Refactor lmes reconcile optoins Signed-off-by: ted chang <[email protected]> * Update controllers/lmes/lmevaljob_controller.go Co-authored-by: Yihong Wang <[email protected]> * Update controllers/lmes/lmevaljob_controller.go Co-authored-by: Yihong Wang <[email protected]> Signed-off-by: ted chang <[email protected]> --------- Signed-off-by: ted chang <[email protected]> Co-authored-by: Yihong Wang <[email protected]>
1 parent fe7c0bf commit 61744ff

File tree

4 files changed

+231
-231
lines changed

4 files changed

+231
-231
lines changed

controllers/lmes/config.go

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
Copyright 2024.
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 lmes
18+
19+
import (
20+
"fmt"
21+
"reflect"
22+
"strconv"
23+
"strings"
24+
"time"
25+
26+
"github.com/go-logr/logr"
27+
corev1 "k8s.io/api/core/v1"
28+
)
29+
30+
var options *serviceOptions = &serviceOptions{
31+
DriverImage: DefaultDriverImage,
32+
PodImage: DefaultPodImage,
33+
PodCheckingInterval: DefaultPodCheckingInterval,
34+
ImagePullPolicy: DefaultImagePullPolicy,
35+
MaxBatchSize: DefaultMaxBatchSize,
36+
DetectDevice: DefaultDetectDevice,
37+
DefaultBatchSize: DefaultBatchSize,
38+
}
39+
40+
type serviceOptions struct {
41+
PodImage string
42+
DriverImage string
43+
PodCheckingInterval time.Duration
44+
ImagePullPolicy corev1.PullPolicy
45+
MaxBatchSize int
46+
DefaultBatchSize int
47+
DetectDevice bool
48+
}
49+
50+
func constructOptionsFromConfigMap(log *logr.Logger, configmap *corev1.ConfigMap) error {
51+
52+
rv := reflect.ValueOf(options).Elem()
53+
var msgs []string
54+
55+
for idx, cap := 0, rv.NumField(); idx < cap; idx++ {
56+
frv := rv.Field(idx)
57+
fname := rv.Type().Field(idx).Name
58+
configKey, ok := optionKeys[fname]
59+
if !ok {
60+
continue
61+
}
62+
63+
if v, found := configmap.Data[configKey]; found {
64+
var err error
65+
switch frv.Type().Name() {
66+
case "string":
67+
frv.SetString(v)
68+
case "bool":
69+
val, err := strconv.ParseBool(v)
70+
if err != nil {
71+
val = DefaultDetectDevice
72+
msgs = append(msgs, fmt.Sprintf("invalid setting for %v: %v, use default setting instead", optionKeys[fname], val))
73+
}
74+
frv.SetBool(val)
75+
case "int":
76+
var intVal int
77+
intVal, err = strconv.Atoi(v)
78+
if err == nil {
79+
frv.SetInt(int64(intVal))
80+
}
81+
case "Duration":
82+
var d time.Duration
83+
d, err = time.ParseDuration(v)
84+
if err == nil {
85+
frv.Set(reflect.ValueOf(d))
86+
}
87+
case "PullPolicy":
88+
if p, found := pullPolicyMap[corev1.PullPolicy(v)]; found {
89+
frv.Set(reflect.ValueOf(p))
90+
} else {
91+
err = fmt.Errorf("invalid PullPolicy")
92+
}
93+
default:
94+
return fmt.Errorf("can not handle the config %v, type: %v", optionKeys[fname], frv.Type().Name())
95+
}
96+
97+
if err != nil {
98+
msgs = append(msgs, fmt.Sprintf("invalid setting for %v: %v, use default setting instead", optionKeys[fname], v))
99+
}
100+
}
101+
}
102+
103+
if len(msgs) > 0 && log != nil {
104+
log.Error(fmt.Errorf("some settings in the configmap are invalid"), strings.Join(msgs, "\n"))
105+
}
106+
107+
return nil
108+
}

controllers/lmes/driver/driver_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func genRandomSocketPath() string {
5454
return p
5555
}
5656

57-
func runDirverAndWait4Complete(t *testing.T, driver Driver, returnError bool) (progressMsgs []string, results string) {
57+
func runDriverAndWait4Complete(t *testing.T, driver Driver, returnError bool) (progressMsgs []string, results string) {
5858
go func() {
5959
if returnError {
6060
assert.NotNil(t, driver.Run())
@@ -88,7 +88,7 @@ func Test_Driver(t *testing.T) {
8888
})
8989
assert.Nil(t, err)
9090

91-
runDirverAndWait4Complete(t, driver, false)
91+
runDriverAndWait4Complete(t, driver, false)
9292

9393
assert.Nil(t, driver.Shutdown())
9494
assert.Nil(t, os.Remove("./stderr.log"))
@@ -105,7 +105,7 @@ func Test_Wait4Shutdown(t *testing.T) {
105105
})
106106
assert.Nil(t, err)
107107

108-
runDirverAndWait4Complete(t, driver, false)
108+
runDriverAndWait4Complete(t, driver, false)
109109

110110
// can still get the status even the user program finishes
111111
time.Sleep(time.Second * 3)
@@ -132,7 +132,7 @@ func Test_ProgressUpdate(t *testing.T) {
132132
})
133133
assert.Nil(t, err)
134134

135-
msgs, _ := runDirverAndWait4Complete(t, driver, false)
135+
msgs, _ := runDriverAndWait4Complete(t, driver, false)
136136

137137
assert.Equal(t, []string{
138138
"initializing the evaluation job",
@@ -156,7 +156,7 @@ func Test_DetectDeviceError(t *testing.T) {
156156
})
157157
assert.Nil(t, err)
158158

159-
msgs, _ := runDirverAndWait4Complete(t, driver, true)
159+
msgs, _ := runDriverAndWait4Complete(t, driver, true)
160160
assert.Equal(t, []string{
161161
"failed to detect available device(s): exit status 1",
162162
}, msgs)
@@ -216,7 +216,7 @@ func Test_TaskRecipes(t *testing.T) {
216216
})
217217
assert.Nil(t, err)
218218

219-
msgs, _ := runDirverAndWait4Complete(t, driver, false)
219+
msgs, _ := runDriverAndWait4Complete(t, driver, false)
220220

221221
assert.Equal(t, []string{
222222
"initializing the evaluation job",
@@ -264,7 +264,7 @@ func Test_CustomCards(t *testing.T) {
264264

265265
os.Mkdir("cards", 0750)
266266

267-
msgs, _ := runDirverAndWait4Complete(t, driver, false)
267+
msgs, _ := runDriverAndWait4Complete(t, driver, false)
268268

269269
assert.Equal(t, []string{
270270
"initializing the evaluation job",
@@ -303,7 +303,7 @@ func Test_ProgramError(t *testing.T) {
303303
})
304304
assert.Nil(t, err)
305305

306-
msgs, _ := runDirverAndWait4Complete(t, driver, true)
306+
msgs, _ := runDriverAndWait4Complete(t, driver, true)
307307

308308
assert.Equal(t, []string{
309309
"initializing the evaluation job",

0 commit comments

Comments
 (0)