@@ -95,34 +95,56 @@ func DescribeFailedDeployment(input WaitForDeploymentsAvailableInput, deployment
95
95
return b .String ()
96
96
}
97
97
98
- // WatchDeploymentLogsInput is the input for WatchDeploymentLogs.
99
- type WatchDeploymentLogsInput struct {
98
+ // WatchDeploymentLogsByLabelSelectorInput is the input for WatchDeploymentLogsByLabelSelector.
99
+ type WatchDeploymentLogsByLabelSelectorInput struct {
100
+ GetLister GetLister
101
+ ClientSet * kubernetes.Clientset
102
+ Labels map [string ]string
103
+ LogPath string
104
+ }
105
+
106
+ // WatchDeploymentLogsByLabelSelector streams logs for all containers for all pods belonging to a deployment on the basis of label. Each container's logs are streamed
107
+ // in a separate goroutine so they can all be streamed concurrently. This only causes a test failure if there are errors
108
+ // retrieving the deployment, its pods, or setting up a log file. If there is an error with the log streaming itself,
109
+ // that does not cause the test to fail.
110
+ func WatchDeploymentLogsByLabelSelector (ctx context.Context , input WatchDeploymentLogsByLabelSelectorInput ) {
111
+ Expect (ctx ).NotTo (BeNil (), "ctx is required for WatchDeploymentLogsByLabelSelector" )
112
+ Expect (input .ClientSet ).NotTo (BeNil (), "input.ClientSet is required for WatchDeploymentLogsByLabelSelector" )
113
+ Expect (input .Labels ).NotTo (BeNil (), "input.Selector is required for WatchDeploymentLogsByLabelSelector" )
114
+
115
+ deploymentList := & appsv1.DeploymentList {}
116
+ Eventually (func () error {
117
+ return input .GetLister .List (ctx , deploymentList , client .MatchingLabels (input .Labels ))
118
+ }, retryableOperationTimeout , retryableOperationInterval ).Should (Succeed (), "Failed to get deployment for labels" )
119
+
120
+ for _ , deployment := range deploymentList .Items {
121
+ watchPodLogs (ctx , watchPodLogsInput {
122
+ GetLister : input .GetLister ,
123
+ ClientSet : input .ClientSet ,
124
+ Namespace : deployment .Namespace ,
125
+ DeploymentName : deployment .Name ,
126
+ Labels : deployment .Spec .Selector .MatchLabels ,
127
+ LogPath : input .LogPath ,
128
+ })
129
+ }
130
+ }
131
+
132
+ // WatchDeploymentLogsByNameInput is the input for WatchDeploymentLogsByName.
133
+ type WatchDeploymentLogsByNameInput struct {
100
134
GetLister GetLister
101
135
ClientSet * kubernetes.Clientset
102
136
Deployment * appsv1.Deployment
103
137
LogPath string
104
138
}
105
139
106
- // logMetadata contains metadata about the logs.
107
- // The format is very similar to the one used by promtail.
108
- type logMetadata struct {
109
- Job string `json:"job"`
110
- Namespace string `json:"namespace"`
111
- App string `json:"app"`
112
- Pod string `json:"pod"`
113
- Container string `json:"container"`
114
- NodeName string `json:"node_name"`
115
- Stream string `json:"stream"`
116
- }
117
-
118
- // WatchDeploymentLogs streams logs for all containers for all pods belonging to a deployment. Each container's logs are streamed
140
+ // WatchDeploymentLogsByName streams logs for all containers for all pods belonging to a deployment. Each container's logs are streamed
119
141
// in a separate goroutine so they can all be streamed concurrently. This only causes a test failure if there are errors
120
142
// retrieving the deployment, its pods, or setting up a log file. If there is an error with the log streaming itself,
121
143
// that does not cause the test to fail.
122
- func WatchDeploymentLogs (ctx context.Context , input WatchDeploymentLogsInput ) {
123
- Expect (ctx ).NotTo (BeNil (), "ctx is required for WatchControllerLogs " )
124
- Expect (input .ClientSet ).NotTo (BeNil (), "input.ClientSet is required for WatchControllerLogs " )
125
- Expect (input .Deployment ).NotTo (BeNil (), "input.Deployment is required for WatchControllerLogs " )
144
+ func WatchDeploymentLogsByName (ctx context.Context , input WatchDeploymentLogsByNameInput ) {
145
+ Expect (ctx ).NotTo (BeNil (), "ctx is required for WatchDeploymentLogsByName " )
146
+ Expect (input .ClientSet ).NotTo (BeNil (), "input.ClientSet is required for WatchDeploymentLogsByName " )
147
+ Expect (input .Deployment ).NotTo (BeNil (), "input.Deployment is required for WatchDeploymentLogsByName " )
126
148
127
149
deployment := & appsv1.Deployment {}
128
150
key := client .ObjectKeyFromObject (input .Deployment )
@@ -131,23 +153,47 @@ func WatchDeploymentLogs(ctx context.Context, input WatchDeploymentLogsInput) {
131
153
}, retryableOperationTimeout , retryableOperationInterval ).Should (Succeed (), "Failed to get deployment %s" , klog .KObj (input .Deployment ))
132
154
133
155
selector , err := metav1 .LabelSelectorAsMap (deployment .Spec .Selector )
134
- Expect (err ).NotTo (HaveOccurred (), "Failed to Pods selector for deployment %s" , klog .KObj (input .Deployment ))
156
+ Expect (err ).NotTo (HaveOccurred (), "Failed to create Pods selector for deployment %s" , klog .KObj (input .Deployment ))
157
+ watchPodLogs (ctx , watchPodLogsInput {
158
+ GetLister : input .GetLister ,
159
+ ClientSet : input .ClientSet ,
160
+ Namespace : deployment .Namespace ,
161
+ DeploymentName : deployment .Name ,
162
+ Labels : selector ,
163
+ LogPath : input .LogPath ,
164
+ })
165
+ }
135
166
167
+ // watchPodLogsInput is the input for watchPodLogs.
168
+ type watchPodLogsInput struct {
169
+ GetLister GetLister
170
+ ClientSet * kubernetes.Clientset
171
+ Namespace string
172
+ DeploymentName string
173
+ Labels map [string ]string
174
+ LogPath string
175
+ }
176
+
177
+ // watchPodLogs streams logs for all containers for all pods belonging to a deployment with the given label. Each container's logs are streamed
178
+ // in a separate goroutine so they can all be streamed concurrently. This only causes a test failure if there are errors
179
+ // retrieving the deployment, its pods, or setting up a log file. If there is an error with the log streaming itself,
180
+ // that does not cause the test to fail.
181
+ func watchPodLogs (ctx context.Context , input watchPodLogsInput ) {
136
182
pods := & corev1.PodList {}
137
- Expect (input .GetLister .List (ctx , pods , client .InNamespace (input .Deployment . Namespace ), client .MatchingLabels (selector ))).To (Succeed (), "Failed to list Pods for deployment %s" , klog .KObj (input .Deployment ))
183
+ Expect (input .GetLister .List (ctx , pods , client .InNamespace (input .Namespace ), client .MatchingLabels (input . Labels ))).To (Succeed (), "Failed to list Pods for deployment %s" , klog .KRef (input .Namespace , input . DeploymentName ))
138
184
139
185
for _ , pod := range pods .Items {
140
- for _ , container := range deployment . Spec . Template .Spec .Containers {
141
- log .Logf ("Creating log watcher for controller %s, pod %s, container %s" , klog .KObj (input .Deployment ), pod .Name , container .Name )
186
+ for _ , container := range pod .Spec .Containers {
187
+ log .Logf ("Creating log watcher for controller %s, pod %s, container %s" , klog .KRef (input .Namespace , input . DeploymentName ), pod .Name , container .Name )
142
188
143
189
// Create log metadata file.
144
- logMetadataFile := filepath .Clean (path .Join (input .LogPath , input .Deployment . Name , pod .Name , container .Name + "-log-metadata.json" ))
190
+ logMetadataFile := filepath .Clean (path .Join (input .LogPath , input .DeploymentName , pod .Name , container .Name + "-log-metadata.json" ))
145
191
Expect (os .MkdirAll (filepath .Dir (logMetadataFile ), 0750 )).To (Succeed ())
146
192
147
193
metadata := logMetadata {
148
- Job : input .Deployment . Namespace + "/" + input .Deployment . Name ,
149
- Namespace : input .Deployment . Namespace ,
150
- App : input .Deployment . Name ,
194
+ Job : input .Namespace + "/" + input .DeploymentName ,
195
+ Namespace : input .Namespace ,
196
+ App : input .DeploymentName ,
151
197
Pod : pod .Name ,
152
198
Container : container .Name ,
153
199
NodeName : pod .Spec .NodeName ,
@@ -161,7 +207,7 @@ func WatchDeploymentLogs(ctx context.Context, input WatchDeploymentLogsInput) {
161
207
go func (pod corev1.Pod , container corev1.Container ) {
162
208
defer GinkgoRecover ()
163
209
164
- logFile := filepath .Clean (path .Join (input .LogPath , input .Deployment . Name , pod .Name , container .Name + ".log" ))
210
+ logFile := filepath .Clean (path .Join (input .LogPath , input .DeploymentName , pod .Name , container .Name + ".log" ))
165
211
Expect (os .MkdirAll (filepath .Dir (logFile ), 0750 )).To (Succeed ())
166
212
167
213
f , err := os .OpenFile (logFile , os .O_APPEND | os .O_CREATE | os .O_WRONLY , 0600 )
@@ -173,7 +219,7 @@ func WatchDeploymentLogs(ctx context.Context, input WatchDeploymentLogsInput) {
173
219
Follow : true ,
174
220
}
175
221
176
- podLogs , err := input .ClientSet .CoreV1 ().Pods (input .Deployment . Namespace ).GetLogs (pod .Name , opts ).Stream (ctx )
222
+ podLogs , err := input .ClientSet .CoreV1 ().Pods (input .Namespace ).GetLogs (pod .Name , opts ).Stream (ctx )
177
223
if err != nil {
178
224
// Failing to stream logs should not cause the test to fail
179
225
log .Logf ("Error starting logs stream for pod %s, container %s: %v" , klog .KRef (pod .Namespace , pod .Name ), container .Name , err )
@@ -193,6 +239,19 @@ func WatchDeploymentLogs(ctx context.Context, input WatchDeploymentLogsInput) {
193
239
}
194
240
}
195
241
242
+ // logMetadata contains metadata about the logs.
243
+ // The format is very similar to the one used by promtail.
244
+ type logMetadata struct {
245
+ Job string `json:"job"`
246
+ Namespace string `json:"namespace"`
247
+ App string `json:"app"`
248
+ Pod string `json:"pod"`
249
+ Container string `json:"container"`
250
+ NodeName string `json:"node_name"`
251
+ Stream string `json:"stream"`
252
+ Labels map [string ]string `json:"labels,omitempty"`
253
+ }
254
+
196
255
type WatchPodMetricsInput struct {
197
256
GetLister GetLister
198
257
ClientSet * kubernetes.Clientset
@@ -215,7 +274,7 @@ func WatchPodMetrics(ctx context.Context, input WatchPodMetricsInput) {
215
274
}, retryableOperationTimeout , retryableOperationInterval ).Should (Succeed (), "Failed to get deployment %s" , klog .KObj (input .Deployment ))
216
275
217
276
selector , err := metav1 .LabelSelectorAsMap (deployment .Spec .Selector )
218
- Expect (err ).NotTo (HaveOccurred (), "Failed to Pods selector for deployment %s" , klog .KObj (input .Deployment ))
277
+ Expect (err ).NotTo (HaveOccurred (), "Failed to create Pods selector for deployment %s" , klog .KObj (input .Deployment ))
219
278
220
279
pods := & corev1.PodList {}
221
280
Eventually (func () error {
0 commit comments