Skip to content

[WIP] Added quantity format #540

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions pkg/validation/strfmt/quantity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Copyright 2025 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package strfmt

import (
"k8s.io/apimachinery/pkg/api/resource"
Copy link
Member

@liggitt liggitt Jun 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, this repo cannot depend on anything published out of kubernetes/kubernetes/staging, like k8s.io/apimachinery. We can't allow cycles between vendored dependencies used within kubernetes/kubernetes and things published out of kubernetes/kubernetes/staging.

(I haven't read the rest of the PR or the context, just followed the link to this from the apimachinery agenda meeting, but wanted to let you know this import isn't going to work before you got too far down this road)

)

type Quantity string

func init() {
quantity := Quantity("")
Default.Add("quantity", &quantity, isQuantity)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this type definition / registration could be done from outside kube-openapi, e.g. defined in kubernetes/kubernetes or apiextensions-apiserver and then registered

If so, that would avoid the kube-openapi → k8s.io/apimachinery dependency

}

// String converts this value to a string
func (q Quantity) String() string {
return string(q)
}

// DeepCopyInto copies the receiver into out. out must be non-nil.
func (q *Quantity) DeepCopyInto(out *Quantity) {
*out = *q
}

// DeepCopy creates a deep copy of Semver
func (q *Quantity) DeepCopy() *Quantity {
if q == nil {
return nil
}
out := new(Quantity)
q.DeepCopyInto(out)
return out
}

// MarshalText turns this instance into text
func (q Quantity) MarshalText() ([]byte, error) {
return []byte(q), nil
}

// UnmarshalText hydrates this instance from text
func (q *Quantity) UnmarshalText(data []byte) error {
*(q) = Quantity(data)
return nil
}

func isQuantity(str string) bool {
_, err := resource.ParseQuantity(str)
return err == nil
}
1 change: 1 addition & 0 deletions pkg/validation/validate/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const (
stringFormatUUID3 = "uuid3"
stringFormatUUID4 = "uuid4"
stringFormatUUID5 = "uuid5"
stringFormatQuantity = "quantity"

integerFormatInt32 = "int32"
integerFormatInt64 = "int64"
Expand Down
2 changes: 2 additions & 0 deletions pkg/validation/validate/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ func (t *typeValidator) schemaInfoForType(data interface{}) (string, string) {
return stringType, stringFormatUUID4
case strfmt.UUID5, *strfmt.UUID5:
return stringType, stringFormatUUID5
case strfmt.Quantity, *strfmt.Quantity:
return stringType, stringFormatQuantity
// TODO: missing binary (io.ReadCloser)
// TODO: missing json.Number
default:
Expand Down
Loading