首页 > 编程语言 >Kubernetes编程——client-go基础—— TypeMeta

Kubernetes编程——client-go基础—— TypeMeta

时间:2023-06-28 14:46:33浏览次数:45  
标签:struct Kubernetes json client TypeMeta runtime type protobuf

TypeMeta

https://github.com/kubernetes/apimachinery/blob/release-1.27/pkg/runtime/types.go

  runtime.Object 只是一个接口,我们想了解它具体时间怎么实现的。k8s.io/api 中的 Kubernetes 对象通过内嵌 k8s.io/apimachinery/meta/v1 中的 metav1.TypeMeta 结构,为 schema.ObjectKind 实现了类型信息的存取函数:

/*
Copyright 2014 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 runtime

// Note that the types provided in this file are not versioned and are intended to be
// safe to use from within all versions of every API object.

// TypeMeta is shared by all top level objects. The proper way to use it is to inline it in your type,
// like this:
//
//	type MyAwesomeAPIObject struct {
//	     runtime.TypeMeta    `json:",inline"`
//	     ... // other fields
//	}
//
// func (obj *MyAwesomeAPIObject) SetGroupVersionKind(gvk *metav1.GroupVersionKind) { metav1.UpdateTypeMeta(obj,gvk) }; GroupVersionKind() *GroupVersionKind
//
// TypeMeta is provided here for convenience. You may use it directly from this package or define
// your own with the same fields.
//
// +k8s:deepcopy-gen=false
// +protobuf=true
// +k8s:openapi-gen=true
type TypeMeta struct {
	// +optional
	APIVersion string `json:"apiVersion,omitempty" yaml:"apiVersion,omitempty" protobuf:"bytes,1,opt,name=apiVersion"`
	// +optional
	Kind string `json:"kind,omitempty" yaml:"kind,omitempty" protobuf:"bytes,2,opt,name=kind"`
}

const (
	ContentTypeJSON     string = "application/json"
	ContentTypeYAML     string = "application/yaml"
	ContentTypeProtobuf string = "application/vnd.kubernetes.protobuf"
)

// RawExtension is used to hold extensions in external versions.
//
// To use this, make a field which has RawExtension as its type in your external, versioned
// struct, and Object in your internal struct. You also need to register your
// various plugin types.
//
// // Internal package:
//
//	type MyAPIObject struct {
//		runtime.TypeMeta `json:",inline"`
//		MyPlugin runtime.Object `json:"myPlugin"`
//	}
//
//	type PluginA struct {
//		AOption string `json:"aOption"`
//	}
//
// // External package:
//
//	type MyAPIObject struct {
//		runtime.TypeMeta `json:",inline"`
//		MyPlugin runtime.RawExtension `json:"myPlugin"`
//	}
//
//	type PluginA struct {
//		AOption string `json:"aOption"`
//	}
//
// // On the wire, the JSON will look something like this:
//
//	{
//		"kind":"MyAPIObject",
//		"apiVersion":"v1",
//		"myPlugin": {
//			"kind":"PluginA",
//			"aOption":"foo",
//		},
//	}
//
// So what happens? Decode first uses json or yaml to unmarshal the serialized data into
// your external MyAPIObject. That causes the raw JSON to be stored, but not unpacked.
// The next step is to copy (using pkg/conversion) into the internal struct. The runtime
// package's DefaultScheme has conversion functions installed which will unpack the
// JSON stored in RawExtension, turning it into the correct object type, and storing it
// in the Object. (TODO: In the case where the object is of an unknown type, a
// runtime.Unknown object will be created and stored.)
//
// +k8s:deepcopy-gen=true
// +protobuf=true
// +k8s:openapi-gen=true
type RawExtension struct {
	// Raw is the underlying serialization of this object.
	//
	// TODO: Determine how to detect ContentType and ContentEncoding of 'Raw' data.
	Raw []byte `json:"-" protobuf:"bytes,1,opt,name=raw"`
	// Object can hold a representation of this extension - useful for working with versioned
	// structs.
	Object Object `json:"-"`
}

// Unknown allows api objects with unknown types to be passed-through. This can be used
// to deal with the API objects from a plug-in. Unknown objects still have functioning
// TypeMeta features-- kind, version, etc.
// TODO: Make this object have easy access to field based accessors and settors for
// metadata and field mutatation.
//
// +k8s:deepcopy-gen=true
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +protobuf=true
// +k8s:openapi-gen=true
type Unknown struct {
	TypeMeta `json:",inline" protobuf:"bytes,1,opt,name=typeMeta"`
	// Raw will hold the complete serialized object which couldn't be matched
	// with a registered type. Most likely, nothing should be done with this
	// except for passing it through the system.
	Raw []byte `json:"-" protobuf:"bytes,2,opt,name=raw"`
	// ContentEncoding is encoding used to encode 'Raw' data.
	// Unspecified means no encoding.
	ContentEncoding string `protobuf:"bytes,3,opt,name=contentEncoding"`
	// ContentType  is serialization method used to serialize 'Raw'.
	// Unspecified means ContentTypeJSON.
	ContentType string `protobuf:"bytes,4,opt,name=contentType"`
}

  

 

标签:struct,Kubernetes,json,client,TypeMeta,runtime,type,protobuf
From: https://www.cnblogs.com/zuoyang/p/17511339.html

相关文章

  • Kubernetes编程——client-go基础—— Go语言中的 Kubernetes 对象介绍
    Go语言中的Kubernetes对象介绍 我们接下来更详细了解在Go语言的语境下的Pod(或者其他任何Kubernetes资源)是什么样的? Kubernetes中的资源(或者更准确说是对象)都是某种类型的实例。我理解意思是说:在Kubernetes中,资源或对象是指由Kubernetes控......
  • Kubernetes编程——修改客户端默认支持 Protobuf
    修改客户端默认支持Protobuf一、在kubernetes客户端中修改默认支持Protobuf确保你已经安装了kubectl命令行工具,并且版本在1.14.0或更高。打开~/.kube/config文件,该文件存储了你的Kubernetes集群配置信息。找到clusters部分,并在你的集群配置下添加extensions字段,示例如下:......
  • kubernetes探针及应用场景
    kubernetes提供了哪几种探针?分别有什么功能?应用场景有哪些?LivenessProbe:容器存活性检查,用于判断容器是否健康。功能:如果LivenessProbe探针探测到容器不健康,则kubelet将删除该容器,并根据容器的重启策略做相应的处理。 如果一个容器不包含LivenessProbe探针,那么kubele......
  • Kubernetes应用编排与管理 —— Deployment升级策略
    1、Deployment概述Deployment是Kubernetes控制器的一种高级别实现,它构建于ReplicaSet控制器之上,它可用于为Pod和ReplicaSet资源提供声明式更新,并能够以自动方式实现跨多个ReplicaSet对象的滚动更新功能。相比较来说,Pod和ReplicaSet是较低级别的资源,以至于很少......
  • Kubernetes 系列:Kubernetes 的安装(三)
    序前面介绍了k8s组件和对象的一些基本概念,了解了k8s具体是做什么的以及架构,那么接下来我们开始介绍怎么去安装k8s,这里我们以windows为例,其他平台可以参考Kubernetes官方文档,其实安装方式都是类似的。先决条件要在系统中安装Kubernetes,以下是一些需要特别注意的先决条件。软件......
  • Kubernetes添加用户
    kubernetes中有两种用户,一种是serviceaccount,另一种是普通用户ServiceAccount认证从1.24开始,创建serviceaccount的同时不再创建secretapiVersion:v1kind:ServiceAccountmetadata:name:kubepi-usernamespace:kube-system---apiVersion:rbac.authoriz......
  • Kubernetes编程——通过命令行使用 API
    通过命令行使用API 长话短说,我们将使用以batchAPI组为例来讲cli相关的操作。 首先,需要在终端运行下面的命令:[root@localhost~]#kubectlproxy--port=8089Startingtoserveon127.0.0.1:8089这个命令把kubernetesAPI服务代理到了本地,并处理了有关身......
  • Kubernetes控制器介绍(二)
    一、DaemonSet1.1介绍与Deployment相似的是,DaemonSet也是基于标签选择器管控一组Pod副本。但是,DaemonSet用于确保所有或指定的工作节点上都运行有一个Pod副本。换句话说,其Pod数量由节点数量而定,因此无需定义replicas字段(Pod的副本数量)。从集群移除节点时,此类Pod对象也将被自动回......
  • Kubernetes 系列:了解 k8s 架构(一)
    Kubernetes概述当下,我们很多项目于都在CloudNative(云原生)的上面,这种方法旨在使组织能够确保可用性并快速响应和适应变化,云原生其实就是一组本质上支持在不同云环境(公共云、私有云或混合云)上大规模构建、运行和管理应用程序的实践和技术。云原生离不开两个概念:容器和微服务,这......
  • 远程桌面客户端(Remote Desktop Client)是一种用于远程连接到另一台计算机的应用程序。
    远程桌面客户端(RemoteDesktopClient)是一种用于远程连接到另一台计算机的应用程序。它允许用户通过网络连接到远程计算机,并在本地计算机上使用远程计算机的桌面环境和应用程序。以下是关于远程桌面客户端的一些重要信息:功能:远程桌面客户端提供了许多功能,包括:连接到远程计算机:通......