Skip to content

Commit

Permalink
util: refactor EvaluateCheckHook
Browse files Browse the repository at this point in the history
Co-Authored-by: Annaraya Narasagond <[email protected]>
Signed-off-by: Raghavendra Talur <[email protected]>
  • Loading branch information
raghavendra-talur and asn1809 committed Dec 9, 2024
1 parent cec48f0 commit 2753759
Showing 1 changed file with 26 additions and 43 deletions.
69 changes: 26 additions & 43 deletions internal/controller/util/json_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,74 +26,57 @@ const (
expectedNumberOfJSONPaths = 2
)

func EvaluateCheckHook(client client.Client, hook *kubeobjects.HookSpec, log logr.Logger) (bool, error) {
timeout := getTimeoutValue(hook)
nsScopedName := types.NamespacedName{
Namespace: hook.Namespace,
Name: hook.NameSelector,
}
func getJSONObject(k8sClient client.Client, hook *kubeobjects.HookSpec) (client.Object, error) {
var obj client.Object

switch hook.SelectResource {
case "pod":
// handle pod type
resource := &corev1.Pod{}

err := WaitUntilResourceExists(client, resource, nsScopedName, time.Duration(timeout)*time.Second)
if err != nil {
return false, err
}

return EvaluateCheckHookExp(hook.Chk.Condition, resource)
obj = &corev1.Pod{}
case "deployment":
// handle deployment type
resource := &appsv1.Deployment{}

err := WaitUntilResourceExists(client, resource, nsScopedName, time.Duration(timeout)*time.Second)
if err != nil {
return false, err
}

return EvaluateCheckHookExp(hook.Chk.Condition, resource)
obj = &appsv1.Deployment{}
case "statefulset":
// handle statefulset type
resource := &appsv1.StatefulSet{}

err := WaitUntilResourceExists(client, resource, nsScopedName, time.Duration(timeout)*time.Second)
if err != nil {
return false, err
}

return EvaluateCheckHookExp(hook.Chk.Condition, resource)
obj = &appsv1.StatefulSet{}
default:
return obj, fmt.Errorf("unsupported resource type %s", hook.SelectResource)
}

return false, nil
err := k8sClient.Get(context.Background(),
types.NamespacedName{Name: hook.NameSelector, Namespace: hook.Namespace},
obj)

return obj, err
}

func WaitUntilResourceExists(client client.Client, obj client.Object, nsScopedName types.NamespacedName,
timeout time.Duration,
) error {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
func EvaluateCheckHook(k8sClient client.Client, hook *kubeobjects.HookSpec, log logr.Logger) (bool, error) {
timeout := getTimeoutValue(hook)

ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
defer cancel()

ticker := time.NewTicker(pollInterval * time.Millisecond) // Poll every 100 milliseconds
ticker := time.NewTicker(pollInterval * time.Millisecond)
defer ticker.Stop()

for {
select {
case <-ctx.Done():
return fmt.Errorf("timeout waiting for resource %s to be ready: %w", nsScopedName.Name, ctx.Err())
return false, fmt.Errorf("timeout waiting for resource %s to be ready: %w", hook.NameSelector, ctx.Err())
case <-ticker.C:
err := client.Get(context.Background(), nsScopedName, obj)
obj, err := getJSONObject(k8sClient, hook)
if err != nil {
if k8serrors.IsNotFound(err) {
// Resource not found, continue polling
continue
}

return err // Some other error occurred, return it
return false, err // Some other error occurred, return it
}

res, err := EvaluateCheckHookExp(hook.Chk.Condition, obj)
if err != nil {
continue // This may mean that expression is not evaluated
}

return nil // Resource is ready
return res, nil
}
}
}
Expand Down

0 comments on commit 2753759

Please sign in to comment.