From 31bbfd279778a1dc25722ccea70c6a1d75c5b37c Mon Sep 17 00:00:00 2001 From: Deng Yun Date: Mon, 17 Jun 2024 12:39:48 +0000 Subject: [PATCH] Add IPAddressUsage resoruce type Adding a new IPAddressUsage resource type which is used for an aggregated API server to get IPAddressUsage from the VPC that is associated with the VPC. --- go.mod | 4 +- go.sum | 2 - pkg/apiserver/apis/doc.go | 7 + pkg/apiserver/apis/v1alpha1/doc.go | 8 ++ .../apis/v1alpha1/ipaddressusage_types.go | 62 +++++++++ pkg/apiserver/apis/v1alpha1/register.go | 30 ++++ .../apis/v1alpha1/zz_generated.deepcopy.go | 128 ++++++++++++++++++ pkg/apiserver/doc.go | 7 + 8 files changed, 244 insertions(+), 4 deletions(-) create mode 100644 pkg/apiserver/apis/doc.go create mode 100644 pkg/apiserver/apis/v1alpha1/doc.go create mode 100644 pkg/apiserver/apis/v1alpha1/ipaddressusage_types.go create mode 100644 pkg/apiserver/apis/v1alpha1/register.go create mode 100644 pkg/apiserver/apis/v1alpha1/zz_generated.deepcopy.go create mode 100644 pkg/apiserver/doc.go diff --git a/go.mod b/go.mod index 0fd988151..df130df9c 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ replace ( github.com/vmware-tanzu/nsx-operator/pkg/apis => ./pkg/apis github.com/vmware-tanzu/nsx-operator/pkg/apis/v1alpha1 => ./pkg/apis/v1alpha1 github.com/vmware-tanzu/nsx-operator/pkg/apis/v1alpha2 => ./pkg/apis/v1alpha2 - github.com/vmware-tanzu/nsx-operator/pkg/client => ./pkg/client + github.com/vmware-tanzu/nsx-operator/pkg/client => ./pkg/client ) require ( @@ -14,6 +14,7 @@ require ( github.com/apparentlymart/go-cidr v1.1.0 github.com/deckarep/golang-set v1.8.0 github.com/go-logr/logr v1.3.0 + github.com/go-logr/zapr v1.2.4 github.com/golang-jwt/jwt v3.2.2+incompatible github.com/golang/mock v1.6.0 github.com/google/uuid v1.3.0 @@ -53,7 +54,6 @@ require ( github.com/evanphx/json-patch/v5 v5.6.0 // indirect github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/gibson042/canonicaljson-go v1.0.3 // indirect - github.com/go-logr/zapr v1.2.4 // indirect github.com/go-openapi/jsonpointer v0.19.6 // indirect github.com/go-openapi/jsonreference v0.20.2 // indirect github.com/go-openapi/swag v0.22.3 // indirect diff --git a/go.sum b/go.sum index e9a5b9a89..21c141b9a 100644 --- a/go.sum +++ b/go.sum @@ -177,8 +177,6 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= -github.com/vmware-tanzu/nsx-operator/pkg/client v0.0.0-20240102061654-537b080e159f h1:EV4eiUQr3QpUGfTtqdVph0+bmE+3cj0aNJpd9n2qTdo= -github.com/vmware-tanzu/nsx-operator/pkg/client v0.0.0-20240102061654-537b080e159f/go.mod h1:dzob8tUzpAREQPtbbjQs4b1UyQDR37B2TiIdg8WJSRM= github.com/vmware-tanzu/vm-operator/api v1.8.2 h1:7cZHVusqAmAMFWvsiU7X5xontxdjasknI/sVfe0p0Z4= github.com/vmware-tanzu/vm-operator/api v1.8.2/go.mod h1:vauVboD3sQxP+pb28TnI9wfrj+0nH2zSEc9Q7AzWJ54= github.com/vmware/govmomi v0.27.4 h1:5kY8TAkhB20lsjzrjE073eRb8+HixBI29PVMG5lxq6I= diff --git a/pkg/apiserver/apis/doc.go b/pkg/apiserver/apis/doc.go new file mode 100644 index 000000000..58e74d1eb --- /dev/null +++ b/pkg/apiserver/apis/doc.go @@ -0,0 +1,7 @@ +/* Copyright © 2024 Broadcom, Inc. All Rights Reserved. + SPDX-License-Identifier: Apache-2.0 */ + +// +// +domain=vpc.nsx.vmware.com + +package apis diff --git a/pkg/apiserver/apis/v1alpha1/doc.go b/pkg/apiserver/apis/v1alpha1/doc.go new file mode 100644 index 000000000..f51b05911 --- /dev/null +++ b/pkg/apiserver/apis/v1alpha1/doc.go @@ -0,0 +1,8 @@ +/* Copyright © 2024 Broadcom, Inc. All Rights Reserved. + SPDX-License-Identifier: Apache-2.0 */ + +// +k8s:openapi-gen=true +// +k8s:deepcopy-gen=package,register +// +k8s:defaulter-gen=TypeMeta +// +groupName=vpc.nsx.vmware.com +package v1alpha1 diff --git a/pkg/apiserver/apis/v1alpha1/ipaddressusage_types.go b/pkg/apiserver/apis/v1alpha1/ipaddressusage_types.go new file mode 100644 index 000000000..9aabe7ba3 --- /dev/null +++ b/pkg/apiserver/apis/v1alpha1/ipaddressusage_types.go @@ -0,0 +1,62 @@ +/* Copyright © 2024 Broadcom, Inc. All Rights Reserved. + SPDX-License-Identifier: Apache-2.0 */ + +package v1alpha1 + +import ( + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +const ( + External Visibility = "EXTERNAL" + Project Visibility = "PROJECT" + Private Visibility = "PRIVATE" +) + +type Visibility string + +type IPAddress struct { + Address string `json:"address,omitempty" protobuf:"bytes,1,opt,name=address"` + Subnet string `json:"subnet,omitempty" protobuf:"bytes,2,opt,name=subnet"` +} + +type AllocatedByVPC struct { + Count int `json:"count,omitempty" protobuf:"bytes,1,opt,name=count"` + IPAddresses []IPAddress `json:"ipAddresses,omitempty" protobuf:"bytes,2,opt,name=ipAddresses"` + CIDR string `json:"cidr,omitempty" protobuf:"bytes,3,opt,name=cidr"` + Visibility Visibility `json:"visibility,omitempty" protobuf:"bytes,4,opt,name=visibility"` +} + +// IPBlock contains the used IPAddresses and CIDR. +type IPBlock struct { + Path string `json:"path,omitempty" protobuf:"bytes,1,rep,name=path"` + Available int `json:"available,omitempty" protobuf:"bytes,2,rep,name=available"` + // AllocatedByVPC contains the cidr and used IP Range. + Allocated AllocatedByVPC `json:"allocatedByVpc,omitempty" protobuf:"bytes,3,rep,name=allocatedByVpc"` +} + +// +genclient +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object + +// IPAddressUsage +// +k8s:openapi-gen=true +// +genclient:Namespaced +// +genclient:onlyVerbs=get +type IPAddressUsage struct { + metav1.TypeMeta `json:",inline"` + metav1.ObjectMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"` + + IPBlocks []IPBlock `json:"ipBlocks" protobuf:"bytes,2,rep,name=ipBlocks"` +} + +// IPAddressUsageList +// +k8s:openapi-gen=true +// +genclient:Namespaced +// +genclient:onlyVerbs=get +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object +type IPAddressUsageList struct { + metav1.TypeMeta `json:",inline"` + metav1.ListMeta `json:"metadata,omitempty"` + + Items []IPAddressUsage `json:"items"` +} diff --git a/pkg/apiserver/apis/v1alpha1/register.go b/pkg/apiserver/apis/v1alpha1/register.go new file mode 100644 index 000000000..bfbfa8895 --- /dev/null +++ b/pkg/apiserver/apis/v1alpha1/register.go @@ -0,0 +1,30 @@ +/* Copyright © 2024 Broadcom, Inc. All Rights Reserved. + SPDX-License-Identifier: Apache-2.0 */ + +package v1alpha1 + +import ( + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/runtime/schema" +) + +const ( + GroupName = "vpc.nsx.vmware.com" + Version = "v1alpha1" + ResourceName = "ipaddressusages" +) + +var AddToScheme = func(scheme *runtime.Scheme) error { + metav1.AddToGroupVersion(scheme, schema.GroupVersion{ + Group: GroupName, + Version: Version, + }) + // +kubebuilder:scaffold:install + + scheme.AddKnownTypes(schema.GroupVersion{ + Group: GroupName, + Version: Version, + }, &IPAddressUsage{}, &IPAddressUsageList{}) + return nil +} diff --git a/pkg/apiserver/apis/v1alpha1/zz_generated.deepcopy.go b/pkg/apiserver/apis/v1alpha1/zz_generated.deepcopy.go new file mode 100644 index 000000000..0ef4f48ee --- /dev/null +++ b/pkg/apiserver/apis/v1alpha1/zz_generated.deepcopy.go @@ -0,0 +1,128 @@ +//go:build !ignore_autogenerated +// +build !ignore_autogenerated + +/* Copyright © 2024 VMware, Inc. All Rights Reserved. + SPDX-License-Identifier: Apache-2.0 */ + +// Code generated by controller-gen. DO NOT EDIT. + +package v1alpha1 + +import ( + "k8s.io/apimachinery/pkg/runtime" +) + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *AllocatedByVPC) DeepCopyInto(out *AllocatedByVPC) { + *out = *in + if in.IPAddresses != nil { + in, out := &in.IPAddresses, &out.IPAddresses + *out = make([]IPAddress, len(*in)) + copy(*out, *in) + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new AllocatedByVPC. +func (in *AllocatedByVPC) DeepCopy() *AllocatedByVPC { + if in == nil { + return nil + } + out := new(AllocatedByVPC) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *IPAddress) DeepCopyInto(out *IPAddress) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new IPAddress. +func (in *IPAddress) DeepCopy() *IPAddress { + if in == nil { + return nil + } + out := new(IPAddress) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *IPAddressUsage) DeepCopyInto(out *IPAddressUsage) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) + if in.IPBlocks != nil { + in, out := &in.IPBlocks, &out.IPBlocks + *out = make([]IPBlock, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new IPAddressUsage. +func (in *IPAddressUsage) DeepCopy() *IPAddressUsage { + if in == nil { + return nil + } + out := new(IPAddressUsage) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *IPAddressUsage) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *IPAddressUsageList) DeepCopyInto(out *IPAddressUsageList) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ListMeta.DeepCopyInto(&out.ListMeta) + if in.Items != nil { + in, out := &in.Items, &out.Items + *out = make([]IPAddressUsage, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new IPAddressUsageList. +func (in *IPAddressUsageList) DeepCopy() *IPAddressUsageList { + if in == nil { + return nil + } + out := new(IPAddressUsageList) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *IPAddressUsageList) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *IPBlock) DeepCopyInto(out *IPBlock) { + *out = *in + in.Allocated.DeepCopyInto(&out.Allocated) +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new IPBlock. +func (in *IPBlock) DeepCopy() *IPBlock { + if in == nil { + return nil + } + out := new(IPBlock) + in.DeepCopyInto(out) + return out +} diff --git a/pkg/apiserver/doc.go b/pkg/apiserver/doc.go new file mode 100644 index 000000000..a40858d94 --- /dev/null +++ b/pkg/apiserver/doc.go @@ -0,0 +1,7 @@ +/* Copyright © 2024 Broadcom, Inc. All Rights Reserved. + SPDX-License-Identifier: Apache-2.0 */ + +// +// +domain=vpc.nsx.vmware.com + +package apiserver