Repository: spidernet-io/spiderpool
Pull request #1416 (2023-03-08 03:22:35)

Adjust most fields of SpiderEndpoit CRD as required
Signed-off-by: iiiceoo <ziqian.xue@daocloud.io>


Patch:
diff --git a/charts/spiderpool/crds/spiderpool.spidernet.io_spiderendpoints.yaml b/charts/spiderpool/crds/spiderpool.spidernet.io_spiderendpoints.yaml
index 319550a6c..ea376f85e 100644
--- a/charts/spiderpool/crds/spiderpool.spidernet.io_spiderendpoints.yaml
+++ b/charts/spiderpool/crds/spiderpool.spidernet.io_spiderendpoints.yaml
@@ -68,13 +68,13 @@ spec:
           status:
             description: WorkloadEndpointStatus defines the observed state of SpiderEndpoint.
             properties:
+              creationTime:
+                format: date-time
+                type: string
               current:
                 properties:
                   containerID:
                     type: string
-                  creationTime:
-                    format: date-time
-                    type: string
                   ips:
                     items:
                       properties:
@@ -122,6 +122,7 @@ spec:
                     type: string
                 required:
                 - containerID
+                - ips
                 - node
                 - uid
                 type: object
@@ -130,6 +131,8 @@ spec:
               ownerControllerType:
                 type: string
             required:
+            - creationTime
+            - current
             - ownerControllerName
             - ownerControllerType
             type: object
