-
Notifications
You must be signed in to change notification settings - Fork 339
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(kuma-cp) validate envoy resource
- Loading branch information
1 parent
5695181
commit aa58e78
Showing
4 changed files
with
94 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package template | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"github.com/envoyproxy/go-control-plane/pkg/cache" | ||
"github.com/ghodss/yaml" | ||
"github.com/golang/protobuf/jsonpb" | ||
"github.com/golang/protobuf/proto" | ||
"github.com/golang/protobuf/ptypes" | ||
"github.com/golang/protobuf/ptypes/any" | ||
) | ||
|
||
func ResourceFromYaml(resYaml string) (proto.Message, error) { | ||
json, err := yaml.YAMLToJSON([]byte(resYaml)) | ||
if err != nil { | ||
json = []byte(resYaml) | ||
} | ||
|
||
var anything any.Any | ||
if err := (&jsonpb.Unmarshaler{}).Unmarshal(bytes.NewReader(json), &anything); err != nil { | ||
return nil, err | ||
} | ||
var dyn ptypes.DynamicAny | ||
if err := ptypes.UnmarshalAny(&anything, &dyn); err != nil { | ||
return nil, err | ||
} | ||
p, ok := dyn.Message.(cache.Resource) | ||
if !ok { | ||
return nil, errors.New("xDS resource doesn't implement all required interfaces") | ||
} | ||
if v, ok := p.(interface{ Validate() error }); ok { | ||
if err := v.Validate(); err != nil { | ||
return nil, err | ||
} | ||
} | ||
return p, nil | ||
} |