/
webhook.go
78 lines (59 loc) · 1.99 KB
/
webhook.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package v1alpha1
import (
"fmt"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:resource:categories=all;pullup
type Webhook struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Status WebhookStatus `json:"status,omitempty"`
Spec WebhookSpec `json:"spec,omitempty"`
}
// +kubebuilder:object:root=true
type WebhookList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []Webhook `json:"items"`
}
type WebhookSpec struct {
Resources []WebhookResource `json:"resources,omitempty"`
Repositories []WebhookRepository `json:"repositories,omitempty"`
ResourceName string `json:"resourceName,omitempty"`
}
type WebhookRepository struct {
// +kubebuilder:validation:Enum=github
// +optional
Type string `json:"type"`
Name string `json:"name"`
Branches WebhookFilter `json:"branches,omitempty"`
}
type WebhookFilter struct {
Include []string `json:"include,omitempty"`
Exclude []string `json:"exclude,omitempty"`
}
type WebhookStatus struct{}
// +kubebuilder:validation:XEmbeddedResource
// +kubebuilder:validation:XPreserveUnknownFields
type WebhookResource struct {
APIVersion string `json:"apiVersion"`
Kind string `json:"kind"`
Metadata metav1.ObjectMeta `json:"metadata"`
unstructured.Unstructured `json:"-"`
}
func (in WebhookResource) MarshalJSON() ([]byte, error) {
return in.Unstructured.MarshalJSON()
}
func (in *WebhookResource) UnmarshalJSON(data []byte) error {
if err := in.Unstructured.UnmarshalJSON(data); err != nil {
return fmt.Errorf("failed to unmarshal WebhookResource: %w", err)
}
in.APIVersion = in.Unstructured.GetAPIVersion()
in.Kind = in.Unstructured.GetKind()
in.Metadata = meta.AsPartialObjectMetadata(&in.Unstructured).ObjectMeta
return nil
}