Documentation
¶
Overview ¶
Package diagnose explains why a deployment failed, or why a change set failed to be created, as upstream's api/diagnosing does, and describes change sets, as its api/change-sets does (EG-2, G.9).
A Diagnoser finds the problem from a deployment's resource errors (Diagnoser.FromErrors), from a change set (Diagnoser.ChangeSet), or from a stack's current state (Diagnoser.FromFresh). A failed change set is diagnosed in upstream's order: one that failed for having no changes is no problem; early validation reads the failed events with DescribeEvents; a nested change set's failure is found among the parent's changes and diagnosed in turn; a failed automatic import has its resources read from the reason; otherwise DescribeEvents, then the change set's failed hooks, then the reason itself. Each problem is traced to the construct that defines it by a SourceTracer, such as NewStackTracer.
As upstream, diagnosing never fails: an error while diagnosing gives a Diagnosis of kind ErrorDiagnosing, which keeps the error, so that Diagnosis.Err wraps it and a cancelled context still satisfies errors.Is (G.9-D1). Diagnosis.Err gives the error upstream's throwOnError throws, a cdkerr.Error of Kind cdkerr.KindDeployment whose message is the diagnosis formatted by Format, byte for byte upstream's, and which carries the diagnosis itself as cdkerr.Error.Diagnosis, where upstream's carries only the text (G.9-D2).
A Describer waits for a change set to settle and describes it, with IncludePropertyValues and every page of its changes; when CloudFormation says it left data out of that description, or the description would hide a difference between the templates the change set goes from and to, which the describer requires, it describes the change set again without property values and puts back what was dropped. The describer and the diagnoser use each other, which is why they share this package: the describer asks the diagnoser whether a change set can be executed, and the diagnoser describes nested change sets, without property values, since it needs only which resources change.
Given exploration access, Options.Exploration, a diagnoser looks into each failed resource, as upstream's resource investigations do: why a failed ECS service's newest stopped task stopped, and its containers' recent logs, and a failed custom resource's Lambda function logs. What they find is added to the problem's context, after its hooks' and before its warnings; a failed investigation is a debug message, and finds nothing. The investigations depart from upstream's where upstream's can report the wrong thing or miss it:
- an ECS service's stopped tasks are only its own, those in the group "service:<name>", newest first and at most 3, where upstream takes the first 3 listed, which its fallback to the cluster-wide listing can fill with other services' tasks;
- an ECS service's container logs are read from 30 minutes before the failure to 2 minutes after it, where upstream reads them from 30 minutes ago to now;
- logs are read up to 10 pages, where upstream reads one, so that an empty first page doesn't hide the logs and the last 50 lines kept are the latest read.
Past 10 pages, the lines kept are the last of those read, not the latest logged. A page that fails after events were read is a debug message, and the lines kept are those of the pages before it. A custom resource's function logs are read, as upstream's are, from 2 minutes before the failure to 15 minutes after it, or from 30 minutes ago to now when the failure has no time. So a handler that never responds, which CloudFormation fails up to an hour after it ran, has no logs found, and, with rollback, the lines of CloudFormation's Delete call to the same function follow those of the failure.
A deployment's failure is also looked into in CloudTrail, as upstream's CloudTrail investigation does: one sweep of LookupEvents, from 5 minutes before the first failure to 2 minutes after the last, of at most 10 pages, for errored calls, other than successful dry runs, whose caller is one of the stacks' roles or functions. A call is attributed to the resource whose name or ARN segment matches its caller best, and is added to that resource's problem, or else, labelled with the resource, to the first problem, with notes about the sweep: that it was cut short, that more errors were found than shown, or, within 15 minutes of the failure and until CloudTrail has delivered an event from after the sweep, that more may yet arrive. A denied lookup is a warning naming the permission it needs; any other failure is a debug message, and finds nothing. The stacks' roles and functions are those on every page of their resources, from ListStackResources, and those the deployment's events named, ErrorsOptions.Principals, as upstream's are. A failure partway through a stack's resources is a debug message, and the roles and functions of the pages read are kept, besides the events', where upstream keeps only the events', so that a role on a page read before the failure can go unmatched.
The diagnose action and its messages are package toolkit's.
Upstream's defects fixed (G.9-D4):
- Diagnoser.FromFresh reads every page of the stack's change sets, and passes over those that failed for having no changes, where upstream reads one page and takes the first failed change set, so that a change set with no changes hides one that failed;
- the change set's hook results are read page by page, where upstream reads one page;
- tracing a nested stack's own problem leaves its path whole, where upstream's tracer shortens the list of nested stacks it is given, which the problem shares, so that the problem loses its innermost nested stack;
- Format sorts a copy of the problems, so that a cdkerr.Diagnosis keeps them oldest first, where upstream sorts them in place;
- the location of a stack's own problem without a construct path is the top-level stack and its nested stacks joined by "/", where upstream writes the nested stacks joined by commas after a "/", and a top-level stack as its ID followed by "/".
Problems are sorted by location in an approximation of the order of JavaScript's localeCompare, which compares letters case-insensitively, then puts lower case first, with punctuation before digits before letters, where a byte comparison would put upper case first (G.9-D5).
Change sets are polled every 5 seconds, and a wait has no timeout but the context's, as upstream's has none at all: upstream's ChangeSetTimeout can't happen there, and isn't ported (G.9-D3).
The CloudFormation client is an interface of the operations used, which *cloudformation.Client satisfies, so tests run real SDK clients against internal/awsfake.
Index ¶
- func Format(d *cdkerr.Diagnosis) string
- func HasNoChanges(cs ChangeSet) bool
- type ChangeSet
- type ChangeSetOptions
- type CloudFormation
- type Describer
- func (d *Describer) DescribeCurrentState(ctx context.Context) (*cloudformation.DescribeChangeSetOutput, error)
- func (d *Describer) DescribeForExecution(ctx context.Context, diagnoser *Diagnoser) (Report, error)
- func (d *Describer) WaitAndCheck(ctx context.Context, diagnoser *Diagnoser) (Report, error)
- func (d *Describer) WaitForGone(ctx context.Context) error
- func (d *Describer) WaitForReport(ctx context.Context, diagnoser *Diagnoser) (Report, error)
- func (d *Describer) WaitForSettled(ctx context.Context) (*cloudformation.DescribeChangeSetOutput, error)
- type Diagnoser
- type Diagnosis
- type ErrorsOptions
- type Kind
- type Options
- type Report
- type SourceTracer
- type Templates
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Format ¶
Format writes a problem for people to read: upstream's formatProblemDiagnosis. It gives a caption for what found the problem, then a block for each failed resource, sorted by location: the construct path, or the stack, its nested stacks and the logical ID; each message, wrapped at 120 characters; the source location; and the problem's additional context.
func HasNoChanges ¶
HasNoChanges reports whether a change set failed for having no changes: upstream's changeSetHasNoChanges. It goes by the status and its reason, not the changes, because a change set whose resources are unchanged can still change outputs and be executed.
Types ¶
type ChangeSet ¶
type ChangeSet struct {
ID string
Name string
StackID string
StackName string
Status types.ChangeSetStatus
StatusReason string
}
ChangeSet is what the diagnoser reads of a change set: upstream's ChangeSetSummary, which DescribeChangeSet's output and ListChangeSets' summaries both give.
func ChangeSetFromDescription ¶
func ChangeSetFromDescription(d *cloudformation.DescribeChangeSetOutput) ChangeSet
ChangeSetFromDescription returns what the diagnoser reads of a change set's description.
func ChangeSetFromSummary ¶
func ChangeSetFromSummary(s types.ChangeSetSummary) ChangeSet
ChangeSetFromSummary returns what the diagnoser reads of a change set's summary.
type ChangeSetOptions ¶
type ChangeSetOptions struct {
// RequireExecutable makes a change set that can't be executed a problem.
// Otherwise only a change set that failed to be created is: one with no changes, or still being created, is no problem, as for deploy those are normal.
RequireExecutable bool
}
ChangeSetOptions configure Diagnoser.ChangeSet.
type CloudFormation ¶
type CloudFormation interface {
stackevents.CloudFormation
cloudformation.DescribeChangeSetAPIClient
cloudformation.ListChangeSetsAPIClient
cloudformation.DescribeEventsAPIClient
DescribeStacks(ctx context.Context, in *cloudformation.DescribeStacksInput, opts ...func(*cloudformation.Options)) (*cloudformation.DescribeStacksOutput, error)
ListHookResults(ctx context.Context, in *cloudformation.ListHookResultsInput, opts ...func(*cloudformation.Options)) (*cloudformation.ListHookResultsOutput, error)
}
CloudFormation is the part of the CloudFormation API the package uses.
type Describer ¶
type Describer struct {
// contains filtered or unexported fields
}
Describer describes one change set: upstream's ChangeSetDescriber.
func NewDescriber ¶
func NewDescriber(cfn cloudformation.DescribeChangeSetAPIClient, io *iohelper.Helper, stackNameOrARN, changeSetNameOrARN string, templates Templates) *Describer
NewDescriber returns a describer of the change set named by changeSetNameOrARN, of the stack named by stackNameOrARN; ARNs are better. The templates are the ones the change set goes from and to, which a description of it is checked against, as Describer.DescribeCurrentState says. They are required, as upstream's are since aws-cdk-cli#2045, so that no description with property values skips the check; Describer.WaitForGone reads neither, so a describer used only for it may have none.
func (*Describer) DescribeCurrentState ¶
func (d *Describer) DescribeCurrentState(ctx context.Context) (*cloudformation.DescribeChangeSetOutput, error)
DescribeCurrentState describes the change set as it is now, without waiting for it to settle, with IncludePropertyValues and every page of its changes.
Such a description doesn't always list every change, and the diff takes a difference between the templates in a value resolved at deploy time that the change set doesn't mention as no change. So in two cases the change set is described again without property values, and what the first description left out is put back:
- CloudFormation says in the reason that it left data out (aws-cdk-cli#1780);
- the change set was created, and its description would hide a difference between the describer's templates, as cfndiff.PropertiesHiddenByChangeSet finds, since CloudFormation can leave out, without a warning, a change to a string holding JSON that only gains a key (aws-cdk-cli#2036).
The second check costs a description only when a difference between the templates would otherwise be hidden.
func (*Describer) DescribeForExecution ¶
DescribeForExecution describes the change set as it is now, without waiting, and fails with its diagnosis' error unless it can be executed. It is for a change set an earlier command created, maybe not ours, on which blocking would be wrong.
func (*Describer) WaitAndCheck ¶
WaitAndCheck waits for the change set to settle, and fails with its diagnosis' error when anything is wrong with it: upstream's waitAndThrowOnProblem. The change set it returns can be executed, or has no changes.
func (*Describer) WaitForGone ¶
WaitForGone waits for the change set to be deleted, or not to exist.
A change set that failed to delete fails with a cdkerr.KindDeployment error with DeploymentCode "ChangeSetDeleteFailed" and CloudFormation's reason. Upstream counts it as gone, and deploy then fails to create a change set of the same name (G.1-D5). When ctx is done it returns an error that names the change set and its stack and wraps ctx's error (G.10).
func (*Describer) WaitForReport ¶
WaitForReport waits for the change set to settle, and has it diagnosed.
func (*Describer) WaitForSettled ¶
func (d *Describer) WaitForSettled(ctx context.Context) (*cloudformation.DescribeChangeSetOutput, error)
WaitForSettled waits for the change set to stop being created, and describes it. A change set that failed is described like any other. When ctx is done it returns an error that names the change set and its stack and wraps ctx's error (G.10).
type Diagnoser ¶
type Diagnoser struct {
// contains filtered or unexported fields
}
Diagnoser diagnoses a stack's failures: upstream's CloudFormationStackDiagnoser.
func (*Diagnoser) FromErrors ¶
func (d *Diagnoser) FromErrors(ctx context.Context, errs []stackevents.ResourceError, stack types.Stack, opts ErrorsOptions) Diagnosis
FromErrors diagnoses a deployment from the resource errors its monitor collected, and the stack as it is now: upstream's diagnoseFromErrorCollection.
type Diagnosis ¶
type Diagnosis struct {
Kind Kind
// Problem is the problem found, for a diagnosis of kind [Problem].
Problem *cdkerr.Diagnosis
// Message says why the diagnosis couldn't be made, for a diagnosis of kind [ErrorDiagnosing], and Cause is the error that stopped it, if one did.
Message string
Cause error
}
Diagnosis is the outcome of diagnosing a stack: upstream's Diagnosis.
func (Diagnosis) Err ¶
Err returns nil for a diagnosis that found no problem, and otherwise the error upstream's throwOnError throws: a cdkerr.Error of Kind cdkerr.KindDeployment. For a problem, its message is the diagnosis as Format writes it, its DeploymentCode the first problem's code, or by default one for what found the problem ("StackDeployFailed", "ChangeSetCreationFailed", "ChangeSetNotReady" or "EarlyValidationFailure"), and it carries the diagnosis. For an error while diagnosing, its DeploymentCode is "ErrorDiagnosisFailed", and it wraps the cause.
func (Diagnosis) StackDiagnosis ¶
func (d Diagnosis) StackDiagnosis() iohost.StackDiagnosis
StackDiagnosis returns d as the diagnose action reports it, with its cause as Err (M.9-D1).
type ErrorsOptions ¶
type ErrorsOptions struct {
// AllowFallback reads the stack's events, back to its most recent deployment attempt, when there are no errors, in case the errors were collected before the failure's events arrived.
AllowFallback bool
// RollbackEnabled says whether the deployment rolls back on failure, which deletes what the investigations would read: upstream's rollbackEnabled.
// The diagnoses of change sets, which have no such setting, take it as enabled, upstream's default.
RollbackEnabled bool
// Principals are the roles and Lambda functions the deployment's events named, [stackevents.ResourceErrors.Principals], which the CloudTrail investigation takes as the stacks' own besides those the stacks hold: upstream's ResourceErrors.principals.
Principals []stackevents.OperationPrincipal
}
ErrorsOptions configure Diagnoser.FromErrors.
type Options ¶
type Options struct {
// CFN is the CloudFormation client of the stack's environment.
CFN CloudFormation
// IO is where the diagnoser sends its debug messages and warnings.
IO *iohelper.Helper
// Tracer traces problems to the constructs that define them; nil means [NoSourceTrace].
Tracer SourceTracer
// TopLevelStackHierarchicalID is the hierarchical ID of the stack being diagnosed, which locates problems that have no construct path.
TopLevelStackHierarchicalID string
// ToolkitVersion gives the bootstrap stack's version for the warning when DescribeEvents is denied, which needs version 30; nil, or an error, means the warning says "unknown".
ToolkitVersion func(context.Context) (int, error)
// FetchHookFailureDetails fetches, with GetHookResult, the details of the hooks that failed resources, and adds them to the problems.
// Deploy leaves it off, because the activity stream already shows them; the diagnose action turns it on.
FetchHookFailureDetails bool
// Exploration returns the configuration for the investigations' read-only calls, typically through the stack's lookup role, since the deploy role may not be allowed them: upstream's additionalExplorationSdkProvider.
// Nil runs no investigations.
// It is called when there is a first problem to investigate, and again only after a call cut short by the context; the diagnosers of nested change sets share what it returns, and a failure is a debug message, after which the investigations are skipped.
Exploration func(context.Context) (aws.Config, error)
}
Options configure a Diagnoser.
type Report ¶
type Report struct {
ChangeSet *cloudformation.DescribeChangeSetOutput
Diagnosis Diagnosis
}
Report is a change set, described, and its diagnosis.
type SourceTracer ¶
type SourceTracer interface {
// TraceResource traces the resource with the given logical ID, in the stack with the given ID, nested in the stacks with the given logical IDs, outermost first.
TraceResource(stackARN string, parents []string, logicalID string) (*cdkerr.SourceTrace, error)
// TraceStack traces the stack with the given ID, nested in the stacks with the given logical IDs, outermost first.
TraceStack(stackARN string, parents []string) (*cdkerr.SourceTrace, error)
}
SourceTracer traces CloudFormation resources and stacks to the CDK code that defines them: upstream's ISourceTracer. A tracer returns nil when it finds no trace.
var NoSourceTrace SourceTracer = noSourceTrace{}
NoSourceTrace is a tracer that finds no traces.
func NewStackTracer ¶
func NewStackTracer(stack *cloudassembly.Stack) SourceTracer
NewStackTracer returns a tracer of the problems of stack, and of the stacks nested in it: upstream's StackArtifactSourceTracer. It reads a resource's construct path from its aws:cdk:path metadata in the template, following nested stacks to their templates by their aws:asset:path metadata, and the creation stack trace from the stack's aws:cdk:logicalId or aws:cdk:creationStack metadata.
type Templates ¶
type Templates struct {
// Deployed is the deployed template the change set goes from.
Deployed *cfntemplate.Object
// Generated is the template the change set was created from.
Generated *cfntemplate.Object
}
Templates are the templates a change set goes from and to: upstream's ChangeSetTemplates. A nil template is an empty one.