func()

in internal/deployers/eksapi/logs.go [68:148]


func (m *logManager) gatherLogsUsingScript(k8sClient *k8sClient, opts *deployerOptions, phase deployerPhase) error {
	klog.Info("gathering logs using script...")
	nodes, err := k8sClient.clientset.CoreV1().Nodes().List(context.TODO(), v1.ListOptions{})
	if err != nil {
		return err
	}
	var instanceIds []string
	if len(nodes.Items) > 0 {
		instanceIds, err = getNodeInstanceIDs(nodes.Items)
		if err != nil {
			return err
		}
	} else {
		klog.Warning("no nodes found in cluster!")
		// if we're using unmanaged nodes, we can track down the instances in the ASG even if they didn't join the cluster
		if opts.UnmanagedNodes {
			klog.Info("fetching instances from unmanaged nodegroup...")
			out, err := m.clients.ASG().DescribeAutoScalingGroups(context.TODO(), &autoscaling.DescribeAutoScalingGroupsInput{
				AutoScalingGroupNames: []string{m.resourceID},
			})
			if err != nil {
				klog.Warningf("failed to describe unmanaged nodegroup ASG: %v", err)
				return nil
			}
			if len(out.AutoScalingGroups) != 1 {
				klog.Warningf("autoscaling group not found: %s", m.resourceID)
			} else {
				for _, asg := range out.AutoScalingGroups {
					for _, instance := range asg.Instances {
						instanceIds = append(instanceIds, aws.ToString(instance.InstanceId))
					}
				}
			}
		}
	}
	if len(instanceIds) == 0 {
		klog.Warning("no nodes to gather logs from!")
		return nil
	}
	doc, err := m.clients.SSM().CreateDocument(context.TODO(), &ssm.CreateDocumentInput{
		Content:        aws.String(logCollectorScriptSsmDocumentContent),
		Name:           aws.String(fmt.Sprintf("%s-log-collector", m.resourceID)),
		DocumentType:   ssmtypes.DocumentTypeCommand,
		DocumentFormat: ssmtypes.DocumentFormatJson,
	})
	if err != nil {
		return err
	}
	defer func() {
		m.clients.SSM().DeleteDocument(context.TODO(), &ssm.DeleteDocumentInput{
			Name: doc.DocumentDescription.Name,
		})
	}()
	command, err := m.clients.SSM().SendCommand(context.TODO(), &ssm.SendCommandInput{
		DocumentName: doc.DocumentDescription.Name,
		InstanceIds:  instanceIds,
		Parameters: map[string][]string{
			"s3Destination": {fmt.Sprintf("s3://%s/node-logs/%s/%s/", opts.LogBucket, m.resourceID, phase)},
		},
	})
	if err != nil {
		return err
	}
	var errs []error
	for _, instanceId := range instanceIds {
		out, err := ssm.NewCommandExecutedWaiter(m.clients.SSM()).WaitForOutput(context.TODO(), &ssm.GetCommandInvocationInput{
			CommandId:  command.Command.CommandId,
			InstanceId: aws.String(instanceId),
		}, logCollectorSsmDocumentTimeout)
		if err != nil {
			errs = append(errs, err)
		} else {
			klog.Infof("log collection command for %s: %s", instanceId, out.Status)
		}
	}
	if len(errs) > 0 {
		return errors.Join(errs...)
	}
	klog.Infof("gathered logs from nodes: %v", instanceIds)
	return nil
}