Skip to content

Commit

Permalink
fix(kuma-cp): don't panic in webhook if k8s object can't convert to c…
Browse files Browse the repository at this point in the history
…ore resource (#4455)

* fix(kuma-cp): don't panic in webhook if k8s object can't convert to core resource
* refactor: better output on error in mapper

Signed-off-by: Mike Beaumont <mjboamail@gmail.com>
  • Loading branch information
michaelbeaumont authored Jun 10, 2022
1 parent 43aa387 commit f4b94a0
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ func (cb *DoNothingPolicy) SetMesh(mesh string) {
cb.Mesh = mesh
}

func (cb *DoNothingPolicy) GetSpec() proto.Message {
return cb.Spec
func (cb *DoNothingPolicy) GetSpec() (proto.Message, error) {
return cb.Spec, nil
}

func (cb *DoNothingPolicy) SetSpec(spec proto.Message) {
Expand Down
12 changes: 10 additions & 2 deletions pkg/plugins/resources/k8s/caching_converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,22 @@ func (c *cachingConverter) ToCoreResource(obj k8s_model.KubernetesObject, out co
if obj.GetResourceVersion() == "" {
// an absent of the ResourceVersion means we decode 'obj' from webhook request,
// all webhooks use SimpleConverter, so this is not supposed to happen
if err := out.SetSpec(obj.GetSpec()); err != nil {
spec, err := obj.GetSpec()
if err != nil {
return err
}
if err := out.SetSpec(spec); err != nil {
return err
}
}
if v, ok := c.cache.Get(key); ok {
return out.SetSpec(v.(core_model.ResourceSpec))
}
if err := out.SetSpec(obj.GetSpec()); err != nil {
spec, err := obj.GetSpec()
if err != nil {
return err
}
if err := out.SetSpec(spec); err != nil {
return err
}
c.cache.SetDefault(key, out.GetSpec())
Expand Down
7 changes: 5 additions & 2 deletions pkg/plugins/resources/k8s/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ func (c *SimpleConverter) ToKubernetesList(rl core_model.ResourceList) (k8s_mode

func (c *SimpleConverter) ToCoreResource(obj k8s_model.KubernetesObject, out core_model.Resource) error {
out.SetMeta(&KubernetesMetaAdapter{ObjectMeta: *obj.GetObjectMeta(), Mesh: obj.GetMesh()})
err := out.SetSpec(obj.GetSpec())
return err
spec, err := obj.GetSpec()
if err != nil {
return err
}
return out.SetSpec(spec)
}

func (c *SimpleConverter) ToCoreList(in k8s_model.KubernetesList, out core_model.ResourceList, predicate k8s_common.ConverterPredicate) error {
Expand Down
Loading

0 comments on commit f4b94a0

Please sign in to comment.