Skip to content

Commit

Permalink
Move parameters to expression
Browse files Browse the repository at this point in the history
  • Loading branch information
wzshiming committed Sep 27, 2024
1 parent 43435cd commit 3968676
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 28 deletions.
3 changes: 2 additions & 1 deletion pkg/kwokctl/cmd/scale/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Expand Down
27 changes: 0 additions & 27 deletions pkg/kwokctl/scale/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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, &param)
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"
)
Expand Down
49 changes: 49 additions & 0 deletions pkg/utils/expression/parameters.go
Original file line number Diff line number Diff line change
@@ -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, &param)
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
}

0 comments on commit 3968676

Please sign in to comment.