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

kubernetes-sigs / kubebuilder / 14101373463

27 Mar 2025 07:43AM CUT coverage: 73.023%. Remained the same
14101373463

Pull #4685

github

kersten
chore: normalize error messages and wrap errors using %w

- Use %w consistently in fmt.Errorf for proper error wrapping
- Normalize error message casing for consistency
- Adjust tests to reflect updated error strings
Pull Request #4685: 🌱 (chore): normalize error messages and wrap errors using %w

6 of 10 new or added lines in 3 files covered. (60.0%)

2309 of 3162 relevant lines covered (73.02%)

13.99 hits per line

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

47.92
/pkg/plugins/golang/options.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 golang
18

19
import (
20
        "path"
21

22
        "sigs.k8s.io/kubebuilder/v4/pkg/config"
23
        "sigs.k8s.io/kubebuilder/v4/pkg/model/resource"
24
)
25

26
var coreGroups = map[string]string{
27
        "admission":             "k8s.io",
28
        "admissionregistration": "k8s.io",
29
        "apps":                  "",
30
        "auditregistration":     "k8s.io",
31
        "apiextensions":         "k8s.io",
32
        "authentication":        "k8s.io",
33
        "authorization":         "k8s.io",
34
        "autoscaling":           "",
35
        "batch":                 "",
36
        "certificates":          "k8s.io",
37
        "coordination":          "k8s.io",
38
        "core":                  "",
39
        "events":                "k8s.io",
40
        "extensions":            "",
41
        "imagepolicy":           "k8s.io",
42
        "networking":            "k8s.io",
43
        "node":                  "k8s.io",
44
        "metrics":               "k8s.io",
45
        "policy":                "",
46
        "rbac.authorization":    "k8s.io",
47
        "scheduling":            "k8s.io",
48
        "setting":               "k8s.io",
49
        "storage":               "k8s.io",
50
}
51

52
// Options contains the information required to build a new resource.Resource.
53
type Options struct {
54
        // Plural is the resource's kind plural form.
55
        Plural string
56

57
        // ExternalAPIPath allows to inform a path for APIs not defined in the project
58
        ExternalAPIPath string
59

60
        // ExternalAPIPath allows to inform the resource domain to build the Qualified Group
61
        // to generate the RBAC markers
62
        ExternalAPIDomain string
63

64
        // Namespaced is true if the resource should be namespaced.
65
        Namespaced bool
66

67
        // Flags that define which parts should be scaffolded
68
        DoAPI        bool
69
        DoController bool
70
        DoDefaulting bool
71
        DoValidation bool
72
        DoConversion bool
73

74
        // Spoke versions for conversion webhook
75
        Spoke []string
76
}
77

78
// UpdateResource updates the provided resource with the options
79
func (opts Options) UpdateResource(res *resource.Resource, c config.Config) {
14✔
80
        if opts.Plural != "" {
16✔
81
                res.Plural = opts.Plural
2✔
82
        }
2✔
83

84
        if opts.DoAPI {
14✔
85
                res.Path = resource.APIPackagePath(c.GetRepository(), res.Group, res.Version, c.IsMultiGroup())
×
86

×
87
                res.API = &resource.API{
×
88
                        CRDVersion: "v1",
×
89
                        Namespaced: opts.Namespaced,
×
90
                }
×
91

×
92
        }
×
93

94
        if opts.DoController {
16✔
95
                res.Controller = true
2✔
96
        }
2✔
97

98
        if opts.DoDefaulting || opts.DoValidation || opts.DoConversion {
14✔
99
                res.Path = resource.APIPackagePath(c.GetRepository(), res.Group, res.Version, c.IsMultiGroup())
×
100

×
101
                res.Webhooks.WebhookVersion = "v1"
×
102
                if opts.DoDefaulting {
×
103
                        res.Webhooks.Defaulting = true
×
104
                }
×
105
                if opts.DoValidation {
×
106
                        res.Webhooks.Validation = true
×
107
                }
×
108
                if opts.DoConversion {
×
109
                        res.Webhooks.Conversion = true
×
110
                        res.Webhooks.Spoke = opts.Spoke
×
111
                }
×
112
        }
113

114
        if len(opts.ExternalAPIPath) > 0 {
14✔
115
                res.External = true
×
116
        }
×
117

118
        // domain and path may need to be changed in case we are referring to a builtin core resource:
119
        //  - Check if we are scaffolding the resource now           => project resource
120
        //  - Check if we already scaffolded the resource            => project resource
121
        //  - Check if the resource group is a well-known core group => builtin core resource
122
        //  - In any other case, default to                          => project resource
123
        if !opts.DoAPI {
28✔
124
                var alreadyHasAPI bool
14✔
125
                loadedRes, err := c.GetResource(res.GVK)
14✔
126
                alreadyHasAPI = err == nil && loadedRes.HasAPI()
14✔
127
                if !alreadyHasAPI {
28✔
128
                        if res.External {
14✔
129
                                res.Path = opts.ExternalAPIPath
×
130
                                res.Domain = opts.ExternalAPIDomain
×
131
                        } else {
14✔
132
                                // Handle core types
14✔
133
                                if domain, found := coreGroups[res.Group]; found {
22✔
134
                                        res.Core = true
8✔
135
                                        res.Domain = domain
8✔
136
                                        res.Path = path.Join("k8s.io", "api", res.Group, res.Version)
8✔
137
                                }
8✔
138
                        }
139
                }
140
        }
141
}
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