From 438145b12e8dfb40a9c85e559cfcd6c145341dc6 Mon Sep 17 00:00:00 2001 From: Jianbo Sun Date: Sat, 25 Jun 2022 18:39:24 +0800 Subject: [PATCH] Fix: add message for terraform resource in error state Signed-off-by: Jianbo Sun --- .../v1alpha2/application/apply.go | 2 +- .../v1alpha2/application/generator.go | 2 +- pkg/controller/utils/capability.go | 1 + ...inux_test.go => capability_config_test.go} | 53 +++---------------- references/cli/provider.go | 5 +- references/plugins/reference_test.go | 3 +- 6 files changed, 15 insertions(+), 51 deletions(-) rename pkg/controller/utils/{capability_linux_test.go => capability_config_test.go} (64%) diff --git a/pkg/controller/core.oam.dev/v1alpha2/application/apply.go b/pkg/controller/core.oam.dev/v1alpha2/application/apply.go index 61628c497..dcf1b70dc 100644 --- a/pkg/controller/core.oam.dev/v1alpha2/application/apply.go +++ b/pkg/controller/core.oam.dev/v1alpha2/application/apply.go @@ -308,12 +308,12 @@ func setStatus(status *common.ApplicationComponentStatus, observedGeneration, ge } return true } + status.Message = message if !isLatest() || state != terraformtypes.Available { status.Healthy = false return false } status.Healthy = true - status.Message = message return true } diff --git a/pkg/controller/core.oam.dev/v1alpha2/application/generator.go b/pkg/controller/core.oam.dev/v1alpha2/application/generator.go index 3f98a8a15..de349f014 100644 --- a/pkg/controller/core.oam.dev/v1alpha2/application/generator.go +++ b/pkg/controller/core.oam.dev/v1alpha2/application/generator.go @@ -199,7 +199,7 @@ func convertStepProperties(step *v1beta1.WorkflowStep, app *v1beta1.Application) } func checkDependsOnValidComponent(dependsOnComponentNames, allComponentNames []string) (string, bool) { - // does not depends on other components + // does not depend on other components if dependsOnComponentNames == nil { return "", true } diff --git a/pkg/controller/utils/capability.go b/pkg/controller/utils/capability.go index 11fbcf14b..a3033274f 100644 --- a/pkg/controller/utils/capability.go +++ b/pkg/controller/utils/capability.go @@ -223,6 +223,7 @@ func GetTerraformConfigurationFromRemote(name, remoteURL, remotePath string) (st // Check if the directory exists. If yes, remove it. entities, err := os.ReadDir(cachePath) if err != nil || len(entities) == 0 { + fmt.Printf("loading terraform module %s into %s from %s\n", name, cachePath, remoteURL) if _, err = git.PlainClone(cachePath, false, &git.CloneOptions{ URL: remoteURL, Progress: os.Stdout, diff --git a/pkg/controller/utils/capability_linux_test.go b/pkg/controller/utils/capability_config_test.go similarity index 64% rename from pkg/controller/utils/capability_linux_test.go rename to pkg/controller/utils/capability_config_test.go index e269a4f0d..310e75ac7 100644 --- a/pkg/controller/utils/capability_linux_test.go +++ b/pkg/controller/utils/capability_config_test.go @@ -17,24 +17,16 @@ package utils import ( - "context" "io/ioutil" "os" "path/filepath" "strings" "testing" - . "github.com/agiledragon/gomonkey/v2" - - "github.com/pkg/errors" - "gopkg.in/src-d/go-git.v4" "gotest.tools/assert" ) func TestGetTerraformConfigurationFromRemote(t *testing.T) { - // If you hit a panic on macOS as below, please fix it by referencing https://github.com/eisenxp/macos-golink-wrapper. - // panic: permission denied [recovered] - // panic: permission denied type want struct { config string errMsg string @@ -46,8 +38,6 @@ func TestGetTerraformConfigurationFromRemote(t *testing.T) { path string data []byte variableFile string - // mockWorkingPath will create `/tmp/terraform` - mockWorkingPath bool } cases := map[string]struct { args args @@ -57,7 +47,7 @@ func TestGetTerraformConfigurationFromRemote(t *testing.T) { args: args{ name: "valid", url: "https://github.com/kubevela-contrib/terraform-modules.git", - path: "", + path: "unittest/", data: []byte(` variable "aaa" { type = list(object({ @@ -85,7 +75,7 @@ variable "aaa" { args: args{ name: "aws-subnet", url: "https://github.com/kubevela-contrib/terraform-modules.git", - path: "aws/subnet", + path: "unittest/aws/subnet", data: []byte(` variable "aaa" { type = list(object({ @@ -109,47 +99,20 @@ variable "aaa" { }`, }, }, - "working path exists": { - args: args{ - variableFile: "main.tf", - mockWorkingPath: true, - }, - want: want{ - errMsg: "failed to remove the directory", - }, - }, } for name, tc := range cases { t.Run(name, func(t *testing.T) { - if tc.args.mockWorkingPath { - err := os.MkdirAll("./tmp/terraform", 0755) - assert.NilError(t, err) - defer os.RemoveAll("./tmp/terraform") - patch1 := ApplyFunc(os.Remove, func(_ string) error { - return errors.New("failed") - }) - defer patch1.Reset() - patch2 := ApplyFunc(os.Open, func(_ string) (*os.File, error) { - return nil, errors.New("failed") - }) - defer patch2.Reset() - } - - patch := ApplyFunc(git.PlainCloneContext, func(ctx context.Context, path string, isBare bool, o *git.CloneOptions) (*git.Repository, error) { - var tmpPath string - if tc.args.path != "" { - tmpPath = filepath.Join("./tmp/terraform", tc.args.name, tc.args.path) - } else { - tmpPath = filepath.Join("./tmp/terraform", tc.args.name) - } + home, _ := os.UserHomeDir() + path := filepath.Join(home, ".vela", "terraform") + tmpPath := filepath.Join(path, tc.args.name, tc.args.path) + if len(tc.args.data) > 0 { err := os.MkdirAll(tmpPath, os.ModePerm) assert.NilError(t, err) err = ioutil.WriteFile(filepath.Clean(filepath.Join(tmpPath, tc.args.variableFile)), tc.args.data, 0644) assert.NilError(t, err) - return nil, nil - }) - defer patch.Reset() + } + defer os.RemoveAll(tmpPath) conf, err := GetTerraformConfigurationFromRemote(tc.args.name, tc.args.url, tc.args.path) if tc.want.errMsg != "" { diff --git a/references/cli/provider.go b/references/cli/provider.go index b3ef40d0f..6fddc4df5 100644 --- a/references/cli/provider.go +++ b/references/cli/provider.go @@ -166,9 +166,8 @@ func prepareProviderAddSubCommand(c common.Args, ioStreams cmdutil.IOStreams) ([ return nil, err } for _, p := range parameters { - if p.Name == providerNameParam { - p.Default = providerType + "-default" - } + // TODO(wonderflow): make the provider default name to be unique but keep the compatiblility as some Application didn't specify the name, + // now it's “default” for every one, the name will conflict if we have more than one cloud provider. cmd.Flags().String(p.Name, fmt.Sprint(p.Default), p.Usage) } cmd.RunE = func(cmd *cobra.Command, args []string) error { diff --git a/references/plugins/reference_test.go b/references/plugins/reference_test.go index 443538848..a24da25c7 100644 --- a/references/plugins/reference_test.go +++ b/references/plugins/reference_test.go @@ -430,7 +430,8 @@ variable "acl" { "configuration is in git remote": { args: args{ cap: types.Capability{ - TerraformConfiguration: "https://github.com/zzxwill/terraform-alibaba-eip.git", + Name: "ecs", + TerraformConfiguration: "https://github.com/wonderflow/terraform-alicloud-ecs-instance.git", ConfigurationType: "remote", }, },