@@ -26,13 +26,13 @@ import (
26
26
"os"
27
27
"path"
28
28
"path/filepath"
29
+ "strings"
29
30
"time"
30
31
31
32
. "github.com/onsi/ginkgo/v2"
32
33
. "github.com/onsi/gomega"
33
34
corev1 "k8s.io/api/core/v1"
34
35
"k8s.io/apimachinery/pkg/types"
35
- "k8s.io/apimachinery/pkg/util/sets"
36
36
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
37
37
expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
38
38
"sigs.k8s.io/cluster-api/test/framework"
@@ -57,6 +57,9 @@ func SetupSpecNamespace(ctx context.Context, specName string, e2eCtx *E2EContext
57
57
return namespace
58
58
}
59
59
60
+ // DumpSpecResourcesAndCleanup dumps all the resources in the spec namespace.
61
+ // This includes OpenStack resources and all the CAPI/CAPO resources in Kubernetes.
62
+ // It also then cleanups the cluster object and the spec namespace itself.
60
63
func DumpSpecResourcesAndCleanup (ctx context.Context , specName string , namespace * corev1.Namespace , e2eCtx * E2EContext ) {
61
64
Logf ("Running DumpSpecResourcesAndCleanup for namespace %q" , namespace .Name )
62
65
// Dump all Cluster API related resources to artifacts before deleting them.
@@ -65,9 +68,6 @@ func DumpSpecResourcesAndCleanup(ctx context.Context, specName string, namespace
65
68
dumpAllResources := func (directory ... string ) {
66
69
dumpSpecResources (ctx , e2eCtx , namespace , directory ... )
67
70
dumpOpenStack (ctx , e2eCtx , e2eCtx .Environment .BootstrapClusterProxy .GetName (), directory ... )
68
-
69
- Logf ("Dumping all OpenStack server instances in the %q namespace" , namespace .Name )
70
- dumpMachines (ctx , e2eCtx , namespace , directory ... )
71
71
}
72
72
73
73
dumpAllResources ()
@@ -100,40 +100,8 @@ func DumpSpecResourcesAndCleanup(ctx context.Context, specName string, namespace
100
100
delete (e2eCtx .Environment .Namespaces , namespace )
101
101
}
102
102
103
- func dumpMachines (ctx context.Context , e2eCtx * E2EContext , namespace * corev1.Namespace , directory ... string ) {
104
- cluster , err := ClusterForSpec (ctx , e2eCtx , namespace )
105
- if err != nil {
106
- _ , _ = fmt .Fprintf (GinkgoWriter , "cannot dump machines, couldn't get cluster in namespace %s: %v\n " , namespace .Name , err )
107
- return
108
- }
109
- if cluster .Status .Bastion == nil || cluster .Status .Bastion .FloatingIP == "" {
110
- _ , _ = fmt .Fprintln (GinkgoWriter , "cannot dump machines, cluster doesn't has a bastion host (yet) with a floating ip" )
111
- return
112
- }
113
- machines , err := machinesForSpec (ctx , e2eCtx .Environment .BootstrapClusterProxy , namespace )
114
- if err != nil {
115
- _ , _ = fmt .Fprintf (GinkgoWriter , "cannot dump machines, could not get machines: %v\n " , err )
116
- return
117
- }
118
-
119
- machineNames := sets .New [string ]()
120
- for _ , machine := range machines .Items {
121
- machineNames .Insert (machine .Name )
122
- }
123
- srvs , err := GetOpenStackServers (e2eCtx , cluster , machineNames )
124
- if err != nil {
125
- _ , _ = fmt .Fprintf (GinkgoWriter , "cannot dump machines, could not get servers from OpenStack: %v\n " , err )
126
- return
127
- }
128
- for _ , m := range machines .Items {
129
- srv , ok := srvs [m .Name ]
130
- if ! ok {
131
- continue
132
- }
133
- dumpMachine (ctx , e2eCtx , m , srv , cluster .Status .Bastion .FloatingIP , directory ... )
134
- }
135
- }
136
-
103
+ // ClusterForSpec returns the OpenStackCluster in the given namespace.
104
+ // It is considered an error if more than 1 OpenStackCluster is found.
137
105
func ClusterForSpec (ctx context.Context , e2eCtx * E2EContext , namespace * corev1.Namespace ) (* infrav1.OpenStackCluster , error ) {
138
106
lister := e2eCtx .Environment .BootstrapClusterProxy .GetClient ()
139
107
list := new (infrav1.OpenStackClusterList )
@@ -146,88 +114,7 @@ func ClusterForSpec(ctx context.Context, e2eCtx *E2EContext, namespace *corev1.N
146
114
return & list .Items [0 ], nil
147
115
}
148
116
149
- func machinesForSpec (ctx context.Context , clusterProxy framework.ClusterProxy , namespace * corev1.Namespace ) (* infrav1.OpenStackMachineList , error ) {
150
- list := new (infrav1.OpenStackMachineList )
151
- if err := clusterProxy .GetClient ().List (ctx , list , client .InNamespace (namespace .GetName ())); err != nil {
152
- return nil , fmt .Errorf ("error listing machines: %v" , err )
153
- }
154
- return list , nil
155
- }
156
-
157
- func dumpMachine (ctx context.Context , e2eCtx * E2EContext , machine infrav1.OpenStackMachine , srv ServerExtWithIP , bastionIP string , directory ... string ) {
158
- paths := append ([]string {e2eCtx .Settings .ArtifactFolder , "clusters" , e2eCtx .Environment .BootstrapClusterProxy .GetName ()}, directory ... )
159
- logPath := filepath .Join (paths ... )
160
- machineLogBase := path .Join (logPath , "instances" , machine .Namespace , machine .Name )
161
- metaLog := path .Join (machineLogBase , "instance.log" )
162
-
163
- if err := os .MkdirAll (filepath .Dir (metaLog ), 0o750 ); err != nil {
164
- _ , _ = fmt .Fprintf (GinkgoWriter , "couldn't create directory %q for file: %s\n " , metaLog , err )
165
- }
166
-
167
- f , err := os .OpenFile (metaLog , os .O_APPEND | os .O_CREATE | os .O_WRONLY , 0o644 )
168
- if err != nil {
169
- _ , _ = fmt .Fprintf (GinkgoWriter , "couldn't open file %q: %s\n " , metaLog , err )
170
- return
171
- }
172
- defer f .Close ()
173
-
174
- serverJSON , err := json .MarshalIndent (srv , "" , " " )
175
- if err != nil {
176
- _ , _ = fmt .Fprintf (GinkgoWriter , "error marshalling server %v: %s" , srv , err )
177
- }
178
- if err := os .WriteFile (path .Join (machineLogBase , "server.txt" ), serverJSON , 0o600 ); err != nil {
179
- _ , _ = fmt .Fprintf (GinkgoWriter , "error writing server JSON %s: %s" , serverJSON , err )
180
- }
181
-
182
- srvUser := e2eCtx .E2EConfig .GetVariable (SSHUserMachine )
183
-
184
- _ , _ = fmt .Fprintf (f , "instance found: %q\n " , srv .ID )
185
- executeCommands (
186
- ctx ,
187
- e2eCtx .Settings .ArtifactFolder ,
188
- e2eCtx .Settings .Debug ,
189
- filepath .Dir (f .Name ()),
190
- srv .ip ,
191
- bastionIP ,
192
- srvUser ,
193
- []command {
194
- // don't do this for now, it just takes to long
195
- // {
196
- // title: "systemd",
197
- // cmd: "journalctl --no-pager --output=short-precise | grep -v 'audit:\\|audit\\['",
198
- // },
199
- {
200
- title : "kern" ,
201
- cmd : "journalctl --no-pager --output=short-precise -k" ,
202
- },
203
- {
204
- title : "containerd-info" ,
205
- cmd : "crictl --runtime-endpoint unix:///run/containerd/containerd.sock info" ,
206
- },
207
- {
208
- title : "containerd-containers" ,
209
- cmd : "crictl --runtime-endpoint unix:///run/containerd/containerd.sock ps" ,
210
- },
211
- {
212
- title : "containerd-pods" ,
213
- cmd : "crictl --runtime-endpoint unix:///run/containerd/containerd.sock pods" ,
214
- },
215
- {
216
- title : "cloud-final" ,
217
- cmd : "journalctl --no-pager -u cloud-final" ,
218
- },
219
- {
220
- title : "kubelet" ,
221
- cmd : "journalctl --no-pager -u kubelet.service" ,
222
- },
223
- {
224
- title : "containerd" ,
225
- cmd : "journalctl --no-pager -u containerd.service" ,
226
- },
227
- },
228
- )
229
- }
230
-
117
+ // dumpSpecResources dumps all CAPI/CAPO resources to yaml.
231
118
func dumpSpecResources (ctx context.Context , e2eCtx * E2EContext , namespace * corev1.Namespace , directory ... string ) {
232
119
paths := append ([]string {e2eCtx .Settings .ArtifactFolder , "clusters" , e2eCtx .Environment .BootstrapClusterProxy .GetName (), "resources" }, directory ... )
233
120
framework .DumpAllResources (ctx , framework.DumpAllResourcesInput {
@@ -287,26 +174,23 @@ func getOpenStackClusterFromMachine(ctx context.Context, client client.Client, m
287
174
return openStackCluster , err
288
175
}
289
176
177
+ // getIDFromProviderID returns the server ID part of a provider ID string.
178
+ func getIDFromProviderID (providerID string ) string {
179
+ return strings .TrimPrefix (providerID , "openstack:///" )
180
+ }
181
+
290
182
type OpenStackLogCollector struct {
291
- E2EContext E2EContext
183
+ E2EContext * E2EContext
292
184
}
293
185
294
186
// CollectMachineLog gets logs for the OpenStack resources related to the given machine.
295
187
func (o OpenStackLogCollector ) CollectMachineLog (ctx context.Context , managementClusterClient client.Client , m * clusterv1.Machine , outputPath string ) error {
296
- machineLogBase := path .Join (outputPath , "instances" , m .Namespace , m .Name )
297
- metaLog := path .Join (machineLogBase , "instance.log" )
188
+ Logf ("Collecting logs for machine %q and storing them in %q" , m .ObjectMeta .Name , outputPath )
298
189
299
- if err := os .MkdirAll (filepath . Dir ( metaLog ) , 0o750 ); err != nil {
300
- _ , _ = fmt .Fprintf ( GinkgoWriter , "couldn't create directory %q for file : %s\n " , metaLog , err )
190
+ if err := os .MkdirAll (outputPath , 0o750 ); err != nil {
191
+ return fmt .Errorf ( "couldn't create directory %q for logs : %s" , outputPath , err )
301
192
}
302
193
303
- f , err := os .OpenFile (metaLog , os .O_APPEND | os .O_CREATE | os .O_WRONLY , 0o644 )
304
- if err != nil {
305
- _ , _ = fmt .Fprintf (GinkgoWriter , "couldn't open file %q: %s\n " , metaLog , err )
306
- return nil
307
- }
308
- defer f .Close ()
309
-
310
194
openStackCluster , err := getOpenStackClusterFromMachine (ctx , managementClusterClient , m )
311
195
if err != nil {
312
196
return fmt .Errorf ("error getting OpenStackCluster for Machine: %s" , err )
@@ -317,30 +201,25 @@ func (o OpenStackLogCollector) CollectMachineLog(ctx context.Context, management
317
201
}
318
202
ip := m .Status .Addresses [0 ].Address
319
203
320
- srvs , err := GetOpenStackServers ( & o .E2EContext , openStackCluster , sets . New ( m .Spec .InfrastructureRef . Name ) )
204
+ srv , err := GetOpenStackServerWithIP ( o .E2EContext , getIDFromProviderID ( * m .Spec .ProviderID ), openStackCluster )
321
205
if err != nil {
322
- return fmt .Errorf ("cannot dump machines, could not get servers from OpenStack: %v" , err )
323
- }
324
- if len (srvs ) != 1 {
325
- return fmt .Errorf ("expected exactly 1 server but got %d" , len (srvs ))
206
+ return fmt .Errorf ("error getting OpenStack server: %w" , err )
326
207
}
327
- srv := srvs [m .Spec .InfrastructureRef .Name ]
328
208
329
209
serverJSON , err := json .MarshalIndent (srv , "" , " " )
330
210
if err != nil {
331
211
return fmt .Errorf ("error marshalling server %v: %s" , srv , err )
332
212
}
333
- if err := os .WriteFile (path .Join (machineLogBase , "server.txt" ), serverJSON , 0o600 ); err != nil {
213
+ if err := os .WriteFile (path .Join (outputPath , "server.txt" ), serverJSON , 0o600 ); err != nil {
334
214
return fmt .Errorf ("error writing server JSON %s: %s" , serverJSON , err )
335
215
}
336
- _ , _ = fmt .Fprintf (f , "instance found: %q\n " , srv .ID )
337
216
338
217
srvUser := o .E2EContext .E2EConfig .GetVariable (SSHUserMachine )
339
218
executeCommands (
340
219
ctx ,
341
220
o .E2EContext .Settings .ArtifactFolder ,
342
221
o .E2EContext .Settings .Debug ,
343
- filepath . Dir ( f . Name ()) ,
222
+ outputPath ,
344
223
ip ,
345
224
openStackCluster .Status .Bastion .FloatingIP ,
346
225
srvUser ,
0 commit comments