-
Notifications
You must be signed in to change notification settings - Fork 5
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
parser: add strict mode #413
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 was deleted.
Oops, something went wrong.
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,16 @@ | ||
testCases: | ||
- description: a test | ||
unknownField: true # not a valid field | ||
wantMatch: true | ||
request: | ||
authority: | ||
- www.example.com | ||
method: | ||
- GET | ||
uri: | ||
- /users | ||
route: | ||
- destination: | ||
host: users.users.svc.cluster.local | ||
port: | ||
number: 80 |
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,16 @@ | ||
apiVersion: networking.istio.io/v1 | ||
kind: VirtualService | ||
metadata: | ||
name: example | ||
namespace: example | ||
spec: | ||
hosts: www.example.com # invalid type | ||
http: | ||
- match: | ||
- uri: | ||
regex: /users(/.*)? | ||
route: | ||
- destination: | ||
host: users.users.svc.cluster.local | ||
port: | ||
number: 80 |
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 |
---|---|---|
@@ -1,70 +1,40 @@ | ||
package parser | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
|
||
"go.uber.org/zap/zapcore" | ||
"golang.org/x/exp/slog" | ||
yamlV3 "gopkg.in/yaml.v3" | ||
networking "istio.io/api/networking/v1alpha3" | ||
v1alpha3 "istio.io/client-go/pkg/apis/networking/v1alpha3" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"istio.io/istio/pilot/pkg/config/kube/crd" | ||
"istio.io/istio/pkg/config/schema/gvk" | ||
) | ||
|
||
func ParseVirtualServices(files []string) ([]*v1alpha3.VirtualService, error) { | ||
out := []*v1alpha3.VirtualService{} | ||
|
||
for _, file := range files { | ||
fileContent, err := os.ReadFile(file) | ||
if err != nil { | ||
return []*v1alpha3.VirtualService{}, fmt.Errorf("reading file '%s' failed: %w", file, err) | ||
return nil, fmt.Errorf("reading file %q failed: %w", file, err) | ||
} | ||
|
||
decoder := yamlV3.NewDecoder(strings.NewReader(string(fileContent))) | ||
|
||
for { | ||
// Reading into interface first. Decoding directly into struct does not work for Uri StringMatch types | ||
var vsInterface interface{} | ||
|
||
if err = decoder.Decode(&vsInterface); err != nil { | ||
// We've read every document in the file and we can break out | ||
if err == io.EOF { | ||
break | ||
} | ||
|
||
slog.Debug("error while trying to unmarshal into interface", zapcore.Field{Key: "file", Type: zapcore.StringType, String: file}) | ||
return out, fmt.Errorf("error while trying to unmarshal into interface (%s): %w", file, err) | ||
} | ||
|
||
jsonBytes, err := json.Marshal(vsInterface) | ||
if err != nil { | ||
slog.Debug("error while trying to marshal to json", zapcore.Field{Key: "file", Type: zapcore.StringType, String: file}) | ||
return out, fmt.Errorf("error while trying to marshal to json (%s): %w", file, err) | ||
} | ||
|
||
meta := &v1.TypeMeta{} | ||
if err = json.Unmarshal(jsonBytes, meta); err != nil { | ||
slog.Debug("error extracting the metadata of the virtualservice", zapcore.Field{Key: "file", Type: zapcore.StringType, String: file}) | ||
configs, _, err := crd.ParseInputs(string(fileContent)) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse CRD %q: %w", file, err) | ||
} | ||
for _, c := range configs { | ||
if c.Meta.GroupVersionKind != gvk.VirtualService { | ||
continue | ||
} | ||
|
||
if meta.Kind != "VirtualService" { | ||
slog.Debug("file is not Kind VirtualService", zapcore.Field{Key: "file", Type: zapcore.StringType, String: file}) | ||
continue | ||
spec, ok := c.Spec.(*networking.VirtualService) | ||
if !ok { | ||
return nil, fmt.Errorf("failed to convert spec in %q to VirtualService: %w", file, err) | ||
} | ||
|
||
virtualService := &v1alpha3.VirtualService{} | ||
if err = json.Unmarshal(jsonBytes, virtualService); err != nil { | ||
slog.Debug("error while trying to unmarshal virtualservice", zapcore.Field{Key: "file", Type: zapcore.StringType, String: file}) | ||
return out, fmt.Errorf("error while trying to unmarshal virtualservice (%s): %w", file, err) | ||
vs := &v1alpha3.VirtualService{ | ||
ObjectMeta: c.ToObjectMeta(), | ||
Spec: *spec, //nolint as deep copying mess up with reflect.DeepEqual comparison. | ||
} | ||
|
||
out = append(out, virtualService) | ||
out = append(out, vs) | ||
} | ||
} | ||
|
||
return out, nil | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️