Skip to content

Commit 762211a

Browse files
authored
Merge pull request #1899 from camilamacedo86/fix-crd-web-version
⚠️ change structure to store crdVersion and webhookversion (go/v3-alpha)
2 parents a4ad49a + 90a7ad3 commit 762211a

File tree

15 files changed

+265
-133
lines changed

15 files changed

+265
-133
lines changed

pkg/model/config/config.go

Lines changed: 85 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type Config struct {
4545

4646
// Resources tracks scaffolded resources in the project
4747
// This info is tracked only in project with version 2
48-
Resources []GVK `json:"resources,omitempty"`
48+
Resources []ResourceData `json:"resources,omitempty"`
4949

5050
// Multigroup tracks if the project has more than one group
5151
MultiGroup bool `json:"multigroup,omitempty"`
@@ -78,32 +78,30 @@ func (c Config) IsV3() bool {
7878
return c.Version == Version3Alpha
7979
}
8080

81-
// HasResource returns true if API resource is already tracked
82-
func (c Config) HasResource(target GVK) bool {
81+
// GetResource returns the GKV if the resource is found
82+
func (c Config) GetResource(target ResourceData) *ResourceData {
8383
// Return true if the target resource is found in the tracked resources
8484
for _, r := range c.Resources {
85-
if r.isEqualTo(target) {
86-
return true
85+
if r.isGVKEqualTo(target) {
86+
return &r
8787
}
8888
}
89-
90-
// Return false otherwise
91-
return false
89+
return nil
9290
}
9391

9492
// UpdateResources either adds gvk to the tracked set or, if the resource already exists,
9593
// updates the the equivalent resource in the set.
96-
func (c *Config) UpdateResources(gvk GVK) {
94+
func (c *Config) UpdateResources(resource ResourceData) {
9795
// If the resource already exists, update it.
9896
for i, r := range c.Resources {
99-
if r.isEqualTo(gvk) {
100-
c.Resources[i].merge(gvk)
97+
if r.isGVKEqualTo(resource) {
98+
c.Resources[i].merge(resource)
10199
return
102100
}
103101
}
104102

105103
// The resource does not exist, append the resource to the tracked ones.
106-
c.Resources = append(c.Resources, gvk)
104+
c.Resources = append(c.Resources, resource)
107105
}
108106

109107
// HasGroup returns true if group is already tracked
@@ -136,9 +134,13 @@ func (c Config) resourceAPIVersionCompatible(verType, version string) bool {
136134
var currVersion string
137135
switch verType {
138136
case "crd":
139-
currVersion = res.CRDVersion
137+
if res.API != nil {
138+
currVersion = res.API.CRDVersion
139+
}
140140
case "webhook":
141-
currVersion = res.WebhookVersion
141+
if res.Webhooks != nil {
142+
currVersion = res.Webhooks.WebhookVersion
143+
}
142144
}
143145
if currVersion != "" && version != currVersion {
144146
return false
@@ -147,33 +149,71 @@ func (c Config) resourceAPIVersionCompatible(verType, version string) bool {
147149
return true
148150
}
149151

150-
// GVK contains information about scaffolded resources
151-
type GVK struct {
152+
// ResourceData contains information about scaffolded resources
153+
type ResourceData struct {
152154
Group string `json:"group,omitempty"`
153155
Version string `json:"version,omitempty"`
154156
Kind string `json:"kind,omitempty"`
155157

156-
// CRDVersion holds the CustomResourceDefinition API version used for the GVK.
158+
// API holds the API data
159+
API *API `json:"api,omitempty"`
160+
161+
// Webhooks holds the Webhooks data
162+
Webhooks *Webhooks `json:"webhooks,omitempty"`
163+
}
164+
165+
// API contains information about scaffolded APIs
166+
type API struct {
167+
// CRDVersion holds the CustomResourceDefinition API version used for the ResourceData.
157168
CRDVersion string `json:"crdVersion,omitempty"`
158-
// WebhookVersion holds the {Validating,Mutating}WebhookConfiguration API version used for the GVK.
169+
}
170+
171+
// Webhooks contains information about scaffolded webhooks
172+
type Webhooks struct {
173+
// WebhookVersion holds the {Validating,Mutating}WebhookConfiguration API version used for the Options.
159174
WebhookVersion string `json:"webhookVersion,omitempty"`
160175
}
161176

162-
// isEqualTo compares it with another resource
163-
func (r GVK) isEqualTo(other GVK) bool {
177+
// isGVKEqualTo compares it with another resource
178+
func (r ResourceData) isGVKEqualTo(other ResourceData) bool {
164179
return r.Group == other.Group &&
165180
r.Version == other.Version &&
166181
r.Kind == other.Kind
167182
}
168183

169184
// merge combines fields of two GVKs that have matching group, version, and kind,
170185
// favoring the receiver's values.
171-
func (r *GVK) merge(other GVK) {
172-
if r.CRDVersion == "" && other.CRDVersion != "" {
173-
r.CRDVersion = other.CRDVersion
186+
func (r *ResourceData) merge(other ResourceData) {
187+
if other.Webhooks != nil {
188+
if r.Webhooks == nil {
189+
r.Webhooks = other.Webhooks
190+
} else {
191+
r.Webhooks.merge(other.Webhooks)
192+
}
174193
}
175-
if r.WebhookVersion == "" && other.WebhookVersion != "" {
176-
r.WebhookVersion = other.WebhookVersion
194+
195+
if other.API != nil {
196+
if r.API == nil {
197+
r.API = other.API
198+
} else {
199+
r.API.merge(other.API)
200+
}
201+
}
202+
}
203+
204+
// merge compares it with another webhook by setting each webhook type individually so existing values are
205+
// not overwritten.
206+
func (w *Webhooks) merge(other *Webhooks) {
207+
if w.WebhookVersion == "" && other.WebhookVersion != "" {
208+
w.WebhookVersion = other.WebhookVersion
209+
}
210+
}
211+
212+
// merge compares it with another api by setting each api type individually so existing values are
213+
// not overwritten.
214+
func (a *API) merge(other *API) {
215+
if a.CRDVersion == "" && other.CRDVersion != "" {
216+
a.CRDVersion = other.CRDVersion
177217
}
178218
}
179219

@@ -182,6 +222,26 @@ func (c Config) Marshal() ([]byte, error) {
182222
// Ignore extra fields at first.
183223
cfg := c
184224
cfg.Plugins = nil
225+
226+
// Ignore some fields if v2.
227+
if cfg.IsV2() {
228+
for i := range cfg.Resources {
229+
cfg.Resources[i].API = nil
230+
cfg.Resources[i].Webhooks = nil
231+
}
232+
}
233+
234+
for i, r := range cfg.Resources {
235+
// If API is empty, omit it (prevents `api: {}`).
236+
if r.API != nil && r.API.CRDVersion == "" {
237+
cfg.Resources[i].API = nil
238+
}
239+
// If Webhooks is empty, omit it (prevents `webhooks: {}`).
240+
if r.Webhooks != nil && r.Webhooks.WebhookVersion == "" {
241+
cfg.Resources[i].Webhooks = nil
242+
}
243+
}
244+
185245
content, err := yaml.Marshal(cfg)
186246
if err != nil {
187247
return nil, fmt.Errorf("error marshalling project configuration: %v", err)

pkg/model/config/config_test.go

Lines changed: 73 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,28 @@ var _ = Describe("PluginConfig", func() {
3030
Data1 string `json:"data-1"`
3131
Data2 string `json:"data-2"`
3232
}
33+
const defaultWebhookVersion = "v1"
34+
35+
resource := ResourceData{Group: "Foo", Kind: "Baz", Version: "v1"}
36+
resource.Webhooks = &Webhooks{defaultWebhookVersion}
37+
38+
It("should return true when has the ResourceData is equals", func() {
39+
Expect(resource.isGVKEqualTo(ResourceData{Group: "Foo", Kind: "Baz", Version: "v1"})).To(BeTrue())
40+
})
41+
42+
It("should return false when ResourceData is NOT equals", func() {
43+
Expect(resource.isGVKEqualTo(ResourceData{Group: "Foo", Kind: "Baz", Version: "v2"})).To(BeFalse())
44+
})
45+
46+
It("IsV2 should return true when the config is V2", func() {
47+
cfg := Config{Version: Version2}
48+
Expect(cfg.IsV2()).To(BeTrue())
49+
})
50+
51+
It("IsV3 should return true when the config is V3", func() {
52+
cfg := Config{Version: Version3Alpha}
53+
Expect(cfg.IsV3()).To(BeTrue())
54+
})
3355

3456
It("should encode correctly", func() {
3557
var (
@@ -165,56 +187,61 @@ var _ = Describe("PluginConfig", func() {
165187
})
166188
})
167189

168-
var _ = Describe("Resource Version Compatibility", func() {
190+
var _ = Describe("ResourceData Version Compatibility", func() {
169191

170192
var (
171-
c *Config
172-
gvk1, gvk2 GVK
193+
c *Config
194+
resource1, resource2 ResourceData
173195

174196
defaultVersion = "v1"
175197
)
176198

177199
BeforeEach(func() {
178200
c = &Config{}
179-
gvk1 = GVK{Group: "example", Version: "v1", Kind: "TestKind"}
180-
gvk2 = GVK{Group: "example", Version: "v1", Kind: "TestKind2"}
201+
resource1 = ResourceData{Group: "example", Version: "v1", Kind: "TestKind"}
202+
resource2 = ResourceData{Group: "example", Version: "v1", Kind: "TestKind2"}
181203
})
182204

183205
Context("resourceAPIVersionCompatible", func() {
184206
It("returns true for a list of empty resources", func() {
185207
Expect(c.resourceAPIVersionCompatible("crd", defaultVersion)).To(BeTrue())
208+
Expect(c.resourceAPIVersionCompatible("webhook", defaultVersion)).To(BeTrue())
186209
})
187210
It("returns true for one resource with an empty version", func() {
188-
c.Resources = []GVK{gvk1}
211+
c.Resources = []ResourceData{resource1}
189212
Expect(c.resourceAPIVersionCompatible("crd", defaultVersion)).To(BeTrue())
213+
Expect(c.resourceAPIVersionCompatible("webhook", defaultVersion)).To(BeTrue())
190214
})
191215
It("returns true for one resource with matching version", func() {
192-
gvk1.CRDVersion = defaultVersion
193-
c.Resources = []GVK{gvk1}
216+
resource1.API = &API{CRDVersion: defaultVersion}
217+
resource1.Webhooks = &Webhooks{WebhookVersion: defaultVersion}
218+
c.Resources = []ResourceData{resource1}
194219
Expect(c.resourceAPIVersionCompatible("crd", defaultVersion)).To(BeTrue())
220+
Expect(c.resourceAPIVersionCompatible("webhook", defaultVersion)).To(BeTrue())
195221
})
196222
It("returns true for two resources with matching versions", func() {
197-
gvk1.CRDVersion = defaultVersion
198-
gvk2.CRDVersion = defaultVersion
199-
c.Resources = []GVK{gvk1, gvk2}
223+
resource1.API = &API{CRDVersion: defaultVersion}
224+
resource1.Webhooks = &Webhooks{WebhookVersion: defaultVersion}
225+
resource2.API = &API{CRDVersion: defaultVersion}
226+
resource2.Webhooks = &Webhooks{WebhookVersion: defaultVersion}
227+
c.Resources = []ResourceData{resource1, resource2}
200228
Expect(c.resourceAPIVersionCompatible("crd", defaultVersion)).To(BeTrue())
229+
Expect(c.resourceAPIVersionCompatible("webhook", defaultVersion)).To(BeTrue())
201230
})
202231
It("returns false for one resource with a non-matching version", func() {
203-
gvk1.CRDVersion = v1beta1
204-
c.Resources = []GVK{gvk1}
232+
resource1.API = &API{CRDVersion: v1beta1}
233+
resource1.Webhooks = &Webhooks{WebhookVersion: v1beta1}
234+
c.Resources = []ResourceData{resource1}
205235
Expect(c.resourceAPIVersionCompatible("crd", defaultVersion)).To(BeFalse())
236+
Expect(c.resourceAPIVersionCompatible("webhook", defaultVersion)).To(BeFalse())
206237
})
207238
It("returns false for two resources containing a non-matching version", func() {
208-
gvk1.CRDVersion = v1beta1
209-
gvk2.CRDVersion = defaultVersion
210-
c.Resources = []GVK{gvk1, gvk2}
239+
resource1.API = &API{CRDVersion: v1beta1}
240+
resource1.Webhooks = &Webhooks{WebhookVersion: v1beta1}
241+
resource2.API = &API{CRDVersion: defaultVersion}
242+
resource2.Webhooks = &Webhooks{WebhookVersion: defaultVersion}
243+
c.Resources = []ResourceData{resource1, resource2}
211244
Expect(c.resourceAPIVersionCompatible("crd", defaultVersion)).To(BeFalse())
212-
})
213-
214-
It("returns false for two resources containing a non-matching version (webhooks)", func() {
215-
gvk1.WebhookVersion = v1beta1
216-
gvk2.WebhookVersion = defaultVersion
217-
c.Resources = []GVK{gvk1, gvk2}
218245
Expect(c.resourceAPIVersionCompatible("webhook", defaultVersion)).To(BeFalse())
219246
})
220247
})
@@ -223,35 +250,49 @@ var _ = Describe("Resource Version Compatibility", func() {
223250
var _ = Describe("Config", func() {
224251
var (
225252
c *Config
226-
gvk1, gvk2 GVK
253+
gvk1, gvk2 ResourceData
227254
)
228255

229256
BeforeEach(func() {
230257
c = &Config{}
231-
gvk1 = GVK{Group: "example", Version: "v1", Kind: "TestKind"}
232-
gvk2 = GVK{Group: "example", Version: "v1", Kind: "TestKind2"}
258+
gvk1 = ResourceData{Group: "example", Version: "v1", Kind: "TestKind"}
259+
gvk2 = ResourceData{Group: "example", Version: "v1", Kind: "TestKind2"}
233260
})
234261

235262
Context("UpdateResource", func() {
236263
It("Adds a non-existing resource", func() {
237264
c.UpdateResources(gvk1)
238-
Expect(c.Resources).To(Equal([]GVK{gvk1}))
265+
Expect(c.Resources).To(Equal([]ResourceData{gvk1}))
239266
// Update again to ensure idempotency.
240267
c.UpdateResources(gvk1)
241-
Expect(c.Resources).To(Equal([]GVK{gvk1}))
268+
Expect(c.Resources).To(Equal([]ResourceData{gvk1}))
242269
})
243270
It("Updates an existing resource", func() {
244271
c.UpdateResources(gvk1)
245-
gvk := GVK{Group: gvk1.Group, Version: gvk1.Version, Kind: gvk1.Kind, CRDVersion: "v1"}
246-
c.UpdateResources(gvk)
247-
Expect(c.Resources).To(Equal([]GVK{gvk}))
272+
resource := ResourceData{Group: gvk1.Group, Version: gvk1.Version, Kind: gvk1.Kind}
273+
c.UpdateResources(resource)
274+
Expect(c.Resources).To(Equal([]ResourceData{resource}))
248275
})
249276
It("Updates an existing resource with more than one resource present", func() {
250277
c.UpdateResources(gvk1)
251278
c.UpdateResources(gvk2)
252-
gvk := GVK{Group: gvk1.Group, Version: gvk1.Version, Kind: gvk1.Kind, CRDVersion: "v1"}
279+
gvk := ResourceData{Group: gvk1.Group, Version: gvk1.Version, Kind: gvk1.Kind}
253280
c.UpdateResources(gvk)
254-
Expect(c.Resources).To(Equal([]GVK{gvk, gvk2}))
281+
Expect(c.Resources).To(Equal([]ResourceData{gvk, gvk2}))
282+
})
283+
})
284+
285+
Context("HasGroup", func() {
286+
It("should return true when config has a resource with the group", func() {
287+
c.UpdateResources(gvk1)
288+
Expect(c.Resources).To(Equal([]ResourceData{gvk1}))
289+
Expect(c.HasGroup(gvk1.Group)).To(BeTrue())
255290
})
291+
It("should return false when config has a resource with not the same group", func() {
292+
c.UpdateResources(gvk1)
293+
Expect(c.Resources).To(Equal([]ResourceData{gvk1}))
294+
Expect(c.HasGroup("hasNot")).To(BeFalse())
295+
})
296+
256297
})
257298
})

0 commit comments

Comments
 (0)