Skip to content

Commit

Permalink
fix: Fixes #18, added ability to use parent resource name in child re…
Browse files Browse the repository at this point in the history
…source

Signed-off-by: Dustin Scott <dustin.scott18@gmail.com>
  • Loading branch information
scottd018 committed Jun 14, 2022
1 parent 43f02c5 commit dc098a7
Show file tree
Hide file tree
Showing 10 changed files with 282 additions and 88 deletions.
9 changes: 8 additions & 1 deletion docs/markers.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,21 @@ field for your workload.
| [replace](#replace-optional) | string | false |
| [description](#description-optional) | string | false |

### Name (required)
### Name (required if Parent is unspecified)

The name you want to use for the field in the custom resource that
Operator Builder will create. If you're not sure what that means, it will
become clear shortly.

ex. +operator-builder:field:name=myName

### Parent (required if Name is unspecified)

The parent field in which you wish to substitute. Currently, only `metadata.name` is supported. This
will allow you to use the parent name as a value in the child resource.

ex. +operator-builder:field:parent=metadata.name

### Type (required)

The other required field is the `type` field which specifies the data type for
Expand Down
18 changes: 10 additions & 8 deletions internal/workload/v1/kinds/workload.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,14 +364,16 @@ func (ws *WorkloadSpec) processMarkerResults(markerResults []*inspect.YAMLResult
}

// add the field to the api specification
if err := ws.APISpecFields.AddField(
marker.GetName(),
marker.GetFieldType(),
comments,
sampleVal,
defaultFound,
); err != nil {
return err
if marker.GetName() != "" {
if err := ws.APISpecFields.AddField(
marker.GetName(),
marker.GetFieldType(),
comments,
sampleVal,
defaultFound,
); err != nil {
return err
}
}

marker.SetForCollection(ws.ForCollection)
Expand Down
21 changes: 19 additions & 2 deletions internal/workload/v1/markers/collection_field_marker.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

const (
CollectionFieldMarkerPrefix = "+operator-builder:collection:field"
CollectionFieldPrefix = "collection"
CollectionFieldSpecPrefix = "collection.Spec"
)

Expand All @@ -24,7 +25,7 @@ type CollectionFieldMarker FieldMarker
//nolint:gocritic //needed to implement string interface
func (cfm CollectionFieldMarker) String() string {
return fmt.Sprintf("CollectionFieldMarker{Name: %s Type: %v Description: %q Default: %v}",
cfm.Name,
cfm.GetName(),
cfm.Type,
cfm.GetDescription(),
cfm.Default,
Expand All @@ -47,7 +48,11 @@ func defineCollectionFieldMarker(registry *marker.Registry) error {
// FieldMarkerProcessor interface methods.
//
func (cfm *CollectionFieldMarker) GetName() string {
return cfm.Name
if cfm.Name == nil {
return ""
}

return *cfm.Name
}

func (cfm *CollectionFieldMarker) GetDefault() interface{} {
Expand All @@ -74,6 +79,10 @@ func (cfm *CollectionFieldMarker) GetReplaceText() string {
return *cfm.Replace
}

func (cfm *CollectionFieldMarker) GetPrefix() string {
return CollectionFieldPrefix
}

func (cfm *CollectionFieldMarker) GetSpecPrefix() string {
return CollectionFieldSpecPrefix
}
Expand All @@ -86,6 +95,14 @@ func (cfm *CollectionFieldMarker) GetOriginalValue() interface{} {
return cfm.originalValue
}

func (cfm *CollectionFieldMarker) GetParent() string {
if cfm.Parent == nil {
return ""
}

return *cfm.Parent
}

func (cfm *CollectionFieldMarker) IsCollectionFieldMarker() bool {
return true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ func TestCollectionFieldMarker_String(t *testing.T) {
t.Parallel()

testString := "cfm test"
testName := "test"

type fields struct {
Name string
Name *string
Type FieldType
Description *string
Default interface{}
Expand All @@ -31,7 +32,7 @@ func TestCollectionFieldMarker_String(t *testing.T) {
{
name: "ensure collection field string output matches expected",
fields: fields{
Name: "test",
Name: &testName,
Type: FieldString,
Description: &testString,
Default: "test",
Expand All @@ -41,7 +42,7 @@ func TestCollectionFieldMarker_String(t *testing.T) {
{
name: "ensure collection field with nil values output matches expected",
fields: fields{
Name: "test",
Name: &testName,
Type: FieldString,
Description: nil,
Default: "test",
Expand Down Expand Up @@ -143,11 +144,14 @@ func TestCollectionFieldMarker_GetDefault(t *testing.T) {
}
}

func TestCollectionFieldMarker_GetDescription(t *testing.T) {
func TestCollectionFieldMarker_GetName(t *testing.T) {
t.Parallel()

name := "test"
emptyName := ""

type fields struct {
Name string
Name *string
}

tests := []struct {
Expand All @@ -158,14 +162,14 @@ func TestCollectionFieldMarker_GetDescription(t *testing.T) {
{
name: "ensure collection field name returns as expected",
fields: fields{
Name: "test",
Name: &name,
},
want: "test",
},
{
name: "ensure collection field name with empty value returns as expected",
fields: fields{
Name: "",
Name: &emptyName,
},
want: "",
},
Expand All @@ -185,7 +189,7 @@ func TestCollectionFieldMarker_GetDescription(t *testing.T) {
}
}

func TestCollectionFieldMarker_GetName(t *testing.T) {
func TestCollectionFieldMarker_GetDescription(t *testing.T) {
t.Parallel()

cfmDescription := "test collection description"
Expand Down
24 changes: 21 additions & 3 deletions internal/workload/v1/markers/field_marker.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var (

const (
FieldMarkerPrefix = "+operator-builder:field"
FieldPrefix = "parent"
FieldSpecPrefix = "parent.Spec"
)

Expand All @@ -25,11 +26,12 @@ const (
// and matches the constants defined by the fieldMarker constant above.
type FieldMarker struct {
// inputs from the marker itself
Name string
Name *string
Type FieldType
Description *string
Default interface{} `marker:",optional"`
Replace *string
Parent *string

// other values which we use to pass information
forCollection bool
Expand All @@ -40,7 +42,7 @@ type FieldMarker struct {
//nolint:gocritic //needed to implement string interface
func (fm FieldMarker) String() string {
return fmt.Sprintf("FieldMarker{Name: %s Type: %v Description: %q Default: %v}",
fm.Name,
fm.GetName(),
fm.Type,
fm.GetDescription(),
fm.Default,
Expand All @@ -63,7 +65,11 @@ func defineFieldMarker(registry *marker.Registry) error {
// FieldMarker Processor interface methods.
//
func (fm *FieldMarker) GetName() string {
return fm.Name
if fm.Name == nil {
return ""
}

return *fm.Name
}

func (fm *FieldMarker) GetDefault() interface{} {
Expand All @@ -90,6 +96,10 @@ func (fm *FieldMarker) GetReplaceText() string {
return *fm.Replace
}

func (fm *FieldMarker) GetPrefix() string {
return FieldPrefix
}

func (fm *FieldMarker) GetSpecPrefix() string {
return FieldSpecPrefix
}
Expand All @@ -98,6 +108,14 @@ func (fm *FieldMarker) GetOriginalValue() interface{} {
return fm.originalValue
}

func (fm *FieldMarker) GetParent() string {
if fm.Parent == nil {
return ""
}

return *fm.Parent
}

func (fm *FieldMarker) GetSourceCodeVariable() string {
return fm.sourceCodeVar
}
Expand Down
20 changes: 12 additions & 8 deletions internal/workload/v1/markers/field_marker_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ import (
func TestFieldMarker_String(t *testing.T) {
t.Parallel()

testName := "test"
testString := "fm test"

type fields struct {
Name string
Name *string
Type FieldType
Description *string
Default interface{}
Expand All @@ -31,7 +32,7 @@ func TestFieldMarker_String(t *testing.T) {
{
name: "ensure field string output matches expected",
fields: fields{
Name: "test",
Name: &testName,
Type: FieldString,
Description: &testString,
Default: "test",
Expand All @@ -41,7 +42,7 @@ func TestFieldMarker_String(t *testing.T) {
{
name: "ensure field with nil values output matches expected",
fields: fields{
Name: "test",
Name: &testName,
Type: FieldString,
Description: nil,
Default: "test",
Expand Down Expand Up @@ -143,11 +144,14 @@ func TestFieldMarker_GetDefault(t *testing.T) {
}
}

func TestFieldMarker_GetDescription(t *testing.T) {
func TestFieldMarker_GetName(t *testing.T) {
t.Parallel()

name := "test"
emptyName := ""

type fields struct {
Name string
Name *string
}

tests := []struct {
Expand All @@ -158,14 +162,14 @@ func TestFieldMarker_GetDescription(t *testing.T) {
{
name: "ensure field name returns as expected",
fields: fields{
Name: "test",
Name: &name,
},
want: "test",
},
{
name: "ensure field name with empty value returns as expected",
fields: fields{
Name: "",
Name: &emptyName,
},
want: "",
},
Expand All @@ -185,7 +189,7 @@ func TestFieldMarker_GetDescription(t *testing.T) {
}
}

func TestFieldMarker_GetName(t *testing.T) {
func TestFieldMarker_GetDescription(t *testing.T) {
t.Parallel()

fmDescription := "test description"
Expand Down
Loading

0 comments on commit dc098a7

Please sign in to comment.