Skip to content

Commit

Permalink
Merge pull request #51 from nukleros/fix-project
Browse files Browse the repository at this point in the history
fix: Fixes #49, inject resource info to update PROJECT file
  • Loading branch information
lander2k2 authored Jul 25, 2022
2 parents 61d21ea + 8ba2cf3 commit d515dc2
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 20 deletions.
18 changes: 13 additions & 5 deletions internal/plugins/workload/v1/scaffolds/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ func (s *apiScaffolder) scaffoldWorkload(
scaffold *machinery.Scaffold,
workload kinds.WorkloadBuilder,
) error {
componentResource := workload.GetComponentResource(
s.config.GetDomain(),
s.config.GetRepository(),
workload.IsClusterScoped(),
)

// override the scaffold if we have a component. this will allow the Resource
// attribute of the scaffolder to be set appropriately so that things like Group,
// Version, and Kind are passed from the child component and not the parent
Expand All @@ -119,14 +125,16 @@ func (s *apiScaffolder) scaffoldWorkload(
scaffold = machinery.NewScaffold(s.fs,
machinery.WithConfig(s.config),
machinery.WithBoilerplate(s.boilerplate),
machinery.WithResource(workload.GetComponentResource(
s.config.GetDomain(),
s.config.GetRepository(),
workload.IsClusterScoped(),
)),
machinery.WithResource(componentResource),
)
}

// inject the resource as this resource so that our PROJECT file is up to date for each
// resource that we loop through
if err := s.config.UpdateResource(*componentResource); err != nil {
return fmt.Errorf("%w; error updating resource", err)
}

// scaffold the workload api. this generates files within the apis/ folder to include
// items such as common resource methods, api type definitions and child resource typed
// object definitions.
Expand Down
21 changes: 11 additions & 10 deletions internal/workload/v1/kinds/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ type APIFields struct {
Children []*APIFields
Default string
Sample string
Last bool
}

func (api *APIFields) AddField(path string, fieldType markers.FieldType, comments []string, sample interface{}, hasDefault bool) error {
Expand Down Expand Up @@ -70,7 +69,6 @@ func (api *APIFields) AddField(path string, fieldType markers.FieldType, comment
}

newChild := obj.newChild(last, fieldType, sample)
newChild.Last = true

newChild.setCommentsAndDefault(comments, sample, hasDefault)

Expand Down Expand Up @@ -265,22 +263,25 @@ func (api *APIFields) setSample(sampleVal interface{}) {

func (api *APIFields) setDefault(sampleVal interface{}) {
api.Default = api.getSampleValue(sampleVal)
api.appendMarkers(
fmt.Sprintf("+kubebuilder:default=%s", api.Default),
"+kubebuilder:validation:Optional",
fmt.Sprintf("(Default: %s)", api.Default),
)
api.setSample(sampleVal)
}

func (api *APIFields) appendMarkers(apiMarkers ...string) {
if len(api.Markers) == 0 {
api.Markers = append(
api.Markers,
fmt.Sprintf("+kubebuilder:default=%s", api.Default),
"+kubebuilder:validation:Optional",
fmt.Sprintf("(Default: %s)", api.Default),
)
api.Markers = append(api.Markers, apiMarkers...)
}

api.setSample(sampleVal)
}

func (api *APIFields) setCommentsAndDefault(comments []string, sampleVal interface{}, hasDefault bool) {
if hasDefault {
api.setDefault(sampleVal)
} else {
api.appendMarkers("+kubebuilder:validation:Required")
}

if comments != nil {
Expand Down
4 changes: 3 additions & 1 deletion internal/workload/v1/kinds/api_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,6 @@ func TestAPIFields_getSampleValue(t *testing.T) {
Children: tt.fields.Children,
Default: tt.fields.Default,
Sample: tt.fields.Sample,
Last: tt.fields.Last,
}
if got := api.getSampleValue(tt.args.sampleVal); got != tt.want {
t.Errorf("APIFields.getSampleValue() = %v, want %v", got, tt.want)
Expand Down Expand Up @@ -683,6 +682,9 @@ func TestAPIFields_setCommentsAndDefault(t *testing.T) {
},
expect: &APIFields{
manifestName: "other",
Markers: []string{
"+kubebuilder:validation:Required",
},
},
},
}
Expand Down
4 changes: 2 additions & 2 deletions internal/workload/v1/kinds/workload.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,15 +347,15 @@ func (ws *WorkloadSpec) processMarkers(manifestFile *manifests.Manifest, markerT
}

func (ws *WorkloadSpec) processMarkerResults(markerResults []*inspect.YAMLResult) error {
for _, markerResult := range markerResults {
for i := range markerResults {
var defaultFound bool

var sampleVal interface{}

// convert to interface
var marker markers.FieldMarkerProcessor

switch t := markerResult.Object.(type) {
switch t := markerResults[i].Object.(type) {
case *markers.FieldMarker:
marker = t
ws.FieldMarkers = append(ws.FieldMarkers, t)
Expand Down
4 changes: 2 additions & 2 deletions internal/workload/v1/markers/markers.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ func initializeMarkerInspector(markerTypes ...MarkerType) (*inspect.Inspector, e

var err error

for _, markerType := range markerTypes {
switch markerType {
for i := range markerTypes {
switch markerTypes[i] {
case FieldMarkerType:
err = defineFieldMarker(registry)
case CollectionMarkerType:
Expand Down

0 comments on commit d515dc2

Please sign in to comment.