Skip to content

Commit

Permalink
Fix bugs: Update status.
Browse files Browse the repository at this point in the history
  • Loading branch information
yongzhi.yang committed Jun 13, 2019
1 parent d1e733f commit 4acec07
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 23 deletions.
40 changes: 33 additions & 7 deletions controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,23 +517,29 @@ func (c *Controller) handleObject(obj interface{}) {
//
func (c *Controller) updateDeleteMigrateStatus(migrate *v1.Migrate, deployments []*appsv1.Deployment) error {
migrateCopy := migrate.DeepCopy()
initialFinished := migrateCopy.Status.Finished
now := metav1.Now()
migrateCopy.Status.LastUpdateTime = &now

upsertCondition(migrateCopy, v1.MigrateCondition{
constant.ConcatConditionType(constant.BlueGroup), "True", now, now, "", "The deployment has been deleted"})
constant.ConcatConditionType(constant.BlueGroup), constant.ConditionStatusTrue, now, now, "", "The release has been deleted"})
upsertCondition(migrateCopy, v1.MigrateCondition{
constant.ConcatConditionType(constant.GreenGroup), "True", now, now, "", "The deployment has been deleted"})
constant.ConcatConditionType(constant.GreenGroup), constant.ConditionStatusTrue, now, now, "", "The release has been deleted"})

if deployments != nil && len(deployments) > 0 {
klog.Infof("The deployments for migrate: %s is null or empty.", migrate.GetName())
for _, deploy := range deployments {
message := fmt.Sprintf("Check the deployment:%s, replica count:%d, available count:%d", deploy.GetName(), deploy.Status.Replicas, deploy.Status.AvailableReplicas)
klog.Info(message)
upsertCondition(migrateCopy, v1.MigrateCondition{
constant.ConcatConditionType(deploy.Labels["sym-group"]), "False", now, now, "", "The deployment still exists"})
constant.ConcatConditionType(deploy.Labels["sym-group"]), constant.ConditionStatusFalse, now, now, "", "The deployment still exists"})
}
}

calFinalStatus(migrateCopy)
if initialFinished == false || migrateCopy.Status.Finished == false {
migrateCopy.Status.LastUpdateTime = &now
}

// NEVER modify objects from the store. It's a read-only, local cache.
// You can use DeepCopy() to make a deep copy of original object and modify this copy
// Or create a copy manually for better performance
Expand All @@ -549,23 +555,27 @@ func (c *Controller) updateDeleteMigrateStatus(migrate *v1.Migrate, deployments

func (c *Controller) updateInstallMigrateStatus(migrate *v1.Migrate, deployments []*appsv1.Deployment) error {
migrateCopy := migrate.DeepCopy()
initialFinished := migrateCopy.Status.Finished
now := metav1.Now()
migrateCopy.Status.LastUpdateTime = &now

if deployments != nil && len(deployments) > 0 {
klog.Infof("The deployments for migrate: %s is null or empty, update the status of the migrate.", migrate.GetName())
for _, deploy := range deployments {
conditionType := constant.ConcatConditionType(deploy.Labels["sym-group"])
message := fmt.Sprintf("Deployment %s has been available, replica count:%d, available count:%d",
deploy.GetName(), deploy.Status.Replicas, deploy.Status.AvailableReplicas)
upsertCondition(migrateCopy, v1.MigrateCondition{conditionType, "False", now, now, "", message})
upsertCondition(migrateCopy, v1.MigrateCondition{conditionType, constant.ConditionStatusFalse, now, now, "", message})
klog.Info(message)
if deploy.Status.Replicas == deploy.Status.AvailableReplicas {
upsertCondition(migrateCopy, v1.MigrateCondition{conditionType, "True", now, now, "", message})
upsertCondition(migrateCopy, v1.MigrateCondition{conditionType, constant.ConditionStatusTrue, now, now, "", message})
}
}
}

calFinalStatus(migrateCopy)
if initialFinished == false || migrateCopy.Status.Finished == false {
migrateCopy.Status.LastUpdateTime = &now
}
// Only for test
//clearConditions(migrateCopy)

Expand Down Expand Up @@ -602,6 +612,22 @@ func upsertCondition(migrateCopy *v1.Migrate, condition v1.MigrateCondition) {
migrateCopy.Status.Conditions = append(migrateCopy.Status.Conditions, condition)
}

func calFinalStatus(migrateCopy *v1.Migrate) {
if len(migrateCopy.Status.Conditions) != 2 {
migrateCopy.Status.Finished = false
return
}

for _, c := range migrateCopy.Status.Conditions {
if c.Status == constant.ConditionStatusFalse {
migrateCopy.Status.Finished = false
return
}
}

migrateCopy.Status.Finished = true
}

//Only use for a test
func clearConditions(migrateCopy *v1.Migrate) {
if len(migrateCopy.Status.Conditions) <= 0 {
Expand Down
25 changes: 12 additions & 13 deletions pkg/apis/devops/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ type MigrateList struct {

// MigrateSpec
type MigrateSpec struct {
AppName string `json:"appName,omitempty"`
Action MigrateActionType `json:"action,omitempty"`
Meta map[string]string `json:"meta,omitempty"`
Chart []byte `json:"chart,omitempty"`
// Releases is all of the helm release
Releases []*ReleasesConfig `json:"releases,omitempty"`
AppName string `json:"appName,omitempty"`
Action MigrateActionType `json:"action,omitempty"`
Meta map[string]string `json:"meta,omitempty"`
OverrideReplicas *int32 `json:"overrideReplicas,omitempty"`
Chart []byte `json:"chart,omitempty"`
Releases []*ReleasesConfig `json:"releases,omitempty"`
}

type MigrateActionType string
Expand All @@ -43,17 +43,16 @@ const (

// ReleasesConfig
type ReleasesConfig struct {
// Name is the name of the release
Name string `json:"name,omitempty"`
// Namespace is the kubernetes namespace of the release.
Namespace string `json:"namespace,omitempty"`
// Config supplies values to the parametrizable templates of a chart.
Raw string `json:"raw,omitempty"`
Values map[string]string `json:"values,omitempty"`
Name string `json:"name,omitempty"`
Namespace string `json:"namespace,omitempty"`
Raw string `json:"raw,omitempty"`
Values map[string]string `json:"values,omitempty"`
Meta map[string]string `json:"meta,omitempty"`
}

// MigrateStatus
type MigrateStatus struct {
Finished bool `json:"finished"`
Conditions []MigrateCondition `json:"conditions,omitempty"`
StartTime *metav1.Time `json:"startTime,omitempty"`
LastUpdateTime *metav1.Time `json:"lastUpdateTime,omitempty"`
Expand Down
12 changes: 12 additions & 0 deletions pkg/apis/devops/v1/zz_generated.deepcopy.go

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

8 changes: 5 additions & 3 deletions pkg/constant/constant.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package constant

const (
ConditionTypePrefix = "OK_"
BlueGroup = "blue"
GreenGroup = "green"
ConditionTypePrefix = "OK_"
BlueGroup = "blue"
GreenGroup = "green"
ConditionStatusTrue = "True"
ConditionStatusFalse = "False"
)

func ConcatConditionType(group string) string {
Expand Down

0 comments on commit 4acec07

Please sign in to comment.