Chore: apimachinery types cleanup (#107988)

pull/106996/head
Ryan McKinley 1 week ago committed by GitHub
parent 13a89d4ae3
commit 06c00e4fa7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 31
      pkg/apimachinery/apis/common/v0alpha1/types.go
  2. 44
      pkg/apimachinery/apis/common/v0alpha1/unstructured.go
  3. 6
      pkg/apimachinery/apis/common/v0alpha1/unstructured_test.go
  4. 27
      pkg/apimachinery/apis/common/v0alpha1/zz_generated.openapi.go
  5. 88
      pkg/apimachinery/utils/blob.go
  6. 82
      pkg/apimachinery/utils/meta.go
  7. 2
      public/api-merged.json
  8. 2
      public/openapi3.json

@ -1,23 +1,46 @@
package v0alpha1 package v0alpha1
import ( import (
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
) )
// Similar to // Similar to
// https://dev-k8sref-io.web.app/docs/common-definitions/objectreference-/ // https://dev-k8sref-io.web.app/docs/common-definitions/objectreference-/
// ObjectReference contains enough information to let you inspect or modify the referred object. // ObjectReference contains enough information to let you inspect or modify the referred object.
type ObjectReference struct { type ObjectReference struct {
Resource string `json:"resource,omitempty"`
Namespace string `json:"namespace,omitempty"`
Name string `json:"name,omitempty"`
// APIGroup is the name of the API group that contains the referred object. // APIGroup is the name of the API group that contains the referred object.
// The empty string represents the core API group. // The empty string represents the core API group.
APIGroup string `json:"apiGroup,omitempty"` APIGroup string `json:"apiGroup,omitempty"`
// APIVersion is the version of the API group that contains the referred object. // APIVersion is the version of the API group that contains the referred object.
APIVersion string `json:"apiVersion,omitempty"` APIVersion string `json:"apiVersion,omitempty"`
// See https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#types-kinds
Kind string `json:"kind,omitempty"`
// Tenant isolation
Namespace string `json:"namespace,omitempty"`
// Explicit resource identifier
Name string `json:"name,omitempty"`
// May contain a valid JSON/Go field access statement
FieldPath string `json:"fieldPath,omitempty"`
// Sepcific deployment of an object
UID types.UID `json:"uid,omitempty"`
}
func (r ObjectReference) ToOwnerReference() metav1.OwnerReference {
return metav1.OwnerReference{
APIVersion: fmt.Sprintf("%s/%s", r.APIGroup, r.APIVersion),
Kind: r.Kind,
Name: r.Name,
UID: r.UID,
}
} }
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

@ -13,9 +13,9 @@ import (
// Unstructured allows objects that do not have Golang structs registered to be manipulated // Unstructured allows objects that do not have Golang structs registered to be manipulated
// generically. // generically.
type Unstructured struct { type Unstructured struct {
// Object is a JSON compatible map with string, float, int, bool, []interface{}, // Object is a JSON compatible map with string, float, int, bool, []any,
// or map[string]interface{} children. // or map[string]any children.
Object map[string]interface{} Object map[string]any
} }
// Produce an API definition that represents map[string]any // Produce an API definition that represents map[string]any
@ -27,7 +27,7 @@ func (u Unstructured) OpenAPIDefinition() openapi.OpenAPIDefinition {
AdditionalProperties: &spec.SchemaOrBool{Allows: true}, AdditionalProperties: &spec.SchemaOrBool{Allows: true},
}, },
VendorExtensible: spec.VendorExtensible{ VendorExtensible: spec.VendorExtensible{
Extensions: map[string]interface{}{ Extensions: map[string]any{
"x-kubernetes-preserve-unknown-fields": true, "x-kubernetes-preserve-unknown-fields": true,
}, },
}, },
@ -35,14 +35,18 @@ func (u Unstructured) OpenAPIDefinition() openapi.OpenAPIDefinition {
} }
} }
func (u *Unstructured) UnstructuredContent() map[string]interface{} { func (u *Unstructured) IsZero() bool {
return len(u.Object) == 0
}
func (u *Unstructured) UnstructuredContent() map[string]any {
if u.Object == nil { if u.Object == nil {
return make(map[string]interface{}) return make(map[string]any)
} }
return u.Object return u.Object
} }
func (u *Unstructured) SetUnstructuredContent(content map[string]interface{}) { func (u *Unstructured) SetUnstructuredContent(content map[string]any) {
u.Object = content u.Object = content
} }
@ -64,7 +68,7 @@ func (u *Unstructured) DeepCopy() *Unstructured {
} }
out := new(Unstructured) out := new(Unstructured)
*out = *u *out = *u
out.Object = deepCopyJSONValue(u.Object).(map[string]interface{}) out.Object = deepCopyJSONValue(u.Object).(map[string]any)
return out return out
} }
@ -78,24 +82,24 @@ func (u *Unstructured) DeepCopyInto(out *Unstructured) {
// runtime.DeepCopyJSON(u.Object) // runtime.DeepCopyJSON(u.Object)
// //
// BUT this avoids panic on int // BUT this avoids panic on int
func deepCopyJSONValue(x interface{}) interface{} { func deepCopyJSONValue(x any) any {
switch x := x.(type) { switch x := x.(type) {
case map[string]interface{}: case map[string]any:
if x == nil { if x == nil {
// Typed nil - an interface{} that contains a type map[string]interface{} with a value of nil // Typed nil - an any that contains a type map[string]any with a value of nil
return x return x
} }
clone := make(map[string]interface{}, len(x)) clone := make(map[string]any, len(x))
for k, v := range x { for k, v := range x {
clone[k] = deepCopyJSONValue(v) clone[k] = deepCopyJSONValue(v)
} }
return clone return clone
case []interface{}: case []any:
if x == nil { if x == nil {
// Typed nil - an interface{} that contains a type []interface{} with a value of nil // Typed nil - an any that contains a type []any with a value of nil
return x return x
} }
clone := make([]interface{}, len(x)) clone := make([]any, len(x))
for i, v := range x { for i, v := range x {
clone[i] = deepCopyJSONValue(v) clone[i] = deepCopyJSONValue(v)
} }
@ -124,23 +128,23 @@ func deepCopyJSONValue(x interface{}) interface{} {
} }
} }
func (u *Unstructured) Set(field string, value interface{}) { func (u *Unstructured) Set(field string, value any) {
if u.Object == nil { if u.Object == nil {
u.Object = make(map[string]interface{}) u.Object = make(map[string]any)
} }
_ = unstructured.SetNestedField(u.Object, value, field) _ = unstructured.SetNestedField(u.Object, value, field)
} }
func (u *Unstructured) Remove(fields ...string) { func (u *Unstructured) Remove(fields ...string) {
if u.Object == nil { if u.Object == nil {
u.Object = make(map[string]interface{}) u.Object = make(map[string]any)
} }
unstructured.RemoveNestedField(u.Object, fields...) unstructured.RemoveNestedField(u.Object, fields...)
} }
func (u *Unstructured) SetNestedField(value interface{}, fields ...string) { func (u *Unstructured) SetNestedField(value any, fields ...string) {
if u.Object == nil { if u.Object == nil {
u.Object = make(map[string]interface{}) u.Object = make(map[string]any)
} }
_ = unstructured.SetNestedField(u.Object, value, fields...) _ = unstructured.SetNestedField(u.Object, value, fields...)
} }

@ -10,13 +10,13 @@ import (
func TestDeepCopyJSON(t *testing.T) { func TestDeepCopyJSON(t *testing.T) {
obj := &Unstructured{ obj := &Unstructured{
Object: map[string]interface{}{ Object: map[string]any{
"int": int(2), "int": int(2),
"int16": int16(2), "int16": int16(2),
"int32": int32(2), "int32": int32(2),
"uint64": uint64(2), "uint64": uint64(2),
"ref": &ObjectReference{ "ref": &ObjectReference{
Resource: "x", Kind: "x",
}, },
"array": []any{ "array": []any{
int(1), int64(2), "hello", int(1), int64(2), "hello",
@ -25,7 +25,7 @@ func TestDeepCopyJSON(t *testing.T) {
"bool": true, "bool": true,
"map": map[string]any{ "map": map[string]any{
"x": &ObjectReference{ "x": &ObjectReference{
Resource: "x", Kind: "x",
}, },
}, },
"object": &v1.APIGroup{ "object": &v1.APIGroup{

@ -83,34 +83,51 @@ func schema_apimachinery_apis_common_v0alpha1_ObjectReference(ref common.Referen
Description: "Similar to https://dev-k8sref-io.web.app/docs/common-definitions/objectreference-/ ObjectReference contains enough information to let you inspect or modify the referred object.", Description: "Similar to https://dev-k8sref-io.web.app/docs/common-definitions/objectreference-/ ObjectReference contains enough information to let you inspect or modify the referred object.",
Type: []string{"object"}, Type: []string{"object"},
Properties: map[string]spec.Schema{ Properties: map[string]spec.Schema{
"resource": { "apiGroup": {
SchemaProps: spec.SchemaProps{ SchemaProps: spec.SchemaProps{
Description: "APIGroup is the name of the API group that contains the referred object. The empty string represents the core API group.",
Type: []string{"string"},
Format: "",
},
},
"apiVersion": {
SchemaProps: spec.SchemaProps{
Description: "APIVersion is the version of the API group that contains the referred object.",
Type: []string{"string"},
Format: "",
},
},
"kind": {
SchemaProps: spec.SchemaProps{
Description: "See https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#types-kinds",
Type: []string{"string"}, Type: []string{"string"},
Format: "", Format: "",
}, },
}, },
"namespace": { "namespace": {
SchemaProps: spec.SchemaProps{ SchemaProps: spec.SchemaProps{
Description: "Tenant isolation",
Type: []string{"string"}, Type: []string{"string"},
Format: "", Format: "",
}, },
}, },
"name": { "name": {
SchemaProps: spec.SchemaProps{ SchemaProps: spec.SchemaProps{
Description: "Explicit resource identifier",
Type: []string{"string"}, Type: []string{"string"},
Format: "", Format: "",
}, },
}, },
"apiGroup": { "fieldPath": {
SchemaProps: spec.SchemaProps{ SchemaProps: spec.SchemaProps{
Description: "APIGroup is the name of the API group that contains the referred object. The empty string represents the core API group.", Description: "May contain a valid JSON/Go field access statement",
Type: []string{"string"}, Type: []string{"string"},
Format: "", Format: "",
}, },
}, },
"apiVersion": { "uid": {
SchemaProps: spec.SchemaProps{ SchemaProps: spec.SchemaProps{
Description: "APIVersion is the version of the API group that contains the referred object.", Description: "Sepcific deployment of an object",
Type: []string{"string"}, Type: []string{"string"},
Format: "", Format: "",
}, },

@ -0,0 +1,88 @@
package utils
import (
"bytes"
"fmt"
"mime"
"strconv"
"strings"
)
type BlobInfo struct {
UID string `json:"uid"`
Size int64 `json:"size,omitempty"`
Hash string `json:"hash,omitempty"`
MimeType string `json:"mime,omitempty"`
Charset string `json:"charset,omitempty"` // content type = mime+charset
}
// Content type is mime + charset
func (b *BlobInfo) SetContentType(v string) {
var params map[string]string
var err error
b.Charset = ""
b.MimeType, params, err = mime.ParseMediaType(v)
if err != nil {
return
}
b.Charset = params["charset"]
}
// Content type is mime + charset
func (b *BlobInfo) ContentType() string {
sb := bytes.NewBufferString(b.MimeType)
if b.Charset != "" {
sb.WriteString("; charset=")
sb.WriteString(b.Charset)
}
return sb.String()
}
func (b *BlobInfo) String() string {
sb := bytes.NewBufferString(b.UID)
if b.Size > 0 {
fmt.Fprintf(sb, "; size=%d", b.Size)
}
if b.Hash != "" {
sb.WriteString("; hash=")
sb.WriteString(b.Hash)
}
if b.MimeType != "" {
sb.WriteString("; mime=")
sb.WriteString(b.MimeType)
}
if b.Charset != "" {
sb.WriteString("; charset=")
sb.WriteString(b.Charset)
}
return sb.String()
}
func ParseBlobInfo(v string) *BlobInfo {
if v == "" {
return nil
}
info := &BlobInfo{}
for i, part := range strings.Split(v, ";") {
if i == 0 {
info.UID = part
continue
}
kv := strings.Split(strings.TrimSpace(part), "=")
if len(kv) == 2 {
val := kv[1]
switch kv[0] {
case "size":
info.Size, _ = strconv.ParseInt(val, 10, 64)
case "hash":
info.Hash = val
case "mime":
info.MimeType = val
case "charset":
info.Charset = val
}
}
}
return info
}

@ -1,12 +1,9 @@
package utils package utils
import ( import (
"bytes"
"fmt" "fmt"
"mime"
"reflect" "reflect"
"strconv" "strconv"
"strings"
"time" "time"
"k8s.io/apimachinery/pkg/api/meta" "k8s.io/apimachinery/pkg/api/meta"
@ -824,82 +821,3 @@ func (m *grafanaMetaAccessor) SetSourceProperties(v SourceProperties) {
m.obj.SetAnnotations(annot) m.obj.SetAnnotations(annot)
} }
type BlobInfo struct {
UID string `json:"uid"`
Size int64 `json:"size,omitempty"`
Hash string `json:"hash,omitempty"`
MimeType string `json:"mime,omitempty"`
Charset string `json:"charset,omitempty"` // content type = mime+charset
}
// Content type is mime + charset
func (b *BlobInfo) SetContentType(v string) {
var params map[string]string
var err error
b.Charset = ""
b.MimeType, params, err = mime.ParseMediaType(v)
if err != nil {
return
}
b.Charset = params["charset"]
}
// Content type is mime + charset
func (b *BlobInfo) ContentType() string {
sb := bytes.NewBufferString(b.MimeType)
if b.Charset != "" {
sb.WriteString("; charset=")
sb.WriteString(b.Charset)
}
return sb.String()
}
func (b *BlobInfo) String() string {
sb := bytes.NewBufferString(b.UID)
if b.Size > 0 {
fmt.Fprintf(sb, "; size=%d", b.Size)
}
if b.Hash != "" {
sb.WriteString("; hash=")
sb.WriteString(b.Hash)
}
if b.MimeType != "" {
sb.WriteString("; mime=")
sb.WriteString(b.MimeType)
}
if b.Charset != "" {
sb.WriteString("; charset=")
sb.WriteString(b.Charset)
}
return sb.String()
}
func ParseBlobInfo(v string) *BlobInfo {
if v == "" {
return nil
}
info := &BlobInfo{}
for i, part := range strings.Split(v, ";") {
if i == 0 {
info.UID = part
continue
}
kv := strings.Split(strings.TrimSpace(part), "=")
if len(kv) == 2 {
val := kv[1]
switch kv[0] {
case "size":
info.Size, _ = strconv.ParseInt(val, 10, 64)
case "hash":
info.Hash = val
case "mime":
info.MimeType = val
case "charset":
info.Charset = val
}
}
}
return info
}

@ -22070,7 +22070,7 @@
"type": "object", "type": "object",
"properties": { "properties": {
"Object": { "Object": {
"description": "Object is a JSON compatible map with string, float, int, bool, []interface{},\nor map[string]interface{} children.", "description": "Object is a JSON compatible map with string, float, int, bool, []any,\nor map[string]any children.",
"type": "object", "type": "object",
"additionalProperties": {} "additionalProperties": {}
} }

@ -12117,7 +12117,7 @@
"properties": { "properties": {
"Object": { "Object": {
"additionalProperties": {}, "additionalProperties": {},
"description": "Object is a JSON compatible map with string, float, int, bool, []interface{},\nor map[string]interface{} children.", "description": "Object is a JSON compatible map with string, float, int, bool, []any,\nor map[string]any children.",
"type": "object" "type": "object"
} }
}, },

Loading…
Cancel
Save