Skip to content

Commit

Permalink
Merge branch 'main' into vishwahiremat/moving_get_output_resources_to…
Browse files Browse the repository at this point in the history
…_driver
  • Loading branch information
vishwahiremat committed Sep 19, 2023
2 parents 79e6d81 + 6d306a1 commit 0966dc9
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 42 deletions.
2 changes: 1 addition & 1 deletion pkg/cli/cmd/resource/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (r *Runner) Run(ctx context.Context) error {
} else {
_, err = client.ShowApplication(ctx, r.ApplicationName)
if clients.Is404Error(err) {
return clierrors.Message("The application %q could not be found in workspace %q.", r.ApplicationName, r.Workspace.Name)
return clierrors.Message("The application %q could not be found in workspace %q. Make sure you specify the correct application with '-a/--application' or switch applications with 'rad app switch'.", r.ApplicationName, r.Workspace.Name)
} else if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cli/cmd/resource/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func Test_Run(t *testing.T) {

err := runner.Run(context.Background())
require.Error(t, err)
require.IsType(t, err, clierrors.Message("The application %q could not be found in workspace %q.", "test-app", radcli.TestWorkspaceName))
require.IsType(t, err, clierrors.Message("The application %q could not be found in workspace %q. Make sure you specify the correct application with '-a/--application' or switch applications with 'rad app switch'.", "test-app", radcli.TestWorkspaceName))
})

