Skip to content

Commit

Permalink
Merge pull request #92 from alauda/feat/add-subnet-status
Browse files Browse the repository at this point in the history
add subnet status
  • Loading branch information
halfcrazy authored Aug 16, 2019
2 parents bfecab4 + d37da1b commit 7d6b950
Show file tree
Hide file tree
Showing 26 changed files with 582 additions and 77 deletions.
155 changes: 155 additions & 0 deletions pkg/apis/kubeovn/v1/condition.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package v1

import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func (m *SubnetStatus) addCondition(ctype ConditionType, status corev1.ConditionStatus, reason, message string) {
now := metav1.Now()
c := &SubnetCondition{
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 *SubnetStatus) setConditionValue(ctype ConditionType, status corev1.ConditionStatus, reason, message string) {
var c *SubnetCondition
for i := range m.Conditions {
if m.Conditions[i].Type == ctype {
c = &m.Conditions[i]
}
}
if c == nil {
m.addCondition(ctype, status, reason, message)
} else {
// check message ?
if c.Status == status && c.Reason == reason && c.Message == message {
return
}
now := metav1.Now()
c.LastUpdateTime = now
if c.Status != status {
c.LastTransitionTime = now
}
c.Status = status
c.Reason = reason
c.Message = message
}
}

// RemoveCondition removes the condition with the provided type.
func (m *SubnetStatus) RemoveCondition(ctype ConditionType) {
for i, c := range m.Conditions {
if c.Type == ctype {
m.Conditions[i] = m.Conditions[len(m.Conditions)-1]
m.Conditions = m.Conditions[:len(m.Conditions)-1]
break
}
}
}

// GetCondition get existing condition
func (m *SubnetStatus) GetCondition(ctype ConditionType) *SubnetCondition {
for i := range m.Conditions {
if m.Conditions[i].Type == ctype {
return &m.Conditions[i]
}
}
return nil
}

// IsConditionTrue - if condition is true
func (m *SubnetStatus) IsConditionTrue(ctype ConditionType) bool {
if c := m.GetCondition(ctype); c != nil {
return c.Status == corev1.ConditionTrue
}
return false
}

// IsReady returns true if ready condition is set
func (m *SubnetStatus) IsReady() bool { return m.IsConditionTrue(Ready) }

// IsNotReady returns true if ready condition is set
func (m *SubnetStatus) IsNotReady() bool { return !m.IsConditionTrue(Ready) }

// ConditionReason - return condition reason
func (m *SubnetStatus) ConditionReason(ctype ConditionType) string {
if c := m.GetCondition(ctype); c != nil {
return c.Reason
}
return ""
}

// Ready - shortcut to set ready contition to true
func (m *SubnetStatus) Ready(reason, message string) {
m.SetCondition(Ready, reason, message)
}

// NotReady - shortcut to set ready contition to false
func (m *SubnetStatus) NotReady(reason, message string) {
m.ClearCondition(Ready, reason, message)
}

// Validated - shortcut to set validated contition to true
func (m *SubnetStatus) Validated(reason, message string) {
m.SetCondition(Validated, reason, message)
}

// NotValidated - shortcut to set validated contition to false
func (m *SubnetStatus) NotValidated(reason, message string) {
m.ClearCondition(Validated, reason, message)
}

// SetError - shortcut to set error condition
func (m *SubnetStatus) SetError(reason, message string) {
m.SetCondition(Error, reason, message)
}

// ClearError - shortcut to set error condition
func (m *SubnetStatus) ClearError() {
m.ClearCondition(Error, "NoError", "No error seen")
}

// EnsureCondition useful for adding default conditions
func (m *SubnetStatus) EnsureCondition(ctype ConditionType) {
if c := m.GetCondition(ctype); c != nil {
return
}
m.addCondition(ctype, corev1.ConditionUnknown, ReasonInit, "Not Observed")
}

// EnsureStandardConditions - helper to inject standard conditions
func (m *SubnetStatus) EnsureStandardConditions() {
m.EnsureCondition(Ready)
m.EnsureCondition(Validated)
m.EnsureCondition(Error)
}

// ClearCondition updates or creates a new condition
func (m *SubnetStatus) ClearCondition(ctype ConditionType, reason, message string) {
m.setConditionValue(ctype, corev1.ConditionFalse, reason, message)
}

// SetCondition updates or creates a new condition
func (m *SubnetStatus) SetCondition(ctype ConditionType, reason, message string) {
m.setConditionValue(ctype, corev1.ConditionTrue, reason, message)
}

// RemoveAllConditions updates or creates a new condition
func (m *SubnetStatus) RemoveAllConditions() {
m.Conditions = []SubnetCondition{}
}

// ClearAllConditions updates or creates a new condition
func (m *SubnetStatus) ClearAllConditions() {
for i := range m.Conditions {
m.Conditions[i].Status = corev1.ConditionFalse
}
}
16 changes: 16 additions & 0 deletions pkg/apis/kubeovn/v1/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package v1

import (
"encoding/json"
"fmt"
)

func (ss *SubnetStatus) Bytes() ([]byte, error) {
//{"availableIPs":65527,"usingIPs":9} => {"status": {"availableIPs":65527,"usingIPs":9}}
bytes, err := json.Marshal(ss)
if err != nil {
return nil, err
}
newStr := fmt.Sprintf(`{"status": %s}`, string(bytes))
return []byte(newStr), nil
}
52 changes: 50 additions & 2 deletions pkg/apis/kubeovn/v1/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v1

import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand All @@ -13,6 +14,18 @@ const (
GWCentralizedType = "centralized"
)

// Constants for condition
const (
// Ready => controller considers this resource Ready
Ready = "Ready"
// Validated => Spec passed validating
Validated = "Validated"
// Error => last recorded error
Error = "Error"

ReasonInit = "Init"
)

// +genclient
// +genclient:noStatus
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down Expand Up @@ -45,15 +58,15 @@ type IPList struct {
}

// +genclient
// +genclient:noStatus
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +genclient:nonNamespaced

type Subnet struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec SubnetSpec `json:"spec"`
Spec SubnetSpec `json:"spec"`
Status SubnetStatus `json:"status,omitempty"`
}

