Skip to content

Commit

Permalink
fix: fix wrong parsing of vm.spec.template.spec.domain.memory.guest i…
Browse files Browse the repository at this point in the history
…n validator

template validator could not parse pointer to resource.Quantity struct.
This PR adds support for it

Signed-off-by: Karel Simon <[email protected]>
  • Loading branch information
ksimon1 authored and kubevirt-bot committed Sep 1, 2023
1 parent a6ee5f8 commit 9fa83e3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
9 changes: 8 additions & 1 deletion internal/template-validator/validation/path/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,14 @@ func (r *Results) AsInt64() ([]int64, error) {
continue
}
}
return nil, fmt.Errorf("mismatching type: %v, not int or resource.Quantity", res[j].Type().Name())
if quantityObj, ok := obj.(*resource.Quantity); ok {
v, ok := quantityObj.AsInt64()
if ok {
ret = append(ret, v)
continue
}
}
return nil, fmt.Errorf("mismatching type: %v, not int or resource.Quantity", res[j].Type())
}
}
return ret, nil
Expand Down
16 changes: 16 additions & 0 deletions internal/template-validator/validation/path/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,22 @@ var _ = Describe("Path", func() {
Expect(vals[0]).To(BeNumerically(">", 1024))
})

It("Should provide integer result for quantity pointer object", func() {
s := "jsonpath::.spec.domain.memory.guest"
p, err := New(s)
Expect(p).To(Not(BeNil()))
Expect(err).ToNot(HaveOccurred())

results, err := p.Find(vmCirros)
Expect(err).ToNot(HaveOccurred())
Expect(results.Len()).To(BeNumerically(">=", 1))

vals, err := results.AsInt64()
Expect(err).ToNot(HaveOccurred())
Expect(vals).To(HaveLen(1))
Expect(vals[0]).To(BeNumerically(">", 2147483647), "value is lower than 2Gi")
})

It("Should provide some string results", func() {
s := "jsonpath::.spec.domain.machine.type"
p, err := New(s)
Expand Down
3 changes: 3 additions & 0 deletions internal/template-validator/validation/test-utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package test_utils

import (
"bytes"

"k8s.io/apimachinery/pkg/util/yaml"

kubevirt "kubevirt.io/api/core/v1"
Expand Down Expand Up @@ -34,6 +35,8 @@ spec:
name: cloudinitdisk
machine:
type: "q35"
memory:
guest: 2Gi
resources:
requests:
memory: 128M
Expand Down

0 comments on commit 9fa83e3

Please sign in to comment.