-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add generation for getting objects from requests
In order to validate objects, the new and old objects, if they exist, should be pull from the webhook request. This process is identical for all objects except that the return types would be different. This is a candidate for code generation, and this generation is added.
- Loading branch information
Donnie Adams
committed
Jul 27, 2021
1 parent
166af4f
commit 42c30d0
Showing
8 changed files
with
547 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,5 @@ | |
/dist | ||
*.swp | ||
.idea | ||
.vscode | ||
/webhook |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,9 @@ | |
] | ||
}, | ||
"run": { | ||
"skip-dirs": [ | ||
"pkg/generated" | ||
], | ||
"skip-files": [ | ||
"/zz_generated_" | ||
], | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package main | ||
|
||
const objectsFromRequestTemplate = ` | ||
package {{ .package }} | ||
import ( | ||
{{ range .types }} | ||
"{{ .Package }}"{{ end }} | ||
"github.com/rancher/wrangler/pkg/webhook" | ||
admissionv1 "k8s.io/api/admission/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
) | ||
{{ range .types }} | ||
// {{ .Name }}OldAndNewFromRequest gets the old and new {{ .Name }} objects, respectively, from the webhook request. | ||
// If the request is a Delete operation, then the new object is the zero value for {{ .Name }}. | ||
// Similarly, if the request is a Create operation, then the old object is the zero value for {{ .Name }}. | ||
func {{ .Name }}OldAndNewFromRequest(request *webhook.Request) ({{ .Type }}, {{ .Type }}, error) { | ||
var object runtime.Object | ||
var err error | ||
if request.Operation != admissionv1.Delete { | ||
object, err = request.DecodeObject() | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
} else { | ||
object = {{ replace .Type "*" "&" }}{} | ||
} | ||
if request.Operation == admissionv1.Create { | ||
return {{ replace .Type "*" "&" }}{}, object.({{ .Type }}), nil | ||
} | ||
oldObject, err := request.DecodeOldObject() | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
return oldObject.({{ .Type }}), object.({{ .Type }}), nil | ||
} | ||
// {{ .Name }}FromRequest returns a {{ .Name }} object from the webhook request. | ||
// If the operation is a Delete operation, then the old object is returned. | ||
// Otherwise, the new object is returned. | ||
func {{ .Name }}FromRequest(request *webhook.Request) ({{ .Type }}, error) { | ||
var object runtime.Object | ||
var err error | ||
if request.Operation == admissionv1.Delete { | ||
object, err = request.DecodeOldObject() | ||
} else { | ||
object, err = request.DecodeObject() | ||
} | ||
if err != nil { | ||
return nil, err | ||
} | ||
return object.({{ .Type }}), nil | ||
} | ||
{{ end }} | ||
` |
Oops, something went wrong.