mirror of
https://github.com/replicatedhq/troubleshoot.git
synced 2026-02-14 18:29:53 +00:00
* feat: add loader APIs to load specs from a list of yaml docs The change introduces a loader package that will contain loader public APIs. The aim of these APIs will be to, given any source of troubleshoot specs, the loaders will fetch the specs and parse out all troubleshoot objects that can be extracted. * Some refactoring * Some more changes * More changes caught when testing vendor portal * Add tests and rename Troubleshoot kinds struct * Additional test * Handle ConfigMap and Secrets with multiple specs in them * Fix failing test * Revert multidoc split implementation * Fix merge conflict * Change LoadFromXXX functions to a single LoadSpecs function
73 lines
1.9 KiB
Go
73 lines
1.9 KiB
Go
package collect
|
|
|
|
import (
|
|
"github.com/pkg/errors"
|
|
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
|
|
"github.com/replicatedhq/troubleshoot/pkg/client/troubleshootclientset/scheme"
|
|
"github.com/replicatedhq/troubleshoot/pkg/docrewrite"
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
)
|
|
|
|
var decoder runtime.Decoder
|
|
|
|
func init() {
|
|
decoder = scheme.Codecs.UniversalDeserializer()
|
|
}
|
|
|
|
func ParseCollectorFromDoc(doc []byte) (*troubleshootv1beta2.Collector, error) {
|
|
doc, err := docrewrite.ConvertToV1Beta2(doc)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "failed to convert to v1beta2")
|
|
}
|
|
|
|
obj, _, err := decoder.Decode(doc, nil, nil)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "failed to parse document")
|
|
}
|
|
|
|
collector, ok := obj.(*troubleshootv1beta2.Collector)
|
|
if ok {
|
|
return collector, nil
|
|
}
|
|
|
|
return nil, errors.New("spec was not parseable as a collector kind")
|
|
}
|
|
|
|
func ParseHostCollectorFromDoc(doc []byte) (*troubleshootv1beta2.HostCollector, error) {
|
|
doc, err := docrewrite.ConvertToV1Beta2(doc)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "failed to convert to v1beta2")
|
|
}
|
|
|
|
obj, _, err := decoder.Decode(doc, nil, nil)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "failed to parse document")
|
|
}
|
|
|
|
collector, ok := obj.(*troubleshootv1beta2.HostCollector)
|
|
if ok {
|
|
return collector, nil
|
|
}
|
|
|
|
return nil, errors.New("spec was not parseable as a host collector kind")
|
|
}
|
|
|
|
func ParseRemoteCollectorFromDoc(doc []byte) (*troubleshootv1beta2.RemoteCollector, error) {
|
|
doc, err := docrewrite.ConvertToV1Beta2(doc)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "failed to convert to v1beta2")
|
|
}
|
|
|
|
obj, _, err := decoder.Decode(doc, nil, nil)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "failed to parse document")
|
|
}
|
|
|
|
collector, ok := obj.(*troubleshootv1beta2.RemoteCollector)
|
|
if ok {
|
|
return collector, nil
|
|
}
|
|
|
|
return nil, errors.New("spec was not parseable as a remote collector kind")
|
|
}
|