Skip to content

Commit

Permalink
fix: Remove enum validation for action type
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy351 committed Dec 16, 2020
1 parent 370ce12 commit 280cbeb
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 30 deletions.
5 changes: 0 additions & 5 deletions deployment/base/crds/pullup.dev_githubwebhooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ spec:
spec:
properties:
action:
enum:
- create
- update
- apply
- delete
type: string
repositories:
items:
Expand Down
5 changes: 0 additions & 5 deletions deployment/base/crds/pullup.dev_httpwebhooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ spec:
spec:
properties:
action:
enum:
- create
- update
- apply
- delete
type: string
schema:
x-kubernetes-preserve-unknown-fields: true
Expand Down
12 changes: 5 additions & 7 deletions internal/webhook/hookutil/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ type RenderedTrigger struct {
type TriggerOptions struct {
Source TriggerSource
Triggers []v1beta1.EventSourceTrigger
DefaultAction v1beta1.Action
Action v1beta1.Action
DefaultAction string
Action string
Event interface{}
}

Expand Down Expand Up @@ -97,7 +97,7 @@ func (t *TriggerHandler) Handle(ctx context.Context, options *TriggerOptions) er
return nil
}

func (t *TriggerHandler) renderAction(options *TriggerOptions) (v1beta1.Action, error) {
func (t *TriggerHandler) renderAction(options *TriggerOptions) (string, error) {
action := options.Action
if action == "" {
action = options.DefaultAction
Expand All @@ -111,13 +111,11 @@ func (t *TriggerHandler) renderAction(options *TriggerOptions) (v1beta1.Action,
return "", fmt.Errorf("failed to marshal data: %w", err)
}

result, err := template.RenderFromJSON(string(action), extv1.JSON{Raw: buf})
action, err = template.RenderFromJSON(action, extv1.JSON{Raw: buf})
if err != nil {
return "", fmt.Errorf("failed to render action: %w", err)
}

action = v1beta1.Action(result)

if !v1beta1.IsActionValid(action) {
return "", ErrInvalidAction
}
Expand Down Expand Up @@ -252,7 +250,7 @@ func (t *TriggerHandler) finalizeData(data extv1.JSON) (extv1.JSON, error) {
return extv1.JSON{Raw: buf}, nil
}

func (t *TriggerHandler) handleTrigger(ctx context.Context, trigger *RenderedTrigger, action v1beta1.Action, options *TriggerOptions) error {
func (t *TriggerHandler) handleTrigger(ctx context.Context, trigger *RenderedTrigger, action string, options *TriggerOptions) error {
var result controller.Result
logger := logr.FromContextOrDiscard(ctx)

Expand Down
8 changes: 4 additions & 4 deletions internal/webhook/http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ var HandlerSet = wire.NewSet(
)

type Body struct {
Namespace string `json:"namespace"`
Name string `json:"name"`
Action v1beta1.Action `json:"action"`
Data extv1.JSON `json:"data"`
Namespace string `json:"namespace"`
Name string `json:"name"`
Action string `json:"action"`
Data extv1.JSON `json:"data"`
}

type Handler struct {
Expand Down
21 changes: 21 additions & 0 deletions internal/webhook/http/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,4 +560,25 @@ var _ = Describe("Handler", func() {
Expect(getChanges()).To(BeEmpty())
})
})

When("action is a template string", func() {
BeforeEach(func() {
req = newRequest(&Body{
Namespace: namespaceMap.GetRandom("test"),
Name: "foobar",
Action: v1beta1.ActionCreate,
Data: extv1.JSON{
Raw: testutil.MustMarshalJSON(map[string]interface{}{
"foo": "update",
}),
},
})
})

testSuccess("action-template")

It("should not have any changes", func() {
Expect(getChanges()).To(BeEmpty())
})
})
})
22 changes: 22 additions & 0 deletions internal/webhook/http/testdata/action-template.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
apiVersion: pullup.dev/v1beta1
kind: Trigger
metadata:
name: foobar
namespace: test
spec:
resourceName: "{{ .trigger.metadata.name }}-rt"
patches:
- apiVersion: v1
kind: Pod
sourceName: foo
---
apiVersion: pullup.dev/v1beta1
kind: HTTPWebhook
metadata:
name: foobar
namespace: test
spec:
action: "{{ .event.foo }}"
triggers:
- name: foobar
2 changes: 1 addition & 1 deletion pkg/apis/pullup/v1beta1/event_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package v1beta1
import extv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"

type EventSourceSpec struct {
Action Action `json:"action,omitempty"`
Action string `json:"action,omitempty"`
Triggers []EventSourceTrigger `json:"triggers,omitempty"`
}

Expand Down
13 changes: 5 additions & 8 deletions pkg/apis/pullup/v1beta1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@ import (
"k8s.io/apimachinery/pkg/types"
)

// +kubebuilder:validation:Enum=create;update;apply;delete
type Action string

const (
ActionCreate Action = "create"
ActionUpdate Action = "update"
ActionApply Action = "apply"
ActionDelete Action = "delete"
ActionCreate = "create"
ActionUpdate = "update"
ActionApply = "apply"
ActionDelete = "delete"
)

func IsActionValid(action Action) bool {
func IsActionValid(action string) bool {
switch action {
case ActionCreate, ActionUpdate, ActionApply, ActionDelete:
return true
Expand Down

0 comments on commit 280cbeb

Please sign in to comment.