K8s: Add apimachinery and apiserver packages (#83190)
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
// +k8s:openapi-gen=true
|
||||
// +k8s:defaulter-gen=TypeMeta
|
||||
// +groupName=common.grafana.app
|
||||
|
||||
package v0alpha1 // import "github.com/grafana/grafana/pkg/apimachinery/apis/common/v0alpha1"
|
||||
@@ -0,0 +1,118 @@
|
||||
package v0alpha1
|
||||
|
||||
import (
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
)
|
||||
|
||||
// ResourceInfo helps define a k8s resource
|
||||
// +k8s:openapi-gen=false
|
||||
type ResourceInfo struct {
|
||||
group string
|
||||
version string
|
||||
resourceName string
|
||||
singularName string
|
||||
shortName string
|
||||
kind string
|
||||
newObj func() runtime.Object
|
||||
newList func() runtime.Object
|
||||
}
|
||||
|
||||
func NewResourceInfo(group, version, resourceName, singularName, kind string,
|
||||
newObj func() runtime.Object, newList func() runtime.Object) ResourceInfo {
|
||||
shortName := "" // an optional alias helpful in kubectl eg ("sa" for serviceaccounts)
|
||||
return ResourceInfo{group, version, resourceName, singularName, shortName, kind, newObj, newList}
|
||||
}
|
||||
|
||||
func (info *ResourceInfo) WithGroupAndShortName(group string, shortName string) ResourceInfo {
|
||||
return ResourceInfo{
|
||||
group: group,
|
||||
version: info.version,
|
||||
resourceName: info.resourceName,
|
||||
singularName: info.singularName,
|
||||
kind: info.kind,
|
||||
shortName: shortName,
|
||||
newObj: info.newObj,
|
||||
newList: info.newList,
|
||||
}
|
||||
}
|
||||
|
||||
func (info *ResourceInfo) GetSingularName() string {
|
||||
return info.singularName
|
||||
}
|
||||
|
||||
func (info *ResourceInfo) GetShortNames() []string {
|
||||
if info.shortName == "" {
|
||||
return []string{}
|
||||
}
|
||||
return []string{info.shortName}
|
||||
}
|
||||
|
||||
// TypeMeta returns k8s type
|
||||
func (info *ResourceInfo) TypeMeta() metav1.TypeMeta {
|
||||
return metav1.TypeMeta{
|
||||
Kind: info.kind,
|
||||
APIVersion: info.group + "/" + info.version,
|
||||
}
|
||||
}
|
||||
|
||||
func (info *ResourceInfo) GroupVersion() schema.GroupVersion {
|
||||
return schema.GroupVersion{
|
||||
Group: info.group,
|
||||
Version: info.version,
|
||||
}
|
||||
}
|
||||
|
||||
func (info *ResourceInfo) GroupResource() schema.GroupResource {
|
||||
return schema.GroupResource{
|
||||
Group: info.group,
|
||||
Resource: info.resourceName,
|
||||
}
|
||||
}
|
||||
|
||||
func (info *ResourceInfo) GroupVersionKind() schema.GroupVersionKind {
|
||||
return schema.GroupVersionKind{
|
||||
Group: info.group,
|
||||
Version: info.version,
|
||||
Kind: info.kind,
|
||||
}
|
||||
}
|
||||
|
||||
func (info *ResourceInfo) SingularGroupResource() schema.GroupResource {
|
||||
return schema.GroupResource{
|
||||
Group: info.group,
|
||||
Resource: info.singularName,
|
||||
}
|
||||
}
|
||||
|
||||
func (info *ResourceInfo) GroupVersionResource() schema.GroupVersionResource {
|
||||
return schema.GroupVersionResource{
|
||||
Group: info.group,
|
||||
Version: info.version,
|
||||
Resource: info.resourceName,
|
||||
}
|
||||
}
|
||||
|
||||
func (info *ResourceInfo) StoragePath(sub ...string) string {
|
||||
switch len(sub) {
|
||||
case 0:
|
||||
return info.resourceName
|
||||
case 1:
|
||||
return info.resourceName + "/" + sub[0]
|
||||
}
|
||||
panic("invalid subresource path")
|
||||
}
|
||||
|
||||
func (info *ResourceInfo) NewFunc() runtime.Object {
|
||||
return info.newObj()
|
||||
}
|
||||
|
||||
func (info *ResourceInfo) NewListFunc() runtime.Object {
|
||||
return info.newList()
|
||||
}
|
||||
|
||||
func (info *ResourceInfo) NewNotFound(name string) *errors.StatusError {
|
||||
return errors.NewNotFound(info.SingularGroupResource(), name)
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package v0alpha1
|
||||
|
||||
// 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 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.
|
||||
// The empty string represents the core API group.
|
||||
APIGroup string `json:"apiGroup,omitempty"`
|
||||
|
||||
// APIVersion is the version of the API group that contains the referred object.
|
||||
APIVersion string `json:"apiVersion,omitempty"`
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
package v0alpha1
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
runtime "k8s.io/apimachinery/pkg/runtime"
|
||||
openapi "k8s.io/kube-openapi/pkg/common"
|
||||
spec "k8s.io/kube-openapi/pkg/validation/spec"
|
||||
)
|
||||
|
||||
// Unstructured allows objects that do not have Golang structs registered to be manipulated
|
||||
// generically.
|
||||
type Unstructured struct {
|
||||
// Object is a JSON compatible map with string, float, int, bool, []interface{},
|
||||
// or map[string]interface{} children.
|
||||
Object map[string]any
|
||||
}
|
||||
|
||||
// Produce an API definition that represents map[string]any
|
||||
func (u Unstructured) OpenAPIDefinition() openapi.OpenAPIDefinition {
|
||||
return openapi.OpenAPIDefinition{
|
||||
Schema: spec.Schema{
|
||||
SchemaProps: spec.SchemaProps{
|
||||
Type: []string{"object"},
|
||||
AdditionalProperties: &spec.SchemaOrBool{Allows: true},
|
||||
},
|
||||
VendorExtensible: spec.VendorExtensible{
|
||||
Extensions: map[string]interface{}{
|
||||
"x-kubernetes-preserve-unknown-fields": true,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (u *Unstructured) UnstructuredContent() map[string]interface{} {
|
||||
if u.Object == nil {
|
||||
return make(map[string]interface{})
|
||||
}
|
||||
return u.Object
|
||||
}
|
||||
|
||||
func (u *Unstructured) SetUnstructuredContent(content map[string]interface{}) {
|
||||
u.Object = content
|
||||
}
|
||||
|
||||
// MarshalJSON ensures that the unstructured object produces proper
|
||||
// JSON when passed to Go's standard JSON library.
|
||||
func (u *Unstructured) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(u.Object)
|
||||
}
|
||||
|
||||
// UnmarshalJSON ensures that the unstructured object properly decodes
|
||||
// JSON when passed to Go's standard JSON library.
|
||||
func (u *Unstructured) UnmarshalJSON(b []byte) error {
|
||||
return json.Unmarshal(b, &u.Object)
|
||||
}
|
||||
|
||||
func (u *Unstructured) DeepCopy() *Unstructured {
|
||||
if u == nil {
|
||||
return nil
|
||||
}
|
||||
out := new(Unstructured)
|
||||
*out = *u
|
||||
out.Object = runtime.DeepCopyJSON(u.Object)
|
||||
return out
|
||||
}
|
||||
|
||||
func (u *Unstructured) DeepCopyInto(out *Unstructured) {
|
||||
clone := u.DeepCopy()
|
||||
*out = *clone
|
||||
}
|
||||
|
||||
func (u *Unstructured) Set(field string, value interface{}) {
|
||||
if u.Object == nil {
|
||||
u.Object = make(map[string]interface{})
|
||||
}
|
||||
_ = unstructured.SetNestedField(u.Object, value, field)
|
||||
}
|
||||
|
||||
func (u *Unstructured) Remove(fields ...string) {
|
||||
if u.Object == nil {
|
||||
u.Object = make(map[string]interface{})
|
||||
}
|
||||
unstructured.RemoveNestedField(u.Object, fields...)
|
||||
}
|
||||
|
||||
func (u *Unstructured) SetNestedField(value interface{}, fields ...string) {
|
||||
if u.Object == nil {
|
||||
u.Object = make(map[string]interface{})
|
||||
}
|
||||
_ = unstructured.SetNestedField(u.Object, value, fields...)
|
||||
}
|
||||
|
||||
func (u *Unstructured) GetNestedString(fields ...string) string {
|
||||
val, found, err := unstructured.NestedString(u.Object, fields...)
|
||||
if !found || err != nil {
|
||||
return ""
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
func (u *Unstructured) GetNestedStringSlice(fields ...string) []string {
|
||||
val, found, err := unstructured.NestedStringSlice(u.Object, fields...)
|
||||
if !found || err != nil {
|
||||
return nil
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
func (u *Unstructured) GetNestedInt64(fields ...string) int64 {
|
||||
val, found, err := unstructured.NestedInt64(u.Object, fields...)
|
||||
if !found || err != nil {
|
||||
return 0
|
||||
}
|
||||
return val
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
//go:build !ignore_autogenerated
|
||||
// +build !ignore_autogenerated
|
||||
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
// Code generated by defaulter-gen. DO NOT EDIT.
|
||||
|
||||
package v0alpha1
|
||||
|
||||
import (
|
||||
runtime "k8s.io/apimachinery/pkg/runtime"
|
||||
)
|
||||
|
||||
// RegisterDefaults adds defaulters functions to the given scheme.
|
||||
// Public to allow building arbitrary schemes.
|
||||
// All generated defaulters are covering - they call all nested defaulters.
|
||||
func RegisterDefaults(scheme *runtime.Scheme) error {
|
||||
return nil
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,37 @@
|
||||
API rule violation: list_type_missing,k8s.io/apimachinery/pkg/apis/meta/v1,APIGroup,ServerAddressByClientCIDRs
|
||||
API rule violation: list_type_missing,k8s.io/apimachinery/pkg/apis/meta/v1,APIGroup,Versions
|
||||
API rule violation: list_type_missing,k8s.io/apimachinery/pkg/apis/meta/v1,APIGroupList,Groups
|
||||
API rule violation: list_type_missing,k8s.io/apimachinery/pkg/apis/meta/v1,APIResource,Categories
|
||||
API rule violation: list_type_missing,k8s.io/apimachinery/pkg/apis/meta/v1,APIResource,ShortNames
|
||||
API rule violation: list_type_missing,k8s.io/apimachinery/pkg/apis/meta/v1,APIResourceList,APIResources
|
||||
API rule violation: list_type_missing,k8s.io/apimachinery/pkg/apis/meta/v1,APIVersions,ServerAddressByClientCIDRs
|
||||
API rule violation: list_type_missing,k8s.io/apimachinery/pkg/apis/meta/v1,APIVersions,Versions
|
||||
API rule violation: list_type_missing,k8s.io/apimachinery/pkg/apis/meta/v1,ApplyOptions,DryRun
|
||||
API rule violation: list_type_missing,k8s.io/apimachinery/pkg/apis/meta/v1,CreateOptions,DryRun
|
||||
API rule violation: list_type_missing,k8s.io/apimachinery/pkg/apis/meta/v1,DeleteOptions,DryRun
|
||||
API rule violation: list_type_missing,k8s.io/apimachinery/pkg/apis/meta/v1,FieldsV1,Raw
|
||||
API rule violation: list_type_missing,k8s.io/apimachinery/pkg/apis/meta/v1,LabelSelector,MatchExpressions
|
||||
API rule violation: list_type_missing,k8s.io/apimachinery/pkg/apis/meta/v1,LabelSelectorRequirement,Values
|
||||
API rule violation: list_type_missing,k8s.io/apimachinery/pkg/apis/meta/v1,ObjectMeta,Finalizers
|
||||
API rule violation: list_type_missing,k8s.io/apimachinery/pkg/apis/meta/v1,ObjectMeta,ManagedFields
|
||||
API rule violation: list_type_missing,k8s.io/apimachinery/pkg/apis/meta/v1,ObjectMeta,OwnerReferences
|
||||
API rule violation: list_type_missing,k8s.io/apimachinery/pkg/apis/meta/v1,PatchOptions,DryRun
|
||||
API rule violation: list_type_missing,k8s.io/apimachinery/pkg/apis/meta/v1,RootPaths,Paths
|
||||
API rule violation: list_type_missing,k8s.io/apimachinery/pkg/apis/meta/v1,StatusDetails,Causes
|
||||
API rule violation: list_type_missing,k8s.io/apimachinery/pkg/apis/meta/v1,Table,ColumnDefinitions
|
||||
API rule violation: list_type_missing,k8s.io/apimachinery/pkg/apis/meta/v1,Table,Rows
|
||||
API rule violation: list_type_missing,k8s.io/apimachinery/pkg/apis/meta/v1,TableRow,Cells
|
||||
API rule violation: list_type_missing,k8s.io/apimachinery/pkg/apis/meta/v1,TableRow,Conditions
|
||||
API rule violation: list_type_missing,k8s.io/apimachinery/pkg/apis/meta/v1,UpdateOptions,DryRun
|
||||
API rule violation: list_type_missing,k8s.io/apimachinery/pkg/runtime,RawExtension,Raw
|
||||
API rule violation: list_type_missing,k8s.io/apimachinery/pkg/runtime,Unknown,Raw
|
||||
API rule violation: names_match,github.com/grafana/grafana/pkg/apimachinery/apis/common/v0alpha1,Unstructured,Object
|
||||
API rule violation: names_match,k8s.io/apimachinery/pkg/apis/meta/v1,APIResourceList,APIResources
|
||||
API rule violation: names_match,k8s.io/apimachinery/pkg/apis/meta/v1,Duration,Duration
|
||||
API rule violation: names_match,k8s.io/apimachinery/pkg/apis/meta/v1,InternalEvent,Object
|
||||
API rule violation: names_match,k8s.io/apimachinery/pkg/apis/meta/v1,InternalEvent,Type
|
||||
API rule violation: names_match,k8s.io/apimachinery/pkg/apis/meta/v1,MicroTime,Time
|
||||
API rule violation: names_match,k8s.io/apimachinery/pkg/apis/meta/v1,StatusCause,Type
|
||||
API rule violation: names_match,k8s.io/apimachinery/pkg/apis/meta/v1,Time,Time
|
||||
API rule violation: names_match,k8s.io/apimachinery/pkg/runtime,Unknown,ContentEncoding
|
||||
API rule violation: names_match,k8s.io/apimachinery/pkg/runtime,Unknown,ContentType
|
||||
Reference in New Issue
Block a user