Skip to content

Commit

Permalink
Handle nil values in Hardware spec when templating:
Browse files Browse the repository at this point in the history
If a value used in the template contract is nil,
handle properly.

Signed-off-by: Jacob Weinstock <[email protected]>
  • Loading branch information
jacobweinstock committed Apr 6, 2024
1 parent 7a3238d commit 478d9ce
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion docs/Template.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ There are a number of built in functions that Go provides and that can be used i
| Function Name | Description | Use | Examples |
| ------------- | ----------- | ------- | -------- |
| `contains` | Contains returns a bool for whether `substr` is within `s`. | `{{ contains "HELLO" "H" }}` | `contains <s> <substr>` |
| `contains` | contains returns a bool for whether `substr` is within `s`. | `{{ contains "HELLO" "H" }}` | `contains <s> <substr>` |
| `hasPrefix` | hasPrefix returns a bool for whether the string s begins with prefix. | `{{ hasPrefix "HELLO" "HE" }}` | `hasPrefix <s> <prefix>` |
| `hasSuffix` | hasSuffix returns a bool for whether the string s ends with suffix. | `{{ hasPrefix "HELLO" "HE" }}` | `hasSuffix <s> <suffix>` |
| `formatPartition` | formatPartition formats a device path with partition for the specific device type. Supported devices: `/dev/nvme`, `/dev/sd`, `/dev/vd`, `/dev/xvd`, `/dev/hd`. | `{{ formatPartition ( index .Hardware.Disks 0 ) 2 }}` | `formatPartition("/dev/nvme0n1", 0) -> /dev/nvme0n1p1`, `formatPartition("/dev/sda", 1) -> /dev/sda1` |
16 changes: 12 additions & 4 deletions internal/deprecated/workflow/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,18 @@ func toTemplateHardwareData(hardware v1alpha1.Hardware) templateHardwareData {
for _, disk := range hardware.Spec.Disks {
contract.Disks = append(contract.Disks, disk.Device)
}
contract.Interfaces = hardware.Spec.Interfaces
contract.UserData = ptr.StringValue(hardware.Spec.UserData)
contract.Metadata = *hardware.Spec.Metadata
contract.VendorData = ptr.StringValue(hardware.Spec.VendorData)
if len(hardware.Spec.Interfaces) > 0 {
contract.Interfaces = hardware.Spec.Interfaces
}
if hardware.Spec.UserData != nil {
contract.UserData = ptr.StringValue(hardware.Spec.UserData)
}
if hardware.Spec.Metadata != nil {
contract.Metadata = *hardware.Spec.Metadata
}
if hardware.Spec.VendorData != nil {
contract.VendorData = ptr.StringValue(hardware.Spec.VendorData)
}
return contract
}

Expand Down
5 changes: 3 additions & 2 deletions internal/deprecated/workflow/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package workflow
import (
"context"
"errors"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -347,7 +348,7 @@ tasks:
},
},
},
wantErr: errors.New("parsing yaml data: yaml: line 2: found character that cannot start any token"),
wantErr: errors.New("found character that cannot start any token"),
},
{
name: "MissingTemplate",
Expand Down Expand Up @@ -821,7 +822,7 @@ tasks:
if gotErr != nil {
if tc.wantErr == nil {
t.Errorf(`Got unexpected error: %v"`, gotErr)
} else if gotErr.Error() != tc.wantErr.Error() {
} else if !strings.Contains(gotErr.Error(), tc.wantErr.Error()) {
t.Errorf(`Got unexpected error: got "%v" wanted "%v"`, gotErr, tc.wantErr)
}
return
Expand Down

0 comments on commit 478d9ce

Please sign in to comment.