Skip to content

Commit

Permalink
🎨 Format exporter code
Browse files Browse the repository at this point in the history
  • Loading branch information
sunny299 authored and Pohfy123 committed Mar 17, 2020
1 parent dfed19c commit 2d1c519
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 33 deletions.
4 changes: 3 additions & 1 deletion internal/samsahai/activepromotion/postactive.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ func (c *controller) runPostActive(ctx context.Context, atpComp *s2hv1beta1.Acti
if err := c.setOutdatedDuration(ctx, atpComp); err != nil {
return err
}
exporter.SetOutdatedComponentMetric(atpComp)
if atpComp != nil && atpComp.Status.Result != s2hv1beta1.ActivePromotionCanceled {
exporter.SetOutdatedComponentMetric(atpComp)
}

histName, err := c.createActivePromotionHistory(ctx, atpComp)
if err != nil && !k8serrors.IsAlreadyExists(err) {
Expand Down
26 changes: 13 additions & 13 deletions internal/samsahai/exporter/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ func SetQueueMetric(queueList *v1beta1.QueueList, teamList map[string]internal.C
for i := range queueList.Items {
queueStateList := map[string]float64{"waiting": 0, "testing": 0, "finished": 0, "deploying": 0, "cleaning": 0}
queue := queueList.Items[i]
switch string(queue.Status.State) {
case "waiting":
switch queue.Status.State {
case v1beta1.Waiting:
queueStateList["waiting"] = 1
for state, val := range queueStateList {
QueueMetric.WithLabelValues(
Expand All @@ -95,7 +95,7 @@ func SetQueueMetric(queueList *v1beta1.QueueList, teamList map[string]internal.C
state,
strconv.Itoa(queue.Status.NoOfProcessed)).Set(val)
}
case "testing", "collecting":
case v1beta1.Testing, v1beta1.Collecting:
queueStateList["testing"] = 1
for state, val := range queueStateList {
QueueMetric.WithLabelValues(
Expand All @@ -106,7 +106,7 @@ func SetQueueMetric(queueList *v1beta1.QueueList, teamList map[string]internal.C
state,
strconv.Itoa(queue.Status.NoOfProcessed)).Set(val)
}
case "finished":
case v1beta1.Finished:
queueStateList["finished"] = 1
for state, val := range queueStateList {
QueueMetric.WithLabelValues(
Expand All @@ -117,7 +117,7 @@ func SetQueueMetric(queueList *v1beta1.QueueList, teamList map[string]internal.C
state,
strconv.Itoa(queue.Status.NoOfProcessed)).Set(val)
}
case "detect_missing_image", "creating":
case v1beta1.DetectingImageMissing, v1beta1.Creating:
queueStateList["deploying"] = 1
for state, val := range queueStateList {
QueueMetric.WithLabelValues(
Expand All @@ -128,7 +128,7 @@ func SetQueueMetric(queueList *v1beta1.QueueList, teamList map[string]internal.C
state,
strconv.Itoa(queue.Status.NoOfProcessed)).Set(val)
}
case "cleaning_before", "cleaning_after":
case v1beta1.CleaningBefore, v1beta1.CleaningAfter:
queueStateList["cleaning"] = 1
for state, val := range queueStateList {
QueueMetric.WithLabelValues(
Expand Down Expand Up @@ -173,38 +173,38 @@ func SetActivePromotionMetric(activePromotionList *v1beta1.ActivePromotionList)
for i := range activePromotionList.Items {
activePromStateList := map[string]float64{"waiting": 0, "deploying": 0, "testing": 0, "promoting": 0, "destroying": 0}
activeProm := activePromotionList.Items[i]
atpState := string(activeProm.Status.State)
atpState := activeProm.Status.State
if atpState != "" {
switch atpState {
case "waiting":
case v1beta1.ActivePromotionWaiting:
activePromStateList["waiting"] = 1
for state, val := range activePromStateList {
ActivePromotionMetric.WithLabelValues(
activeProm.Name,
state).Set(val)
}
case "deployingStableComponents", "creatingPreActiveEnvironment":
case v1beta1.ActivePromotionDeployingComponents, v1beta1.ActivePromotionCreatingPreActive:
activePromStateList["deploying"] = 1
for state, val := range activePromStateList {
ActivePromotionMetric.WithLabelValues(
activeProm.Name,
state).Set(val)
}
case "testingPreActiveEnvironment", "collectingPreActiveResult":
case v1beta1.ActivePromotionTestingPreActive, v1beta1.ActivePromotionCollectingPreActiveResult:
activePromStateList["testing"] = 1
for state, val := range activePromStateList {
ActivePromotionMetric.WithLabelValues(
activeProm.Name,
state).Set(val)
}
case "promotingActiveEnvironment", "demotingActiveEnvironment":
case v1beta1.ActivePromotionActiveEnvironment, v1beta1.ActivePromotionDemoting:
activePromStateList["promoting"] = 1
for state, val := range activePromStateList {
ActivePromotionMetric.WithLabelValues(
activeProm.Name,
state).Set(val)
}
case "destroyingPreviousActiveEnvironment", "destroyingPreActiveEnvironment", "finished":
case v1beta1.ActivePromotionDestroyingPreActive, v1beta1.ActivePromotionDestroyingPreviousActive, v1beta1.ActivePromotionFinished:
activePromStateList["destroying"] = 1
for state, val := range activePromStateList {
ActivePromotionMetric.WithLabelValues(
Expand Down Expand Up @@ -318,7 +318,7 @@ func SetOutdatedComponentMetric(outdatedComponent *v1beta1.ActivePromotion) {
teamName := outdatedComponent.Name
for i := range outdatedComponent.Status.OutdatedComponents {
outdated := outdatedComponent.Status.OutdatedComponents[i]
outdatedDays := outdated.OutdatedDuration / 24 * time.Hour
outdatedDays := outdated.OutdatedDuration / (24 * time.Hour)
OutdatedComponentMetric.WithLabelValues(
teamName,
outdated.Name,
Expand Down
10 changes: 5 additions & 5 deletions internal/samsahai/exporter/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ var _ = Describe("Samsahai Exporter", func() {
Namespace: namespace,
},
Status: s2hv1beta1.ActivePromotionStatus{
State: "waiting",
State: s2hv1beta1.ActivePromotionWaiting,
},
},
{
Expand All @@ -183,7 +183,7 @@ var _ = Describe("Samsahai Exporter", func() {
Namespace: namespace,
},
Status: s2hv1beta1.ActivePromotionStatus{
State: "finished",
State: s2hv1beta1.ActivePromotionFinished,
},
},
},
Expand All @@ -202,7 +202,7 @@ var _ = Describe("Samsahai Exporter", func() {
Spec: s2hv1beta1.ActivePromotionHistorySpec{
ActivePromotion: &s2hv1beta1.ActivePromotion{
Status: s2hv1beta1.ActivePromotionStatus{
State: "DestroyingPreviousActiveEnvironment",
State: s2hv1beta1.ActivePromotionDestroyingPreActive,
StartedAt: startDate(2019, 12, 10, 2, 22, 02),
PreActiveQueue: s2hv1beta1.QueueStatus{
Conditions: []s2hv1beta1.QueueCondition{
Expand Down Expand Up @@ -350,9 +350,9 @@ var _ = Describe("Samsahai Exporter", func() {
defer close(done)
data, err := http.Get("http://localhost:8008/metrics")
g.Expect(err).NotTo(HaveOccurred())
expectedData := strings.Contains(string(data), `samsahai_outdated_component{component="testOCName1",currentVer="2019.12.07.00",desiredVer="2019.12.10.00",teamName="testOCTeamName"} 2.432554678116942e+18`)
expectedData := strings.Contains(string(data), `samsahai_outdated_component{component="testOCName1",currentVer="2019.12.07.00",desiredVer="2019.12.10.00",teamName="testOCTeamName"} 1`)
g.Expect(expectedData).To(BeTrue())
expectedData = strings.Contains(string(data), `samsahai_outdated_component{component="testOCName2",currentVer="2019.12.07.00",desiredVer="2019.12.10.00",teamName="testOCTeamName"} 4.214431078557532e+17`)
expectedData = strings.Contains(string(data), `samsahai_outdated_component{component="testOCName2",currentVer="2019.12.07.00",desiredVer="2019.12.10.00",teamName="testOCTeamName"} 1`)
g.Expect(expectedData).To(BeTrue())
}, timeout)

Expand Down
38 changes: 24 additions & 14 deletions internal/samsahai/internal_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ type updateTeamGit string
type updateHealth struct {
}

type exportMetric struct {
}

// updateTeamDesiredComponent defines which component of which team to be checked and updated
type updateTeamDesiredComponent struct {
TeamName string
Expand All @@ -58,8 +61,8 @@ func (c *controller) Start(stop <-chan struct{}) {
}, jitterPeriod, c.internalStop)
}

c.exportAllMetric()
c.queue.Add(updateHealth{})
c.queue.AddAfter(exportMetric{}, (30*time.Second))

<-stop

Expand Down Expand Up @@ -103,6 +106,8 @@ func (c *controller) process() bool {
err = c.updateTeamDesiredComponent(v)
case updateHealth:
err = c.updateHealthMetric()
case exportMetric:
err = c.exportAllMetric()
default:
c.queue.Forget(obj)
return true
Expand Down Expand Up @@ -448,7 +453,7 @@ type outdatedComponentTime struct {
CreatedTime *metav1.Time
}

func (c *controller) exportAllMetric() {
func (c *controller) exportAllMetric() error {

//team name
exporter.SetTeamNameMetric(c.teamConfigs)
Expand Down Expand Up @@ -484,27 +489,32 @@ func (c *controller) exportAllMetric() {
//outdated component
oc := map[string]outdatedComponentTime{}
for _, atpHistories := range atpHisList.Items {
teamName := atpHistories.Labels["samsahai.io/teamname"]
if atpHistories.Spec.ActivePromotion == nil {
teamName := atpHistories.Spec.TeamName
if teamName == "" {
teamName = atpHistories.Labels["samsahai.io/teamname"]
}
if atpHistories.Spec.ActivePromotion == nil {
continue
}
if atpHistories.Spec.ActivePromotion.Status.Result == s2hv1beta1.ActivePromotionCanceled {
continue
}
itemCreateTime := atpHistories.Spec.ActivePromotion.CreationTimestamp
itemCreateTime := atpHistories.CreationTimestamp
if obj, ok := oc[teamName]; ok {
if obj.CreatedTime.Before(&itemCreateTime) {
oc[teamName] = outdatedComponentTime{
atpHistories.Spec.ActivePromotion,
obj.CreatedTime}
if !obj.CreatedTime.Before(&itemCreateTime) {
continue
}
} else {
oc[teamName] = outdatedComponentTime{
atpHistories.Spec.ActivePromotion,
&itemCreateTime}
}
atpHistories.Spec.ActivePromotion.Name = teamName
oc[teamName] = outdatedComponentTime{
atpHistories.Spec.ActivePromotion,
&itemCreateTime,
}
}
for _, obj := range oc {
exporter.SetOutdatedComponentMetric(obj.Component)
}

return nil
}

func (c *controller) updateHealthMetric() error {
Expand Down

0 comments on commit 2d1c519

Please sign in to comment.