From 84a9ed657f19acfe2b1bc87d20898d72f269fb4a Mon Sep 17 00:00:00 2001 From: Fernando Cainelli Date: Wed, 4 Sep 2024 15:33:23 +0200 Subject: [PATCH] support v1 apis (#418) Signed-off-by: Fernando Cainelli --- examples/delegate_virtualservice.yml | 2 +- examples/multidocument_virtualservice.yml | 2 +- internal/pkg/parser/virtualservice.go | 10 +- internal/pkg/unit/unit.go | 14 +- internal/pkg/unit/unit_test.go | 186 +++++++++++----------- 5 files changed, 107 insertions(+), 107 deletions(-) diff --git a/examples/delegate_virtualservice.yml b/examples/delegate_virtualservice.yml index 5d89e5a2..c3152cb7 100644 --- a/examples/delegate_virtualservice.yml +++ b/examples/delegate_virtualservice.yml @@ -1,5 +1,5 @@ --- -apiVersion: networking.istio.io/v1alpha3 +apiVersion: networking.istio.io/v1 kind: VirtualService metadata: name: merchants diff --git a/examples/multidocument_virtualservice.yml b/examples/multidocument_virtualservice.yml index ad7b6eaa..8173f041 100644 --- a/examples/multidocument_virtualservice.yml +++ b/examples/multidocument_virtualservice.yml @@ -23,7 +23,7 @@ spec: set: x-custom-header: ok --- -apiVersion: networking.istio.io/v1alpha3 +apiVersion: networking.istio.io/v1beta1 kind: VirtualService metadata: name: example diff --git a/internal/pkg/parser/virtualservice.go b/internal/pkg/parser/virtualservice.go index d1ecdb4a..4950b1ee 100644 --- a/internal/pkg/parser/virtualservice.go +++ b/internal/pkg/parser/virtualservice.go @@ -4,14 +4,14 @@ import ( "fmt" "os" - networking "istio.io/api/networking/v1alpha3" - v1alpha3 "istio.io/client-go/pkg/apis/networking/v1alpha3" + networking "istio.io/api/networking/v1" + v1 "istio.io/client-go/pkg/apis/networking/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{} +func ParseVirtualServices(files []string) ([]*v1.VirtualService, error) { + out := []*v1.VirtualService{} for _, file := range files { fileContent, err := os.ReadFile(file) if err != nil { @@ -29,7 +29,7 @@ func ParseVirtualServices(files []string) ([]*v1alpha3.VirtualService, error) { if !ok { return nil, fmt.Errorf("failed to convert spec in %q to VirtualService: %w", file, err) } - vs := &v1alpha3.VirtualService{ + vs := &v1.VirtualService{ ObjectMeta: c.ToObjectMeta(), Spec: *spec, //nolint as deep copying mess up with reflect.DeepEqual comparison. } diff --git a/internal/pkg/unit/unit.go b/internal/pkg/unit/unit.go index 9957da4d..4e5fff2b 100644 --- a/internal/pkg/unit/unit.go +++ b/internal/pkg/unit/unit.go @@ -9,8 +9,8 @@ import ( "slices" "github.com/getyourguide/istio-config-validator/internal/pkg/parser" - networkingv1alpha3 "istio.io/api/networking/v1alpha3" - v1alpha3 "istio.io/client-go/pkg/apis/networking/v1alpha3" + networking "istio.io/api/networking/v1" + v1 "istio.io/client-go/pkg/apis/networking/v1" ) // Run is the entrypoint to run all unit tests defined in test cases @@ -56,7 +56,7 @@ func Run(testfiles, configfiles []string, strict bool) ([]string, []string, erro return summary, details, fmt.Errorf("error getting delegate virtual service: %v", err) } checkHosts = false - route, err = GetRoute(input, []*v1alpha3.VirtualService{vs}, checkHosts) + route, err = GetRoute(input, []*v1.VirtualService{vs}, checkHosts) if err != nil { details = append(details, fmt.Sprintf("FAIL input:[%v]", input)) return summary, details, fmt.Errorf("error getting destinations for delegate %v: %v", route.Delegate, err) @@ -100,7 +100,7 @@ func Run(testfiles, configfiles []string, strict bool) ([]string, []string, erro } // GetRoute returns the route that matched a given input. -func GetRoute(input parser.Input, virtualServices []*v1alpha3.VirtualService, checkHosts bool) (*networkingv1alpha3.HTTPRoute, error) { +func GetRoute(input parser.Input, virtualServices []*v1.VirtualService, checkHosts bool) (*networking.HTTPRoute, error) { for _, vs := range virtualServices { spec := &vs.Spec if checkHosts && !slices.Contains(spec.Hosts, input.Authority) { @@ -113,7 +113,7 @@ func GetRoute(input parser.Input, virtualServices []*v1alpha3.VirtualService, ch } for _, matchBlock := range httpRoute.Match { if match, err := matchRequest(input, matchBlock); err != nil { - return &networkingv1alpha3.HTTPRoute{}, err + return &networking.HTTPRoute{}, err } else if match { return httpRoute, nil } @@ -121,11 +121,11 @@ func GetRoute(input parser.Input, virtualServices []*v1alpha3.VirtualService, ch } } - return &networkingv1alpha3.HTTPRoute{}, nil + return &networking.HTTPRoute{}, nil } // GetDelegatedVirtualService returns the virtualservice matching namespace/name matching the delegate argument. -func GetDelegatedVirtualService(delegate *networkingv1alpha3.Delegate, virtualServices []*v1alpha3.VirtualService) (*v1alpha3.VirtualService, error) { +func GetDelegatedVirtualService(delegate *networking.Delegate, virtualServices []*v1.VirtualService) (*v1.VirtualService, error) { for _, vs := range virtualServices { if vs.Name == delegate.Name { if delegate.Namespace != "" && vs.Namespace != delegate.Namespace { diff --git a/internal/pkg/unit/unit_test.go b/internal/pkg/unit/unit_test.go index 7f56223b..733eb4a3 100644 --- a/internal/pkg/unit/unit_test.go +++ b/internal/pkg/unit/unit_test.go @@ -6,9 +6,9 @@ import ( "github.com/getyourguide/istio-config-validator/internal/pkg/parser" "github.com/stretchr/testify/require" - networkingv1alpha3 "istio.io/api/networking/v1alpha3" - v1alpha3 "istio.io/client-go/pkg/apis/networking/v1alpha3" - v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + networking "istio.io/api/networking/v1" + v1 "istio.io/client-go/pkg/apis/networking/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) func TestRun(t *testing.T) { @@ -30,26 +30,26 @@ func TestRunDelegate(t *testing.T) { func TestGetRoute(t *testing.T) { type args struct { input parser.Input - virtualServices []*v1alpha3.VirtualService + virtualServices []*v1.VirtualService checkHosts bool } tests := []struct { name string args args - want *networkingv1alpha3.HTTPRoute + want *networking.HTTPRoute wantErr bool }{ { name: "no host match, empty destination", args: args{ input: parser.Input{Authority: "www.exemple.com", URI: "/"}, - virtualServices: []*v1alpha3.VirtualService{{ - Spec: networkingv1alpha3.VirtualService{ + virtualServices: []*v1.VirtualService{{ + Spec: networking.VirtualService{ Hosts: []string{"www.another-example.com"}, - Http: []*networkingv1alpha3.HTTPRoute{{ - Match: []*networkingv1alpha3.HTTPMatchRequest{{ - Uri: &networkingv1alpha3.StringMatch{ - MatchType: &networkingv1alpha3.StringMatch_Exact{ + Http: []*networking.HTTPRoute{{ + Match: []*networking.HTTPMatchRequest{{ + Uri: &networking.StringMatch{ + MatchType: &networking.StringMatch_Exact{ Exact: "/", }, }, @@ -59,27 +59,27 @@ func TestGetRoute(t *testing.T) { }}, checkHosts: true, }, - want: &networkingv1alpha3.HTTPRoute{}, + want: &networking.HTTPRoute{}, wantErr: false, }, { name: "match a fallback destination", args: args{ input: parser.Input{Authority: "www.example.com", URI: "/path-to-fallback"}, - virtualServices: []*v1alpha3.VirtualService{{ - Spec: networkingv1alpha3.VirtualService{ + virtualServices: []*v1.VirtualService{{ + Spec: networking.VirtualService{ Hosts: []string{"www.example.com"}, - Http: []*networkingv1alpha3.HTTPRoute{{ - Match: []*networkingv1alpha3.HTTPMatchRequest{{ - Uri: &networkingv1alpha3.StringMatch{ - MatchType: &networkingv1alpha3.StringMatch_Exact{ + Http: []*networking.HTTPRoute{{ + Match: []*networking.HTTPMatchRequest{{ + Uri: &networking.StringMatch{ + MatchType: &networking.StringMatch_Exact{ Exact: "/", }, }, }}, }, { - Route: []*networkingv1alpha3.HTTPRouteDestination{{ - Destination: &networkingv1alpha3.Destination{ + Route: []*networking.HTTPRouteDestination{{ + Destination: &networking.Destination{ Host: "fallback.fallback.svc.cluster.local", }, }}, @@ -88,9 +88,9 @@ func TestGetRoute(t *testing.T) { }}, checkHosts: true, }, - want: &networkingv1alpha3.HTTPRoute{ - Route: []*networkingv1alpha3.HTTPRouteDestination{{ - Destination: &networkingv1alpha3.Destination{ + want: &networking.HTTPRoute{ + Route: []*networking.HTTPRouteDestination{{ + Destination: &networking.Destination{ Host: "fallback.fallback.svc.cluster.local", }, }}, @@ -101,18 +101,18 @@ func TestGetRoute(t *testing.T) { name: "match single destination, multiple virtualservices", args: args{ input: parser.Input{Authority: "www.match.com", URI: "/"}, - virtualServices: []*v1alpha3.VirtualService{{ - Spec: networkingv1alpha3.VirtualService{ + virtualServices: []*v1.VirtualService{{ + Spec: networking.VirtualService{ Hosts: []string{"www.notmatch.com"}, - Http: []*networkingv1alpha3.HTTPRoute{{ - Route: []*networkingv1alpha3.HTTPRouteDestination{{ - Destination: &networkingv1alpha3.Destination{ + Http: []*networking.HTTPRoute{{ + Route: []*networking.HTTPRouteDestination{{ + Destination: &networking.Destination{ Host: "notmatch.notmatch.svc.cluster.local", }, }}, - Match: []*networkingv1alpha3.HTTPMatchRequest{{ - Uri: &networkingv1alpha3.StringMatch{ - MatchType: &networkingv1alpha3.StringMatch_Exact{ + Match: []*networking.HTTPMatchRequest{{ + Uri: &networking.StringMatch{ + MatchType: &networking.StringMatch_Exact{ Exact: "/", }, }, @@ -120,17 +120,17 @@ func TestGetRoute(t *testing.T) { }}, }, }, { - Spec: networkingv1alpha3.VirtualService{ + Spec: networking.VirtualService{ Hosts: []string{"www.match.com"}, - Http: []*networkingv1alpha3.HTTPRoute{{ - Route: []*networkingv1alpha3.HTTPRouteDestination{{ - Destination: &networkingv1alpha3.Destination{ + Http: []*networking.HTTPRoute{{ + Route: []*networking.HTTPRouteDestination{{ + Destination: &networking.Destination{ Host: "match.match.svc.cluster.local", }, }}, - Match: []*networkingv1alpha3.HTTPMatchRequest{{ - Uri: &networkingv1alpha3.StringMatch{ - MatchType: &networkingv1alpha3.StringMatch_Exact{ + Match: []*networking.HTTPMatchRequest{{ + Uri: &networking.StringMatch{ + MatchType: &networking.StringMatch_Exact{ Exact: "/", }, }, @@ -140,14 +140,14 @@ func TestGetRoute(t *testing.T) { }}, checkHosts: true, }, - want: &networkingv1alpha3.HTTPRoute{ - Route: []*networkingv1alpha3.HTTPRouteDestination{{ - Destination: &networkingv1alpha3.Destination{ + want: &networking.HTTPRoute{ + Route: []*networking.HTTPRouteDestination{{ + Destination: &networking.Destination{ Host: "match.match.svc.cluster.local", }, - }}, Match: []*networkingv1alpha3.HTTPMatchRequest{{ - Uri: &networkingv1alpha3.StringMatch{ - MatchType: &networkingv1alpha3.StringMatch_Exact{ + }}, Match: []*networking.HTTPMatchRequest{{ + Uri: &networking.StringMatch{ + MatchType: &networking.StringMatch_Exact{ Exact: "/", }, }, @@ -159,18 +159,18 @@ func TestGetRoute(t *testing.T) { name: "match and assert rewrite and destination", args: args{ input: parser.Input{Authority: "www.match.com", URI: "/"}, - virtualServices: []*v1alpha3.VirtualService{{ - Spec: networkingv1alpha3.VirtualService{ + virtualServices: []*v1.VirtualService{{ + Spec: networking.VirtualService{ Hosts: []string{"www.match.com"}, - Http: []*networkingv1alpha3.HTTPRoute{{ - Route: []*networkingv1alpha3.HTTPRouteDestination{{ - Destination: &networkingv1alpha3.Destination{ + Http: []*networking.HTTPRoute{{ + Route: []*networking.HTTPRouteDestination{{ + Destination: &networking.Destination{ Host: "match.match.svc.cluster.local", }, }}, - Match: []*networkingv1alpha3.HTTPMatchRequest{{ - Uri: &networkingv1alpha3.StringMatch{ - MatchType: &networkingv1alpha3.StringMatch_Exact{ + Match: []*networking.HTTPMatchRequest{{ + Uri: &networking.StringMatch{ + MatchType: &networking.StringMatch_Exact{ Exact: "/", }, }, @@ -180,15 +180,15 @@ func TestGetRoute(t *testing.T) { }}, checkHosts: true, }, - want: &networkingv1alpha3.HTTPRoute{ - Route: []*networkingv1alpha3.HTTPRouteDestination{{ - Destination: &networkingv1alpha3.Destination{ + want: &networking.HTTPRoute{ + Route: []*networking.HTTPRouteDestination{{ + Destination: &networking.Destination{ Host: "match.match.svc.cluster.local", }, }}, - Match: []*networkingv1alpha3.HTTPMatchRequest{{ - Uri: &networkingv1alpha3.StringMatch{ - MatchType: &networkingv1alpha3.StringMatch_Exact{ + Match: []*networking.HTTPMatchRequest{{ + Uri: &networking.StringMatch{ + MatchType: &networking.StringMatch_Exact{ Exact: "/", }, }, @@ -200,17 +200,17 @@ func TestGetRoute(t *testing.T) { name: "match virtualservice with no hosts", args: args{ input: parser.Input{Authority: "www.match.com", URI: "/"}, - virtualServices: []*v1alpha3.VirtualService{{ - Spec: networkingv1alpha3.VirtualService{ - Http: []*networkingv1alpha3.HTTPRoute{{ - Route: []*networkingv1alpha3.HTTPRouteDestination{{ - Destination: &networkingv1alpha3.Destination{ + virtualServices: []*v1.VirtualService{{ + Spec: networking.VirtualService{ + Http: []*networking.HTTPRoute{{ + Route: []*networking.HTTPRouteDestination{{ + Destination: &networking.Destination{ Host: "match.match.svc.cluster.local", }, }}, - Match: []*networkingv1alpha3.HTTPMatchRequest{{ - Uri: &networkingv1alpha3.StringMatch{ - MatchType: &networkingv1alpha3.StringMatch_Exact{ + Match: []*networking.HTTPMatchRequest{{ + Uri: &networking.StringMatch{ + MatchType: &networking.StringMatch_Exact{ Exact: "/", }, }, @@ -220,14 +220,14 @@ func TestGetRoute(t *testing.T) { }}, checkHosts: false, }, - want: &networkingv1alpha3.HTTPRoute{ - Route: []*networkingv1alpha3.HTTPRouteDestination{{ - Destination: &networkingv1alpha3.Destination{ + want: &networking.HTTPRoute{ + Route: []*networking.HTTPRouteDestination{{ + Destination: &networking.Destination{ Host: "match.match.svc.cluster.local", }, - }}, Match: []*networkingv1alpha3.HTTPMatchRequest{{ - Uri: &networkingv1alpha3.StringMatch{ - MatchType: &networkingv1alpha3.StringMatch_Exact{ + }}, Match: []*networking.HTTPMatchRequest{{ + Uri: &networking.StringMatch{ + MatchType: &networking.StringMatch_Exact{ Exact: "/", }, }, @@ -252,30 +252,30 @@ func TestGetRoute(t *testing.T) { func TestGetDelegatedVirtualService(t *testing.T) { type args struct { - delegate *networkingv1alpha3.Delegate - virtualServices []*v1alpha3.VirtualService + delegate *networking.Delegate + virtualServices []*v1.VirtualService } tests := []struct { name string args args - want *v1alpha3.VirtualService + want *v1.VirtualService wantErr bool }{ { name: "match", args: args{ - delegate: &networkingv1alpha3.Delegate{ + delegate: &networking.Delegate{ Name: "delegate", }, - virtualServices: []*v1alpha3.VirtualService{{ - ObjectMeta: v1.ObjectMeta{ + virtualServices: []*v1.VirtualService{{ + ObjectMeta: metav1.ObjectMeta{ Name: "delegate", Namespace: "default", }, }}, }, - want: &v1alpha3.VirtualService{ - ObjectMeta: v1.ObjectMeta{ + want: &v1.VirtualService{ + ObjectMeta: metav1.ObjectMeta{ Name: "delegate", Namespace: "default", }, @@ -284,24 +284,24 @@ func TestGetDelegatedVirtualService(t *testing.T) { }, { name: "match with namespace", args: args{ - delegate: &networkingv1alpha3.Delegate{ + delegate: &networking.Delegate{ Name: "delegate", Namespace: "test", }, - virtualServices: []*v1alpha3.VirtualService{{ - ObjectMeta: v1.ObjectMeta{ + virtualServices: []*v1.VirtualService{{ + ObjectMeta: metav1.ObjectMeta{ Name: "delegate", Namespace: "default", }, }, { - ObjectMeta: v1.ObjectMeta{ + ObjectMeta: metav1.ObjectMeta{ Name: "delegate", Namespace: "test", }, }}, }, - want: &v1alpha3.VirtualService{ - ObjectMeta: v1.ObjectMeta{ + want: &v1.VirtualService{ + ObjectMeta: metav1.ObjectMeta{ Name: "delegate", Namespace: "test", }, @@ -310,11 +310,11 @@ func TestGetDelegatedVirtualService(t *testing.T) { }, { name: "no match", args: args{ - delegate: &networkingv1alpha3.Delegate{ + delegate: &networking.Delegate{ Name: "delegate", }, - virtualServices: []*v1alpha3.VirtualService{{ - ObjectMeta: v1.ObjectMeta{ + virtualServices: []*v1.VirtualService{{ + ObjectMeta: metav1.ObjectMeta{ Name: "delegate-abc", Namespace: "default", }, @@ -325,17 +325,17 @@ func TestGetDelegatedVirtualService(t *testing.T) { }, { name: "no match with namespace", args: args{ - delegate: &networkingv1alpha3.Delegate{ + delegate: &networking.Delegate{ Name: "delegate", Namespace: "production", }, - virtualServices: []*v1alpha3.VirtualService{{ - ObjectMeta: v1.ObjectMeta{ + virtualServices: []*v1.VirtualService{{ + ObjectMeta: metav1.ObjectMeta{ Name: "delegate", Namespace: "default", }, }, { - ObjectMeta: v1.ObjectMeta{ + ObjectMeta: metav1.ObjectMeta{ Name: "delegate", Namespace: "test", },