Skip to content

Commit

Permalink
fix: tf resource directory name to comply with Windows directory nami…
Browse files Browse the repository at this point in the history
…ng conventions (#1160)
  • Loading branch information
SparkYuan committed Jun 12, 2024
1 parent 7e50571 commit 71c91ed
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions pkg/engine/runtime/terraform/terraform_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"time"

"github.com/spf13/afero"

"github.com/patrickmn/go-cache"

apiv1 "kusionstack.io/kusion/pkg/apis/api.kusion.io/v1"
v1 "kusionstack.io/kusion/pkg/apis/status/v1"
"kusionstack.io/kusion/pkg/engine"
Expand Down Expand Up @@ -47,7 +49,8 @@ func (t *TerraformRuntime) Apply(ctx context.Context, request *runtime.ApplyRequ

plan := request.PlanResource
stackPath := request.Stack.Path
tfCacheDir := filepath.Join(stackPath, "."+plan.ResourceKey())
key := plan.ResourceKey()
tfCacheDir := buildTFCacheDir(stackPath, key)
t.WorkSpace.SetStackDir(stackPath)
t.WorkSpace.SetCacheDir(tfCacheDir)
t.WorkSpace.SetResource(plan)
Expand Down Expand Up @@ -112,17 +115,17 @@ func (t *TerraformRuntime) Apply(ctx context.Context, request *runtime.ApplyRequ
}()

// Prepare the event channel and send the resource ID to watch channel.
log.Infof("Started to watch %s with the type of %s", plan.ResourceKey(), plan.Type)
log.Infof("Started to watch %s with the type of %s", key, plan.Type)
eventCh := make(chan runtime.TFEvent)

// Prevent concurrent operations on resources with the same ID.
if _, ok := tfEvents.Get(plan.ResourceKey()); ok {
err = fmt.Errorf("failed to initiate the event channel for watching terraform resource %s as: conflict resource ID", plan.ResourceKey())
if _, ok := tfEvents.Get(key); ok {
err = fmt.Errorf("failed to initiate the event channel for watching terraform resource %s as: conflict resource ID", key)
log.Error(err)
return &runtime.ApplyResponse{Resource: nil, Status: v1.NewErrorStatus(err)}
}
tfEvents.Set(plan.ResourceKey(), eventCh, cache.NoExpiration)
watchCh <- plan.ResourceKey()
tfEvents.Set(key, eventCh, cache.NoExpiration)
watchCh <- key

// Wait for the apply to be finished.
shouldBreak := false
Expand Down Expand Up @@ -168,19 +171,23 @@ func (t *TerraformRuntime) Apply(ctx context.Context, request *runtime.ApplyRequ
}
}

func buildTFCacheDir(stackPath string, key string) string {
// replace ':' with '_' to comply with Windows directory naming conventions.
return filepath.Join(stackPath, "."+strings.ReplaceAll(key, ":", "_"))
}

// Read terraform show state
func (t *TerraformRuntime) Read(ctx context.Context, request *runtime.ReadRequest) *runtime.ReadResponse {
priorResource := request.PriorResource
planResource := request.PlanResource

// When the operation is create or update, the planResource is set to planResource,
// when the operation is delete, planResource is nil, the planResource is set to priorResource,
// tf runtime uses planResource to rebuild tfcache resources.
if planResource == nil && priorResource != nil {
// planResource is nil representing that this is a Delete action.
// We only need to refresh the tf.state files and return the latest resources state in this method.
// Most fields in attributes in resources aren't necessary for the command `terraform apply -refresh-only` and will make errors
// if fields copied from kusion_state.json but read-only in main.tf.json
// Most fields in the `attributes` field of resource aren't necessary for the command `terraform apply -refresh-only`.
// These fields will cause errors if they are copied from kusion_state.json but read-only in main.tf.json
planResource = &apiv1.Resource{
ID: priorResource.ID,
Type: priorResource.Type,
Expand All @@ -197,7 +204,7 @@ func (t *TerraformRuntime) Read(ctx context.Context, request *runtime.ReadReques
t.mu.Lock()
defer t.mu.Unlock()
stackPath := request.Stack.Path
tfCacheDir := filepath.Join(stackPath, "."+planResource.ResourceKey())
tfCacheDir := buildTFCacheDir(stackPath, planResource.ResourceKey())
t.WorkSpace.SetStackDir(stackPath)
t.WorkSpace.SetCacheDir(tfCacheDir)
t.WorkSpace.SetResource(planResource)
Expand Down

0 comments on commit 71c91ed

Please sign in to comment.