Skip to content

Commit

Permalink
feat: validate groups and kinds, fix double processing (#285)
Browse files Browse the repository at this point in the history
* refactor: split v1 package into independent packages

- Fixes #270, validate that the same kind does not exist within the same group
- Fixes #262, prevent full config parsing from running twice

Signed-off-by: Dustin Scott <sdustin@vmware.com>

* refactor: switch to new WorkloadBuilder interface

Signed-off-by: Dustin Scott <sdustin@vmware.com>

* fix: added test object for companion cli and fixed linting

Signed-off-by: Dustin Scott <sdustin@vmware.com>

* chore: rename test cases, application > standalone/platform > collection

Signed-off-by: Dustin Scott <sdustin@vmware.com>

* test: add tests for parsing of configs

Signed-off-by: Dustin Scott <sdustin@vmware.com>

* refactor: split resources package

Signed-off-by: Dustin Scott <sdustin@vmware.com>

* ci: fix linter

Signed-off-by: Dustin Scott <sdustin@vmware.com>

* test: renamed tests to make it more obvious to non-ob developers

Signed-off-by: Dustin Scott <sdustin@vmware.com>

* chore: fix linting

Signed-off-by: Dustin Scott <sdustin@vmware.com>

* refactor: move to new manifests package

Signed-off-by: Dustin Scott <sdustin@vmware.com>

* test: added pathtype to v1 ingress resource

Signed-off-by: Dustin Scott <sdustin@vmware.com>

* ci: use specific go version

Signed-off-by: Dustin Scott <sdustin@vmware.com>

* test: switch back to beta ingress version

Signed-off-by: Dustin Scott <sdustin@vmware.com>

* test: switch back to v1 ingress version with proper fields

Signed-off-by: Dustin Scott <sdustin@vmware.com>

* test: update kubectl/kind for tests

Signed-off-by: Dustin Scott <sdustin@vmware.com>
  • Loading branch information
scottd018 authored May 2, 2022
1 parent a42d2eb commit 2699460
Show file tree
Hide file tree
Showing 103 changed files with 2,064 additions and 1,335 deletions.
4 changes: 2 additions & 2 deletions .github/common-actions/e2e-test/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ runs:
echo "found kubectl executable at $(which kubectl)...not installing"
fi
env:
KIND_VERSION: v0.10.0
KUBECTL_VERSION: v1.20.0
KIND_VERSION: v0.12.0
KUBECTL_VERSION: v1.23.0

- name: Setup KIND Cluster
shell: bash
Expand Down
10 changes: 7 additions & 3 deletions .github/workflows/check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,15 @@ jobs:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v3
with:
go-version: 1.17

- name: Checkout Code
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Lint
uses: golangci/golangci-lint-action@v2
uses: golangci/golangci-lint-action@v3
with:
version: v1.44.0
version: v1.45.2
args: --timeout 3m
4 changes: 2 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ jobs:
include:
- name: Standalone Operator
artifact: standalone-codebase
test-workload-path: test/cases/application
test-workload-path: test/cases/standalone
- name: Standalone Edge Cases Operator
artifact: standalone-edge-codebase
test-workload-path: test/cases/edge-standalone
- name: Workload Collection Operator
artifact: collection-codebase
test-workload-path: test/cases/platform
test-workload-path: test/cases/collection
- name: Workload Collection Edge Cases Operator
artifact: collection-edge-codebase
test-workload-path: test/cases/edge-collection
Expand Down
8 changes: 4 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
bin/*
.idea/*
dist/*
test/cases/application/*
test/cases/standalone/*
test/cases/edge-collection/*
test/cases/edge-standalone/*
test/cases/platform/*
!test/cases/application/.workloadConfig/
test/cases/collection/*
!test/cases/standalone/.workloadConfig/
!test/cases/edge-collection/.workloadConfig/
!test/cases/edge-standalone/.workloadConfig/
!test/cases/platform/.workloadConfig/
!test/cases/collection/.workloadConfig/
24 changes: 12 additions & 12 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
{
"configurations": [
{
"name": "Test Platform Init",
"name": "Test Collection Init",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceFolder}/cmd/operator-builder",
"args": [
"init",
"--workload-config",
"${workspaceFolder}/test/cases/platform/.workloadConfig/workload.yaml",
"${workspaceFolder}/test/cases/collection/.workloadConfig/workload.yaml",
"--repo",
"github.com/acme/acme-cnp-mgr",
"--skip-go-version-check"
],
"cwd": "${workspaceFolder}/test/cases/platform",
"cwd": "${workspaceFolder}/test/cases/collection",
},
{
"name": "Test Platform Create",
"name": "Test Collection Create",
"type": "go",
"request": "launch",
"mode": "debug",
Expand All @@ -26,30 +26,30 @@
"create",
"api",
"--workload-config",
"${workspaceFolder}/test/cases/platform/.workloadConfig/workload.yaml",
"${workspaceFolder}/test/cases/collection/.workloadConfig/workload.yaml",
"--controller",
"--resource"
],
"cwd": "${workspaceFolder}/test/cases/platform",
"cwd": "${workspaceFolder}/test/cases/collection",
},
{
"name": "Test Application Init",
"name": "Test Standalone Init",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceFolder}/cmd/operator-builder",
"args": [
"init",
"--workload-config",
"${workspaceFolder}/test/cases/application/.workloadConfig/workload.yaml",
"${workspaceFolder}/test/cases/standalone/.workloadConfig/workload.yaml",
"--repo",
"github.com/acme/acme-webstore-mgr",
"--skip-go-version-check"
],
"cwd": "${workspaceFolder}/test/cases/application",
"cwd": "${workspaceFolder}/test/cases/standalone",
},
{
"name": "Test Application Create",
"name": "Test Standalone Create",
"type": "go",
"request": "launch",
"mode": "debug",
Expand All @@ -58,11 +58,11 @@
"create",
"api",
"--workload-config",
"${workspaceFolder}/test/cases/application/.workloadConfig/workload.yaml",
"${workspaceFolder}/test/cases/standalone/.workloadConfig/workload.yaml",
"--controller",
"--resource"
],
"cwd": "${workspaceFolder}/test/cases/application",
"cwd": "${workspaceFolder}/test/cases/standalone",
},
{
"name": "Test Edge Standalone Init",
Expand Down
6 changes: 4 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ In order to test the effect of changes made to Operator Builder, use `make
func-test` or `make debug`.

At a minimum, ensure your changes work for:
- application: This tests a basic standalone workload use case.
- platform: This tests a basic workload colleciton use case.
- standalone: This tests a basic standalone workload use case.
- collection: This tests a basic workload colleciton use case.
- edge-standalone: This tests standalone workloads which contain edge cases.
- edge-collection: This tests a collection workload which contains edge cases.

See the [testing docs](docs/testing.md) for insturctions on how to run these
tests.
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ install: build
#
# traditional testing
#
GOLANGCI_LINT_VERSION ?= v1.44.0
GOLANGCI_LINT_VERSION ?= v1.45.2
install-linter:
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | \
sh -s -- -b $$(go env GOPATH)/bin $(GOLANGCI_LINT_VERSION)
Expand All @@ -45,15 +45,15 @@ test:
go test -cover -coverprofile=./bin/coverage.out ./...

test-coverage-view: test
go tool cover -html=./bin/coverage.out
go tool cover -html=./bin/coverage.out

test-commit:
test/scripts/commit-check-latest.sh

#
# debug testing with delve
#
TEST_WORKLOAD_PATH ?= test/cases/application
TEST_WORKLOAD_PATH ?= test/cases/standalone

debug-clean:
rm -rf $(TEST_WORKLOAD_PATH)/*
Expand Down
2 changes: 1 addition & 1 deletion docs/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ integration tests will be added to test the generated operator.

## Run Functional Testing

To run the default `application` test in the default `FUNC_TEST_PATH`:
To run the default `application` (based on a standalone use case) test in the default `FUNC_TEST_PATH`:

make func-test

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/stretchr/testify v1.7.0
github.com/vmware-tanzu-labs/object-code-generator-for-k8s v0.5.0
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
k8s.io/apimachinery v0.22.2
k8s.io/client-go v0.22.2
Expand Down
31 changes: 14 additions & 17 deletions internal/plugins/config/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import (
"sigs.k8s.io/kubebuilder/v3/pkg/model/resource"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"

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

type createAPISubcommand struct {
workloadConfigPath string
workload kinds.WorkloadBuilder
}

var _ plugin.CreateAPISubcommand = &createAPISubcommand{}
Expand All @@ -26,43 +28,38 @@ func (p *createAPISubcommand) BindFlags(fs *pflag.FlagSet) {
}

func (p *createAPISubcommand) InjectConfig(c config.Config) error {
workload, err := workloadv1.ProcessInitConfig(
p.workloadConfigPath,
)
processor, err := workloadconfig.Parse(p.workloadConfigPath)
if err != nil {
return fmt.Errorf("unable to inject config into %s, %w", p.workloadConfigPath, err)
}

pluginConfig := workloadv1.PluginConfig{
p.workload = processor.Workload

pluginConfig := workloadconfig.Plugin{
WorkloadConfigPath: p.workloadConfigPath,
CliRootCommandName: workload.GetRootCommand().Name,
CliRootCommandName: processor.Workload.GetRootCommand().Name,
}

if err := c.EncodePluginConfig(workloadv1.PluginConfigKey, pluginConfig); err != nil {
return fmt.Errorf("unable to encode plugin config at key %s, %w", workloadv1.PluginConfigKey, err)
if err := c.EncodePluginConfig(workloadconfig.PluginKey, pluginConfig); err != nil {
return fmt.Errorf("unable to encode plugin config at key %s, %w", workloadconfig.PluginKey, err)
}

return nil
}

func (p *createAPISubcommand) InjectResource(res *resource.Resource) error {
workload, err := workloadv1.ProcessAPIConfig(p.workloadConfigPath)
if err != nil {
return fmt.Errorf("unable to inject resource into %s, %w", p.workloadConfigPath, err)
}

// set from config file if not provided with command line flag
if res.Group == "" {
res.Group = workload.GetAPIGroup()
res.Group = p.workload.GetAPIGroup()
}

if res.Version == "" {
res.Version = workload.GetAPIVersion()
res.Version = p.workload.GetAPIVersion()
}

if res.Kind == "" {
res.Kind = workload.GetAPIKind()
res.Plural = resource.RegularPlural(workload.GetAPIKind())
res.Kind = p.workload.GetAPIKind()
res.Plural = resource.RegularPlural(p.workload.GetAPIKind())
}

return nil
Expand Down
14 changes: 6 additions & 8 deletions internal/plugins/config/v1/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"

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

type initSubcommand struct {
Expand All @@ -25,23 +25,21 @@ func (p *initSubcommand) BindFlags(fs *pflag.FlagSet) {
}

func (p *initSubcommand) InjectConfig(c config.Config) error {
workload, err := workloadv1.ProcessInitConfig(
p.workloadConfigPath,
)
processor, err := workloadconfig.Parse(p.workloadConfigPath)
if err != nil {
return fmt.Errorf("unable to inject config into %s, %w", p.workloadConfigPath, err)
}

pluginConfig := workloadv1.PluginConfig{
pluginConfig := workloadconfig.Plugin{
WorkloadConfigPath: p.workloadConfigPath,
CliRootCommandName: workload.GetRootCommand().Name,
CliRootCommandName: processor.Workload.GetRootCommand().Name,
}

if err := c.EncodePluginConfig(workloadv1.PluginConfigKey, pluginConfig); err != nil {
if err := c.EncodePluginConfig(workloadconfig.PluginKey, pluginConfig); err != nil {
return fmt.Errorf("unable to encode operatorbuilder config key at %s, %w", p.workloadConfigPath, err)
}

if err := c.SetDomain(workload.GetDomain()); err != nil {
if err := c.SetDomain(processor.Workload.GetDomain()); err != nil {
return fmt.Errorf("unable to set project domain, %w", err)
}

Expand Down
28 changes: 15 additions & 13 deletions internal/plugins/workload/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package v1

import (
"errors"
"fmt"

"sigs.k8s.io/kubebuilder/v3/pkg/config"
Expand All @@ -12,7 +13,9 @@ import (
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"

"github.com/vmware-tanzu-labs/operator-builder/internal/plugins/workload/v1/scaffolds"
workloadv1 "github.com/vmware-tanzu-labs/operator-builder/internal/workload/v1"
"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"
)

type createAPISubcommand struct {
Expand All @@ -22,9 +25,11 @@ type createAPISubcommand struct {

workloadConfigPath string
cliRootCommandName string
workload workloadv1.WorkloadAPIBuilder
workload kinds.WorkloadBuilder
}

var ErrScaffoldCreateAPI = errors.New("unable to scaffold api")

var _ plugin.CreateAPISubcommand = &createAPISubcommand{}

func (p *createAPISubcommand) UpdateMetadata(cliMeta plugin.CLIMetadata, subcmdMeta *plugin.SubcommandMetadata) {
Expand All @@ -38,8 +43,8 @@ func (p *createAPISubcommand) UpdateMetadata(cliMeta plugin.CLIMetadata, subcmdM
func (p *createAPISubcommand) InjectConfig(c config.Config) error {
p.config = c

var pluginConfig workloadv1.PluginConfig
if err := c.DecodePluginConfig(workloadv1.PluginConfigKey, &pluginConfig); err != nil {
var pluginConfig workloadconfig.Plugin
if err := c.DecodePluginConfig(workloadconfig.PluginKey, &pluginConfig); err != nil {
return fmt.Errorf("unable to decode operatorbuilder config key at %s, %w", p.workloadConfigPath, err)
}

Expand All @@ -56,19 +61,16 @@ func (p *createAPISubcommand) InjectResource(res *resource.Resource) error {
}

func (p *createAPISubcommand) PreScaffold(machinery.Filesystem) error {
// load the workload config
workload, err := workloadv1.ProcessAPIConfig(p.workloadConfigPath)
processor, err := workloadconfig.Parse(p.workloadConfigPath)
if err != nil {
return fmt.Errorf("unable to process api config for %s, %w", p.workloadConfigPath, err)
return fmt.Errorf("%s for %s, %w", ErrScaffoldCreateAPI, p.workloadConfigPath, err)
}

// validate the workload config
err = workload.Validate()
if err != nil {
return fmt.Errorf("unable to validate config %s, %w", p.workloadConfigPath, err)
if err := subcommand.CreateAPI(processor); err != nil {
return fmt.Errorf("%s for %s, %w", ErrScaffoldCreateAPI, p.workloadConfigPath, err)
}

p.workload = workload
p.workload = processor.Workload

return nil
}
Expand All @@ -83,7 +85,7 @@ func (p *createAPISubcommand) Scaffold(fs machinery.Filesystem) error {
scaffolder.InjectFS(fs)

if err := scaffolder.Scaffold(); err != nil {
return fmt.Errorf("unable to scaffold api, %w", err)
return fmt.Errorf("%s for %s, %w", ErrScaffoldInit, p.workloadConfigPath, err)
}

return nil
Expand Down
Loading

0 comments on commit 2699460

Please sign in to comment.