diff --git a/pkg/workload/v1/collection.go b/pkg/workload/v1/collection.go index e32182d..2035ecb 100644 --- a/pkg/workload/v1/collection.go +++ b/pkg/workload/v1/collection.go @@ -132,12 +132,33 @@ func (c WorkloadCollection) IsCollection() bool { } func (c *WorkloadCollection) SetSpecFields(workloadPath string) error { - - apiSpecFields, err := processCollectionMarkers(workloadPath, c.Spec.Components) - if err != nil { - return err + var specFields []APISpecField + + for _, component := range c.Spec.Components { + componentSpecFields, err := processMarkers( + component.Spec.ConfigPath, + component.Spec.Resources, + true, + ) + if err != nil { + return err + } + + // add to spec fields if not present + for _, csf := range *componentSpecFields { + fieldPresent := false + for _, sf := range specFields { + if csf == sf { + fieldPresent = true + } + } + if !fieldPresent { + specFields = append(specFields, csf) + } + } } - c.Spec.APISpecFields = *apiSpecFields + + c.Spec.APISpecFields = specFields return nil } diff --git a/pkg/workload/v1/component.go b/pkg/workload/v1/component.go index 9dbc072..4f5ba12 100644 --- a/pkg/workload/v1/component.go +++ b/pkg/workload/v1/component.go @@ -100,7 +100,7 @@ func (c ComponentWorkload) IsCollection() bool { } func (c *ComponentWorkload) SetSpecFields(workloadPath string) error { - apiSpecFields, err := processMarkers(workloadPath, workloadMarkerStr, c.Spec.Resources) + apiSpecFields, err := processMarkers(workloadPath, c.Spec.Resources, false) if err != nil { return err } diff --git a/pkg/workload/v1/markers.go b/pkg/workload/v1/markers.go index b91a21e..9679f0d 100644 --- a/pkg/workload/v1/markers.go +++ b/pkg/workload/v1/markers.go @@ -8,10 +8,7 @@ import ( "strings" ) -const ( - workloadMarkerStr = "+workload" - collectionMarkerStr = "+collection" -) +const markerStr = "+workload" // SupportedMarkerDataTypes returns the supported data types that can be used in // workload markers @@ -19,38 +16,7 @@ func SupportedMarkerDataTypes() []string { return []string{"bool", "string", "int", "int32", "int64", "float32", "float64"} } -func processCollectionMarkers(workloadPath string, components []ComponentWorkload) (*[]APISpecField, error) { - - var specFields []APISpecField - - for _, component := range components { - componentSpecFields, err := processMarkers( - component.Spec.ConfigPath, - collectionMarkerStr, - component.Spec.Resources, - ) - if err != nil { - return nil, err - } - - // add to spec fields if not present - for _, csf := range *componentSpecFields { - fieldPresent := false - for _, sf := range specFields { - if csf == sf { - fieldPresent = true - } - } - if !fieldPresent { - specFields = append(specFields, csf) - } - } - } - - return &specFields, nil -} - -func processMarkers(workloadPath, markerStr string, resources []string) (*[]APISpecField, error) { +func processMarkers(workloadPath string, resources []string, collection bool) (*[]APISpecField, error) { var specFields []APISpecField for _, manifestFile := range resources { @@ -62,7 +28,7 @@ func processMarkers(workloadPath, markerStr string, resources []string) (*[]APIS } // extract all workload markers from yaml content - markers, err := processManifest(string(manifestContent), markerStr) + markers, err := processManifest(string(manifestContent)) if err != nil { return nil, err } @@ -75,6 +41,14 @@ func processMarkers(workloadPath, markerStr string, resources []string) (*[]APIS } } + // only include collection markers if processing for a collection + // and vise versa + if collection && !m.Collection { + continue MARKERS + } else if !collection && m.Collection { + continue MARKERS + } + var specField APISpecField specField.FieldName = strings.Title(m.FieldName) specField.ManifestFieldName = m.FieldName @@ -105,12 +79,12 @@ func processMarkers(workloadPath, markerStr string, resources []string) (*[]APIS return &specFields, nil } -func processManifest(manifest, markerStr string) ([]Marker, error) { +func processManifest(manifest string) ([]Marker, error) { var markers []Marker lines := strings.Split(string(manifest), "\n") for _, line := range lines { - if containsMarker(line, markerStr) { - marker, err := processMarker(line, markerStr) + if containsMarker(line) { + marker, err := processMarkerLine(line) if err != nil { return nil, err } @@ -121,7 +95,7 @@ func processManifest(manifest, markerStr string) ([]Marker, error) { return markers, nil } -func processMarkedComments(line, markerStr string) (processed string) { +func processMarkedComments(line string) (processed string) { codeCommentSplit := strings.Split(line, "//") code := codeCommentSplit[0] comment := codeCommentSplit[1] @@ -129,10 +103,10 @@ func processMarkedComments(line, markerStr string) (processed string) { fieldName := commentSplit[1] var fieldPath string - if markerStr == workloadMarkerStr { - fieldPath = fmt.Sprintf("parent.Spec.%s", strings.Title(fieldName)) - } else if markerStr == collectionMarkerStr { + if strings.Contains(line, "collection=true") { fieldPath = fmt.Sprintf("collection.Spec.%s", strings.Title(fieldName)) + } else { + fieldPath = fmt.Sprintf("parent.Spec.%s", strings.Title(fieldName)) } if strings.Contains(code, ":") { @@ -146,7 +120,7 @@ func processMarkedComments(line, markerStr string) (processed string) { return processed } -func processMarker(line, markerStr string) (Marker, error) { +func processMarkerLine(line string) (Marker, error) { var marker Marker // count leading spaces @@ -202,6 +176,18 @@ func processMarker(line, markerStr string) (Marker, error) { marker.DataType = strings.Split(element, "=")[1] } else if strings.Contains(element, "default=") { marker.Default = strings.Split(element, "=")[1] + } else if strings.Contains(element, "collection=") { + collectionVal := strings.Split(element, "=")[1] + switch collectionVal { + case "true": + marker.Collection = true + case "false": + marker.Collection = false + default: + msg := fmt.Sprintf("collection value %s found - must be either 'true' or 'false'", collectionVal) + return marker, errors.New(msg) + } + //marker.Collection = strings.Split(element, "=")[1] } else { marker.FieldName = element } @@ -225,7 +211,7 @@ func zeroValue(val interface{}) (string, error) { } } -func containsMarker(line, markerStr string) bool { +func containsMarker(line string) bool { return strings.Contains(line, markerStr) } diff --git a/pkg/workload/v1/resource.go b/pkg/workload/v1/resource.go index 041c1a2..dca589a 100644 --- a/pkg/workload/v1/resource.go +++ b/pkg/workload/v1/resource.go @@ -181,11 +181,8 @@ func extractManifests(manifestContent []byte) []string { func addVariables(resourceContent string) (string, error) { lines := strings.Split(string(resourceContent), "\n") for i, line := range lines { - if containsMarker(line, workloadMarkerStr) { - markedLine := processMarkedComments(line, workloadMarkerStr) - lines[i] = markedLine - } else if containsMarker(line, collectionMarkerStr) { - markedLine := processMarkedComments(line, collectionMarkerStr) + if containsMarker(line) { + markedLine := processMarkedComments(line) lines[i] = markedLine } } @@ -206,8 +203,8 @@ func groupResourceRecorded(rbacRules *[]RBACRule, newRBACRule *RBACRule) bool { func addTemplating(rawContent string) (string, error) { lines := strings.Split(string(rawContent), "\n") for i, line := range lines { - if containsMarker(line, workloadMarkerStr) { - marker, err := processMarker(line, workloadMarkerStr) + if containsMarker(line) { + marker, err := processMarkerLine(line) if err != nil { return "", err } diff --git a/pkg/workload/v1/standalone.go b/pkg/workload/v1/standalone.go index 124f829..fe311fc 100644 --- a/pkg/workload/v1/standalone.go +++ b/pkg/workload/v1/standalone.go @@ -132,7 +132,7 @@ func (s StandaloneWorkload) IsCollection() bool { } func (s *StandaloneWorkload) SetSpecFields(workloadPath string) error { - apiSpecFields, err := processMarkers(workloadPath, workloadMarkerStr, s.Spec.Resources) + apiSpecFields, err := processMarkers(workloadPath, s.Spec.Resources, false) if err != nil { return err } diff --git a/pkg/workload/v1/types.go b/pkg/workload/v1/types.go index 5268f70..80a8b72 100644 --- a/pkg/workload/v1/types.go +++ b/pkg/workload/v1/types.go @@ -126,6 +126,7 @@ type Marker struct { DataType string Default string LeadingSpaces int + Collection bool } // RBACRule contains the info needed to create the kubebuilder:rbac markers in diff --git a/test/platform.sh b/test/platform.sh index 17f0271..3ae7043 100755 --- a/test/platform.sh +++ b/test/platform.sh @@ -327,7 +327,7 @@ kind: Namespace metadata: name: ingress-system # +workload:namespace:default=ingress-system:type=string labels: - workload-collection: default-collection #+collection:collectionLabel:type=string + workload-collection: default-collection #+workload:collectionLabel:type=string:collection=true EOF cat > .test/ingress/contour-config.yaml <