Skip to content

Commit 0942f85

Browse files
authored
feat: add json output to status converge (#601)
Signed-off-by: Timur Tuktamyshev <timur.tuktamyshev@flant.com>
1 parent 8ad75c6 commit 0942f85

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

pkg/addon-operator/http_server.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package addon_operator
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
67
"net/http"
78
"strings"
@@ -123,12 +124,52 @@ func (op *AddonOperator) handleHealthCheck(writer http.ResponseWriter, _ *http.R
123124
}
124125

125126
// handleConvergeStatus reports the current convergence state
126-
func (op *AddonOperator) handleConvergeStatus(writer http.ResponseWriter, _ *http.Request) {
127+
func (op *AddonOperator) handleConvergeStatus(writer http.ResponseWriter, request *http.Request) {
127128
convergeTasks := ConvergeTasksInQueue(op.engine.TaskQueues.GetMain())
129+
130+
if request.URL.Query().Get("output") == "json" {
131+
writer.Header().Set("Content-Type", "application/json")
132+
response := generateConvergeJSON(op.ConvergeState.GetFirstRunPhase(), convergeTasks)
133+
_ = json.NewEncoder(writer).Encode(response)
134+
return
135+
}
136+
128137
statusLines := generateConvergeStatusLines(op.ConvergeState.GetFirstRunPhase(), convergeTasks)
129138
_, _ = writer.Write([]byte(strings.Join(statusLines, "\n") + "\n"))
130139
}
131140

141+
type status struct {
142+
ConvergeInProgress int `json:"CONVERGE_IN_PROGRESS"`
143+
ConvergeWaitTask bool `json:"CONVERGE_WAIT_TASK"`
144+
StartupConvergeDone bool `json:"STARTUP_CONVERGE_DONE"`
145+
StartupConvergeInProgress int `json:"STARTUP_CONVERGE_IN_PROGRESS"`
146+
StartupConvergeNotStarted bool `json:"STARTUP_CONVERGE_NOT_STARTED"`
147+
}
148+
149+
func generateConvergeJSON(phase converge.FirstConvergePhase, convergeTasks int) status {
150+
response := status{}
151+
152+
switch phase {
153+
case converge.FirstNotStarted:
154+
response.StartupConvergeNotStarted = true
155+
case converge.FirstStarted:
156+
if convergeTasks > 0 {
157+
response.StartupConvergeInProgress = convergeTasks
158+
} else {
159+
response.StartupConvergeDone = true
160+
}
161+
case converge.FirstDone:
162+
response.StartupConvergeDone = true
163+
if convergeTasks > 0 {
164+
response.ConvergeInProgress = convergeTasks
165+
} else {
166+
response.ConvergeWaitTask = true
167+
}
168+
}
169+
170+
return response
171+
}
172+
132173
// generateConvergeStatusLines creates status messages based on the current convergence state
133174
func generateConvergeStatusLines(phase converge.FirstConvergePhase, convergeTasks int) []string {
134175
statusLines := make([]string, 0)

0 commit comments

Comments
 (0)