Skip to content

Commit

Permalink
Add collection field to workload markers (#28)
Browse files Browse the repository at this point in the history
Signed-off-by: Jeff Davis <[email protected]>
  • Loading branch information
Richard Lander authored and Jeff Davis committed Sep 10, 2021
1 parent f42bac3 commit c5f5e28
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 68 deletions.
31 changes: 26 additions & 5 deletions pkg/workload/v1/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/workload/v1/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
78 changes: 32 additions & 46 deletions pkg/workload/v1/markers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,15 @@ import (
"strings"
)

const (
workloadMarkerStr = "+workload"
collectionMarkerStr = "+collection"
)
const markerStr = "+workload"

// SupportedMarkerDataTypes returns the supported data types that can be used in
// workload markers
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 {
Expand All @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -121,18 +95,18 @@ 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]
commentSplit := strings.Split(comment, ":")
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, ":") {
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
}

Expand Down
11 changes: 4 additions & 7 deletions pkg/workload/v1/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/workload/v1/standalone.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions pkg/workload/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 6 additions & 8 deletions test/platform.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 <<EOF
Expand All @@ -336,8 +336,6 @@ kind: ConfigMap
metadata:
name: contour-configmap
namespace: ingress-system # +workload:namespace:default=ingress-system:type=string
labels:
workload-collection: default-collection #+collection:collectionLabel:type=string
data:
config.yaml: |
---
Expand All @@ -351,7 +349,7 @@ metadata:
name: contour-secret
namespace: 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
stringData:
some: secretstuff
EOF
Expand All @@ -363,7 +361,7 @@ metadata:
name: contour-deploy
namespace: 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
spec:
replicas: 2 # +workload:ContourReplicas:default=2:type=int
selector:
Expand All @@ -388,7 +386,7 @@ metadata:
name: contour-svc
namespace: 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
spec:
selector:
app: contour
Expand All @@ -404,7 +402,7 @@ kind: DaemonSet
metadata:
labels:
app.kubernetes.io/name: envoy
workload-collection: default-collection #+collection:collectionLabel:type=string
workload-collection: default-collection #+workload:collectionLabel:type=string:collection=true
name: envoy-ds
namespace: ingress-system # +workload:namespace:default=ingress-system:type=string
spec:
Expand All @@ -415,7 +413,7 @@ spec:
metadata:
labels:
app.kubernetes.io/name: envoy
workload-collection: default-collection #+collection:collectionLabel:type=string
workload-collection: default-collection #+workload:collectionLabel:type=string:collection=true
spec:
containers:
- name: envoy
Expand Down

0 comments on commit c5f5e28

Please sign in to comment.