Skip to content

Commit

Permalink
feat: improve logging with interpolated executable name
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasMrqes committed Sep 23, 2024
1 parent 0e624b5 commit fa5d310
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 32 deletions.
2 changes: 1 addition & 1 deletion internal/controllers/terraformlayer/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
return ctrl.Result{RequeueAfter: r.Config.Controller.Timers.OnError}, err
}
if locked {
log.Infof("terraform layer %s is locked, skipping reconciliation.", layer.Name)
log.Infof("TerraformLayer %s is locked, skipping reconciliation.", layer.Name)
return ctrl.Result{RequeueAfter: r.Config.Controller.Timers.WaitAction}, nil
}
repository := &configv1alpha1.TerraformRepository{}
Expand Down
30 changes: 15 additions & 15 deletions internal/runner/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,24 @@ func (r *Runner) ExecAction() error {

err := annotations.Add(context.TODO(), r.Client, r.Layer, ann)
if err != nil {
log.Errorf("could not update terraform layer annotations: %s", err)
log.Errorf("could not update TerraformLayer annotations: %s", err)
return err
}
log.Infof("successfully updated terraform layer annotations")
log.Infof("successfully updated TerraformLayer annotations")

return nil
}

// Run the `init` command
func (r *Runner) ExecInit() error {
log.Infof("launching terraform init in %s", r.workingDir)
log.Infof("launching %s init in %s", r.exec.TenvName(), r.workingDir)
if r.exec == nil {
err := errors.New("terraform or terragrunt binary not installed")
return err
}
err := r.exec.Init(r.workingDir)
if err != nil {
log.Errorf("error executing terraform init: %s", err)
log.Errorf("error executing %s init: %s", r.exec.TenvName(), err)
return err
}
return nil
Expand All @@ -78,24 +78,24 @@ func (r *Runner) ExecInit() error {
// Run the `plan` command and save the plan artifact in the datastore
// Returns the sha256 sum of the plan artifact
func (r *Runner) execPlan() (string, error) {
log.Infof("starting terraform plan")
log.Infof("running %s plan", r.exec.TenvName())
if r.exec == nil {
err := errors.New("terraform or terragrunt binary not installed")
return "", err
}
err := r.exec.Plan(PlanArtifact)
if err != nil {
log.Errorf("error executing terraform plan: %s", err)
log.Errorf("error executing %s plan: %s", r.exec.TenvName(), err)
return "", err
}
planJsonBytes, err := r.exec.Show(PlanArtifact, "json")
if err != nil {
log.Errorf("error getting terraform plan json: %s", err)
log.Errorf("error getting %s plan json: %s", r.exec.TenvName(), err)
return "", err
}
prettyPlan, err := r.exec.Show(PlanArtifact, "pretty")
if err != nil {
log.Errorf("error getting terraform pretty plan: %s", err)
log.Errorf("error getting %s pretty plan: %s", r.exec.TenvName(), err)
return "", err
}
log.Infof("sending plan to datastore")
Expand All @@ -106,7 +106,7 @@ func (r *Runner) execPlan() (string, error) {
plan := &tfjson.Plan{}
err = json.Unmarshal(planJsonBytes, plan)
if err != nil {
log.Errorf("error parsing terraform json plan: %s", err)
log.Errorf("error parsing %s json plan: %s", r.exec.TenvName(), err)
return "", err
}
_, shortDiff := runnerutils.GetDiff(plan)
Expand All @@ -129,16 +129,16 @@ func (r *Runner) execPlan() (string, error) {
log.Errorf("could not put plan binary in cache: %s", err)
return "", err
}
log.Infof("terraform plan ran successfully")
log.Infof("%s plan ran successfully", r.exec.TenvName())
return b64.StdEncoding.EncodeToString(sum[:]), nil
}

// Run the `apply` command, by default with the plan artifact from the previous plan run
// Returns the sha256 sum of the plan artifact used
func (r *Runner) execApply() (string, error) {
log.Info("starting terraform apply")
log.Infof("starting %s apply", r.exec.TenvName())
if r.exec == nil {
err := errors.New("terraform or terragrunt binary not installed")
err := fmt.Errorf("%s binary not installed", r.exec.TenvName())
return "", err
}
log.Infof("getting plan binary in datastore at key %s/%s/%s/%s", r.Layer.Namespace, r.Layer.Name, r.Run.Spec.Artifact.Run, r.Run.Spec.Artifact.Attempt)
Expand All @@ -153,21 +153,21 @@ func (r *Runner) execApply() (string, error) {
log.Errorf("could not write plan artifact to disk: %s", err)
return "", err
}
log.Info("launching terraform apply")
log.Infof("launching %s apply", r.exec.TenvName())
if configv1alpha1.GetApplyWithoutPlanArtifactEnabled(r.Repository, r.Layer) {
log.Infof("applying without reusing plan artifact from previous plan run")
err = r.exec.Apply("")
} else {
err = r.exec.Apply(PlanArtifact)
}
if err != nil {
log.Errorf("error executing terraform apply: %s", err)
log.Errorf("error executing %s apply: %s", r.exec.TenvName(), err)
return "", err
}
err = r.Datastore.PutPlan(r.Layer.Namespace, r.Layer.Name, r.Run.Name, strconv.Itoa(r.Run.Status.Retries), "short", []byte("Apply Successful"))
if err != nil {
log.Errorf("could not put short plan in datastore: %s", err)
}
log.Info("terraform apply ran successfully")
log.Infof("%s apply ran successfully", r.exec.TenvName())
return b64.StdEncoding.EncodeToString(sum[:]), nil
}
2 changes: 1 addition & 1 deletion internal/runner/tools/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func InstallBinaries(layer *configv1alpha1.TerraformLayer, repo *configv1alpha1.
if err := install(binaryPath, "terragrunt", terragruntVersion); err != nil {
return nil, err
}
log.Infof("using Terragrunt version %s as wrapper for %s", terragruntVersion, baseExec)
log.Infof("using Terragrunt version %s as wrapper for %s", terragruntVersion, baseExec.TenvName())
if baseExec.TenvName() == "terraform" {
return &tg.Terragrunt{
ExecPath: filepath.Join(binaryPath, "Terragrunt", terragruntVersion, "terragrunt"),
Expand Down
4 changes: 2 additions & 2 deletions internal/server/api/layers.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ func (a *API) getLayersAndRuns() ([]configv1alpha1.TerraformLayer, map[string]co
layers := &configv1alpha1.TerraformLayerList{}
err := a.Client.List(context.Background(), layers)
if err != nil {
log.Errorf("could not list terraform layers: %s", err)
log.Errorf("could not list TerraformLayers: %s", err)
return nil, nil, err
}
runs := &configv1alpha1.TerraformRunList{}
indexedRuns := map[string]configv1alpha1.TerraformRun{}
err = a.Client.List(context.Background(), runs)
if err != nil {
log.Errorf("could not list terraform runs: %s", err)
log.Errorf("could not list TerraformRuns: %s", err)
return nil, nil, err
}
for _, run := range runs.Items {
Expand Down
2 changes: 1 addition & 1 deletion internal/server/api/repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (a *API) RepositoriesHandler(c echo.Context) error {
repositories := &configv1alpha1.TerraformRepositoryList{}
err := a.Client.List(context.Background(), repositories)
if err != nil {
log.Errorf("could not list terraform repositories: %s", err)
log.Errorf("could not list TerraformRepositories: %s", err)
return c.String(http.StatusInternalServerError, "could not list terraform repositories")
}
results := []repository{}
Expand Down
8 changes: 4 additions & 4 deletions internal/webhook/event/pullrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (e *PullRequestEvent) Handle(c client.Client) error {
repositories := &configv1alpha1.TerraformRepositoryList{}
err := c.List(context.Background(), repositories)
if err != nil {
log.Errorf("could not list terraform repositories: %s", err)
log.Errorf("could not list TerraformRepositories: %s", err)
return err
}
affectedRepositories := e.getAffectedRepositories(repositories.Items)
Expand All @@ -52,7 +52,7 @@ func (e *PullRequestEvent) Handle(c client.Client) error {
func batchCreatePullRequests(ctx context.Context, c client.Client, prs []configv1alpha1.TerraformPullRequest) error {
var errResult error
for _, pr := range prs {
log.Infof("creating terraform pull request %s/%s", pr.Namespace, pr.Name)
log.Infof("creating TerraformPullRequest %s/%s", pr.Namespace, pr.Name)
err := c.Create(ctx, &pr)
if err != nil {
errResult = multierror.Append(errResult, err)
Expand All @@ -64,7 +64,7 @@ func batchCreatePullRequests(ctx context.Context, c client.Client, prs []configv
func batchDeletePullRequests(ctx context.Context, c client.Client, prs []configv1alpha1.TerraformPullRequest) error {
var errResult error
for _, pr := range prs {
log.Infof("deleting terraform pull request %s/%s", pr.Namespace, pr.Name)
log.Infof("deleting TerraformPullRequest %s/%s", pr.Namespace, pr.Name)
err := c.Delete(ctx, &pr)
if err != nil {
errResult = multierror.Append(errResult, err)
Expand Down Expand Up @@ -104,7 +104,7 @@ func (e *PullRequestEvent) generateTerraformPullRequests(repositories []configv1
func (e *PullRequestEvent) getAffectedRepositories(repositories []configv1alpha1.TerraformRepository) []configv1alpha1.TerraformRepository {
affectedRepositories := []configv1alpha1.TerraformRepository{}
for _, repo := range repositories {
log.Infof("evaluating terraform repository %s for url %s", repo.Name, repo.Spec.Repository.Url)
log.Infof("evaluating TerraformRepository %s for url %s", repo.Name, repo.Spec.Repository.Url)
log.Infof("comparing normalized url %s with received URL from payload %s", utils.NormalizeUrl(repo.Spec.Repository.Url), e.URL)
if e.URL == utils.NormalizeUrl(repo.Spec.Repository.Url) {
affectedRepositories = append(affectedRepositories, repo)
Expand Down
16 changes: 8 additions & 8 deletions internal/webhook/event/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ func (e *PushEvent) Handle(c client.Client) error {
repositories := &configv1alpha1.TerraformRepositoryList{}
err := c.List(context.Background(), repositories)
if err != nil {
log.Errorf("could not list terraform repositories: %s", err)
log.Errorf("could not list TerraformRepositories: %s", err)
return err
}
layers := &configv1alpha1.TerraformLayerList{}
err = c.List(context.Background(), layers)
if err != nil {
log.Errorf("could not list terraform layers: %s", err)
log.Errorf("could not list TerraformLayers: %s", err)
return err
}
prs := &configv1alpha1.TerraformPullRequestList{}
err = c.List(context.Background(), prs)
if err != nil {
log.Errorf("could not list terraform prs: %s", err)
log.Errorf("could not list TerraformPullRequests: %s", err)
return err
}
affectedRepositories := e.getAffectedRepositories(repositories.Items)
Expand All @@ -47,16 +47,16 @@ func (e *PushEvent) Handle(c client.Client) error {
ann[annotations.LastBranchCommitDate] = date
err := annotations.Add(context.TODO(), c, &repo, ann)
if err != nil {
log.Errorf("could not add annotation to terraform repository %s", err)
log.Errorf("could not add annotation to TerraformRepository %s", err)
return err
}
}

for _, layer := range e.getAffectedLayers(layers.Items, affectedRepositories) {
ann := map[string]string{}
log.Printf("evaluating terraform layer %s for revision %s", layer.Name, e.Revision)
log.Printf("evaluating TerraformLayer %s for revision %s", layer.Name, e.Revision)
if layer.Spec.Branch != e.Revision {
log.Infof("branch %s for terraform layer %s not matching revision %s", layer.Spec.Branch, layer.Name, e.Revision)
log.Infof("branch %s for TerraformLayer %s not matching revision %s", layer.Spec.Branch, layer.Name, e.Revision)
continue
}
ann[annotations.LastBranchCommit] = e.ChangeInfo.ShaAfter
Expand All @@ -70,7 +70,7 @@ func (e *PushEvent) Handle(c client.Client) error {

err := annotations.Add(context.TODO(), c, &layer, ann)
if err != nil {
log.Errorf("could not add annotation to terraform layer %s", err)
log.Errorf("could not add annotation to TerraformLayer %s", err)
return err
}
}
Expand All @@ -81,7 +81,7 @@ func (e *PushEvent) Handle(c client.Client) error {
ann[annotations.LastBranchCommitDate] = date
err := annotations.Add(context.TODO(), c, &pr, ann)
if err != nil {
log.Errorf("could not add annotation to terraform pr %s", err)
log.Errorf("could not add annotation to TerraformPullRequest %s", err)
return err
}
}
Expand Down

0 comments on commit fa5d310

Please sign in to comment.