Skip to content

Commit

Permalink
Remove Webhook and CR versions since it is no longer used
Browse files Browse the repository at this point in the history
The versions were added when we v1beta1 was deprecated and we need to
support both v1beta1 and v1. It was in k8s 1.16 and is no longer
used since k8s 1.21 where v1beta1 was removed. Either go/v4 does
not support those old versions as any dependency. Therefore,
we are cleaning the code source to have a better manuntance ability.
  • Loading branch information
camilamacedo86 committed Sep 22, 2024
1 parent 983a929 commit 8ab4559
Show file tree
Hide file tree
Showing 36 changed files with 552 additions and 355 deletions.
2 changes: 0 additions & 2 deletions docs/book/src/cronjob-tutorial/testdata/project/PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ projectName: project
repo: tutorial.kubebuilder.io/project
resources:
- api:
crdVersion: v1
namespaced: true
controller: true
domain: tutorial.kubebuilder.io
Expand All @@ -20,5 +19,4 @@ resources:
webhooks:
defaulting: true
validation: true
webhookVersion: v1
version: "3"
1 change: 0 additions & 1 deletion docs/book/src/getting-started/testdata/project/PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ projectName: project
repo: example.com/memcached
resources:
- api:
crdVersion: v1
namespaced: true
controller: true
domain: example.com
Expand Down
4 changes: 0 additions & 4 deletions docs/book/src/multiversion-tutorial/testdata/project/PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ projectName: project
repo: tutorial.kubebuilder.io/project
resources:
- api:
crdVersion: v1
namespaced: true
controller: true
domain: tutorial.kubebuilder.io
Expand All @@ -20,9 +19,7 @@ resources:
webhooks:
defaulting: true
validation: true
webhookVersion: v1
- api:
crdVersion: v1
namespaced: true
domain: tutorial.kubebuilder.io
group: batch
Expand All @@ -33,5 +30,4 @@ resources:
conversion: true
defaulting: true
validation: true
webhookVersion: v1
version: "3"
3 changes: 1 addition & 2 deletions pkg/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ layout: go.kubebuilder.io/v3
projectName: demo-zeus-operator
repo: github.com/jmrodri/demo-zeus-operator
resources:
- crdVersion: v1
group: test
- group: test
kind: Test
version: v1
version: 3-alpha
Expand Down
2 changes: 0 additions & 2 deletions pkg/cli/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,9 @@ var _ = Describe("resourceOptions", func() {
// Plural is checked in the next test
Expect(resource.Path).To(Equal(""))
Expect(resource.API).NotTo(BeNil())
Expect(resource.API.CRDVersion).To(Equal(""))
Expect(resource.API.Namespaced).To(BeFalse())
Expect(resource.Controller).To(BeFalse())
Expect(resource.Webhooks).NotTo(BeNil())
Expect(resource.Webhooks.WebhookVersion).To(Equal(""))
Expect(resource.Webhooks.Defaulting).To(BeFalse())
Expect(resource.Webhooks.Validation).To(BeFalse())
Expect(resource.Webhooks.Conversion).To(BeFalse())
Expand Down
4 changes: 0 additions & 4 deletions pkg/config/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,6 @@ type Config interface {

// HasGroup checks if the provided group is the same as any of the tracked resources.
HasGroup(group string) bool
// ListCRDVersions returns a list of the CRD versions in use by the tracked resources.
ListCRDVersions() []string
// ListWebhookVersions returns a list of the webhook versions in use by the tracked resources.
ListWebhookVersions() []string

/* Plugins */

Expand Down
36 changes: 0 additions & 36 deletions pkg/config/v3/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,42 +253,6 @@ func (c Cfg) HasGroup(group string) bool {
return false
}

// ListCRDVersions implements config.Config
func (c Cfg) ListCRDVersions() []string {
// Make a map to remove duplicates
versionSet := make(map[string]struct{})
for _, r := range c.Resources {
if r.API != nil && r.API.CRDVersion != "" {
versionSet[r.API.CRDVersion] = struct{}{}
}
}

// Convert the map into a slice
versions := make([]string, 0, len(versionSet))
for version := range versionSet {
versions = append(versions, version)
}
return versions
}

// ListWebhookVersions implements config.Config
func (c Cfg) ListWebhookVersions() []string {
// Make a map to remove duplicates
versionSet := make(map[string]struct{})
for _, r := range c.Resources {
if r.Webhooks != nil && r.Webhooks.WebhookVersion != "" {
versionSet[r.Webhooks.WebhookVersion] = struct{}{}
}
}

// Convert the map into a slice
versions := make([]string, 0, len(versionSet))
for version := range versionSet {
versions = append(versions, version)
}
return versions
}

// DecodePluginConfig implements config.Config
func (c Cfg) DecodePluginConfig(key string, configObj interface{}) error {
if len(c.Plugins) == 0 {
Expand Down
81 changes: 8 additions & 73 deletions pkg/config/v3/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package v3

import (
"errors"
"sort"
"testing"

. "github.com/onsi/ginkgo/v2"
Expand Down Expand Up @@ -145,15 +144,13 @@ var _ = Describe("Cfg", func() {
Plural: "kinds",
Path: "api/v1",
API: &resource.API{
CRDVersion: "v1",
Namespaced: true,
},
Controller: true,
Webhooks: &resource.Webhooks{
WebhookVersion: "v1",
Defaulting: true,
Validation: true,
Conversion: true,
Defaulting: true,
Validation: true,
Conversion: true,
},
}
resWithoutPlural = res.Copy()
Expand All @@ -172,15 +169,13 @@ var _ = Describe("Cfg", func() {
Expect(result.API).To(BeNil())
} else {
Expect(result.API).NotTo(BeNil())
Expect(result.API.CRDVersion).To(Equal(expected.API.CRDVersion))
Expect(result.API.Namespaced).To(Equal(expected.API.Namespaced))
}
Expect(result.Controller).To(Equal(expected.Controller))
if expected.Webhooks == nil {
Expect(result.Webhooks).To(BeNil())
} else {
Expect(result.Webhooks).NotTo(BeNil())
Expect(result.Webhooks.WebhookVersion).To(Equal(expected.Webhooks.WebhookVersion))
Expect(result.Webhooks.Defaulting).To(Equal(expected.Webhooks.Defaulting))
Expect(result.Webhooks.Validation).To(Equal(expected.Webhooks.Validation))
Expect(result.Webhooks.Conversion).To(Equal(expected.Webhooks.Conversion))
Expand Down Expand Up @@ -283,62 +278,6 @@ var _ = Describe("Cfg", func() {
c.Resources = append(c.Resources, res)
Expect(c.HasGroup("other-group")).To(BeFalse())
})

It("ListCRDVersions should return an empty list with no tracked resources", func() {
Expect(c.ListCRDVersions()).To(BeEmpty())
})

It("ListCRDVersions should return a list of tracked resources CRD versions", func() {
c.Resources = append(c.Resources,
resource.Resource{
GVK: resource.GVK{
Group: res.Group,
Version: res.Version,
Kind: res.Kind,
},
API: &resource.API{CRDVersion: "v1beta1"},
},
resource.Resource{
GVK: resource.GVK{
Group: res.Group,
Version: res.Version,
Kind: "OtherKind",
},
API: &resource.API{CRDVersion: "v1"},
},
)
versions := c.ListCRDVersions()
sort.Strings(versions) // ListCRDVersions has no order guarantee so sorting for reproducibility
Expect(versions).To(Equal([]string{"v1", "v1beta1"}))
})

It("ListWebhookVersions should return an empty list with no tracked resources", func() {
Expect(c.ListWebhookVersions()).To(BeEmpty())
})

It("ListWebhookVersions should return a list of tracked resources webhook versions", func() {
c.Resources = append(c.Resources,
resource.Resource{
GVK: resource.GVK{
Group: res.Group,
Version: res.Version,
Kind: res.Kind,
},
Webhooks: &resource.Webhooks{WebhookVersion: "v1beta1"},
},
resource.Resource{
GVK: resource.GVK{
Group: res.Group,
Version: res.Version,
Kind: "OtherKind",
},
Webhooks: &resource.Webhooks{WebhookVersion: "v1"},
},
)
versions := c.ListWebhookVersions()
sort.Strings(versions) // ListWebhookVersions has no order guarantee so sorting for reproducibility
Expect(versions).To(Equal([]string{"v1", "v1beta1"}))
})
})

Context("Plugins", func() {
Expand Down Expand Up @@ -461,9 +400,9 @@ var _ = Describe("Cfg", func() {
Version: "v1",
Kind: "Kind2",
},
API: &resource.API{CRDVersion: "v1"},
API: &resource.API{},
Controller: true,
Webhooks: &resource.Webhooks{WebhookVersion: "v1"},
Webhooks: &resource.Webhooks{},
},
{
GVK: resource.GVK{
Expand All @@ -482,15 +421,13 @@ var _ = Describe("Cfg", func() {
Kind: "Kind",
},
API: &resource.API{
CRDVersion: "v1",
Namespaced: true,
},
Controller: true,
Webhooks: &resource.Webhooks{
WebhookVersion: "v1",
Defaulting: true,
Validation: true,
Conversion: true,
Defaulting: true,
Validation: true,
Conversion: true,
},
},
},
Expand Down Expand Up @@ -539,7 +476,6 @@ resources:
kind: Kind
version: v1
- api:
crdVersion: v1
controller: true
group: group
kind: Kind2
Expand All @@ -551,7 +487,6 @@ resources:
plural: kindes
version: v1-beta
- api:
crdVersion: v1
namespaced: true
controller: true
group: group2
Expand Down
24 changes: 2 additions & 22 deletions pkg/model/resource/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,15 @@ limitations under the License.

package resource

import (
"fmt"
)

// API contains information about scaffolded APIs
type API struct {
// CRDVersion holds the CustomResourceDefinition API version used for the resource.
CRDVersion string `json:"crdVersion,omitempty"`

// Namespaced is true if the API is namespaced.
Namespaced bool `json:"namespaced,omitempty"`
}

// Validate checks that the API is valid.
func (api API) Validate() error {
// Validate the CRD version
if err := validateAPIVersion(api.CRDVersion); err != nil {
return fmt.Errorf("invalid CRD version: %w", err)
}

// TODO: Validate API
return nil
}

Expand All @@ -53,15 +42,6 @@ func (api *API) Update(other *API) error {
return nil
}

// Update the version.
if other.CRDVersion != "" {
if api.CRDVersion == "" {
api.CRDVersion = other.CRDVersion
} else if api.CRDVersion != other.CRDVersion {
return fmt.Errorf("CRD versions do not match")
}
}

// Update the namespace.
api.Namespaced = api.Namespaced || other.Namespaced

Expand All @@ -70,5 +50,5 @@ func (api *API) Update(other *API) error {

// IsEmpty returns if the API's fields all contain zero-values.
func (api API) IsEmpty() bool {
return api.CRDVersion == "" && !api.Namespaced
return !api.Namespaced
}
Loading

0 comments on commit 8ab4559

Please sign in to comment.