-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2125490
commit 031e9d0
Showing
7 changed files
with
188 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package eksapi | ||
|
||
import ( | ||
"context" | ||
_ "embed" | ||
"errors" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/ssm" | ||
ssmtypes "github.com/aws/aws-sdk-go-v2/service/ssm/types" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/klog/v2" | ||
) | ||
|
||
type logManager struct { | ||
clients *awsClients | ||
resourceID string | ||
} | ||
|
||
type deployerPhase string | ||
|
||
const ( | ||
deployerPhaseUp = "up" | ||
deployerPhaseDown = "down" | ||
) | ||
|
||
func NewLogManager(clients *awsClients, resourceID string) *logManager { | ||
return &logManager{ | ||
clients: clients, | ||
resourceID: resourceID, | ||
} | ||
} | ||
|
||
func (m *logManager) gatherLogsFromNodes(k8sClient *kubernetes.Clientset, opts *deployerOptions, phase deployerPhase) error { | ||
if opts.LogBucket == "" { | ||
klog.Info("--log-bucket is empty, no logs will be gathered!") | ||
return nil | ||
} | ||
switch opts.UserDataFormat { | ||
case "bootstrap.sh", "nodeadm", "": // if no --user-data-format was passed, we must be using managed nodes, which default to AL-based AMIs | ||
return m.gatherLogsUsingScript(k8sClient, opts, phase) | ||
default: | ||
klog.Warningf("unable to gather logs for userDataFormat: %s\n", opts.UserDataFormat) | ||
return nil | ||
} | ||
} | ||
|
||
const logCollectorScriptSsmDocumentContent = `{ | ||
"schemaVersion": "2.2", | ||
"description": "Collect logs from an Amazon Linux EKS node", | ||
"parameters": { | ||
"s3Destination": { | ||
"type": "String" | ||
} | ||
}, | ||
"mainSteps": [ | ||
{ | ||
"action": "aws:runShellScript", | ||
"name": "collectAndUploadLogs", | ||
"precondition": { | ||
"StringEquals": ["platformType", "Linux"] | ||
}, | ||
"inputs": { | ||
"runCommand": [ | ||
"bash /etc/eks/log-collector-script/eks-log-collector.sh >/dev/null 2>&1", | ||
"aws s3 cp /var/log/eks_i* {{s3Destination}}" | ||
] | ||
} | ||
} | ||
] | ||
} | ||
` | ||
|
||
const logCollectorSsmDocumentTimeout = 5 * time.Minute | ||
|
||
func (m *logManager) gatherLogsUsingScript(k8sClient *kubernetes.Clientset, opts *deployerOptions, phase deployerPhase) error { | ||
nodes, err := k8sClient.CoreV1().Nodes().List(context.TODO(), v1.ListOptions{}) | ||
if err != nil { | ||
return err | ||
} | ||
instanceIds, err := getNodeInstanceIDs(nodes.Items) | ||
if err != nil { | ||
return err | ||
} | ||
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": []string{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) | ||
} | ||
} | ||
return errors.Join(errs...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters