Skip to content

Refactor some lmesreconcile methods #323

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions controllers/lmes/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
Copyright 2024.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package lmes

import (
"fmt"
"reflect"
"strconv"
"strings"
"time"

"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
)

var options *serviceOptions = &serviceOptions{
DriverImage: DefaultDriverImage,
PodImage: DefaultPodImage,
PodCheckingInterval: DefaultPodCheckingInterval,
ImagePullPolicy: DefaultImagePullPolicy,
MaxBatchSize: DefaultMaxBatchSize,
DetectDevice: DefaultDetectDevice,
DefaultBatchSize: DefaultBatchSize,
}

type serviceOptions struct {
PodImage string
DriverImage string
PodCheckingInterval time.Duration
ImagePullPolicy corev1.PullPolicy
MaxBatchSize int
DefaultBatchSize int
DetectDevice bool
}

func constructOptionsFromConfigMap(log *logr.Logger, configmap *corev1.ConfigMap) error {

rv := reflect.ValueOf(options).Elem()
var msgs []string

for idx, cap := 0, rv.NumField(); idx < cap; idx++ {
frv := rv.Field(idx)
fname := rv.Type().Field(idx).Name
configKey, ok := optionKeys[fname]
if !ok {
continue
}

if v, found := configmap.Data[configKey]; found {
var err error
switch frv.Type().Name() {
case "string":
frv.SetString(v)
case "bool":
val, err := strconv.ParseBool(v)
if err != nil {
val = DefaultDetectDevice
msgs = append(msgs, fmt.Sprintf("invalid setting for %v: %v, use default setting instead", optionKeys[fname], val))
}
frv.SetBool(val)
case "int":
var intVal int
intVal, err = strconv.Atoi(v)
if err == nil {
frv.SetInt(int64(intVal))
}
case "Duration":
var d time.Duration
d, err = time.ParseDuration(v)
if err == nil {
frv.Set(reflect.ValueOf(d))
}
case "PullPolicy":
if p, found := pullPolicyMap[corev1.PullPolicy(v)]; found {
frv.Set(reflect.ValueOf(p))
} else {
err = fmt.Errorf("invalid PullPolicy")
}
default:
return fmt.Errorf("can not handle the config %v, type: %v", optionKeys[fname], frv.Type().Name())
}

if err != nil {
msgs = append(msgs, fmt.Sprintf("invalid setting for %v: %v, use default setting instead", optionKeys[fname], v))
}
}
}

if len(msgs) > 0 && log != nil {
log.Error(fmt.Errorf("some settings in the configmap are invalid"), strings.Join(msgs, "\n"))
}

return nil
}
16 changes: 8 additions & 8 deletions controllers/lmes/driver/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func genRandomSocketPath() string {
return p
}

func runDirverAndWait4Complete(t *testing.T, driver Driver, returnError bool) (progressMsgs []string, results string) {
func runDriverAndWait4Complete(t *testing.T, driver Driver, returnError bool) (progressMsgs []string, results string) {
go func() {
if returnError {
assert.NotNil(t, driver.Run())
Expand Down Expand Up @@ -88,7 +88,7 @@ func Test_Driver(t *testing.T) {
})
assert.Nil(t, err)

runDirverAndWait4Complete(t, driver, false)
runDriverAndWait4Complete(t, driver, false)

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

runDirverAndWait4Complete(t, driver, false)
runDriverAndWait4Complete(t, driver, false)

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

msgs, _ := runDirverAndWait4Complete(t, driver, false)
msgs, _ := runDriverAndWait4Complete(t, driver, false)

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

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

msgs, _ := runDirverAndWait4Complete(t, driver, false)
msgs, _ := runDriverAndWait4Complete(t, driver, false)

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

os.Mkdir("cards", 0750)

msgs, _ := runDirverAndWait4Complete(t, driver, false)
msgs, _ := runDriverAndWait4Complete(t, driver, false)

assert.Equal(t, []string{
"initializing the evaluation job",
Expand Down Expand Up @@ -303,7 +303,7 @@ func Test_ProgramError(t *testing.T) {
})
assert.Nil(t, err)

msgs, _ := runDirverAndWait4Complete(t, driver, true)
msgs, _ := runDriverAndWait4Complete(t, driver, true)

assert.Equal(t, []string{
"initializing the evaluation job",
Expand Down
Loading
Loading