diff --git a/pkg/ipam/allocate.go b/pkg/ipam/allocate.go
index 7dbe69978..83cc5759d 100644
--- a/pkg/ipam/allocate.go
+++ b/pkg/ipam/allocate.go
@@ -5,7 +5,6 @@ package ipam
 
 import (
 	"context"
-	"errors"
 	"fmt"
 	"strings"
 	"sync"
@@ -97,10 +96,6 @@ func (i *ipam) retrieveStsIPAllocation(ctx context.Context, containerID string,
 		return nil, nil
 	}
 
-	if endpoint.Status.Current == nil {
-		return nil, errors.New("data is broken, Endpoint doesn't have current IP allocation")
-	}
-
 	logger.Info("Concurrently refresh IP records of IPPools")
 	if err := i.reallocateIPPoolIPRecords(ctx, string(pod.UID), endpoint); err != nil {
 		return nil, err
diff --git a/pkg/k8s/apis/spiderpool.spidernet.io/v1/spiderendpoint_types.go b/pkg/k8s/apis/spiderpool.spidernet.io/v1/spiderendpoint_types.go
index b46e076e3..8a5b26dca 100644
--- a/pkg/k8s/apis/spiderpool.spidernet.io/v1/spiderendpoint_types.go
+++ b/pkg/k8s/apis/spiderpool.spidernet.io/v1/spiderendpoint_types.go
@@ -9,14 +9,17 @@ import (
 
 // WorkloadEndpointStatus defines the observed state of SpiderEndpoint.
 type WorkloadEndpointStatus struct {
-	// +kubebuilder:validation:Optional
-	Current *PodIPAllocation `json:"current,omitempty"`
+	// +kubebuilder:validation:Required
+	Current PodIPAllocation `json:"current"`
 
 	// +kubebuilder:validation:Required
 	OwnerControllerType string `json:"ownerControllerType"`
 
 	// +kubebuilder:validation:Required
 	OwnerControllerName string `json:"ownerControllerName"`
+
+	// +kubebuilder:validation:Required
+	CreationTime metav1.Time `json:"creationTime"`
 }
 
 type PodIPAllocation struct {
@@ -29,11 +32,8 @@ type PodIPAllocation struct {
 	// +kubebuilder:validation:Required
 	Node string `json:"node"`
 
-	// +kubebuilder:validation:Optional
-	IPs []IPAllocationDetail `json:"ips,omitempty"`
-
-	// +kubebuilder:validation:Optional
-	CreationTime *metav1.Time `json:"creationTime,omitempty"`
+	// +kubebuilder:validation:Required
+	IPs []IPAllocationDetail `json:"ips"`
 }
 
 type IPAllocationDetail struct {
diff --git a/pkg/k8s/apis/spiderpool.spidernet.io/v1/spiderippool_types.go b/pkg/k8s/apis/spiderpool.spidernet.io/v1/spiderippool_types.go
index ba60c6b31..3feed4c63 100644
--- a/pkg/k8s/apis/spiderpool.spidernet.io/v1/spiderippool_types.go
+++ b/pkg/k8s/apis/spiderpool.spidernet.io/v1/spiderippool_types.go
@@ -82,17 +82,10 @@ type IPPoolStatus struct {
 type PoolIPAllocations map[string]PoolIPAllocation
 
 type PoolIPAllocation struct {
-	// +kubebuilder:validation:Required
-	NIC string `json:"interface"`
-
-	// +kubebuilder:validation:Required
+	NIC       string `json:"interface"`
 	Namespace string `json:"namespace"`
-
-	// +kubebuilder:validation:Required
-	Pod string `json:"pod"`
-
-	// +kubebuilder:validation:Required
-	UID string `json:"uid"`
+	Pod       string `json:"pod"`
+	UID       string `json:"uid"`
 }
 
 // +kubebuilder:resource:categories={spiderpool},path="spiderippools",scope="Cluster",shortName={sp},singular="spiderippool"
diff --git a/pkg/k8s/apis/spiderpool.spidernet.io/v1/types_string.go b/pkg/k8s/apis/spiderpool.spidernet.io/v1/types_string.go
index 469bb70a8..fdb3e103a 100644
--- a/pkg/k8s/apis/spiderpool.spidernet.io/v1/types_string.go
+++ b/pkg/k8s/apis/spiderpool.spidernet.io/v1/types_string.go
@@ -88,6 +88,7 @@ func (in *WorkloadEndpointStatus) String() string {
 		`Current:` + fmt.Sprintf("%v", in.Current) + `,`,
 		`OwnerControllerType:` + fmt.Sprintf("%v", in.OwnerControllerType) + `,`,
 		`OwnerControllerName` + fmt.Sprintf("%v", in.OwnerControllerName) + `,`,
+		`CreationTime:` + fmt.Sprintf("%v", in.CreationTime) + `,`,
 		`}`,
 	}, "")
 	return s
@@ -107,9 +108,9 @@ func (in *PodIPAllocation) String() string {
 
 	s := strings.Join([]string{`&PodIPAllocation{`,
 		`ContainerID:` + fmt.Sprintf("%+v", in.ContainerID) + `,`,
+		`UID:` + fmt.Sprintf("%+v", in.UID) + `,`,
 		`Node:` + stringutil.ValueToStringGenerated(in.Node) + `,`,
 		`IPs:` + repeatedStringForIPs + `,`,
-		`CreationTime:` + fmt.Sprintf("%v", in.CreationTime) + `,`,
 		`}`,
 	}, "")
 	return s
diff --git a/pkg/k8s/apis/spiderpool.spidernet.io/v1/zz_generated.deepcopy.go b/pkg/k8s/apis/spiderpool.spidernet.io/v1/zz_generated.deepcopy.go
index ee8924908..e40f6da47 100644
--- a/pkg/k8s/apis/spiderpool.spidernet.io/v1/zz_generated.deepcopy.go
+++ b/pkg/k8s/apis/spiderpool.spidernet.io/v1/zz_generated.deepcopy.go
@@ -188,10 +188,6 @@ func (in *PodIPAllocation) DeepCopyInto(out *PodIPAllocation) {
 			(*in)[i].DeepCopyInto(&(*out)[i])
 		}
 	}
-	if in.CreationTime != nil {
-		in, out := &in.CreationTime, &out.CreationTime
-		*out = (*in).DeepCopy()
-	}
 }
 
 // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new PodIPAllocation.
@@ -640,11 +636,8 @@ func (in *SubnetStatus) DeepCopy() *SubnetStatus {
 // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
 func (in *WorkloadEndpointStatus) DeepCopyInto(out *WorkloadEndpointStatus) {
 	*out = *in
-	if in.Current != nil {
-		in, out := &in.Current, &out.Current
-		*out = new(PodIPAllocation)
-		(*in).DeepCopyInto(*out)
-	}
+	in.Current.DeepCopyInto(&out.Current)
+	in.CreationTime.DeepCopyInto(&out.CreationTime)
 }
 
 // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new WorkloadEndpointStatus.
diff --git a/pkg/workloadendpointmanager/utils.go b/pkg/workloadendpointmanager/utils.go
index 2a7fe45ce..92de553ca 100644
--- a/pkg/workloadendpointmanager/utils.go
+++ b/pkg/workloadendpointmanager/utils.go
@@ -11,14 +11,11 @@ func RetrieveIPAllocation(uid, nic string, endpoint *spiderpoolv1.SpiderEndpoint
 	if endpoint == nil {
 		return nil
 	}
-	if endpoint.Status.Current == nil {
-		return nil
-	}
 
 	if endpoint.Status.Current.UID == uid {
 		for _, d := range endpoint.Status.Current.IPs {
 			if d.NIC == nic {
-				return endpoint.Status.Current
+				return &endpoint.Status.Current
 			}
 		}
 	}
diff --git a/pkg/workloadendpointmanager/workloadendpoint_manager.go b/pkg/workloadendpointmanager/workloadendpoint_manager.go
index 74e300ca3..ab6981076 100644
--- a/pkg/workloadendpointmanager/workloadendpoint_manager.go
+++ b/pkg/workloadendpointmanager/workloadendpoint_manager.go
@@ -5,7 +5,6 @@ package workloadendpointmanager
 
 import (
 	"context"
-	"errors"
 	"fmt"
 	"math/rand"
 	"time"
@@ -108,12 +107,11 @@ func (em *workloadEndpointManager) PatchIPAllocationResults(ctx context.Context,
 		return fmt.Errorf("pod %w", constant.ErrMissingRequiredParam)
 	}
 
-	allocation := &spiderpoolv1.PodIPAllocation{
-		ContainerID:  containerID,
-		UID:          string(pod.UID),
-		Node:         pod.Spec.NodeName,
-		IPs:          convert.ConvertResultsToIPDetails(results),
-		CreationTime: &metav1.Time{Time: time.Now()},
+	allocation := spiderpoolv1.PodIPAllocation{
+		ContainerID: containerID,
+		UID:         string(pod.UID),
+		Node:        pod.Spec.NodeName,
+		IPs:         convert.ConvertResultsToIPDetails(results),
 	}
 
 	if endpoint == nil {
@@ -126,6 +124,7 @@ func (em *workloadEndpointManager) PatchIPAllocationResults(ctx context.Context,
 				Current:             allocation,
 				OwnerControllerType: podController.Kind,
 				OwnerControllerName: podController.Name,
+				CreationTime:        metav1.Time{Time: time.Now()},
 			},
 		}
 
@@ -142,8 +141,7 @@ func (em *workloadEndpointManager) PatchIPAllocationResults(ctx context.Context,
 		return em.client.Create(ctx, endpoint)
 	}
 
-	if endpoint.Status.Current != nil &&
-		endpoint.Status.Current.ContainerID == containerID &&
+	if endpoint.Status.Current.ContainerID == containerID &&
 		endpoint.Status.Current.UID == string(pod.UID) {
 		return nil
 	}
@@ -157,10 +155,6 @@ func (em *workloadEndpointManager) ReallocateCurrentIPAllocation(ctx context.Con
 		return fmt.Errorf("endpoint %w", constant.ErrMissingRequiredParam)
 	}
 
-	if endpoint.Status.Current == nil {
-		return errors.New("data is broken, Endpoint doesn't have current IP allocation")
-	}
-
 	if endpoint.Status.Current.ContainerID == containerID &&
 		endpoint.Status.Current.UID == uid {
 		return nil


Test patch:
diff --git a/pkg/workloadendpointmanager/utils_test.go b/pkg/workloadendpointmanager/utils_test.go
index 7f0e1d3b6..b1518a5d8 100644
--- a/pkg/workloadendpointmanager/utils_test.go
+++ b/pkg/workloadendpointmanager/utils_test.go
@@ -37,14 +37,14 @@ var _ = Describe("WorkloadEndpointManager utils", Label("workloadendpoint_manage
 	Describe("Test RetrieveIPAllocation", func() {
 		var nic1, nic2 string
 		var uid string
-		var allocationT *spiderpoolv1.PodIPAllocation
+		var allocationT spiderpoolv1.PodIPAllocation
 
 		BeforeEach(func() {
 			nic1 = "eth0"
 			nic2 = "net1"
 
 			uid = string(uuid.NewUUID())
-			allocationT = &spiderpoolv1.PodIPAllocation{
+			allocationT = spiderpoolv1.PodIPAllocation{
 				UID: uid,
 				IPs: []spiderpoolv1.IPAllocationDetail{
 					{
@@ -68,11 +68,6 @@ var _ = Describe("WorkloadEndpointManager utils", Label("workloadendpoint_manage
 			Expect(allocation).To(BeNil())
 		})
 
-		It("retrieves the IP allocation but the current record is nil", func() {
-			allocation := workloadendpointmanager.RetrieveIPAllocation(uid, nic2, endpointT)
-			Expect(allocation).To(BeNil())
-		})
-
 		It("retrieves non-existent current IP allocation", func() {
 			endpointT.Status.Current = allocationT
 
@@ -84,7 +79,7 @@ var _ = Describe("WorkloadEndpointManager utils", Label("workloadendpoint_manage
 			endpointT.Status.Current = allocationT
 
 			allocation := workloadendpointmanager.RetrieveIPAllocation(uid, nic2, endpointT)
-			Expect(allocation).To(Equal(allocationT))
+			Expect(*allocation).To(Equal(allocationT))
 		})
 	})
 })
diff --git a/pkg/workloadendpointmanager/workloadendpoint_manager_test.go b/pkg/workloadendpointmanager/workloadendpoint_manager_test.go
index 776fcc86c..6274f2ed3 100644
--- a/pkg/workloadendpointmanager/workloadendpoint_manager_test.go
+++ b/pkg/workloadendpointmanager/workloadendpoint_manager_test.go
@@ -72,7 +72,7 @@ var _ = Describe("WorkloadEndpointManager", Label("workloadendpoint_manager_test
 					Labels:    labels,
 				},
 				Status: spiderpoolv1.WorkloadEndpointStatus{
-					Current: &spiderpoolv1.PodIPAllocation{},
+					Current: spiderpoolv1.PodIPAllocation{},
 				},
 			}
 		})
@@ -409,14 +409,6 @@ var _ = Describe("WorkloadEndpointManager", Label("workloadendpoint_manager_test
 				Expect(err).To(MatchError(constant.ErrMissingRequiredParam))
 			})
 
-			It("re-allocates but the Endpoint data is broken", func() {
-				endpointT.Status.Current = nil
-
-				ctx := context.TODO()
-				err := endpointManager.ReallocateCurrentIPAllocation(ctx, "", string(uuid.NewUUID()), "node1", endpointT)
-				Expect(err).To(HaveOccurred())
-			})
-
 			It("re-allocates the current IP allocation with the same container ID and Pod UID", func() {
 				uid := uuid.NewUUID()
 				endpointT.Status.Current.UID = string(uid)