-
Notifications
You must be signed in to change notification settings - Fork 3
Add pool/claim types and controllers for numbered resource allocation #258
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
base: main
Are you sure you want to change the base?
Changes from all commits
04cafef
3a8565c
cd42eba
4836fc9
bc6da88
3df6d1a
d78ee8b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| // SPDX-FileCopyrightText: 2025 SAP SE or an SAP affiliate company and IronCore contributors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| package v1alpha1 | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "net/netip" | ||
|
|
||
| "k8s.io/apimachinery/pkg/api/equality" | ||
| ) | ||
|
|
||
| // IPAddr represents a single IP address (IPv4 or IPv6). | ||
| // | ||
| // +kubebuilder:validation:Type=string | ||
| // +kubebuilder:validation:Format=ip | ||
| // +kubebuilder:validation:Example="192.168.1.1" | ||
| // +kubebuilder:validation:Example="2001:db8::1" | ||
| // +kubebuilder:object:generate=false | ||
| type IPAddr struct { | ||
| netip.Addr `json:"-"` | ||
| } | ||
|
|
||
| func ParseAddr(s string) (IPAddr, error) { | ||
| addr, err := netip.ParseAddr(s) | ||
| if err != nil { | ||
| return IPAddr{}, err | ||
| } | ||
| return IPAddr{addr}, nil | ||
| } | ||
|
|
||
| func MustParseAddr(s string) IPAddr { | ||
| return IPAddr{netip.MustParseAddr(s)} | ||
| } | ||
|
|
||
| // IsZero reports whether a represents the zero value. | ||
| func (a IPAddr) IsZero() bool { | ||
| return !a.IsValid() | ||
| } | ||
|
|
||
| // Equal reports whether a and b represent the same address. | ||
| func (a IPAddr) Equal(b IPAddr) bool { | ||
| return a.Addr == b.Addr | ||
| } | ||
|
|
||
| // MarshalJSON implements [json.Marshaler]. | ||
| func (a IPAddr) MarshalJSON() ([]byte, error) { | ||
| if !a.IsValid() { | ||
| return []byte("null"), nil | ||
| } | ||
| return json.Marshal(a.String()) | ||
| } | ||
|
|
||
| // UnmarshalJSON implements [json.Unmarshaler]. | ||
| func (a *IPAddr) UnmarshalJSON(data []byte) error { | ||
| var str string | ||
| if err := json.Unmarshal(data, &str); err != nil { | ||
| return err | ||
| } | ||
| if str == "" || str == "null" { | ||
| *a = IPAddr{} | ||
| return nil | ||
| } | ||
| addr, err := netip.ParseAddr(str) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| *a = IPAddr{addr} | ||
| return nil | ||
| } | ||
|
|
||
| // DeepCopyInto copies all properties of this object into another object of the same type. | ||
| func (in *IPAddr) DeepCopyInto(out *IPAddr) { | ||
| *out = *in | ||
| } | ||
|
|
||
| // DeepCopy creates a deep copy of the IPAddr. | ||
| func (in *IPAddr) DeepCopy() *IPAddr { | ||
| if in == nil { | ||
| return nil | ||
| } | ||
| out := new(IPAddr) | ||
| in.DeepCopyInto(out) | ||
| return out | ||
| } | ||
|
|
||
| func init() { | ||
| // IPAddr embeds [netip.Addr] which contains unexported fields. | ||
| // [equality.Semantic.DeepEqual] panics on unexported fields, so an | ||
| // explicit equality function is registered here. | ||
| if err := equality.Semantic.AddFunc(func(a, b IPAddr) bool { | ||
| return a.Equal(b) | ||
| }); err != nil { | ||
| panic(err) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| // SPDX-FileCopyrightText: 2025 SAP SE or an SAP affiliate company and IronCore contributors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package v1alpha1 | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "strconv" | ||
| "strings" | ||
| ) | ||
|
|
||
| // IndexRange represents an inclusive range of indices. | ||
| // +kubebuilder:validation:Type=string | ||
| // +kubebuilder:validation:Pattern=`^[0-9]+\.\.[0-9]+$` | ||
| // +kubebuilder:object:generate=false | ||
| type IndexRange struct { | ||
| Start uint64 `json:"-"` | ||
| End uint64 `json:"-"` | ||
|
Comment on lines
+18
to
+19
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The API conventions document does not seem to apply here as this is then later serialized as a string in |
||
| } | ||
|
|
||
| // ParseIndexRange parses a string in the format "start..end" into an [IndexRange]. | ||
| func ParseIndexRange(s string) (IndexRange, error) { | ||
| parts := strings.Split(s, "..") | ||
| if len(parts) != 2 { | ||
| return IndexRange{}, fmt.Errorf("invalid index range %q", s) | ||
| } | ||
| start, err := strconv.ParseUint(strings.TrimSpace(parts[0]), 10, 64) | ||
| if err != nil { | ||
| return IndexRange{}, fmt.Errorf("invalid index range start %q: %w", parts[0], err) | ||
| } | ||
| end, err := strconv.ParseUint(strings.TrimSpace(parts[1]), 10, 64) | ||
| if err != nil { | ||
| return IndexRange{}, fmt.Errorf("invalid index range end %q: %w", parts[1], err) | ||
| } | ||
| if start > end { | ||
| return IndexRange{}, fmt.Errorf("invalid index range %q: start greater than end", s) | ||
| } | ||
| return IndexRange{Start: start, End: end}, nil | ||
| } | ||
|
|
||
| // MustParseIndexRange calls [ParseIndexRange] and panics if it returns an error. | ||
| func MustParseIndexRange(s string) IndexRange { | ||
| r, err := ParseIndexRange(s) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| return r | ||
| } | ||
|
|
||
| // String implements [fmt.Stringer]. | ||
| func (r IndexRange) String() string { | ||
| return fmt.Sprintf("%d..%d", r.Start, r.End) | ||
| } | ||
|
|
||
| // MarshalJSON implements [json.Marshaler]. | ||
| func (r IndexRange) MarshalJSON() ([]byte, error) { | ||
| return json.Marshal(r.String()) | ||
| } | ||
|
|
||
| // UnmarshalJSON implements [json.Unmarshaler]. | ||
| func (r *IndexRange) UnmarshalJSON(data []byte) error { | ||
| var str string | ||
| if err := json.Unmarshal(data, &str); err != nil { | ||
| return err | ||
| } | ||
| if str == "" || str == "null" { | ||
| *r = IndexRange{} | ||
| return nil | ||
| } | ||
| parsed, err := ParseIndexRange(str) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| *r = parsed | ||
| return nil | ||
| } | ||
|
|
||
| // DeepCopyInto copies all properties of this object into another object of the same type. | ||
| func (in *IndexRange) DeepCopyInto(out *IndexRange) { | ||
| *out = *in | ||
| } | ||
|
|
||
| // DeepCopy creates a deep copy of the IndexRange. | ||
| func (in *IndexRange) DeepCopy() *IndexRange { | ||
| if in == nil { | ||
| return nil | ||
| } | ||
| out := new(IndexRange) | ||
| in.DeepCopyInto(out) | ||
| return out | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
2025->2026(other new files are also affected)