Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log info fails #126

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions pkg/validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"golang.org/x/exp/maps"
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
"k8s.io/apiextensions-apiserver/pkg/registry/customresource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand Down Expand Up @@ -49,12 +48,18 @@ func New(client openapi.Client) (*Validator, error) {
// It will return errors when there is an issue parsing the object, or if
// it contains fields unknown to the schema, or if the schema was recursive.
func (s *Validator) Parse(document []byte) (schema.GroupVersionKind, *unstructured.Unstructured, error) {
metadata := metav1.TypeMeta{}
if err := yaml.Unmarshal(document, &metadata); err != nil {
// Initialize an empty unstructured object
obj := &unstructured.Unstructured{}

// Parse the entire document into the unstructured object
if err := yaml.Unmarshal(document, obj); err != nil {
omerap12 marked this conversation as resolved.
Show resolved Hide resolved
return schema.GroupVersionKind{}, nil, fmt.Errorf("failed to parse yaml: %w", err)
}

gvk := metadata.GetObjectKind().GroupVersionKind()
// Get the GroupVersionKind of the object
gvk := obj.GroupVersionKind()

// gvk := metadata.GetObjectKind().GroupVersionKind()
if gvk.Empty() {
return schema.GroupVersionKind{}, nil, fmt.Errorf("GVK cannot be empty")
}
Expand All @@ -76,10 +81,13 @@ func (s *Validator) Parse(document []byte) (schema.GroupVersionKind, *unstructur
return gvk, nil, fmt.Errorf("unsupported media type %q", mediaType)
}

// Get the name of the object
objName := obj.GetName()

dec := decoder.DecoderToVersion(info.StrictSerializer, gvk.GroupVersion())
runtimeObj, _, err := dec.Decode(document, &gvk, &unstructured.Unstructured{})
if err != nil {
return gvk, nil, err
return gvk, nil, fmt.Errorf("failed to parse YAML for object - Name: %s version: %s, kind: %s\n%w", objName, gvk.Version, gvk.Kind, err)
}

return gvk, runtimeObj.(*unstructured.Unstructured), nil
Expand Down