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

Generate Quantity helper methods for dies #148

Merged
merged 1 commit into from
Jun 19, 2024

Conversation

scothis
Copy link
Collaborator

@scothis scothis commented Jun 18, 2024

Dies that have fields of resource.Quantity or corev1.ResourceList often have setter methods to parse a string formatted quantity into the structured value. Rather than the user defining these values, we can generate them when we see a field of the type.

for resource.Quantity:

func (d *MyResourceDie) CapacityString(quantity string) *MyResourceDie {
	q := resource.MustParse(quantity)
	return d.Capacity(q)
}

for corev1.ResourceList:

func (d *MyResourceDie) AddLimit(name corev1.ResourceName, quantity resource.Quantity) *MyResourceDie {
	return d.DieStamp(func(r *corev1.ResourceQuotaSpec) {
		if r.Limits == nil {
			r.Limits = corev1.ResourceList{}
		}
		r.Limits[name] = quantity
	})
}

func (d *MyResourceDie) AddLimitString(name corev1.ResourceName, quantity string) *MyResourceDie {
    q := resource.MustParse(quantity)
	return d.AddLimit(name, q)
}

@scothis scothis requested a review from mamachanko June 18, 2024 22:37
Dies that have fields of resource.Quantity or corev1.ResourceList often
have setter methods to parse a string formatted quantity into the
structured value. Rather than the user defining these values, we can
generate them when we see a field of the type.

for resource.Quantity:
```
func (d *MyResourceDie) CapacityString(quantity string) *MyResourceDie {
	q := resource.MustParse(quantity)
	return d.Capacity(q)
}
```

for corev1.ResourceList:
```
func (d *MyResourceDie) AddLimit(name corev1.ResourceName, quantity resource.Quantity) *MyResourceDie {
	return d.DieStamp(func(r *corev1.ResourceQuotaSpec) {
		if r.Limits == nil {
			r.Limits = corev1.ResourceList{}
		}
		r.Limits[name] = quantity
	})
}

func (d *MyResourceDie) AddLimitString(name corev1.ResourceName, quantity string) *MyResourceDie {
    q := resource.MustParse(quantity)
	return d.AddLimit(name, q)
}
```

Signed-off-by: Scott Andrews <scott@andrews.me>
Copy link

codecov bot commented Jun 19, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 9.71%. Comparing base (9bf2706) to head (f3a783e).

Additional details and impacted files
@@           Coverage Diff            @@
##            main    #148      +/-   ##
========================================
+ Coverage   9.24%   9.71%   +0.47%     
========================================
  Files         69      69              
  Lines       3385    3221     -164     
========================================
  Hits         313     313              
+ Misses      3069    2905     -164     
  Partials       3       3              

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

@scothis scothis merged commit b4c0177 into reconcilerio:main Jun 19, 2024
4 checks passed
@scothis scothis deleted the quantity branch June 19, 2024 17:50
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