Skip to content

Commit

Permalink
feat: add support for arbitrary field markers (#57)
Browse files Browse the repository at this point in the history
* feat: add support for arbitrary field markers

Arbitrary field markers allow custom resource fields to be added that do
not map directly to child resource values.  Arbitrary fields may be
referenced by resource markers or leveraged in custom mutation logic.

Signed-off-by: Rich Lander <lander2k2@protonmail.com>

* fix: fix lint errors

Signed-off-by: Rich Lander <lander2k2@protonmail.com>

* fix: fix operator-builder imports

Signed-off-by: Rich Lander <lander2k2@protonmail.com>

* test: update tests for field marker

Signed-off-by: Rich Lander <lander2k2@protonmail.com>

* test: update repo in CI config

Signed-off-by: Rich Lander <lander2k2@protonmail.com>

Signed-off-by: Rich Lander <lander2k2@protonmail.com>
  • Loading branch information
lander2k2 authored Oct 12, 2022
1 parent 5db1fac commit 4388aa9
Show file tree
Hide file tree
Showing 77 changed files with 266 additions and 162 deletions.
12 changes: 6 additions & 6 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ linters-settings:
lines: 100
statements: 50
gci:
local-prefixes: github.com/vmware-tanzu-labs/operator-builder
local-prefixes: github.com/nukleros/operator-builder
goconst:
min-len: 2
min-occurrences: 2
Expand All @@ -26,7 +26,7 @@ linters-settings:
gocyclo:
min-complexity: 15
goimports:
local-prefixes: github.com/vmware-tanzu-labs/operator-builder
local-prefixes: github.com/nukleros/operator-builder
golint:
min-confidence: 0
gomnd:
Expand All @@ -43,10 +43,10 @@ linters-settings:
settings:
printf:
funcs:
- (github.com/vmware-tanzu-labs/operator-builder/pkg/logutils.Log).Infof
- (github.com/vmware-tanzu-labs/operator-builder/logutils.Log).Warnf
- (github.com/vmware-tanzu-labs/operator-builder/pkg/logutils.Log).Errorf
- (github.com/vmware-tanzu-labs/operator-builder/pkg/logutils.Log).Fatalf
- (github.com/nukleros/operator-builder/pkg/logutils.Log).Infof
- (github.com/nukleros/operator-builder/logutils.Log).Warnf
- (github.com/nukleros/operator-builder/pkg/logutils.Log).Errorf
- (github.com/nukleros/operator-builder/pkg/logutils.Log).Fatalf
lll:
line-length: 140
maligned:
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[![Go Reference](https://pkg.go.dev/badge/github.com/vmware-tanzu-labs/operator-builder.svg)](https://pkg.go.dev/github.com/vmware-tanzu-labs/operator-builder)
[![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/vmware-tanzu-labs/operator-builder)](https://golang.org/)
[![Go Report Card](https://goreportcard.com/badge/github.com/vmware-tanzu-labs/operator-builder)](https://goreportcard.com/report/github.com/vmware-tanzu-labs/operator-builder)
[![GitHub](https://img.shields.io/github/license/vmware-tanzu-labs/operator-builder)](https://github.com/vmware-tanzu-labs/operator-builder/blob/main/LICENSE)[![GitHub release (latest by date)](https://img.shields.io/github/v/release/vmware-tanzu-labs/operator-builder)](https://github.com/vmware-tanzu-labs/operator-builder/releases)
[![Hombrew](https://img.shields.io/badge/dynamic/json.svg?url=https://raw.githubusercontent.com/vmware-tanzu-labs/homebrew-tap/master/Info/operator-builder.json&query=$.versions.stable&label=homebrew)](https://github.com/vmware-tanzu-labs/operator-builder/releases)
[![Go Reference](https://pkg.go.dev/badge/github.com/nukleros/operator-builder.svg)](https://pkg.go.dev/github.com/nukleros/operator-builder)
[![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/nukleros/operator-builder)](https://golang.org/)
[![Go Report Card](https://goreportcard.com/badge/github.com/nukleros/operator-builder)](https://goreportcard.com/report/github.com/nukleros/operator-builder)
[![GitHub](https://img.shields.io/github/license/nukleros/operator-builder)](https://github.com/nukleros/operator-builder/blob/main/LICENSE)[![GitHub release (latest by date)](https://img.shields.io/github/v/release/nukleros/operator-builder)](https://github.com/nukleros/operator-builder/releases)
[![Hombrew](https://img.shields.io/badge/dynamic/json.svg?url=https://raw.githubusercontent.com/nukleros/homebrew-tap/master/Info/operator-builder.json&query=$.versions.stable&label=homebrew)](https://github.com/nukleros/operator-builder/releases)
<!---[![Get it from the Snap Store](https://badgen.net/snapcraft/v/operator-builder)](https://snapcraft.io/operator-builder)-->
![Github Downloads (by Release)](https://img.shields.io/github/downloads/vmware-tanzu-labs/operator-builder/total.svg)
![Github Downloads (by Release)](https://img.shields.io/github/downloads/nukleros/operator-builder/total.svg)

# Operator Builder

Expand Down
2 changes: 1 addition & 1 deletion cmd/operator-builder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package main
import (
log "github.com/sirupsen/logrus"

"github.com/vmware-tanzu-labs/operator-builder/pkg/cli"
"github.com/nukleros/operator-builder/pkg/cli"
)

func main() {
Expand Down
76 changes: 73 additions & 3 deletions docs/markers.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ field for your workload.
| [type](#type-required) | string{string, int, bool} | true |
| [default](#default-optional) | [type](#supported-field-types) | false |
| [replace](#replace-optional) | string | false |
| [arbitrary](#arbitrary-optional) | bool | false |
| [description](#description-optional) | string | false |

### Name (required if Parent is unspecified)
Expand All @@ -46,15 +47,15 @@ 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.
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
the value.
the value.

[#supported-field-types]() The supported data types are:

Expand Down Expand Up @@ -128,6 +129,69 @@ data:
justtesting: myoption
```

### Arbitrary (optional)

If you wish to create a field for a custom resource that does not directly map
to a value in a child resource, mark a field as arbitrary.

Here is an example of how to mark a field as arbitrary:

```yaml
---
# +operator-builder:field:name=nginx.installType,arbitrary,default="deployment",type=string,description=`
# +kubebuilder:validation:Enum=deployment;daemonset
# Method of install nginx ingress controller. One of: deployment | daemonset.`
apiVersion: v1
kind: Namespace
metadata:
# +operator-builder:field:name=namespace,default="nukleros-ingress-system",type=string,description=`
# Namespace to use for ingress support services.`
name: nukleros-ingress-system
```
On the first line you can see the `nginx.installType` custom resource field is
marked as arbitrary with the `arbitrary` marker field. Where you place this
marker is unimportant but it is recommended you put all arbitrary fields at the
beginning of one chosen manifest for ease of maintenance.

This will result in a custom resource sample that looks as follows:

```yaml
apiVersion: platform.addons.nukleros.io/v1alpha1
kind: IngressComponent
metadata:
name: ingresscomponent-sample
spec:
#collection:
#name: "supportservices-sample"
#namespace: ""
nginx:
installType: "deployment" # <---- arbitary field
image: "nginx/nginx-ingress"
version: "2.3.0"
replicas: 2
namespace: "nukleros-ingress-system"
```

This arbitrary field will not map to any child resource value. However it can
be leveraged by some custom mutation code or by a resource marker such as this:

```yaml
---
# +operator-builder:resource:field=nginx.installType,value="deployment",include
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-ingress
namespace: nukleros-ingress-system # +operator-builder:field:name=namespace,default="nukleros-ingress-system",type=string
...
```

The marker on line one indicates the deployment resource only be created if
`nginx.installType` has a value of `deployment` (as shown in the custom resource
sample above). In this example, we are providing an option to install the Nginx
Ingress Controller as a deployment _or_ a daemonset.

### Description (optional)

An optional description can be provided which will be used in the source code as
Expand Down Expand Up @@ -213,6 +277,12 @@ resource.
Defined as `+operator-builder:resource` this marker can be used to control a specific
resource with arguments in the marker.

Note: a resource marker must reference a field defined by a field marker. If
you include a resource marker with a unique field name that is not also defined
by a field marker you will get an error. You may use an [arbitrary field](#arbitrary-optional)
on a field marker if you don't wish to associate the field with a value in a
child resource.

| Field | Type | Required |
| --------------------------------------------------- | ------------------------------ | -------- |
| [field](#field--collectionfield-required) | string | true |
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/vmware-tanzu-labs/operator-builder
module github.com/nukleros/operator-builder

go 1.17

Expand Down
4 changes: 2 additions & 2 deletions internal/markers/inspect/inspector.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
package inspect

import (
"github.com/vmware-tanzu-labs/operator-builder/internal/markers/marker"
"github.com/vmware-tanzu-labs/operator-builder/internal/markers/parser"
"github.com/nukleros/operator-builder/internal/markers/marker"
"github.com/nukleros/operator-builder/internal/markers/parser"
)

type Inspector struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/markers/inspect/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

"gopkg.in/yaml.v3"

"github.com/vmware-tanzu-labs/operator-builder/internal/markers/parser"
"github.com/nukleros/operator-builder/internal/markers/parser"
)

type YAMLResult struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/markers/lexer/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"github.com/stretchr/testify/require"

"github.com/vmware-tanzu-labs/operator-builder/internal/markers/lexer"
"github.com/nukleros/operator-builder/internal/markers/lexer"
)

func GetTestLexer(buf string) *lexer.Lexer {
Expand Down
2 changes: 1 addition & 1 deletion internal/markers/marker/argument.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"reflect"
"strings"

"github.com/vmware-tanzu-labs/operator-builder/internal/markers/parser"
"github.com/nukleros/operator-builder/internal/markers/parser"
)

var (
Expand Down
2 changes: 1 addition & 1 deletion internal/markers/marker/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

package marker

import "github.com/vmware-tanzu-labs/operator-builder/internal/markers/parser"
import "github.com/nukleros/operator-builder/internal/markers/parser"

type Registry struct {
registry map[string]*Definition
Expand Down
2 changes: 1 addition & 1 deletion internal/markers/parser/consumed.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

package parser

import "github.com/vmware-tanzu-labs/operator-builder/internal/markers/lexer"
import "github.com/nukleros/operator-builder/internal/markers/lexer"

func (p *Parser) consumed(lxt lexer.LexemeType) bool {
if p.peek().Type == lxt {
Expand Down
2 changes: 1 addition & 1 deletion internal/markers/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package parser
import (
"bytes"

"github.com/vmware-tanzu-labs/operator-builder/internal/markers/lexer"
"github.com/nukleros/operator-builder/internal/markers/lexer"
)

type stateFn func(*Parser) stateFn
Expand Down
2 changes: 1 addition & 1 deletion internal/markers/parser/peek.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

package parser

import "github.com/vmware-tanzu-labs/operator-builder/internal/markers/lexer"
import "github.com/nukleros/operator-builder/internal/markers/lexer"

func (p *Parser) peek() lexer.Lexeme {
if p.peekCount > 0 {
Expand Down
2 changes: 1 addition & 1 deletion internal/markers/parser/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"errors"
"strconv"

"github.com/vmware-tanzu-labs/operator-builder/internal/markers/lexer"
"github.com/nukleros/operator-builder/internal/markers/lexer"
)

func startParse(p *Parser) stateFn {
Expand Down
4 changes: 2 additions & 2 deletions internal/plugins/config/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"sigs.k8s.io/kubebuilder/v3/pkg/model/resource"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"

workloadconfig "github.com/vmware-tanzu-labs/operator-builder/internal/workload/v1/config"
"github.com/vmware-tanzu-labs/operator-builder/internal/workload/v1/kinds"
workloadconfig "github.com/nukleros/operator-builder/internal/workload/v1/config"
"github.com/nukleros/operator-builder/internal/workload/v1/kinds"
)

type createAPISubcommand struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/plugins/config/v1/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"

workloadconfig "github.com/vmware-tanzu-labs/operator-builder/internal/workload/v1/config"
workloadconfig "github.com/nukleros/operator-builder/internal/workload/v1/config"
)

type initSubcommand struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/plugins/config/v1/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
cfgv3 "sigs.k8s.io/kubebuilder/v3/pkg/config/v3"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"

"github.com/vmware-tanzu-labs/operator-builder/internal/plugins"
"github.com/nukleros/operator-builder/internal/plugins"
)

const pluginName = "config." + plugins.DefaultNameQualifier
Expand Down
2 changes: 1 addition & 1 deletion internal/plugins/license/v1/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"

"github.com/vmware-tanzu-labs/operator-builder/internal/license"
"github.com/nukleros/operator-builder/internal/license"
)

var _ plugin.InitSubcommand = &initSubcommand{}
Expand Down
2 changes: 1 addition & 1 deletion internal/plugins/license/v1/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
cfgv3 "sigs.k8s.io/kubebuilder/v3/pkg/config/v3"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"

"github.com/vmware-tanzu-labs/operator-builder/internal/plugins"
"github.com/nukleros/operator-builder/internal/plugins"
)

const pluginName = "license." + plugins.DefaultNameQualifier
Expand Down
8 changes: 4 additions & 4 deletions internal/plugins/workload/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import (
"sigs.k8s.io/kubebuilder/v3/pkg/model/resource"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"

"github.com/vmware-tanzu-labs/operator-builder/internal/plugins/workload/v1/scaffolds"
"github.com/vmware-tanzu-labs/operator-builder/internal/workload/v1/commands/subcommand"
workloadconfig "github.com/vmware-tanzu-labs/operator-builder/internal/workload/v1/config"
"github.com/vmware-tanzu-labs/operator-builder/internal/workload/v1/kinds"
"github.com/nukleros/operator-builder/internal/plugins/workload/v1/scaffolds"
"github.com/nukleros/operator-builder/internal/workload/v1/commands/subcommand"
workloadconfig "github.com/nukleros/operator-builder/internal/workload/v1/config"
"github.com/nukleros/operator-builder/internal/workload/v1/kinds"
)

type createAPISubcommand struct {
Expand Down
8 changes: 4 additions & 4 deletions internal/plugins/workload/v1/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"

"github.com/vmware-tanzu-labs/operator-builder/internal/plugins/workload/v1/scaffolds"
"github.com/vmware-tanzu-labs/operator-builder/internal/workload/v1/commands/subcommand"
workloadconfig "github.com/vmware-tanzu-labs/operator-builder/internal/workload/v1/config"
"github.com/vmware-tanzu-labs/operator-builder/internal/workload/v1/kinds"
"github.com/nukleros/operator-builder/internal/plugins/workload/v1/scaffolds"
"github.com/nukleros/operator-builder/internal/workload/v1/commands/subcommand"
workloadconfig "github.com/nukleros/operator-builder/internal/workload/v1/config"
"github.com/nukleros/operator-builder/internal/workload/v1/kinds"
)

type initSubcommand struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/plugins/workload/v1/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
cfgv3 "sigs.k8s.io/kubebuilder/v3/pkg/config/v3"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"

"github.com/vmware-tanzu-labs/operator-builder/internal/plugins"
"github.com/nukleros/operator-builder/internal/plugins"
)

const pluginName = "workload." + plugins.DefaultNameQualifier
Expand Down
22 changes: 11 additions & 11 deletions internal/plugins/workload/v1/scaffolds/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ import (
"sigs.k8s.io/kubebuilder/v3/pkg/model/resource"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins"

"github.com/vmware-tanzu-labs/operator-builder/internal/plugins/workload/v1/scaffolds/templates"
"github.com/vmware-tanzu-labs/operator-builder/internal/plugins/workload/v1/scaffolds/templates/api"
"github.com/vmware-tanzu-labs/operator-builder/internal/plugins/workload/v1/scaffolds/templates/api/resources"
"github.com/vmware-tanzu-labs/operator-builder/internal/plugins/workload/v1/scaffolds/templates/cli"
"github.com/vmware-tanzu-labs/operator-builder/internal/plugins/workload/v1/scaffolds/templates/config/crd"
"github.com/vmware-tanzu-labs/operator-builder/internal/plugins/workload/v1/scaffolds/templates/config/samples"
"github.com/vmware-tanzu-labs/operator-builder/internal/plugins/workload/v1/scaffolds/templates/controller"
"github.com/vmware-tanzu-labs/operator-builder/internal/plugins/workload/v1/scaffolds/templates/int/dependencies"
"github.com/vmware-tanzu-labs/operator-builder/internal/plugins/workload/v1/scaffolds/templates/int/mutate"
"github.com/vmware-tanzu-labs/operator-builder/internal/plugins/workload/v1/scaffolds/templates/test/e2e"
"github.com/vmware-tanzu-labs/operator-builder/internal/workload/v1/kinds"
"github.com/nukleros/operator-builder/internal/plugins/workload/v1/scaffolds/templates"
"github.com/nukleros/operator-builder/internal/plugins/workload/v1/scaffolds/templates/api"
"github.com/nukleros/operator-builder/internal/plugins/workload/v1/scaffolds/templates/api/resources"
"github.com/nukleros/operator-builder/internal/plugins/workload/v1/scaffolds/templates/cli"
"github.com/nukleros/operator-builder/internal/plugins/workload/v1/scaffolds/templates/config/crd"
"github.com/nukleros/operator-builder/internal/plugins/workload/v1/scaffolds/templates/config/samples"
"github.com/nukleros/operator-builder/internal/plugins/workload/v1/scaffolds/templates/controller"
"github.com/nukleros/operator-builder/internal/plugins/workload/v1/scaffolds/templates/int/dependencies"
"github.com/nukleros/operator-builder/internal/plugins/workload/v1/scaffolds/templates/int/mutate"
"github.com/nukleros/operator-builder/internal/plugins/workload/v1/scaffolds/templates/test/e2e"
"github.com/nukleros/operator-builder/internal/workload/v1/kinds"
)

const boilerplatePath = "hack/boilerplate.go.txt"
Expand Down
8 changes: 4 additions & 4 deletions internal/plugins/workload/v1/scaffolds/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import (
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins"

"github.com/vmware-tanzu-labs/operator-builder/internal/plugins/workload/v1/scaffolds/templates"
"github.com/vmware-tanzu-labs/operator-builder/internal/plugins/workload/v1/scaffolds/templates/cli"
"github.com/vmware-tanzu-labs/operator-builder/internal/plugins/workload/v1/scaffolds/templates/test/e2e"
"github.com/vmware-tanzu-labs/operator-builder/internal/workload/v1/kinds"
"github.com/nukleros/operator-builder/internal/plugins/workload/v1/scaffolds/templates"
"github.com/nukleros/operator-builder/internal/plugins/workload/v1/scaffolds/templates/cli"
"github.com/nukleros/operator-builder/internal/plugins/workload/v1/scaffolds/templates/test/e2e"
"github.com/nukleros/operator-builder/internal/workload/v1/kinds"
)

var _ plugins.Scaffolder = &initScaffolder{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"sigs.k8s.io/kubebuilder/v3/pkg/machinery"

"github.com/vmware-tanzu-labs/operator-builder/internal/workload/v1/kinds"
"github.com/nukleros/operator-builder/internal/workload/v1/kinds"
)

var _ machinery.Template = &Constants{}
Expand Down
Loading

0 comments on commit 4388aa9

Please sign in to comment.