Skip to content

Commit

Permalink
🎨 [minor] Update stable and active components in team to map (#26)
Browse files Browse the repository at this point in the history
* 🎨 Convert stable components in team to map

* 🎨 Convert active components in team to map

* 🎨 Format e2e tests
  • Loading branch information
zinref authored Apr 20, 2020
1 parent e69dc5e commit 3f70ada
Show file tree
Hide file tree
Showing 15 changed files with 151 additions and 171 deletions.
8 changes: 4 additions & 4 deletions api/v1beta1/activepromotion_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ type ActivePromotionStatus struct {
IsTimeout bool `json:"isTimeout,omitempty"`
// ActiveComponents represents a list of promoted active components
// +optional
ActiveComponents []StableComponent `json:"activeComponents,omitempty"`
ActiveComponents map[string]StableComponent `json:"activeComponents,omitempty"`
// OutdatedComponents represents map of outdated components
// +optional
OutdatedComponents map[string]OutdatedComponent `json:"outdatedComponents,omitempty"`
Expand Down Expand Up @@ -217,12 +217,12 @@ func (s *ActivePromotionStatus) SetPreActiveQueue(qs QueueStatus) {
}

func (s *ActivePromotionStatus) SetActiveComponents(comps []StableComponent) {
s.ActiveComponents = make([]StableComponent, 0)
s.ActiveComponents = make(map[string]StableComponent)
for _, currentComp := range comps {
s.ActiveComponents = append(s.ActiveComponents, StableComponent{
s.ActiveComponents[currentComp.Spec.Name] = StableComponent{
Spec: currentComp.Spec,
Status: currentComp.Status,
})
}
}
}

Expand Down
76 changes: 38 additions & 38 deletions api/v1beta1/team_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ type TeamStatus struct {

// StableComponentList represents a list of stable components
// +optional
StableComponents []StableComponent `json:"stableComponents,omitempty"`
StableComponents map[string]StableComponent `json:"stableComponents,omitempty"`

// ActiveComponents represents a list of stable components in active namespace
// +optional
ActiveComponents []StableComponent `json:"activeComponents,omitempty"`
ActiveComponents map[string]StableComponent `json:"activeComponents,omitempty"`

// Conditions contains observations of the resource's state e.g.,
// Team namespace is created, destroyed
Expand All @@ -108,15 +108,17 @@ type TeamStatus struct {
DesiredComponentImageCreatedTime map[string]map[string]DesiredImageTime `json:"desiredComponentImageCreatedTime,omitempty"`
}

func (ts *TeamStatus) GetStableComponent(stableCompName string) *StableComponent {
for i := 0; i < len(ts.StableComponents); i++ {
comp := ts.StableComponents[i]
if comp.Spec.Name == stableCompName {
return &comp
}
func (ts *TeamStatus) GetStableComponent(stableCompName string) StableComponent {
if len(ts.StableComponents) == 0 {
return StableComponent{}
}

stableComp, ok := ts.StableComponents[stableCompName]
if !ok {
return StableComponent{}
}

return nil
return stableComp
}

// SetStableComponents sets stable components
Expand All @@ -125,49 +127,47 @@ func (ts *TeamStatus) SetStableComponents(stableComp *StableComponent, isDeleted
return false
}

for i := 0; i < len(ts.StableComponents); i++ {
comp := ts.StableComponents[i]
if isDeleted {
if comp.Spec.Name == stableComp.Spec.Name {
ts.StableComponents[i] = ts.StableComponents[len(ts.StableComponents)-1]
ts.StableComponents = ts.StableComponents[:len(ts.StableComponents)-1]
return true
}

continue
}

if comp.Spec.Name == stableComp.Spec.Name {
if comp.Spec != stableComp.Spec {
ts.StableComponents[i].Spec = stableComp.Spec
ts.StableComponents[i].Status = stableComp.Status
return true
}

return false
}
if ts.StableComponents == nil {
ts.StableComponents = make(map[string]StableComponent)
}

if !isDeleted {
tStableComp, ok := ts.StableComponents[stableComp.Name]
if !ok && !isDeleted {
// add new stable component
ts.StableComponents = append(ts.StableComponents, StableComponent{
ts.StableComponents[stableComp.Name] = StableComponent{
Spec: stableComp.Spec,
Status: stableComp.Status,
})
}
return true
}

if ok {
if isDeleted {
delete(ts.StableComponents, stableComp.Name)
return true
}

if tStableComp.Spec != stableComp.Spec {
sComp := StableComponent{
Spec: stableComp.Spec,
Status: stableComp.Status,
}
ts.StableComponents[stableComp.Name] = sComp
return true
}
}

return false
}

// SetActiveComponents sets active components
func (ts *TeamStatus) SetActiveComponents(comps []StableComponent) {
ts.ActiveComponents = make([]StableComponent, 0)
for _, currentComp := range comps {
ts.ActiveComponents = append(ts.ActiveComponents, StableComponent{
func (ts *TeamStatus) SetActiveComponents(comps map[string]StableComponent) {
ts.ActiveComponents = make(map[string]StableComponent)
for compName, currentComp := range comps {
ts.ActiveComponents[compName] = StableComponent{
Spec: currentComp.Spec,
Status: currentComp.Status,
})
}
}
}

Expand Down
18 changes: 9 additions & 9 deletions api/v1beta1/zz_generated.deepcopy.go

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

8 changes: 4 additions & 4 deletions config/crds/env.samsahai.io_activepromotionhistories.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ spec:
ActivePromotion
properties:
activeComponents:
description: ActiveComponents represents a list of promoted
active components
items:
additionalProperties:
description: StableComponent is the Schema for the stablecomponents
API
properties:
Expand Down Expand Up @@ -120,7 +118,9 @@ spec:
type: string
type: object
type: object
type: array
description: ActiveComponents represents a list of promoted
active components
type: object
activePromotionHistoryName:
description: ActivePromotionHistoryName represents created ActivePromotionHistoryName
name
Expand Down
6 changes: 3 additions & 3 deletions config/crds/env.samsahai.io_activepromotions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ spec:
description: ActivePromotionStatus defines the observed state of ActivePromotion
properties:
activeComponents:
description: ActiveComponents represents a list of promoted active components
items:
additionalProperties:
description: StableComponent is the Schema for the stablecomponents
API
properties:
Expand Down Expand Up @@ -96,7 +95,8 @@ spec:
type: string
type: object
type: object
type: array
description: ActiveComponents represents a list of promoted active components
type: object
activePromotionHistoryName:
description: ActivePromotionHistoryName represents created ActivePromotionHistoryName
name
Expand Down
14 changes: 7 additions & 7 deletions config/crds/env.samsahai.io_teams.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,7 @@ spec:
description: TeamStatus defines the observed state of Team
properties:
activeComponents:
description: ActiveComponents represents a list of stable components
in active namespace
items:
additionalProperties:
description: StableComponent is the Schema for the stablecomponents
API
properties:
Expand Down Expand Up @@ -193,7 +191,9 @@ spec:
type: string
type: object
type: object
type: array
description: ActiveComponents represents a list of stable components
in active namespace
type: object
conditions:
description: Conditions contains observations of the resource's state
e.g., Team namespace is created, destroyed
Expand Down Expand Up @@ -232,8 +232,7 @@ spec:
type: string
type: object
stableComponents:
description: StableComponentList represents a list of stable components
items:
additionalProperties:
description: StableComponent is the Schema for the stablecomponents
API
properties:
Expand Down Expand Up @@ -285,7 +284,8 @@ spec:
type: string
type: object
type: object
type: array
description: StableComponentList represents a list of stable components
type: object
type: object
type: object
version: v1beta1
Expand Down
17 changes: 4 additions & 13 deletions docs/docs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
// This file was generated by swaggo/swag at
// 2020-04-01 21:22:44.065972 +0700 +07 m=+0.125130578
// 2020-04-16 13:30:12.640214 +0700 +07 m=+0.112977605

package docs

Expand Down Expand Up @@ -733,10 +733,7 @@ var doc = `{
"properties": {
"activeComponents": {
"description": "ActiveComponents represents a list of promoted active components\n+optional",
"type": "array",
"items": {
"$ref": "#/definitions/v1beta1.StableComponent"
}
"type": "object"
},
"activePromotionHistoryName": {
"description": "ActivePromotionHistoryName represents created ActivePromotionHistoryName name\n+optional",
Expand Down Expand Up @@ -1547,10 +1544,7 @@ var doc = `{
"properties": {
"activeComponents": {
"description": "ActiveComponents represents a list of stable components in active namespace\n+optional",
"type": "array",
"items": {
"$ref": "#/definitions/v1beta1.StableComponent"
}
"type": "object"
},
"conditions": {
"description": "Conditions contains observations of the resource's state e.g.,\nTeam namespace is created, destroyed\n+optional\n+patchMergeKey=type\n+patchStrategy=merge",
Expand All @@ -1570,10 +1564,7 @@ var doc = `{
},
"stableComponents": {
"description": "StableComponentList represents a list of stable components\n+optional",
"type": "array",
"items": {
"$ref": "#/definitions/v1beta1.StableComponent"
}
"type": "object"
}
}
},
Expand Down
15 changes: 3 additions & 12 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -712,10 +712,7 @@
"properties": {
"activeComponents": {
"description": "ActiveComponents represents a list of promoted active components\n+optional",
"type": "array",
"items": {
"$ref": "#/definitions/v1beta1.StableComponent"
}
"type": "object"
},
"activePromotionHistoryName": {
"description": "ActivePromotionHistoryName represents created ActivePromotionHistoryName name\n+optional",
Expand Down Expand Up @@ -1526,10 +1523,7 @@
"properties": {
"activeComponents": {
"description": "ActiveComponents represents a list of stable components in active namespace\n+optional",
"type": "array",
"items": {
"$ref": "#/definitions/v1beta1.StableComponent"
}
"type": "object"
},
"conditions": {
"description": "Conditions contains observations of the resource's state e.g.,\nTeam namespace is created, destroyed\n+optional\n+patchMergeKey=type\n+patchStrategy=merge",
Expand All @@ -1549,10 +1543,7 @@
},
"stableComponents": {
"description": "StableComponentList represents a list of stable components\n+optional",
"type": "array",
"items": {
"$ref": "#/definitions/v1beta1.StableComponent"
}
"type": "object"
}
}
},
Expand Down
12 changes: 3 additions & 9 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ definitions:
description: |-
ActiveComponents represents a list of promoted active components
+optional
items:
$ref: '#/definitions/v1beta1.StableComponent'
type: array
type: object
activePromotionHistoryName:
description: |-
ActivePromotionHistoryName represents created ActivePromotionHistoryName name
Expand Down Expand Up @@ -740,9 +738,7 @@ definitions:
description: |-
ActiveComponents represents a list of stable components in active namespace
+optional
items:
$ref: '#/definitions/v1beta1.StableComponent'
type: array
type: object
conditions:
description: |-
Conditions contains observations of the resource's state e.g.,
Expand All @@ -767,9 +763,7 @@ definitions:
description: |-
StableComponentList represents a list of stable components
+optional
items:
$ref: '#/definitions/v1beta1.StableComponent'
type: array
type: object
type: object
v1beta1.Teamcity:
properties:
Expand Down
2 changes: 1 addition & 1 deletion internal/samsahai.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ type SamsahaiController interface {
CreatePreActiveEnvironment(teamName, namespace string) error

// PromoteActiveEnvironment switches environment from pre-active to active and stores current active components
PromoteActiveEnvironment(teamComp *s2hv1beta1.Team, namespace string, comps []s2hv1beta1.StableComponent) error
PromoteActiveEnvironment(teamComp *s2hv1beta1.Team, namespace string, comps map[string]s2hv1beta1.StableComponent) error

// DestroyActiveEnvironment destroys active environment when active demotion is failure.
DestroyActiveEnvironment(teamName, namespace string) error
Expand Down
Loading

0 comments on commit 3f70ada

Please sign in to comment.