Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add converge to field step which improves upon converge to selector #168

Merged
merged 7 commits into from
Jan 23, 2025

Conversation

dpadhiar
Copy link
Collaborator

@dpadhiar dpadhiar commented Jan 22, 2025

Currently the converge to selector syntax for a step in a feature file does not accommodate for two cases that came up in our testing:

  • if the selector contains an array (ie. containers[0].image) it will not be able to traverse this path to check the value
  • if the value of a selector's path is not a string, the value will not be checked properly (ie. replicaCount=2 will fail since 2 is considered a string when passed as part of the key=value selector)

This pull requests adds support for both of these cases by incorporating a new step: ResourceShouldConvergeToField

For managing arrays in a path, I was able to work with a colleague @afugazzotto who has previously added a similar function to the Numaplane open source project.

Here is the function that the solution in this pull request is based on which is used in Numaplane to get the full path to a field in a Kubernetes resource.

The function takes data as the resource and path as the key value of our selector which is a path to our field we want to check. It recurses after moving down each field of the path and to accommodate for arrays, we check if the current field contains the [ character which tells us that it is an array. After doing so, we can isolate the index value and recurse on the array[index] value. There are new unit test cases created for the new ResourceShouldConvergeToField function test to show this new functionality.

func ExtractField(data any, path []string) (any, error) {
	if len(path) == 0 || data == nil {
		return data, nil
	}

	currKey := path[0]

	maybeArr := strings.Split(currKey, "[")
	if len(maybeArr) >= 2 {
		indexStr := strings.TrimSuffix(maybeArr[1], "]")
		i, err := strconv.Atoi(indexStr)
		if err != nil {
			return nil, err
		}
		arr := data.(map[string]any)[maybeArr[0]].([]any)
		return ExtractField(arr[i], path[1:])
	}

	for key, val := range data.(map[string]any) {
		if key == currKey {
			return ExtractField(val, path[1:])
		}
	}

	return nil, nil
}

After getting the value from ExtractField, we need to check what type it is to accurately check it against a string or integer.
Once we know the type of the returned value, we will convert our value from the selector to an integer if needed or continue as a string.

var convertedValue any
switch val.(type) {
case int, int64:
	convertedValue, err = strconv.ParseInt(value, 10, 64)
	if err != nil {
		return err
	}
case string:
	convertedValue = value

@dpadhiar dpadhiar changed the title feat: improve converge to selector command feat: add converge to field step which improves upon converge to selector Jan 22, 2025
@garomonegro
Copy link
Collaborator

@dpadhiar thank you for the contribution!

Copy link

codecov bot commented Jan 23, 2025

Codecov Report

Attention: Patch coverage is 60.75949% with 31 lines in your changes missing coverage. Please review.

Project coverage is 43.88%. Comparing base (89d8f79) to head (1a39bc5).
Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
pkg/kube/unstructured/unstructured.go 60.41% 14 Missing and 5 partials ⚠️
pkg/kube/kube.go 0.00% 6 Missing ⚠️
internal/util/util.go 79.16% 4 Missing and 1 partial ⚠️
kubedog.go 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #168      +/-   ##
==========================================
- Coverage   44.86%   43.88%   -0.99%     
==========================================
  Files          19       19              
  Lines        2309     2388      +79     
==========================================
+ Hits         1036     1048      +12     
- Misses       1155     1217      +62     
- Partials      118      123       +5     
Flag Coverage Δ
unittests 43.88% <60.75%> (-0.99%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@dpadhiar dpadhiar requested a review from garomonegro January 23, 2025 18:35
@garomonegro garomonegro marked this pull request as ready for review January 23, 2025 21:06
@garomonegro garomonegro requested review from a team as code owners January 23, 2025 21:06
@garomonegro garomonegro merged commit ecb36b9 into keikoproj:master Jan 23, 2025
6 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants