Skip to content

Commit

Permalink
IPPool controller added
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengxiexie committed Jan 4, 2023
1 parent 0f5d43f commit 88c62e3
Show file tree
Hide file tree
Showing 29 changed files with 2,545 additions and 29 deletions.
9 changes: 8 additions & 1 deletion PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,11 @@ resources:
kind: SubnetPort
path: github.com/vmware-tanzu/nsx-operator/pkg/api/v1alpha1
version: v1alpha1
version: "3"
- api:
crdVersion: v1
namespaced: true
domain: nsx.vmware.com
kind: IPPool
path: github.com/vmware-tanzu/nsx-operator/pkg/api/v1alpha2
version: v1alpha2
version: "3"
160 changes: 160 additions & 0 deletions build/yaml/crd/nsx.vmware.com_ippools.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.7.0
creationTimestamp: null
name: ippools.nsx.vmware.com
spec:
group: nsx.vmware.com
names:
kind: IPPool
listKind: IPPoolList
plural: ippools
singular: ippool
scope: Namespaced
versions:
- name: v1alpha1
schema:
openAPIV3Schema:
properties:
spec:
properties:
subnets:
items:
properties:
ipFamily:
pattern: ^ipv(4|6)$
type: string
name:
type: string
prefixLength:
minimum: 1
type: integer
type: object
type: array
type: object
type: object
x-kubernetes-preserve-unknown-fields: true
served: true
storage: false
- name: v1alpha2
schema:
openAPIV3Schema:
description: IPPool is the Schema for the ippools API
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation
of an object. Servers should convert recognized schemas to the latest
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
type: string
kind:
description: 'Kind is a string value representing the REST resource this
object represents. Servers may infer this from the endpoint the client
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
metadata:
type: object
spec:
description: IPPoolSpec defines the desired state of IPPool.
properties:
subnets:
description: Subnets defines set of subnets need to be allocated.
items:
description: SubnetRequest defines the subnet allocation request.
properties:
ipFamily:
default: IPv4
description: IPFamily defines the IP family type for this subnet,
could be IPv4 or IPv6. This is optional, the default is IPv4.
enum:
- IPv4
- IPv6
type: string
name:
description: Name defines the name of this subnet.
type: string
prefixLength:
description: PrefixLength defines prefix length for this subnet.
type: integer
required:
- name
type: object
type: array
type:
default: public
description: Type defines the type of this IPPool, public or private.
enum:
- public
- private
type: string
required:
- type
type: object
status:
description: IPPoolStatus defines the observed state of IPPool.
properties:
conditions:
description: Conditions defines current state of the IPPool.
items:
description: Condition defines condition of custom resource.
properties:
lastTransitionTime:
description: Last time the condition transitioned from one status
to another. This should be when the underlying condition changed.
If that is not known, then using the time when the API field
changed is acceptable.
format: date-time
type: string
message:
description: Message shows a human-readable message about condition.
type: string
reason:
description: Reason shows a brief reason of condition.
type: string
status:
description: Status of the condition, one of True, False, Unknown.
type: string
type:
description: Type defines condition type.
type: string
required:
- status
- type
type: object
type: array
subnets:
description: Subnets defines subnets allocation result.
items:
description: SubnetResult defines the subnet allocation result.
properties:
cidr:
description: CIDR defines the allocated CIDR.
type: string
name:
description: Name defines the name of this subnet.
type: string
required:
- cidr
- name
type: object
type: array
required:
- conditions
- subnets
type: object
required:
- metadata
- spec
type: object
served: true
storage: true
subresources:
status: {}
status:
acceptedNames:
kind: ""
plural: ""
conditions: []
storedVersions: ["v1alpha2"]
27 changes: 26 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"os"
"time"

"github.com/vmware-tanzu/nsx-operator/pkg/logger"

"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
Expand All @@ -16,12 +18,14 @@ import (
logf "sigs.k8s.io/controller-runtime/pkg/log"

"github.com/vmware-tanzu/nsx-operator/pkg/apis/v1alpha1"
"github.com/vmware-tanzu/nsx-operator/pkg/apis/v1alpha2"
"github.com/vmware-tanzu/nsx-operator/pkg/config"
ippool2 "github.com/vmware-tanzu/nsx-operator/pkg/controllers/ippool"
securitypolicycontroller "github.com/vmware-tanzu/nsx-operator/pkg/controllers/securitypolicy"
"github.com/vmware-tanzu/nsx-operator/pkg/logger"
"github.com/vmware-tanzu/nsx-operator/pkg/metrics"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/common"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/ippool"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/securitypolicy"
)

Expand All @@ -35,6 +39,7 @@ var (
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(v1alpha1.AddToScheme(scheme))
utilruntime.Must(v1alpha2.AddToScheme(scheme))
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8384", "The address the probe endpoint binds to.")
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8093", "The address the metrics endpoint binds to.")
config.AddFlags()
Expand Down Expand Up @@ -68,6 +73,23 @@ func StartSecurityPolicyController(mgr ctrl.Manager, commonService common.Servic
}
}

func StartIPPoolController(mgr ctrl.Manager, commonService common.Service) {
ippoolReconcile := &ippool2.Reconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}
if ipPoolService, err := ippool.InitializeIPPool(commonService); err != nil {
log.Error(err, "failed to initialize ippool commonService", "controller", "IPPool")
os.Exit(1)
} else {
ippoolReconcile.Service = ipPoolService
}
if err := ippoolReconcile.Start(mgr); err != nil {
log.Error(err, "failed to create controller", "controller", "IPPool")
os.Exit(1)
}
}

func main() {
log.Info("starting NSX Operator")

Expand Down Expand Up @@ -99,6 +121,9 @@ func main() {
// Start the security policy controller.
StartSecurityPolicyController(mgr, commonService)

// Start the ip pool controller.
StartIPPoolController(mgr, commonService)

if metrics.AreMetricsExposed(cf) {
go updateHealthMetricsPeriodically(nsxClient)
}
Expand Down
23 changes: 23 additions & 0 deletions pkg/apis/v1alpha2/groupversion_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* Copyright © 2022 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0 */

// Package v1alpha2 contains API Schema definitions for the v1alpha2 API group
//+kubebuilder:object:generate=true
//+groupName=nsx.vmware.com
package v1alpha2

import (
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/scheme"
)

var (
// GroupVersion is group version used to register these objects
GroupVersion = schema.GroupVersion{Group: "nsx.vmware.com", Version: "v1alpha2"}

// SchemeBuilder is used to add go types to the GroupVersionKind scheme
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}

// AddToScheme adds the types in this group-version to the given scheme.
AddToScheme = SchemeBuilder.AddToScheme
)
79 changes: 79 additions & 0 deletions pkg/apis/v1alpha2/ippool_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/* Copyright © 2022 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0 */

package v1alpha2

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/vmware-tanzu/nsx-operator/pkg/apis/v1alpha1"
)

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status

// IPPool is the Schema for the ippools API
type IPPool struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata"`

Spec IPPoolSpec `json:"spec"`
Status IPPoolStatus `json:"status,omitempty"`
}

//+kubebuilder:object:root=true

// IPPoolList contains a list of IPPool
type IPPoolList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []IPPool `json:"items"`
}

// IPPoolSpec defines the desired state of IPPool.
type IPPoolSpec struct {
// Type defines the type of this IPPool, public or private.
// +kubebuilder:validation:Enum=public;private
// +kubebuilder:default=public
Type string `json:"type"`
// Subnets defines set of subnets need to be allocated.
// +optional
Subnets []SubnetRequest `json:"subnets"`
}

// IPPoolStatus defines the observed state of IPPool.
type IPPoolStatus struct {
// Subnets defines subnets allocation result.
Subnets []SubnetResult `json:"subnets"`
// Conditions defines current state of the IPPool.
Conditions []v1alpha1.Condition `json:"conditions"`
}

// SubnetRequest defines the subnet allocation request.
type SubnetRequest struct {
// PrefixLength defines prefix length for this subnet.
// +optional
PrefixLength int `json:"prefixLength,omitempty"`

// IPFamily defines the IP family type for this subnet, could be IPv4 or IPv6.
// This is optional, the default is IPv4.
// +kubebuilder:validation:Enum=IPv4;IPv6
// +kubebuilder:default=IPv4
IPFamily string `json:"ipFamily,omitempty"`

// Name defines the name of this subnet.
Name string `json:"name"`
}

// SubnetResult defines the subnet allocation result.
type SubnetResult struct {
// CIDR defines the allocated CIDR.
CIDR string `json:"cidr"`

// Name defines the name of this subnet.
Name string `json:"name"`
}

func init() {
SchemeBuilder.Register(&IPPool{}, &IPPoolList{})
}
Loading

0 comments on commit 88c62e3

Please sign in to comment.