Skip to content

Commit

Permalink
fix: make fail fast when parsing invalid content in Kubefile (#1569)
Browse files Browse the repository at this point in the history
Signed-off-by: Allen Sun <shlallen1990@gmail.com>
  • Loading branch information
allencloud authored Jul 12, 2022
1 parent 884513e commit a9919aa
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions pkg/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
v1 "github.com/sealerio/sealer/types/api/v1"
strUtils "github.com/sealerio/sealer/utils/strings"
"github.com/sealerio/sealer/version"
"github.com/sirupsen/logrus"
)

const (
Expand Down Expand Up @@ -111,7 +110,9 @@ func (p *Parser) Parse(kubeFile []byte) (*v1.Image, error) {

switch layerType {
case Arg:
dispatchArg(layerValue, image)
if err := dispatchArg(layerValue, image); err != nil {
return nil, err
}
case Cmd:
dispatchCmd(layerValue, image)
default:
Expand All @@ -134,7 +135,7 @@ func decodeLine(line string) (string, string, error) {
return cmd, cmdline[1], nil
}

func dispatchArg(layerValue string, ima *v1.Image) {
func dispatchArg(layerValue string, ima *v1.Image) error {
if ima.Spec.ImageConfig.Args.Current == nil {
ima.Spec.ImageConfig.Args.Current = map[string]string{}
}
Expand All @@ -143,16 +144,15 @@ func dispatchArg(layerValue string, ima *v1.Image) {
for _, element := range kv {
valueLine := strings.SplitN(element, "=", 2)
if len(valueLine) != 2 {
logrus.Errorf("invalid ARG value %s: ARG format must be key=value", layerValue)
return
return fmt.Errorf("invalid ARG value %s. ARG format must be key=value", layerValue)
}
k := strings.TrimSpace(valueLine[0])
if !strUtils.IsLetterOrNumber(k) {
logrus.Errorf("ARG key must be letter or number, invalid ARG format will ignore this key %s", k)
return
return fmt.Errorf("ARG key must be letter or number, invalid ARG format will ignore this key %s", k)
}
ima.Spec.ImageConfig.Args.Current[k] = strings.TrimSpace(valueLine[1])
}
return nil
}

func dispatchCmd(layerValue string, ima *v1.Image) {
Expand Down

0 comments on commit a9919aa

Please sign in to comment.