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

kubernetes-sigs / kubebuilder / 14165978911

31 Mar 2025 07:06AM CUT coverage: 72.693% (-0.3%) from 72.977%
14165978911

Pull #4675

github

kersten
refactor: convert CLI methods to use pointer receivers

Updated all CLI method receivers to use pointer semantics (*CLI)
instead of value (CLI), ensuring consistent behavior across command
setup functions and reducing potential for unintentional value copying.
Pull Request #4675: ⚠️ 🐛 (API) convert CLI methods to use pointer receivers

22 of 22 new or added lines in 9 files covered. (100.0%)

34 existing lines in 4 files now uncovered.

2300 of 3164 relevant lines covered (72.69%)

16.74 hits per line

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

48.94
/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

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

14✔
UNCOV
98
        if opts.DoDefaulting || opts.DoValidation || opts.DoConversion {
×
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

14✔
UNCOV
114
        if len(opts.ExternalAPIPath) > 0 {
×
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
28✔
123
        if !opts.DoAPI {
14✔
124
                var alreadyHasAPI bool
14✔
125
                loadedRes, err := c.GetResource(res.GVK)
14✔
126
                alreadyHasAPI = err == nil && loadedRes.HasAPI()
28✔
127
                if !alreadyHasAPI {
14✔
UNCOV
128
                        if res.External {
×
129
                                res.Path = opts.ExternalAPIPath
×
130
                                res.Domain = opts.ExternalAPIDomain
14✔
131
                        } else {
14✔
132
                                // Handle core types
22✔
133
                                if domain, found := coreGroups[res.Group]; found {
8✔
134
                                        res.Core = true
8✔
135
                                        res.Domain = domain
8✔
136
                                        res.Path = path.Join("k8s.io", "api", res.Group, res.Version)
8✔
137
                                }
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