云容器实例 CCI-使用client-go访问CCI的CRD资源Network:定义CRD资源Network

时间:2025-02-12 14:55:06

定义CRD资源Network

创建文件夹pkg/apis/networking.cci.io/v1beta1,其中networking.cci.io为CRD资源的group,v1beta1为CRD资源版本

mkdir -p pkg/apis/networking.cci.io/v1beta1

在新建文件夹中新建以下文件:

doc.go

// +k8s:deepcopy-gen=package// +groupName=networking.cci.io// +groupGoName=NetworkingCCIpackage v1beta1

types.go

package v1beta1import (metav1 "k8s.io/apimachinery/pkg/apis/meta/v1")// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object// NetworkList is a list of network resource in container.type NetworkList struct {metav1.TypeMeta `json:",inline"`// Standard list metadata.// More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata// +optionalmetav1.ListMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`Items           []Network `json:"items" protobuf:"bytes,2,rep,name=items"`}// +genclient// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object// Network is a network resource in container.type Network struct {metav1.TypeMeta `json:",inline"`// +optionalmetav1.ObjectMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`// Spec defines the attributes on a network// +optionalSpec NetworkSpec `json:"spec,omitempty" protobuf:"bytes,2,opt,name=spec"`// Status describes the network status// +optionalStatus NetworkStatus `json:"status,omitempty" protobuf:"bytes,3,opt,name=status"`}// NetworkSpec describes the attributes on a network resource.type NetworkSpec struct {// network typeNetworkType string `json:"networkType,omitempty" protobuf:"bytes,5,opt,name=networkType"`// ID of the VPC to attachAttachedVPC string `json:"attachedVPC,omitempty" protobuf:"bytes,4,opt,name=attachedVPC"`// network IDNetworkID string `json:"networkID,omitempty" protobuf:"bytes,7,opt,name=networkID"`// Subnet IDSubnetID string `json:"subnetID,omitempty" protobuf:"bytes,8,opt,name=subnetID"`// available zoneAvailableZone string `json:"availableZone,omitempty" protobuf:"bytes,9,opt,name=availableZone"`// The CIDR of the networkCIDR string `json:"cidr,omitempty" protobuf:"bytes,3,opt,name=cidr"`}// NetworkStatus describes the status of a networktype NetworkStatus struct {// State describes the network state// +optionalState string `json:"state" protobuf:"bytes,1,opt,name=state"`// Message describes why network is in current state// +optionalMessage string `json:"message,omitempty" protobuf:"bytes,2,opt,name=message"`}const (// NetworkInitializing means the network is initializingNetworkInitializing = "Initializing"// NetworkPending means the network is processingNetworkPending = "Pending"// NetworkActive means the network is availableNetworkActive = "Active"// NetworkFailed means the network is not availableNetworkFailed = "Failed"// NetworkTerminating means the network is undergoing graceful terminationNetworkTerminating = "Terminating")

register.go

package v1beta1import (metav1 "k8s.io/apimachinery/pkg/apis/meta/v1""k8s.io/apimachinery/pkg/runtime""k8s.io/apimachinery/pkg/runtime/schema")// GroupName is the group name use in this packageconst GroupName = "networking.cci.io"// SchemeGroupVersion is group version used to register these objectsvar SchemeGroupVersion = schema.GroupVersion{Group: GroupName, Version: "v1beta1"}// Resource takes an unqualified resource and returns a Group qualified GroupResourcefunc Resource(resource string) schema.GroupResource {return SchemeGroupVersion.WithResource(resource).GroupResource()}var (// localSchemeBuilder and AddToScheme will stay in k8s.io/kubernetes.SchemeBuilder      = runtime.NewSchemeBuilder(addKnownTypes)localSchemeBuilder = &SchemeBuilderAddToScheme        = localSchemeBuilder.AddToScheme)// Adds the list of known types to the given scheme.func addKnownTypes(scheme *runtime.Scheme) error {scheme.AddKnownTypes(SchemeGroupVersion,&Network{},&NetworkList{},)// Add the watch version that appliesmetav1.AddToGroupVersion(scheme, SchemeGroupVersion)return nil}
support.huaweicloud.com/sdkreference-cci/cci_09_0002.html