-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* parser: strict parsing manifests and tests Signed-off-by: Fernando Cainelli <fernando.cainelli-external@getyourguide.com>
- Loading branch information
Showing
11 changed files
with
117 additions
and
140 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 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.