Merge "Fixed TODO about error definitions"
This commit is contained in:
commit
121adcddc8
@ -46,19 +46,10 @@ func GetCloudData(docBundle document.Bundle) (userData []byte, netConf []byte, e
|
|||||||
func getUserData(docBundle document.Bundle) ([]byte, error) {
|
func getUserData(docBundle document.Bundle) ([]byte, error) {
|
||||||
// find the user-data document
|
// find the user-data document
|
||||||
selector := document.NewEphemeralCloudDataSelector()
|
selector := document.NewEphemeralCloudDataSelector()
|
||||||
docs, err := docBundle.Select(selector)
|
userDataDoc, err := docBundle.SelectOne(selector)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
var userDataDoc document.Document = &document.Factory{}
|
|
||||||
switch numDocsFound := len(docs); {
|
|
||||||
case numDocsFound == 0:
|
|
||||||
return nil, document.ErrDocNotFound{Selector: selector}
|
|
||||||
case numDocsFound > 1:
|
|
||||||
return nil, document.ErrMultipleDocsFound{Selector: selector}
|
|
||||||
case numDocsFound == 1:
|
|
||||||
userDataDoc = docs[0]
|
|
||||||
}
|
|
||||||
|
|
||||||
// finally, try and retrieve the data we want from the document
|
// finally, try and retrieve the data we want from the document
|
||||||
userData, err := decodeData(userDataDoc, userDataKey)
|
userData, err := decodeData(userDataDoc, userDataKey)
|
||||||
@ -72,44 +63,24 @@ func getUserData(docBundle document.Bundle) ([]byte, error) {
|
|||||||
func getNetworkData(docBundle document.Bundle) ([]byte, error) {
|
func getNetworkData(docBundle document.Bundle) ([]byte, error) {
|
||||||
// find the baremetal host indicated as the ephemeral node
|
// find the baremetal host indicated as the ephemeral node
|
||||||
selector := document.NewEphemeralBMHSelector()
|
selector := document.NewEphemeralBMHSelector()
|
||||||
docs, err := docBundle.Select(selector)
|
d, err := docBundle.SelectOne(selector)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var bmhDoc document.Document = &document.Factory{}
|
|
||||||
switch numDocsFound := len(docs); {
|
|
||||||
case numDocsFound == 0:
|
|
||||||
return nil, document.ErrDocNotFound{Selector: selector}
|
|
||||||
case numDocsFound > 1:
|
|
||||||
return nil, document.ErrMultipleDocsFound{Selector: selector}
|
|
||||||
case numDocsFound == 1:
|
|
||||||
bmhDoc = docs[0]
|
|
||||||
}
|
|
||||||
|
|
||||||
// try and find these documents in our bundle
|
// try and find these documents in our bundle
|
||||||
selector, err = document.NewNetworkDataSelector(bmhDoc)
|
selector, err = document.NewNetworkDataSelector(d)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
docs, err = docBundle.Select(selector)
|
d, err = docBundle.SelectOne(selector)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var networkDataDoc document.Document = &document.Factory{}
|
|
||||||
switch numDocsFound := len(docs); {
|
|
||||||
case numDocsFound == 0:
|
|
||||||
return nil, document.ErrDocNotFound{Selector: selector}
|
|
||||||
case numDocsFound > 1:
|
|
||||||
return nil, document.ErrMultipleDocsFound{Selector: selector}
|
|
||||||
case numDocsFound == 1:
|
|
||||||
networkDataDoc = docs[0]
|
|
||||||
}
|
|
||||||
|
|
||||||
// finally, try and retrieve the data we want from the document
|
// finally, try and retrieve the data we want from the document
|
||||||
netData, err := decodeData(networkDataDoc, networkDataKey)
|
netData, err := decodeData(d, networkDataKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -54,7 +54,7 @@ func TestGetCloudData(t *testing.T) {
|
|||||||
labelFilter: "test=ephemeralduplicate",
|
labelFilter: "test=ephemeralduplicate",
|
||||||
expectedUserData: nil,
|
expectedUserData: nil,
|
||||||
expectedNetData: nil,
|
expectedNetData: nil,
|
||||||
expectedErr: document.ErrMultipleDocsFound{
|
expectedErr: document.ErrMultiDocsFound{
|
||||||
Selector: document.NewSelector().
|
Selector: document.NewSelector().
|
||||||
ByLabel(document.EphemeralHostSelector).
|
ByLabel(document.EphemeralHostSelector).
|
||||||
ByKind("BareMetalHost"),
|
ByKind("BareMetalHost"),
|
||||||
|
@ -161,11 +161,8 @@ func TestGetStatusForResource(t *testing.T) {
|
|||||||
testStatusMap, err := cluster.NewStatusMap(bundle)
|
testStatusMap, err := cluster.NewStatusMap(bundle)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// TODO(howell): Replace with with SelectOne when it becomes available
|
doc, err := bundle.SelectOne(tt.selector)
|
||||||
docs, err := bundle.Select(tt.selector)
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Len(t, docs, 1)
|
|
||||||
doc := docs[0]
|
|
||||||
|
|
||||||
actualStatus, err := testStatusMap.GetStatusForResource(tt.testClient, doc)
|
actualStatus, err := testStatusMap.GetStatusForResource(tt.testClient, doc)
|
||||||
if tt.err != nil {
|
if tt.err != nil {
|
||||||
|
@ -16,7 +16,6 @@ package document
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/v3/k8sdeps/kunstruct"
|
"sigs.k8s.io/kustomize/v3/k8sdeps/kunstruct"
|
||||||
@ -192,25 +191,9 @@ func (b *BundleFactory) GetAllDocuments() ([]Document, error) {
|
|||||||
return docSet, nil
|
return docSet, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetByName finds a document by name, error if more than one document found
|
// GetByName finds a document by name
|
||||||
// or if no documents found
|
|
||||||
func (b *BundleFactory) GetByName(name string) (Document, error) {
|
func (b *BundleFactory) GetByName(name string) (Document, error) {
|
||||||
resSet := make([]*resource.Resource, 0, len(b.ResMap.Resources()))
|
return b.SelectOne(NewSelector().ByName(name))
|
||||||
for _, res := range b.ResMap.Resources() {
|
|
||||||
if res.GetName() == name {
|
|
||||||
resSet = append(resSet, res)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// alanmeadows(TODO): improve this and other error potentials by
|
|
||||||
// by adding strongly typed errors
|
|
||||||
switch found := len(resSet); {
|
|
||||||
case found == 0:
|
|
||||||
return &Factory{}, fmt.Errorf("no documents found with name %s", name)
|
|
||||||
case found > 1:
|
|
||||||
return &Factory{}, fmt.Errorf("more than one document found with name %s", name)
|
|
||||||
default:
|
|
||||||
return NewDocument(resSet[0])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Select offers an interface to pass a Selector, built on top of kustomize Selector
|
// Select offers an interface to pass a Selector, built on top of kustomize Selector
|
||||||
@ -253,7 +236,7 @@ func (b *BundleFactory) SelectOne(selector Selector) (Document, error) {
|
|||||||
case numDocsFound == 0:
|
case numDocsFound == 0:
|
||||||
return nil, ErrDocNotFound{Selector: selector}
|
return nil, ErrDocNotFound{Selector: selector}
|
||||||
case numDocsFound > 1:
|
case numDocsFound > 1:
|
||||||
return nil, ErrMultipleDocsFound{Selector: selector}
|
return nil, ErrMultiDocsFound{Selector: selector}
|
||||||
}
|
}
|
||||||
return docSet[0], nil
|
return docSet[0], nil
|
||||||
}
|
}
|
||||||
|
@ -18,11 +18,16 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ErrDocNotFound returned if desired document not found
|
// ErrDocNotFound returned if desired document not found by selector
|
||||||
type ErrDocNotFound struct {
|
type ErrDocNotFound struct {
|
||||||
Selector Selector
|
Selector Selector
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ErrMultiDocsFound returned if multiple documents were found by selector
|
||||||
|
type ErrMultiDocsFound struct {
|
||||||
|
Selector Selector
|
||||||
|
}
|
||||||
|
|
||||||
// ErrDocumentDataKeyNotFound returned if desired key within a document not found
|
// ErrDocumentDataKeyNotFound returned if desired key within a document not found
|
||||||
type ErrDocumentDataKeyNotFound struct {
|
type ErrDocumentDataKeyNotFound struct {
|
||||||
DocName string
|
DocName string
|
||||||
@ -36,23 +41,18 @@ type ErrDocumentMalformed struct {
|
|||||||
Message string
|
Message string
|
||||||
}
|
}
|
||||||
|
|
||||||
// ErrMultipleDocsFound returned if desired document not found
|
func (e ErrDocNotFound) Error() string {
|
||||||
type ErrMultipleDocsFound struct {
|
return fmt.Sprintf("document filtered by selector %v found no documents", e.Selector)
|
||||||
Selector Selector
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e ErrDocNotFound) Error() string {
|
func (e ErrMultiDocsFound) Error() string {
|
||||||
return fmt.Sprintf("Document filtered by selector %v found no documents", e.Selector)
|
return fmt.Sprintf("document filtered by selector %v found more than one document", e.Selector)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e ErrDocumentDataKeyNotFound) Error() string {
|
func (e ErrDocumentDataKeyNotFound) Error() string {
|
||||||
return fmt.Sprintf("Document %q cannot retrieve data key %q", e.DocName, e.Key)
|
return fmt.Sprintf("document %q cannot retrieve data key %q", e.DocName, e.Key)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e ErrDocumentMalformed) Error() string {
|
func (e ErrDocumentMalformed) Error() string {
|
||||||
return fmt.Sprintf("Document %q is malformed: %q", e.DocName, e.Message)
|
return fmt.Sprintf("document %q is malformed: %q", e.DocName, e.Message)
|
||||||
}
|
|
||||||
|
|
||||||
func (e ErrMultipleDocsFound) Error() string {
|
|
||||||
return fmt.Sprintf("Document filtered by selector %v found more than one document", e.Selector)
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user