Skip to content

Commit

Permalink
Update Config
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 Jan 7, 2021
1 parent 58c9524 commit df7d0b6
Show file tree
Hide file tree
Showing 10 changed files with 1,787 additions and 0 deletions.
75 changes: 75 additions & 0 deletions pkg/config/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package config

import (
"fmt"

"sigs.k8s.io/kubebuilder/v2/pkg/model/resource"
)

// UnsupportedField is returned when a project configuration version does not support
// one of the fields as interface must be common for all the versions
type UnsupportedField struct {
Version Version
Field string
}

// Error implements error interface
func (e UnsupportedField) Error() string {
return fmt.Sprintf("version %s does not support the %s field", e.Version, e.Field)
}

// UnknownResource is returned by Config.GetResource when the provided GVK cannot be found
type UnknownResource struct {
GVK resource.GVK
}

// Error implements error interface
func (e UnknownResource) Error() string {
return fmt.Sprintf("resource %v could not be found", e.GVK)
}

// MarshalError is returned by Config.Marshal when something went wrong while marshalling to YAML
type MarshalError struct {
Err error
}

// Error implements error interface
func (e MarshalError) Error() string {
return fmt.Sprintf("error marshalling project configuration: %v", e.Err)
}

// Unwrap implements Wrapper interface
func (e MarshalError) Unwrap() error {
return e.Err
}

// UnmarshalError is returned by Config.Unmarshal when something went wrong while unmarshalling from YAML
type UnmarshalError struct {
Err error
}

// Error implements error interface
func (e UnmarshalError) Error() string {
return fmt.Sprintf("error unmarshalling project configuration: %v", e.Err)
}

// Unwrap implements Wrapper interface
func (e UnmarshalError) Unwrap() error {
return e.Err
}
93 changes: 93 additions & 0 deletions pkg/config/errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package config

import (
"fmt"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"sigs.k8s.io/kubebuilder/v2/pkg/model/resource"
)

var _ = Describe("UnsupportedField", func() {
var err = UnsupportedField{
Version: Version{Number: 1},
Field: "name",
}

Context("Error", func() {
It("should return the correct error message", func() {
Expect(err.Error()).To(Equal("version 1 does not support the name field"))
})
})
})

var _ = Describe("UnknownResource", func() {
var err = UnknownResource{
GVK: resource.GVK{
Group: "group",
Version: "v1",
Kind: "Kind",
},
}

Context("Error", func() {
It("should return the correct error message", func() {
Expect(err.Error()).To(Equal("resource {group v1 Kind} could not be found"))
})
})
})

var _ = Describe("MarshalError", func() {
var (
wrapped = fmt.Errorf("error message")
err = MarshalError{Err: wrapped}
)

Context("Error", func() {
It("should return the correct error message", func() {
Expect(err.Error()).To(Equal(fmt.Sprintf("error marshalling project configuration: %v", wrapped)))
})
})

Context("Unwrap", func() {
It("should unwrap to the wrapped error", func() {
Expect(err.Unwrap()).To(Equal(wrapped))
})
})
})

var _ = Describe("UnmarshalError", func() {
var (
wrapped = fmt.Errorf("error message")
err = UnmarshalError{Err: wrapped}
)

Context("Error", func() {
It("should return the correct error message", func() {
Expect(err.Error()).To(Equal(fmt.Sprintf("error unmarshalling project configuration: %v", wrapped)))
})
})

Context("Unwrap", func() {
It("should unwrap to the wrapped error", func() {
Expect(err.Unwrap()).To(Equal(wrapped))
})
})
})
103 changes: 103 additions & 0 deletions pkg/config/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package config

import (
"sigs.k8s.io/kubebuilder/v2/pkg/model/resource"
)

// Config defines the interface that project configuration types must follow
type Config interface {
/* Version */

// GetVersion returns the current project version
GetVersion() Version

/* String fields */

// GetDomain returns the project domain
GetDomain() string
// SetDomain sets the project domain
SetDomain(domain string) error

// GetRepository returns the project repository.
GetRepository() string
// SetRepository sets the project repository
SetRepository(repository string) error

// GetName returns the project name
// This method was introduced in project version 3-alpha.
GetName() string
// SetName sets the project name
// This method was introduced in project version 3-alpha.
SetName(name string) error

// GetLayout returns the config layout
// This method was introduced in project version 3-alpha.
GetLayout() string
// SetLayout sets the Config layout
// This method was introduced in project version 3-alpha.
SetLayout(layout string) error

/* Boolean fields */

// IsMultiGroup checks if multi-group is enabled
IsMultiGroup() bool
// SetMultiGroup enables multi-group
SetMultiGroup() error
// ClearMultiGroup disables multi-group
ClearMultiGroup() error

// IsComponentConfig checks if component config is enabled
// This method was introduced in project version 3-alpha.
IsComponentConfig() bool
// SetComponentConfig enables component config
// This method was introduced in project version 3-alpha.
SetComponentConfig() error
// ClearComponentConfig disables component config
// This method was introduced in project version 3-alpha.
ClearComponentConfig() error

/* Resources */

// HasResource checks ifthe provided GVK is stored in the Config
HasResource(gvk resource.GVK) bool
// GetResource returns the stored resource matching the provided GVK
GetResource(gvk resource.GVK) (resource.Resource, error)
// AddResource adds the provided resource if it was not present, no-op if it was already present
AddResource(res resource.Resource) error
// UpdateResource adds the provided resource if it was not present, modifies it if it was already present
UpdateResource(res resource.Resource) error

/* Plugins */

// DecodePluginConfig decodes a plugin config stored in Config into configObj, which must be a pointer.
// This method is intended to be used for custom configuration objects, which were introduced in project version
// 3-alpha.
DecodePluginConfig(key string, configObj interface{}) error
// EncodePluginConfig encodes a config object into Config by overwriting the existing object stored under key.
// This method is intended to be used for custom configuration objects, which were introduced in project version
// 3-alpha.
EncodePluginConfig(key string, configObj interface{}) error

/* Persistence */

// Marshal returns the YAML representation of the Config
Marshal() ([]byte, error)
// Unmarshal loads the Config fields from its YAML representation
Unmarshal([]byte) error
}
29 changes: 29 additions & 0 deletions pkg/config/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package config

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestConfig(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Config Suite")
}
Loading

0 comments on commit df7d0b6

Please sign in to comment.