t.Run("Success", func(t *testing.T) {
Expand Down
12 changes: 1 addition & 11 deletions pkg/recipes/terraform/config/backends/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,14 @@ func generateSecretSuffix(resourceRecipe *recipes.ResourceMetadata) (string, err
return "", err
}

prefix := fmt.Sprintf("%s-%s-%s", parsedEnvID.Name(), parsedAppID.Name(), parsedResourceID.Name())

// Kubernetes enforces a character limit of 63 characters on the suffix for state file stored in kubernetes secret.
// 22 = 63 (max length of Kubernetes secret suffix) - 40 (hex hash length) - 1 (dot separator)
maxResourceNameLen := 22
if len(prefix) >= maxResourceNameLen {
prefix = prefix[:maxResourceNameLen]
}

hasher := sha1.New()
_, err = hasher.Write([]byte(strings.ToLower(fmt.Sprintf("%s-%s-%s", parsedEnvID.Name(), parsedAppID.Name(), parsedResourceID.String()))))
if err != nil {
return "", err
}
hash := hasher.Sum(nil)

// example: env-app-redis.ec291e26078b7ea8a74abfac82530005a0ecbf15
return fmt.Sprintf("%s.%x", prefix, hash), nil
return fmt.Sprintf("%x", hash), nil
}

// generateKubernetesBackendConfig returns Terraform backend configuration to store Terraform state file for the deployment.
Expand Down
46 changes: 32 additions & 14 deletions pkg/recipes/terraform/config/backends/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ const (
testTemplatePath = "Azure/redis/azurerm"
testRecipeName = "redis-azure"
testTemplateVersion = "1.1.0"
envName = "env"
appName = "app"
resourceName = "redis"
)

var (
Expand Down Expand Up @@ -82,28 +85,43 @@ func Test_GenerateKubernetesBackendConfig(t *testing.T) {
require.Equal(t, expectedConfig, actualConfig)
}

func Test_GenerateKubernetesBackendConfig_Error(t *testing.T) {
t.Setenv("KUBERNETES_SERVICE_HOST", "testvalue")
t.Setenv("KUBERNETES_SERVICE_PORT", "1111")

backend, err := generateKubernetesBackendConfig("test-suffix")
require.Error(t, err)
require.Nil(t, backend)
}

func Test_GenerateSecretSuffix(t *testing.T) {
_, resourceRecipe := getTestInputs()
hasher := sha1.New()
_, err := hasher.Write([]byte(strings.ToLower(fmt.Sprintf("%s-%s-%s", envName, appName, resourceRecipe.ResourceID))))
require.NoError(t, err)
expSecret := fmt.Sprintf("%x", hasher.Sum(nil))
secret, err := generateSecretSuffix(&resourceRecipe)
require.NoError(t, err)
require.Equal(t, expSecret, secret)
}

func Test_GenerateSecretSuffix_invalid_resourceid(t *testing.T) {
_, resourceRecipe := getTestInputs()
resourceRecipe.ResourceID = "invalid"
_, err := generateSecretSuffix(&resourceRecipe)
require.Equal(t, err.Error(), "'invalid' is not a valid resource id")
}

func Test_GenerateSecretSuffix_with_lengthy_resource_name(t *testing.T) {
func Test_GenerateSecretSuffix_invalid_envid(t *testing.T) {
_, resourceRecipe := getTestInputs()
act, err := generateSecretSuffix(&resourceRecipe)
require.NoError(t, err)
hasher := sha1.New()
_, _ = hasher.Write([]byte(strings.ToLower("env-app-" + resourceRecipe.ResourceID)))
hash := hasher.Sum(nil)
require.Equal(t, act, "env-app-redis."+fmt.Sprintf("%x", hash))
resourceRecipe.EnvironmentID = "invalid"
_, err := generateSecretSuffix(&resourceRecipe)
require.Equal(t, err.Error(), "'invalid' is not a valid resource id")
}

func Test_GenerateKubernetesBackendConfig_Error(t *testing.T) {
t.Setenv("KUBERNETES_SERVICE_HOST", "testvalue")
t.Setenv("KUBERNETES_SERVICE_PORT", "1111")

backend, err := generateKubernetesBackendConfig("test-suffix")
require.Error(t, err)
require.Nil(t, backend)
func Test_GenerateSecretSuffix_invalid_appid(t *testing.T) {
_, resourceRecipe := getTestInputs()
resourceRecipe.ApplicationID = "invalid"
_, err := generateSecretSuffix(&resourceRecipe)
require.Equal(t, err.Error(), "'invalid' is not a valid resource id")
}
28 changes: 13 additions & 15 deletions test/functional/shared/resources/recipe_terraform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ package resource_test

import (
"context"
"crypto/sha1"
"encoding/base64"
"encoding/json"
"fmt"
Expand All @@ -39,6 +38,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/radius-project/radius/pkg/recipes"
"github.com/radius-project/radius/pkg/recipes/terraform/config/backends"
"github.com/radius-project/radius/pkg/ucp/resources"
resources_radius "github.com/radius-project/radius/pkg/ucp/resources/radius"
"github.com/radius-project/radius/test/functional"
Expand Down Expand Up @@ -382,24 +382,22 @@ func testSecretDeletion(t *testing.T, ctx context.Context, test shared.RPTest, a
}

func getSecretSuffix(resourceID, envName, appName string) (string, error) {
parsedResourceID, err := resources.Parse(resourceID)
if err != nil {
return "", err
}

prefix := fmt.Sprintf("%s-%s-%s", envName, appName, parsedResourceID.Name())
maxResourceNameLen := 22
if len(prefix) >= maxResourceNameLen {
prefix = prefix[:maxResourceNameLen]
envID := "/planes/radius/local/resourcegroups/kind-radius/providers/Applications.Core/environments/" + envName
appID := "/planes/radius/local/resourcegroups/kind-radius/providers/Applications.Core/applications/" + appName

resourceRecipe := recipes.ResourceMetadata{
EnvironmentID: envID,
ApplicationID: appID,
ResourceID: resourceID,
Parameters: nil,
}

hasher := sha1.New()
_, err = hasher.Write([]byte(strings.ToLower(fmt.Sprintf("%s-%s-%s", envName, appName, parsedResourceID.String()))))
backend := backends.NewKubernetesBackend()
secretMap, err := backend.BuildBackend(&resourceRecipe)
if err != nil {
return "", err
}
hash := hasher.Sum(nil)
kubernetes := secretMap["kubernetes"].(map[string]any)

// example: env-app-redis.ec291e26078b7ea8a74abfac82530005a0ecbf15
return fmt.Sprintf("%s.%x", prefix, hash), nil
return kubernetes["secret_suffix"].(string), nil
}

0 comments on commit 0966dc9

Please sign in to comment.