Skip to content

Commit

Permalink
Adding support for terraform private module source for git (radius-pr…
Browse files Browse the repository at this point in the history
…oject#7167)

- Added a RecipeConfig property to the environment
  - Typesspec changes
  - Conversion and unit tests
  - Datamodel changes
- Adding changes to the config to append the template path with
credential informaiton
   - changes to query list secret to get secret information
   - creating credential appended template path

Design Doc: radius-project/design-notes#37

<!--

Please select **one** of the following options that describes your
change and delete the others. Clearly identifying the type of change you
are making will help us review your PR faster, and is used in authoring
release notes.

If you are making a bug fix or functionality change to Radius and do not
have an associated issue link please create one now.

-->

- This pull request adds or changes features of Radius and has an
approved issue (issue link required).
- This pull request is a minor refactor, code cleanup, test improvement,
or other maintenance task and doesn't change the functionality of Radius
(issue link optional).

<!--

Please update the following to link the associated issue. This is
required for some kinds of changes (see above).

-->

Fixes: radius-project#6911

---------

Signed-off-by: Vishwanath Hiremath <[email protected]>
  • Loading branch information
vishwahiremat authored and lakshmimsft committed Feb 28, 2024
1 parent db82383 commit a4fb7b2
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
26 changes: 26 additions & 0 deletions pkg/recipes/terraform/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"errors"
"fmt"
"os"
"time"

install "github.com/hashicorp/hc-install"
Expand Down Expand Up @@ -88,6 +89,13 @@ func (e *executor) Deploy(ctx context.Context, options Options) (*tfjson.State,
return nil, err
}

// Set environment variables for the Terraform process reading input from the environment configuration.
// This is required for the Terraform process to read the environment variables and use them as input for the recipe deployment.
err = e.setEnvironmentVariables(ctx, options.EnvConfig)
if err != nil {
return nil, err
}

// Run TF Init and Apply in the working directory
state, err := initAndApply(ctx, tf)
if err != nil {
Expand Down Expand Up @@ -194,6 +202,24 @@ func (e *executor) GetRecipeMetadata(ctx context.Context, options Options) (map[
}, nil
}

// setEnvironmentVariables sets environment variables for the Terraform process reading input from the environment configuration.
func (e executor) setEnvironmentVariables(ctx context.Context, envConfig *recipes.Configuration) error {
logger := ucplog.FromContextOrDiscard(ctx)

// Set environment variables for the Terraform process reading input from the environment configuration.
// This is required for the Terraform process to read the environment variables and use them as input for the recipe deployment.
if envConfig != nil && envConfig.RecipeConfig.EnvVars.AdditionalProperties != nil {
for key, value := range envConfig.RecipeConfig.EnvVars.AdditionalProperties {
if err := os.Setenv(key, value); err != nil {
logger.Info(fmt.Sprintf("Failed to set environment variable %s: %s", key, err.Error()))
return fmt.Errorf("error setting environment variable %s: %w", key, err)
}
}
}

return nil
}

// generateConfig generates Terraform configuration with required inputs for the module, providers and backend to be initialized and applied.
func (e *executor) generateConfig(ctx context.Context, tf *tfexec.Terraform, options Options) (string, error) {
logger := ucplog.FromContextOrDiscard(ctx)
Expand Down
61 changes: 61 additions & 0 deletions pkg/recipes/terraform/execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ limitations under the License.
package terraform

import (
"os"
"path/filepath"
"testing"

"github.com/hashicorp/terraform-exec/tfexec"
dm "github.com/radius-project/radius/pkg/corerp/datamodel"
"github.com/radius-project/radius/pkg/recipes"
"github.com/radius-project/radius/pkg/recipes/terraform/config"
"github.com/radius-project/radius/test/testcontext"
Expand Down Expand Up @@ -114,3 +116,62 @@ func Test_GetTerraformConfig_InvalidDirectory(t *testing.T) {
require.Error(t, err)
require.Contains(t, err.Error(), "error creating file: open invalid-directory/main.tf.json: no such file or directory")
}

func TestSetEnvironmentVariables(t *testing.T) {
testCase := []struct {
name string
opts Options
}{
{
name: "set environment variables",
opts: Options{
EnvConfig: &recipes.Configuration{
RecipeConfig: dm.RecipeConfigProperties{
EnvVars: dm.EnvironmentVariables{
AdditionalProperties: map[string]string{
"TEST_ENV_VAR1": "value1",
"TEST_ENV_VAR2": "value2",
},
},
},
},
},
},
{
name: "no environment variables",
opts: Options{
EnvConfig: &recipes.Configuration{
RecipeConfig: dm.RecipeConfigProperties{},
},
},
},
}
for _, tc := range testCase {
t.Run(tc.name, func(t *testing.T) {
ctx := testcontext.New(t)

// Get the environment variables to set
envVars := tc.opts.EnvConfig.RecipeConfig.EnvVars.AdditionalProperties

// Create an executor
e := executor{}

// Check that the environment variables are not set
for key, expectedValue := range envVars {
err := os.Unsetenv(key)
require.NoError(t, err)

// Call the function to set environment variables
err = e.setEnvironmentVariables(ctx, tc.opts.EnvConfig)
require.NoError(t, err)

actualValue, ok := os.LookupEnv(key)
require.True(t, ok)
require.Equal(t, expectedValue, actualValue)

// Ensure the environment variable is unset after the test execution
defer os.Unsetenv(key)
}
})
}
}

0 comments on commit a4fb7b2

Please sign in to comment.