type SubnetSpec struct {
Expand All @@ -72,6 +85,41 @@ type SubnetSpec struct {
AllowSubnets []string `json:"allowSubnets,omitempty"`
}

// ConditionType encodes information on the condition
type ConditionType string

// Condition describes the state of an object at a certain point.
// +k8s:deepcopy-gen=true
type SubnetCondition struct {
// Type of condition.
Type ConditionType `json:"type"`
// Status of the condition, one of True, False, Unknown.
Status corev1.ConditionStatus `json:"status"`
// The reason for the condition's last transition.
// +optional
Reason string `json:"reason,omitempty"`
// A human readable message indicating details about the transition.
// +optional
Message string `json:"message,omitempty"`
// Last time the condition was probed
// +optional
LastUpdateTime metav1.Time `json:"lastUpdateTime,omitempty"`
// Last time the condition transitioned from one status to another.
// +optional
LastTransitionTime metav1.Time `json:"lastTransitionTime,omitempty"`
}

type SubnetStatus struct {
// Conditions represents the latest state of the object
// +optional
// +patchMergeKey=type
// +patchStrategy=merge
Conditions []SubnetCondition `json:"conditions,omitempty" patchStrategy:"merge" patchMergeKey:"type"`

AvailableIPs uint64 `json:"availableIPs"`
UsingIPs uint64 `json:"usingIPs"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

type SubnetList struct {
Expand Down
44 changes: 43 additions & 1 deletion pkg/apis/kubeovn/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 10 additions & 3 deletions pkg/client/clientset/versioned/clientset.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions pkg/client/clientset/versioned/fake/register.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions pkg/client/clientset/versioned/scheme/register.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7d6b950

Please sign in to comment.