Skip to content

Commit

Permalink
fix: failed to update opsrequest (#4951) (#4965)
Browse files Browse the repository at this point in the history
  • Loading branch information
sophon-zt authored Sep 4, 2023
1 parent 01f020d commit 1b7a59b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
2 changes: 1 addition & 1 deletion controllers/apps/operations/reconfigure_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func updateOpsLabelWithReconfigure(obj *appsv1alpha1.OpsRequest, params []core.P
return
}
maxLabelCount--
obj.Labels[key] = cast.ToString(val)
obj.Labels[key] = core.FromValueToString(val)
}
}
updateAnnotation := func(keyFile string, param map[string]interface{}) {
Expand Down
14 changes: 14 additions & 0 deletions internal/configuration/core/config_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ package core

import (
"context"
"regexp"

"github.com/spf13/cast"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"

Expand All @@ -34,6 +36,18 @@ type ParamPairs struct {
UpdatedParams map[string]interface{}
}

const pattern = `^[a-z0-9A-Z]([a-zA-Z0-9\.\-\_]*[a-zA-Z0-9])?$`

var regxPattern = regexp.MustCompile(pattern)

func FromValueToString(val interface{}) string {
str := cast.ToString(val)
if regxPattern.MatchString(str) {
return str
}
return ""
}

// MergeUpdatedConfig replaces the file content of the changed key.
// baseMap is the original configuration file,
// updatedMap is the updated configuration file
Expand Down
54 changes: 54 additions & 0 deletions internal/configuration/core/config_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,57 @@ test=test
})
}
}

func TestFromValueToString(t *testing.T) {
type args struct {
val interface{}
}
tests := []struct {
name string
args args
want string
}{{
name: "test",
args: args{
val: "testTest",
},
want: "testTest",
}, {
name: "test",
args: args{
val: "",
},
want: "",
}, {
name: "test",
args: args{
val: nil,
},
want: "",
}, {
name: "test",
args: args{
val: "/abdet/sds",
},
want: "",
}, {
name: "test",
args: args{
val: "abdet/sds-",
},
want: "",
}, {
name: "test",
args: args{
val: "abcdASls-sda_102.382",
},
want: "abcdASls-sda_102.382",
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := FromValueToString(tt.args.val); got != tt.want {
t.Errorf("FromValueToString() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 1b7a59b

Please sign in to comment.