Skip to content

Commit

Permalink
Resolving some PR conversations, low hanging fruit
Browse files Browse the repository at this point in the history
Signed-off-by: Nick Beenham <[email protected]>
  • Loading branch information
superbeeny committed Jul 19, 2024
1 parent 8ccb477 commit 53fdafa
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 12 deletions.
6 changes: 4 additions & 2 deletions pkg/corerp/api/v20231001preview/container_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,11 @@ func (src *ContainerResource) ConvertTo() (v1.DataModelInterface, error) {
}
}

convertedEnvironmentVariables, err := toEnvDataModel(src.Properties.Container.Env)
convertedEnvironmentVariables, err := toEnvironmentVariableDataModel(src.Properties.Container.Env)
if err != nil {
return nil, err
}

converted := &datamodel.ContainerResource{
BaseResource: v1.BaseResource{
TrackedResource: v1.TrackedResource{
Expand Down Expand Up @@ -157,14 +158,15 @@ func (src *ContainerResource) ConvertTo() (v1.DataModelInterface, error) {
}

// toEnvDataModel: Converts from versioned datamodel to base datamodel
func toEnvDataModel(e map[string]*EnvironmentVariable) (map[string]datamodel.EnvironmentVariable, error) {
func toEnvironmentVariableDataModel(e map[string]*EnvironmentVariable) (map[string]datamodel.EnvironmentVariable, error) {

m := map[string]datamodel.EnvironmentVariable{}

for key, val := range e {
if val == nil {
return nil, v1.NewClientErrInvalidRequest(fmt.Sprintf("Environment variable %s is nil", key))
}
// An environment variable can have either value(Value) or secret value(ValueFrom), but not both
if val.Value != nil && val.ValueFrom != nil {
return nil, v1.NewClientErrInvalidRequest(fmt.Sprintf("Environment variable %s has both value and secret value", key))
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/corerp/api/v20231001preview/zz_generated_models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions pkg/corerp/renderers/container/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,13 @@ func (r Renderer) GetDependencyIDs(ctx context.Context, dm v1.DataModelInterface
}
}

for _, envvars := range properties.Container.Env {
if envvars.ValueFrom != nil && envvars.ValueFrom.SecretRef != nil {
if strings.HasPrefix(envvars.ValueFrom.SecretRef.Source, "/") {
resourceID, err := resources.ParseResource(envvars.ValueFrom.SecretRef.Source)
// Environment variables can be sourced from secrets, which are resources. We need to iterate over the environment to handle any possible instances.
for _, envVars := range properties.Container.Env {
if envVars.ValueFrom != nil && envVars.ValueFrom.SecretRef != nil {
if strings.HasPrefix(envVars.ValueFrom.SecretRef.Source, "/") {
resourceID, err := resources.ParseResource(envVars.ValueFrom.SecretRef.Source)
if err != nil {
return nil, nil, v1.NewClientErrInvalidRequest(fmt.Sprintf("invalid source: %s. Must be either a kubernetes secret name or a valid resourceID", envvars.ValueFrom.SecretRef.Source))
return nil, nil, v1.NewClientErrInvalidRequest(fmt.Sprintf("invalid source: %s. Must be either a kubernetes secret name or a valid resourceID", envVars.ValueFrom.SecretRef.Source))
}
if resources_radius.IsRadiusResource(resourceID) {
radiusResourceIDs = append(radiusResourceIDs, resourceID)
Expand Down
5 changes: 5 additions & 0 deletions pkg/to/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ func TestString(t *testing.T) {
require.Exactly(t, v, *Ptr(v))
}

func TestStringPtr(t *testing.T) {
v := "foo"
require.Exactly(t, v, *StringPtr(v))
}

func TestStringSlice(t *testing.T) {
v := []string{}
require.Exactly(t, v, StringSlice(&v))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3322,7 +3322,7 @@
},
"EnvironmentVariable": {
"type": "object",
"description": "Envinronment variables type",
"description": "Environment variables type",
"properties": {
"value": {
"type": "string",
Expand Down Expand Up @@ -3391,7 +3391,7 @@
},
"EnvironmentVariableUpdate": {
"type": "object",
"description": "Envinronment variables type",
"description": "Environment variables type",
"properties": {
"value": {
"type": "string",
Expand Down
2 changes: 1 addition & 1 deletion typespec/Applications.Core/containers.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ model Container {
workingDir?: string;
}

@doc("Envinronment variables type")
@doc("Environment variables type")
model EnvironmentVariable {
@doc("The value of the environment variable")
value?: string;
Expand Down

0 comments on commit 53fdafa

Please sign in to comment.