From 3968676255b4d2fa378ce5955910603e5a29ffba Mon Sep 17 00:00:00 2001 From: Shiming Zhang Date: Mon, 26 Aug 2024 22:26:31 +0800 Subject: [PATCH] Move parameters to expression --- pkg/kwokctl/cmd/scale/scale.go | 3 +- pkg/kwokctl/scale/scale.go | 27 ---------------- pkg/utils/expression/parameters.go | 49 ++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 28 deletions(-) create mode 100644 pkg/utils/expression/parameters.go diff --git a/pkg/kwokctl/cmd/scale/scale.go b/pkg/kwokctl/cmd/scale/scale.go index 9818a792a..d51547c27 100644 --- a/pkg/kwokctl/cmd/scale/scale.go +++ b/pkg/kwokctl/cmd/scale/scale.go @@ -34,6 +34,7 @@ import ( "sigs.k8s.io/kwok/pkg/kwokctl/scale" "sigs.k8s.io/kwok/pkg/log" "sigs.k8s.io/kwok/pkg/utils/client" + "sigs.k8s.io/kwok/pkg/utils/expression" "sigs.k8s.io/kwok/pkg/utils/slices" ) @@ -115,7 +116,7 @@ func runE(ctx context.Context, flags *flagpole, args []string) error { } } - parameters, err := scale.NewParameters(ctx, krc.Parameters, flags.Params) + parameters, err := expression.NewParameters(ctx, krc.Parameters, flags.Params) if err != nil { return err } diff --git a/pkg/kwokctl/scale/scale.go b/pkg/kwokctl/scale/scale.go index 609371b4d..ba9d8d692 100644 --- a/pkg/kwokctl/scale/scale.go +++ b/pkg/kwokctl/scale/scale.go @@ -36,7 +36,6 @@ import ( "sigs.k8s.io/kwok/pkg/kwokctl/snapshot" "sigs.k8s.io/kwok/pkg/log" "sigs.k8s.io/kwok/pkg/utils/client" - "sigs.k8s.io/kwok/pkg/utils/expression" "sigs.k8s.io/kwok/pkg/utils/gotpl" utilsnet "sigs.k8s.io/kwok/pkg/utils/net" "sigs.k8s.io/kwok/pkg/utils/yaml" @@ -300,32 +299,6 @@ func Scale(ctx context.Context, clientset client.Clientset, conf Config) error { return nil } -// NewParameters parses the parameters. -func NewParameters(ctx context.Context, raw json.RawMessage, params []string) (any, error) { - var param any - err := json.Unmarshal(raw, ¶m) - if err != nil { - return nil, fmt.Errorf("unmarshal params error: %w", err) - } - - for _, p := range params { - q, err := expression.NewQuery(p) - if err != nil { - return nil, fmt.Errorf("parse param %s error: %w", p, err) - } - datas, err := q.Execute(ctx, param) - if err != nil { - return nil, fmt.Errorf("execute param %s with %v error: %w", p, param, err) - } - if len(datas) != 1 { - return nil, fmt.Errorf("unexpected result: %v", datas) - } - param = datas[0] - } - - return param, nil -} - var ( labelNameKey = "kwok.x-k8s.io/kwokctl-scale" ) diff --git a/pkg/utils/expression/parameters.go b/pkg/utils/expression/parameters.go new file mode 100644 index 000000000..01fee06d2 --- /dev/null +++ b/pkg/utils/expression/parameters.go @@ -0,0 +1,49 @@ +/* +Copyright 2024 The Kubernetes Authors. + +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 expression + +import ( + "context" + "encoding/json" + "fmt" +) + +// NewParameters parses the parameters. +func NewParameters(ctx context.Context, raw json.RawMessage, params []string) (any, error) { + var param any + err := json.Unmarshal(raw, ¶m) + if err != nil { + return nil, fmt.Errorf("unmarshal params error: %w", err) + } + + for _, p := range params { + q, err := NewQuery(p) + if err != nil { + return nil, fmt.Errorf("parse param %s error: %w", p, err) + } + datas, err := q.Execute(ctx, param) + if err != nil { + return nil, fmt.Errorf("execute param %s with %v error: %w", p, param, err) + } + if len(datas) != 1 { + return nil, fmt.Errorf("unexpected result: %v", datas) + } + param = datas[0] + } + + return param, nil +}