From 852296eff440c0644fb53426fffa01534182c85a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=A5=96=E5=BB=BA?= Date: Wed, 21 Jun 2023 09:51:29 +0800 Subject: [PATCH] some fixes --- pkg/apis/kubeovn/v1/condition.go | 67 ++++++++++++++++---------------- pkg/controller/controller.go | 1 + pkg/controller/ippool.go | 25 +++++------- 3 files changed, 43 insertions(+), 50 deletions(-) diff --git a/pkg/apis/kubeovn/v1/condition.go b/pkg/apis/kubeovn/v1/condition.go index 4c67bd86609..856869950ce 100644 --- a/pkg/apis/kubeovn/v1/condition.go +++ b/pkg/apis/kubeovn/v1/condition.go @@ -160,29 +160,28 @@ func (m *SubnetStatus) ClearAllConditions() { } } -func (m *IPPoolStatus) addCondition(ctype ConditionType, status corev1.ConditionStatus, reason, message string) { +func (s *IPPoolStatus) addCondition(ctype ConditionType, status corev1.ConditionStatus, reason, message string) { now := metav1.Now() - c := &IPPoolCondition{ + s.Conditions = append(s.Conditions, IPPoolCondition{ Type: ctype, LastUpdateTime: now, LastTransitionTime: now, Status: status, Reason: reason, Message: message, - } - m.Conditions = append(m.Conditions, *c) + }) } // setConditionValue updates or creates a new condition -func (m *IPPoolStatus) setConditionValue(ctype ConditionType, status corev1.ConditionStatus, reason, message string) { +func (s *IPPoolStatus) setConditionValue(ctype ConditionType, status corev1.ConditionStatus, reason, message string) { var c *IPPoolCondition - for i := range m.Conditions { - if m.Conditions[i].Type == ctype { - c = &m.Conditions[i] + for i := range s.Conditions { + if s.Conditions[i].Type == ctype { + c = &s.Conditions[i] } } if c == nil { - m.addCondition(ctype, status, reason, message) + s.addCondition(ctype, status, reason, message) } else { // check message ? if c.Status == status && c.Reason == reason && c.Message == message { @@ -200,69 +199,69 @@ func (m *IPPoolStatus) setConditionValue(ctype ConditionType, status corev1.Cond } // GetCondition get existing condition -func (m *IPPoolStatus) GetCondition(ctype ConditionType) *IPPoolCondition { - for i := range m.Conditions { - if m.Conditions[i].Type == ctype { - return &m.Conditions[i] +func (s *IPPoolStatus) GetCondition(ctype ConditionType) *IPPoolCondition { + for i := range s.Conditions { + if s.Conditions[i].Type == ctype { + return &s.Conditions[i] } } return nil } // EnsureCondition useful for adding default conditions -func (m *IPPoolStatus) EnsureCondition(ctype ConditionType) { - if c := m.GetCondition(ctype); c != nil { +func (s *IPPoolStatus) EnsureCondition(ctype ConditionType) { + if c := s.GetCondition(ctype); c != nil { return } - m.addCondition(ctype, corev1.ConditionUnknown, ReasonInit, "Not Observed") + s.addCondition(ctype, corev1.ConditionUnknown, ReasonInit, "Not Observed") } // EnsureStandardConditions - helper to inject standard conditions -func (m *IPPoolStatus) EnsureStandardConditions() { - m.EnsureCondition(Ready) - m.EnsureCondition(Error) +func (s *IPPoolStatus) EnsureStandardConditions() { + s.EnsureCondition(Ready) + s.EnsureCondition(Error) } // SetCondition updates or creates a new condition -func (m *IPPoolStatus) SetCondition(ctype ConditionType, reason, message string) { - m.setConditionValue(ctype, corev1.ConditionTrue, reason, message) +func (s *IPPoolStatus) SetCondition(ctype ConditionType, reason, message string) { + s.setConditionValue(ctype, corev1.ConditionTrue, reason, message) } // ClearCondition updates or creates a new condition -func (m *IPPoolStatus) ClearCondition(ctype ConditionType, reason, message string) { - m.setConditionValue(ctype, corev1.ConditionFalse, reason, message) +func (s *IPPoolStatus) ClearCondition(ctype ConditionType, reason, message string) { + s.setConditionValue(ctype, corev1.ConditionFalse, reason, message) } // Ready - shortcut to set ready condition to true -func (m *IPPoolStatus) Ready(reason, message string) { - m.SetCondition(Ready, reason, message) +func (s *IPPoolStatus) Ready(reason, message string) { + s.SetCondition(Ready, reason, message) } // NotReady - shortcut to set ready condition to false -func (m *IPPoolStatus) NotReady(reason, message string) { - m.ClearCondition(Ready, reason, message) +func (s *IPPoolStatus) NotReady(reason, message string) { + s.ClearCondition(Ready, reason, message) } // SetError - shortcut to set error condition -func (m *IPPoolStatus) SetError(reason, message string) { - m.SetCondition(Error, reason, message) +func (s *IPPoolStatus) SetError(reason, message string) { + s.SetCondition(Error, reason, message) } // ClearError - shortcut to set error condition -func (m *IPPoolStatus) ClearError() { - m.ClearCondition(Error, "NoError", "No error seen") +func (s *IPPoolStatus) ClearError() { + s.ClearCondition(Error, "NoError", "No error seen") } // IsConditionTrue - if condition is true -func (m *IPPoolStatus) IsConditionTrue(ctype ConditionType) bool { - if c := m.GetCondition(ctype); c != nil { +func (s IPPoolStatus) IsConditionTrue(ctype ConditionType) bool { + if c := s.GetCondition(ctype); c != nil { return c.Status == corev1.ConditionTrue } return false } // IsReady returns true if ready condition is set -func (m *IPPoolStatus) IsReady() bool { return m.IsConditionTrue(Ready) } +func (s IPPoolStatus) IsReady() bool { return s.IsConditionTrue(Ready) } // SetVlanError - shortcut to set error condition func (v *VlanStatus) SetVlanError(reason, message string) { diff --git a/pkg/controller/controller.go b/pkg/controller/controller.go index 0969d71a855..5b4b18f5bc5 100644 --- a/pkg/controller/controller.go +++ b/pkg/controller/controller.go @@ -853,6 +853,7 @@ func (c *Controller) shutdown() { c.syncVirtualPortsQueue.ShutDown() c.addOrUpdateIPPoolQueue.ShutDown() + c.updateIPPoolStatusQueue.ShutDown() c.deleteIPPoolQueue.ShutDown() c.addNodeQueue.ShutDown() diff --git a/pkg/controller/ippool.go b/pkg/controller/ippool.go index 340cc521d59..8b8d5cb98e1 100644 --- a/pkg/controller/ippool.go +++ b/pkg/controller/ippool.go @@ -174,7 +174,7 @@ func (c *Controller) handleAddOrUpdateIPPool(key string) error { ippool.Status.EnsureStandardConditions() if err = c.ipam.AddOrUpdateIPPool(ippool.Spec.Subnet, ippool.Name, ippool.Spec.IPs); err != nil { klog.Errorf("failed to add/update ippool %s with IPs %v in subnet %s: %v", ippool.Name, ippool.Spec.IPs, ippool.Spec.Subnet, err) - if patchErr := c.patchIPPoolStatus(ippool, "UpdateIPAMFailed", err.Error()); patchErr != nil { + if patchErr := c.patchIPPoolStatusCondition(ippool, "UpdateIPAMFailed", err.Error()); patchErr != nil { klog.Error(patchErr) } return err @@ -190,7 +190,7 @@ func (c *Controller) handleAddOrUpdateIPPool(key string) error { ippool.Status.V6AvailableIPRange = v6as ippool.Status.V6UsingIPRange = v6us - if err = c.patchIPPoolStatus(ippool, "UpdateIPAMSucceeded", ""); err != nil { + if err = c.patchIPPoolStatusCondition(ippool, "UpdateIPAMSucceeded", ""); err != nil { klog.Error(err) return err } @@ -253,21 +253,10 @@ func (c *Controller) handleUpdateIPPoolStatus(key string) error { return nil } - bytes, err := ippool.Status.Bytes() - if err != nil { - klog.Errorf("failed to generate json representation for status of ippool %s: %v", ippool.Name, err) - return err - } - _, err = c.config.KubeOvnClient.KubeovnV1().IPPools().Patch(context.Background(), ippool.Name, types.MergePatchType, bytes, metav1.PatchOptions{}, "status") - if err != nil { - klog.Errorf("failed to patch status of ippool %s: %v", ippool.Name, err) - return err - } - - return nil + return c.patchIPPoolStatus(ippool) } -func (c Controller) patchIPPoolStatus(ippool *kubeovnv1.IPPool, reason, errMsg string) error { +func (c Controller) patchIPPoolStatusCondition(ippool *kubeovnv1.IPPool, reason, errMsg string) error { if errMsg != "" { ippool.Status.SetError(reason, errMsg) ippool.Status.NotReady(reason, errMsg) @@ -277,9 +266,13 @@ func (c Controller) patchIPPoolStatus(ippool *kubeovnv1.IPPool, reason, errMsg s c.recorder.Eventf(ippool, corev1.EventTypeNormal, reason, errMsg) } + return c.patchIPPoolStatus(ippool) +} + +func (c Controller) patchIPPoolStatus(ippool *kubeovnv1.IPPool) error { bytes, err := ippool.Status.Bytes() if err != nil { - klog.Error(err) + klog.Errorf("failed to generate json representation for status of ippool %s: %v", ippool.Name, err) return err } if _, err = c.config.KubeOvnClient.KubeovnV1().IPPools().Patch(context.Background(), ippool.Name, types.MergePatchType, bytes, metav1.PatchOptions{}, "status"); err != nil {