Skip to content

Commit

Permalink
Generate Quantity helper methods for dies (#148)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
scothis authored Jun 19, 2024
1 parent 9bf2706 commit b4c0177
Show file tree
Hide file tree
Showing 21 changed files with 1,193 additions and 315 deletions.
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

- [Using dies](#using-dies)
- [Common methods](#common-methods)
- [Helper methods](#helper-methods)
- [k8s.io/apimachinery/pkg/util/intstr#IntOrString](#k8sioapimachinerypkgutilintstrintorstring)
- [k8s.io/apimachinery/pkg/api/resource#Quantity](#k8sioapimachinerypkgapiresourcequantity)
- [k8s.io/api/core/v1#ResourceList](#k8sioapicorev1resourcelist)
- [Creating dies](#creating-dies)
- [diegen](#diegen)
- [die markers](#die-markers)
Expand Down Expand Up @@ -241,6 +245,58 @@ type MyResourceDie interface {
}
```

### Helper methods

Each exported field on the resource backing the die has a setter method defined. Some types offer additional helper methods to make common conversions easier.

#### k8s.io/apimachinery/pkg/util/intstr#IntOrString

```go
type MyResourceDie interface {
// continued

Port(port intstr.IntOrString) *MyResourceDie

// PortInt sets Port with the int value.
PortInt(port int) *MyResourceDie

// PortString sets Port with the string value.
PortString(port string) *MyResourceDie
}
```

#### k8s.io/apimachinery/pkg/api/resource#Quantity

```go
type MyResourceDie interface {
// continued

Memory(memory resource.Quantity) *MyResourceDie

// MemoryString sets Memory by parsing the string as a Quantity. Panics if
// the string is not parsable.
MemoryString(memory string) *MyResourceDie
}
```

#### k8s.io/api/core/v1#ResourceList

```go
type MyResourceDie interface {
// continued

Limits(limits corev1.ResourceList) *MyResourceDie

// AddLimit sets a single quantity on the Limits resource list.
AddLimit(name string, limit resource.Quantity) *MyResourceDie

// AddLimitString parses the quantity setting a single value on the Limits
// resource list. Panics if the string is not parsable.
AddLimitString(name string, limit string) *MyResourceDie
}

```

## Creating dies

Dies are primarily generated for types from [die markers](#die-markers) using
Expand Down Expand Up @@ -284,6 +340,8 @@ func (d *DeploymentStatusDie) ConditionsDie(conditions ...*diemetav1.ConditionDi
}
```



### diegen

Install diegen:
Expand Down
250 changes: 250 additions & 0 deletions apis/apps/v1/zz_generated.die.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b4c0177

Please sign in to comment.