Skip to content

Commit ca0f798

Browse files
authored
Add workload id to pod label search (#464)
1 parent e1731ff commit ca0f798

File tree

2 files changed

+21
-40
lines changed

2 files changed

+21
-40
lines changed

pkg/operator/endpoints/logs.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ func ReadLogs(w http.ResponseWriter, r *http.Request) {
4646
resourceType := getOptionalQParam("resourceType", r)
4747

4848
podLabels := map[string]string{
49-
"appName": appName,
50-
"userFacing": "true",
49+
"appName": appName,
50+
"userFacing": "true",
51+
"workloadType": resource.APIType.String(),
5152
}
5253

5354
if workloadID != "" {

pkg/operator/workloads/logs.go

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ import (
2525
"github.com/gorilla/websocket"
2626

2727
awslib "github.com/cortexlabs/cortex/pkg/lib/aws"
28-
"github.com/cortexlabs/cortex/pkg/lib/k8s"
2928
"github.com/cortexlabs/cortex/pkg/lib/sets/strset"
3029
s "github.com/cortexlabs/cortex/pkg/lib/strings"
3130
"github.com/cortexlabs/cortex/pkg/operator/api/context"
31+
"github.com/cortexlabs/cortex/pkg/operator/api/resource"
3232
"github.com/cortexlabs/cortex/pkg/operator/config"
3333
)
3434

@@ -38,7 +38,7 @@ const (
3838
socketMaxMessageSize = 8192
3939

4040
maxLogLinesPerRequest = 500
41-
pollPeriod = 250 * time.Millisecond
41+
pollPeriod = 250 // milliseconds
4242
)
4343

4444
func ReadLogs(appName string, podLabels map[string]string, socket *websocket.Conn) {
@@ -69,7 +69,6 @@ func StreamFromCloudWatch(podCheckCancel chan struct{}, appName string, podLabel
6969

7070
lastTimestamp := int64(0)
7171
previousEvents := strset.New()
72-
wrotePending := false
7372

7473
var currentContextID string
7574
var prefix string
@@ -91,45 +90,30 @@ func StreamFromCloudWatch(podCheckCancel chan struct{}, appName string, podLabel
9190

9291
if ctx.ID != currentContextID {
9392
if len(currentContextID) != 0 {
94-
if apiName, ok := podLabels["apiName"]; ok {
93+
if podLabels["workloadType"] == resource.APIType.String() {
94+
apiName := podLabels["apiName"]
9595
if _, ok := ctx.APIs[apiName]; !ok {
9696
writeString(socket, "\napi "+apiName+" was not found in latest deployment")
9797
closeSocket(socket)
9898
continue
9999
}
100-
podLabels["workloadID"] = ctx.APIs[apiName].WorkloadID
101100
writeString(socket, "\na new deployment was detected, streaming logs from the latest deployment")
102101
} else {
103-
writeString(socket, "\nnew deployment detected, shutting down log stream") // unexpected for now, should only occur when logging non api resources
102+
writeString(socket, "\nlogging non-api workloads is not supported") // unexpected
104103
closeSocket(socket)
105104
continue
106105
}
106+
} else {
107+
lastTimestamp = ctx.CreatedEpoch * 1000
107108
}
108109

109-
currentContextID = ctx.ID
110-
111-
pods, err := config.Kubernetes.ListPodsByLabels(podLabels)
112-
if err != nil {
113-
writeString(socket, err.Error())
114-
closeSocket(socket)
115-
continue
110+
if podLabels["workloadType"] == resource.APIType.String() {
111+
podLabels["workloadID"] = ctx.APIs[podLabels["apiName"]].WorkloadID
116112
}
117113

118-
allPodsPending := true
119-
for _, pod := range pods {
120-
if k8s.GetPodStatus(&pod) != k8s.PodStatusPending {
121-
allPodsPending = false
122-
break
123-
}
124-
}
125-
126-
if allPodsPending {
127-
writeString(socket, "\npending...")
128-
wrotePending = true
129-
} else {
130-
wrotePending = false
131-
}
114+
currentContextID = ctx.ID
132115

116+
writeString(socket, "\nretrieving logs...")
133117
prefix = ""
134118
}
135119

@@ -147,21 +131,18 @@ func StreamFromCloudWatch(podCheckCancel chan struct{}, appName string, podLabel
147131
continue
148132
}
149133

134+
endTime := time.Now().Unix() * 1000
135+
startTime := lastTimestamp - pollPeriod
150136
logEventsOutput, err := config.AWS.CloudWatchLogsClient.FilterLogEvents(&cloudwatchlogs.FilterLogEventsInput{
151137
LogGroupName: aws.String(config.Cortex.LogGroup),
152138
LogStreamNamePrefix: aws.String(prefix),
153-
StartTime: aws.Int64(lastTimestamp),
154-
EndTime: aws.Int64(time.Now().Unix() * 1000), // requires milliseconds
139+
StartTime: aws.Int64(startTime),
140+
EndTime: aws.Int64(endTime), // requires milliseconds
155141
Limit: aws.Int64(int64(maxLogLinesPerRequest)),
156142
})
157143

158144
if err != nil {
159-
if awslib.CheckErrCode(err, "ResourceNotFoundException") {
160-
if !wrotePending {
161-
writeString(socket, "pending...")
162-
wrotePending = true
163-
}
164-
} else {
145+
if !awslib.CheckErrCode(err, "ResourceNotFoundException") {
165146
writeString(socket, "error encountered while fetching logs from cloudwatch: "+err.Error())
166147
closeSocket(socket)
167148
continue
@@ -175,7 +156,6 @@ func StreamFromCloudWatch(podCheckCancel chan struct{}, appName string, podLabel
175156

176157
if !previousEvents.Has(*logEvent.EventId) {
177158
socket.WriteMessage(websocket.TextMessage, []byte(log.Log))
178-
179159
if *logEvent.Timestamp > lastTimestamp {
180160
lastTimestamp = *logEvent.Timestamp
181161
}
@@ -185,10 +165,11 @@ func StreamFromCloudWatch(podCheckCancel chan struct{}, appName string, podLabel
185165

186166
if len(logEventsOutput.Events) == maxLogLinesPerRequest {
187167
socket.WriteMessage(websocket.TextMessage, []byte("---- Showing at most "+s.Int(maxLogLinesPerRequest)+" lines. Visit AWS cloudwatch logs console and search for \""+prefix+"\" in log group \""+config.Cortex.LogGroup+"\" for complete logs ----"))
168+
lastTimestamp = endTime
188169
}
189170

190171
previousEvents = newEvents
191-
timer.Reset(pollPeriod)
172+
timer.Reset(pollPeriod * time.Millisecond)
192173
}
193174
}
194175
}
@@ -214,7 +195,6 @@ func getPrefix(searchLabels map[string]string) (string, error) {
214195
}
215196

216197
func writeString(socket *websocket.Conn, message string) {
217-
socket.SetWriteDeadline(time.Now().Add(socketWriteDeadlineWait))
218198
socket.WriteMessage(websocket.TextMessage, []byte(message))
219199
}
220200

0 commit comments

Comments
 (0)