Skip to content

Commit

Permalink
some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangzujian committed Jun 21, 2023
1 parent d4a8924 commit 852296e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 50 deletions.
67 changes: 33 additions & 34 deletions pkg/apis/kubeovn/v1/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,7 @@ func (c *Controller) shutdown() {
c.syncVirtualPortsQueue.ShutDown()

c.addOrUpdateIPPoolQueue.ShutDown()
c.updateIPPoolStatusQueue.ShutDown()
c.deleteIPPoolQueue.ShutDown()

c.addNodeQueue.ShutDown()
Expand Down
25 changes: 9 additions & 16 deletions pkg/controller/ippool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down

0 comments on commit 852296e

Please sign in to comment.