• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kubernetes-sigs / kubebuilder / 16522004449

25 Jul 2025 12:30PM UTC coverage: 64.151%. Remained the same
16522004449

Pull #4898

github

web-flow
Merge branch 'master' into version/refactor-add-tests
Pull Request #4898: ✨ Improve version command output: add runtime fallbacks and unit tests.

2627 of 4095 relevant lines covered (64.15%)

13.59 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.43
/pkg/model/resource/webhooks.go
1
/*
2
Copyright 2022 The Kubernetes Authors.
3

4
Licensed under the Apache License, Version 2.0 (the "License");
5
you may not use this file except in compliance with the License.
6
You may obtain a copy of the License at
7

8
    http://www.apache.org/licenses/LICENSE-2.0
9

10
Unless required by applicable law or agreed to in writing, software
11
distributed under the License is distributed on an "AS IS" BASIS,
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
See the License for the specific language governing permissions and
14
limitations under the License.
15
*/
16

17
package resource
18

19
import (
20
        "fmt"
21
)
22

23
// Webhooks contains information about scaffolded webhooks
24
type Webhooks struct {
25
        // WebhookVersion holds the {Validating,Mutating}WebhookConfiguration API version used for the resource.
26
        WebhookVersion string `json:"webhookVersion,omitempty"`
27

28
        // Defaulting specifies if a defaulting webhook is associated to the resource.
29
        Defaulting bool `json:"defaulting,omitempty"`
30

31
        // Validation specifies if a validation webhook is associated to the resource.
32
        Validation bool `json:"validation,omitempty"`
33

34
        // Conversion specifies if a conversion webhook is associated to the resource.
35
        Conversion bool `json:"conversion,omitempty"`
36

37
        Spoke []string `json:"spoke,omitempty"`
38
}
39

40
// Validate checks that the Webhooks is valid.
41
func (webhooks Webhooks) Validate() error {
5✔
42
        // Validate the Webhook version
5✔
43
        if err := validateAPIVersion(webhooks.WebhookVersion); err != nil {
7✔
44
                return fmt.Errorf("invalid Webhook version: %w", err)
2✔
45
        }
2✔
46

47
        // Validate that Spoke versions are unique
48
        seen := map[string]bool{}
3✔
49
        for _, version := range webhooks.Spoke {
3✔
50
                if seen[version] {
×
51
                        return fmt.Errorf("duplicate spoke version: %s", version)
×
52
                }
×
53
                seen[version] = true
×
54
        }
55

56
        return nil
3✔
57
}
58

59
// Copy returns a deep copy of the API that can be safely modified without affecting the original.
60
func (webhooks Webhooks) Copy() Webhooks {
29✔
61
        // Deep copy the Spoke slice
29✔
62
        var spokeCopy []string
29✔
63
        if len(webhooks.Spoke) > 0 {
29✔
64
                spokeCopy = make([]string, len(webhooks.Spoke))
×
65
                copy(spokeCopy, webhooks.Spoke)
×
66
        } else {
29✔
67
                spokeCopy = nil
29✔
68
        }
29✔
69

70
        return Webhooks{
29✔
71
                WebhookVersion: webhooks.WebhookVersion,
29✔
72
                Defaulting:     webhooks.Defaulting,
29✔
73
                Validation:     webhooks.Validation,
29✔
74
                Conversion:     webhooks.Conversion,
29✔
75
                Spoke:          spokeCopy,
29✔
76
        }
29✔
77
}
78

79
// Update combines fields of the webhooks of two resources.
80
func (webhooks *Webhooks) Update(other *Webhooks) error {
28✔
81
        // If other is nil, nothing to merge
28✔
82
        if other == nil {
36✔
83
                return nil
8✔
84
        }
8✔
85

86
        // Update the version.
87
        if other.WebhookVersion != "" {
26✔
88
                if webhooks.WebhookVersion == "" {
9✔
89
                        webhooks.WebhookVersion = other.WebhookVersion
3✔
90
                } else if webhooks.WebhookVersion != other.WebhookVersion {
8✔
91
                        return fmt.Errorf("webhook versions do not match")
2✔
92
                }
2✔
93
        }
94

95
        // Update defaulting.
96
        webhooks.Defaulting = webhooks.Defaulting || other.Defaulting
18✔
97

18✔
98
        // Update validation.
18✔
99
        webhooks.Validation = webhooks.Validation || other.Validation
18✔
100

18✔
101
        // Update conversion.
18✔
102
        webhooks.Conversion = webhooks.Conversion || other.Conversion
18✔
103

18✔
104
        // Update Spoke (merge without duplicates)
18✔
105
        if len(other.Spoke) > 0 {
19✔
106
                existingSpokes := make(map[string]struct{})
1✔
107
                for _, spoke := range webhooks.Spoke {
2✔
108
                        existingSpokes[spoke] = struct{}{}
1✔
109
                }
1✔
110
                for _, spoke := range other.Spoke {
3✔
111
                        if _, exists := existingSpokes[spoke]; !exists {
3✔
112
                                webhooks.Spoke = append(webhooks.Spoke, spoke)
1✔
113
                        }
1✔
114
                }
115
        }
116

117
        return nil
18✔
118
}
119

120
// IsEmpty returns if the Webhooks' fields all contain zero-values.
121
func (webhooks Webhooks) IsEmpty() bool {
44✔
122
        return webhooks.WebhookVersion == "" &&
44✔
123
                !webhooks.Defaulting && !webhooks.Validation &&
44✔
124
                !webhooks.Conversion && len(webhooks.Spoke) == 0
44✔
125
}
44✔
126

127
// AddSpoke adds a new spoke version to the Webhooks configuration.
128
func (webhooks *Webhooks) AddSpoke(version string) {
×
129
        // Ensure the version is not already present
×
130
        for _, v := range webhooks.Spoke {
×
131
                if v == version {
×
132
                        return
×
133
                }
×
134
        }
135
        webhooks.Spoke = append(webhooks.Spoke, version)
×
136
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc