-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new Kanister function WaitV2 (#1836)
* Switch to go template syntax for Wait conditions (#1635) * Switch to go template syntax for Wait conditions Signed-off-by: Prasad Ghangal <prasad.ghangal@gmail.com> * Update K8ssandra BP as per the new Wait func syntax Signed-off-by: Prasad Ghangal <prasad.ghangal@gmail.com> Signed-off-by: Prasad Ghangal <prasad.ghangal@gmail.com> * Revert "Switch to go template syntax for Wait conditions (#1635)" (#1833) This reverts commit 2be38c8. * Add new Kanister function WaitV2 Signed-off-by: Prasad Ghangal <prasad.ghangal@gmail.com> * Add documentation about new WaitV2 function Signed-off-by: Prasad Ghangal <prasad.ghangal@gmail.com> * Correct copyright headers Signed-off-by: Prasad Ghangal <prasad.ghangal@gmail.com> * Update docs/functions.rst Co-authored-by: Pavan Navarathna <6504783+pavannd1@users.noreply.github.com> * Update docs/functions.rst Co-authored-by: Pavan Navarathna <6504783+pavannd1@users.noreply.github.com> * Update docs/functions.rst Co-authored-by: Pavan Navarathna <6504783+pavannd1@users.noreply.github.com> Signed-off-by: Prasad Ghangal <prasad.ghangal@gmail.com> Co-authored-by: Pavan Navarathna <6504783+pavannd1@users.noreply.github.com>
- Loading branch information
1 parent
dd1ffab
commit 214da0f
Showing
5 changed files
with
445 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// Copyright 2023 The Kanister 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 function | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"strings" | ||
"time" | ||
|
||
"github.com/pkg/errors" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/cli-runtime/pkg/printers" | ||
"k8s.io/client-go/dynamic" | ||
|
||
kanister "github.com/kanisterio/kanister/pkg" | ||
"github.com/kanisterio/kanister/pkg/kube" | ||
"github.com/kanisterio/kanister/pkg/param" | ||
) | ||
|
||
const ( | ||
// WaitV2FuncName specifies the function name | ||
WaitV2FuncName = "WaitV2" | ||
WaitV2TimeoutArg = "timeout" | ||
WaitV2ConditionsArg = "conditions" | ||
) | ||
|
||
func init() { | ||
_ = kanister.Register(&waitV2Func{}) | ||
} | ||
|
||
var _ kanister.Func = (*waitV2Func)(nil) | ||
|
||
type waitV2Func struct{} | ||
|
||
func (*waitV2Func) Name() string { | ||
return WaitV2FuncName | ||
} | ||
|
||
func (ktf *waitV2Func) Exec(ctx context.Context, tp param.TemplateParams, args map[string]interface{}) (map[string]interface{}, error) { | ||
var timeout string | ||
if err := Arg(args, WaitV2TimeoutArg, &timeout); err != nil { | ||
return nil, err | ||
} | ||
|
||
// get the 'conditions' from the unrendered arguments list. | ||
// they will be evaluated in the 'evaluateCondition()` function. | ||
var conditions WaitConditions | ||
if err := Arg(args, WaitV2ConditionsArg, &conditions); err != nil { | ||
return nil, err | ||
} | ||
|
||
dynCli, err := kube.NewDynamicClient() | ||
if err != nil { | ||
return nil, err | ||
} | ||
timeoutDur, err := time.ParseDuration(timeout) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "Failed to parse timeout") | ||
} | ||
err = waitForCondition(ctx, dynCli, conditions, timeoutDur, tp, evaluateWaitV2Condition) | ||
return nil, err | ||
} | ||
|
||
func (*waitV2Func) RequiredArgs() []string { | ||
return []string{ | ||
WaitV2TimeoutArg, | ||
WaitV2ConditionsArg, | ||
} | ||
} | ||
|
||
func (*waitV2Func) Arguments() []string { | ||
return []string{ | ||
WaitV2TimeoutArg, | ||
WaitV2ConditionsArg, | ||
} | ||
} | ||
|
||
// evaluateWaitV2Condition evaluate the go template condition | ||
func evaluateWaitV2Condition(ctx context.Context, dynCli dynamic.Interface, cond Condition, tp param.TemplateParams) (bool, error) { | ||
objRef, err := resolveWaitConditionObjRefs(cond, tp) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
obj, err := fetchObjectFromRef(ctx, dynCli, objRef) | ||
if err != nil { | ||
return false, err | ||
} | ||
value, err := evaluateGoTemplate(obj, cond.Condition) | ||
if err != nil { | ||
return false, err | ||
} | ||
return strings.TrimSpace(value) == "true", nil | ||
} | ||
|
||
func evaluateGoTemplate(obj runtime.Object, goTemplateStr string) (string, error) { | ||
var buff bytes.Buffer | ||
jp, err := printers.NewGoTemplatePrinter([]byte(goTemplateStr)) | ||
if err != nil { | ||
return "", nil | ||
} | ||
err = jp.PrintObj(obj, &buff) | ||
return buff.String(), err | ||
} |
Oops, something went wrong.