-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathwebhooks.go
136 lines (112 loc) · 3.88 KB
/
webhooks.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package resource
import (
"fmt"
)
// Webhooks contains information about scaffolded webhooks
type Webhooks struct {
// WebhookVersion holds the {Validating,Mutating}WebhookConfiguration API version used for the resource.
WebhookVersion string `json:"webhookVersion,omitempty"`
// Defaulting specifies if a defaulting webhook is associated to the resource.
Defaulting bool `json:"defaulting,omitempty"`
// Validation specifies if a validation webhook is associated to the resource.
Validation bool `json:"validation,omitempty"`
// Conversion specifies if a conversion webhook is associated to the resource.
Conversion bool `json:"conversion,omitempty"`
Spoke []string `json:"spoke,omitempty"`
}
// Validate checks that the Webhooks is valid.
func (webhooks Webhooks) Validate() error {
// Validate the Webhook version
if err := validateAPIVersion(webhooks.WebhookVersion); err != nil {
return fmt.Errorf("invalid Webhook version: %w", err)
}
// Validate that Spoke versions are unique
seen := map[string]bool{}
for _, version := range webhooks.Spoke {
if seen[version] {
return fmt.Errorf("duplicate spoke version: %s", version)
}
seen[version] = true
}
return nil
}
// Copy returns a deep copy of the API that can be safely modified without affecting the original.
func (webhooks Webhooks) Copy() Webhooks {
// Deep copy the Spoke slice
var spokeCopy []string
if len(webhooks.Spoke) > 0 {
spokeCopy = make([]string, len(webhooks.Spoke))
copy(spokeCopy, webhooks.Spoke)
} else {
spokeCopy = nil
}
return Webhooks{
WebhookVersion: webhooks.WebhookVersion,
Defaulting: webhooks.Defaulting,
Validation: webhooks.Validation,
Conversion: webhooks.Conversion,
Spoke: spokeCopy,
}
}
// Update combines fields of the webhooks of two resources.
func (webhooks *Webhooks) Update(other *Webhooks) error {
// If other is nil, nothing to merge
if other == nil {
return nil
}
// Update the version.
if other.WebhookVersion != "" {
if webhooks.WebhookVersion == "" {
webhooks.WebhookVersion = other.WebhookVersion
} else if webhooks.WebhookVersion != other.WebhookVersion {
return fmt.Errorf("webhook versions do not match")
}
}
// Update defaulting.
webhooks.Defaulting = webhooks.Defaulting || other.Defaulting
// Update validation.
webhooks.Validation = webhooks.Validation || other.Validation
// Update conversion.
webhooks.Conversion = webhooks.Conversion || other.Conversion
// Update Spoke (merge without duplicates)
if len(other.Spoke) > 0 {
existingSpokes := make(map[string]struct{})
for _, spoke := range webhooks.Spoke {
existingSpokes[spoke] = struct{}{}
}
for _, spoke := range other.Spoke {
if _, exists := existingSpokes[spoke]; !exists {
webhooks.Spoke = append(webhooks.Spoke, spoke)
}
}
}
return nil
}
// IsEmpty returns if the Webhooks' fields all contain zero-values.
func (webhooks Webhooks) IsEmpty() bool {
return webhooks.WebhookVersion == "" &&
!webhooks.Defaulting && !webhooks.Validation &&
!webhooks.Conversion && len(webhooks.Spoke) == 0
}
// AddSpoke adds a new spoke version to the Webhooks configuration.
func (webhooks *Webhooks) AddSpoke(version string) {
// Ensure the version is not already present
for _, v := range webhooks.Spoke {
if v == version {
return
}
}
webhooks.Spoke = append(webhooks.Spoke, version)
}