Skip to content

Commit

Permalink
Update Options and Resource models
Browse files Browse the repository at this point in the history
Signed-off-by: Adrian Orive <adrian.orive.oneca@gmail.com>
  • Loading branch information
Adirio committed Dec 17, 2020
1 parent 4073834 commit 30d8135
Show file tree
Hide file tree
Showing 50 changed files with 1,725 additions and 1,154 deletions.
108 changes: 90 additions & 18 deletions pkg/model/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,86 @@ type ResourceData struct {

// API contains information about scaffolded APIs
type API struct {
// CRDVersion holds the CustomResourceDefinition API version used for the ResourceData.
// 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:"-"`
}

// Update combines fields of the APIs of two resources.
func (api *API) Update(other *API) error {
// If other is nil, nothing to merge
if other == nil {
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

return nil
}

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

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

// Defaulting specifies if a defaulting webhook is associated to the resource.
Defaulting bool `json:"-"`

// Validation specifies if a validation webhook is associated to the resource.
Validation bool `json:"-"`

// Conversion specifies if a conversion webhook is associated to the resource.
Conversion bool `json:"-"`
}

// Update combines fields of the webhooks of two resources.
func (webhooks *Webhooks) Update(other *Webhooks) error {
// If other is nil, nothing to merge
if other == nil {
return nil
}

// Update the version.
if other.WebhookVersion != "" {
if webhooks.WebhookVersion == "" {
webhooks.WebhookVersion = other.WebhookVersion
} else if webhooks.WebhookVersion != other.WebhookVersion {
return fmt.Errorf("webhook versions do not match")
}
}

// Update defaulting.
webhooks.Defaulting = webhooks.Defaulting || other.Defaulting

// Update validation.
webhooks.Validation = webhooks.Validation || other.Validation

// Update conversion.
webhooks.Conversion = webhooks.Conversion || other.Conversion

return nil
}

// IsEmpty returns if the Webhooks' fields all contain zero-values.
func (webhooks Webhooks) IsEmpty() bool {
return webhooks.WebhookVersion == "" && !webhooks.Defaulting && !webhooks.Validation && !webhooks.Conversion
}

// isGVKEqualTo compares it with another resource
Expand All @@ -195,36 +267,36 @@ func (r ResourceData) isGVKEqualTo(other ResourceData) bool {
// merge combines fields of two GVKs that have matching group, version, and kind,
// favoring the receiver's values.
func (r *ResourceData) merge(other ResourceData) {
if other.Webhooks != nil {
if r.Webhooks == nil {
r.Webhooks = other.Webhooks
} else {
r.Webhooks.merge(other.Webhooks)
}
}

if other.API != nil {
if r.API == nil {
r.API = other.API
} else {
r.API.merge(other.API)
}
}

if other.Webhooks != nil {
if r.Webhooks == nil {
r.Webhooks = other.Webhooks
} else {
r.Webhooks.merge(other.Webhooks)
}
}
}

// merge compares it with another webhook by setting each webhook type individually so existing values are
// merge compares it with another api by setting each api type individually so existing values are
// not overwritten.
func (w *Webhooks) merge(other *Webhooks) {
if w.WebhookVersion == "" && other.WebhookVersion != "" {
w.WebhookVersion = other.WebhookVersion
func (api *API) merge(other *API) {
if api.CRDVersion == "" && other.CRDVersion != "" {
api.CRDVersion = other.CRDVersion
}
}

// merge compares it with another api by setting each api type individually so existing values are
// merge compares it with another webhook by setting each webhook type individually so existing values are
// not overwritten.
func (a *API) merge(other *API) {
if a.CRDVersion == "" && other.CRDVersion != "" {
a.CRDVersion = other.CRDVersion
func (webhooks *Webhooks) merge(other *Webhooks) {
if webhooks.WebhookVersion == "" && other.WebhookVersion != "" {
webhooks.WebhookVersion = other.WebhookVersion
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/model/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var _ = Describe("PluginConfig", func() {
const defaultWebhookVersion = "v1"

resource := ResourceData{Group: "Foo", Kind: "Baz", Version: "v1"}
resource.Webhooks = &Webhooks{defaultWebhookVersion}
resource.Webhooks = &Webhooks{WebhookVersion: defaultWebhookVersion}

It("should return true when has the ResourceData is equals", func() {
Expect(resource.isGVKEqualTo(ResourceData{Group: "Foo", Kind: "Baz", Version: "v1"})).To(BeTrue())
Expand Down
Loading

0 comments on commit 30d8135

Please sign in to comment.