func()

in internal/deployers/eksapi/logs.go [152:219]


func (m *logManager) gatherLogsUsingNodeDiagnostic(k8sClient *k8sClient, opts *deployerOptions, phase deployerPhase) error {
	klog.Info("gathering logs using NodeDiagnostic...")
	nodes, err := k8sClient.clientset.CoreV1().Nodes().List(context.TODO(), v1.ListOptions{})
	if err != nil {
		return err
	}
	if len(nodes.Items) == 0 {
		klog.Warning("no nodes to gather logs from!")
		return nil
	}
	instanceIds, err := getNodeInstanceIDs(nodes.Items)
	if err != nil {
		return err
	}
	var errs []error
	var nodeDiagnostics []unstructured.Unstructured
	for _, instanceId := range instanceIds {
		presignedPut, err := m.clients.S3Presign().PresignPutObject(context.TODO(), &s3.PutObjectInput{
			Bucket: aws.String(opts.LogBucket),
			Key:    aws.String(fmt.Sprintf("node-logs/%s/%s/%s.tar.gz", m.resourceID, phase, instanceId)),
		})
		if err != nil {
			errs = append(errs, fmt.Errorf("failed to create presigned PUT for %s: %v", instanceId, err))
			continue
		}
		nodeDiagnostic := unstructured.Unstructured{
			Object: map[string]interface{}{
				"apiVersion": "eks.amazonaws.com/v1alpha1",
				"kind":       "NodeDiagnostic",
				"metadata": v1.ObjectMeta{
					Name: instanceId,
				},
				"spec": map[string]interface{}{
					"logCapture": map[string]interface{}{
						"destination": presignedPut.URL,
					},
				},
			},
		}
		if err := k8sClient.client.Create(context.TODO(), &nodeDiagnostic); err != nil {
			errs = append(errs, err)
		} else {
			nodeDiagnostics = append(nodeDiagnostics, nodeDiagnostic)
		}
	}
	outcomes, err := m.waitForNodeDiagnostics(k8sClient, nodeDiagnostics)
	if err != nil {
		errs = append(errs, fmt.Errorf("failed to wait for node diagnostics: %v", err))
	}
	for instanceId, reasons := range outcomes {
		for _, reason := range reasons {
			// consider SuccessWithErrors a success, this isn't high stakes
			if !slices.Contains([]string{"Success", "SuccessWithErrors"}, reason) {
				errs = append(errs, fmt.Errorf("node diagnostic outcome reason for %s: %s", instanceId, reason))
			}
		}
	}
	for _, nodeDiagnostic := range nodeDiagnostics {
		if err := k8sClient.client.Delete(context.TODO(), &nodeDiagnostic); err != nil {
			errs = append(errs, err)
		}
	}
	if len(errs) > 0 {
		return errors.Join(errs...)
	}
	klog.Infof("gathered logs from nodes: %v", instanceIds)
	return nil
}