Skip to content

Commit

Permalink
address review comments
Browse files Browse the repository at this point in the history
Signed-off-by: Mattia Lavacca <[email protected]>
  • Loading branch information
mlavacca committed Jun 5, 2024
1 parent 79e1f09 commit 5de027b
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 12 deletions.
2 changes: 1 addition & 1 deletion cmd/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func newPrintCommand() *cobra.Command {
if specified with --namespace.`)

cmd.Flags().BoolVar(&pr.allResources, "all-resources", false,
`If present, list all the objects across the selected namespaces. This flag can be set only when reading from a file.`)
`If present, list all the objects across the selected namespaces. This flag can be set only when the --input-file flag is set as well.`)

cmd.Flags().StringSliceVar(&pr.providers, "providers", i2gw.GetSupportedProviders(),
fmt.Sprintf("If present, the tool will try to convert only resources related to the specified providers, supported values are %v.", i2gw.GetSupportedProviders()))
Expand Down
11 changes: 9 additions & 2 deletions pkg/i2gw/ingress2gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func GetResources(ctx context.Context, namespace string, inputFile string, allre
return nil, err
}
if allresources {
if genericResources, err = readGenericResourcesFromFile(inputFile, namespace); err != nil {
if genericResources, err = readGenericResourcesFromFile(inputFile, namespace, providerByName); err != nil {
return nil, err
}
}
Expand Down Expand Up @@ -133,7 +133,7 @@ func readProviderResourcesFromFile(ctx context.Context, providerByName map[Provi
return nil
}

func readGenericResourcesFromFile(inputFile, namespace string) ([]client.Object, error) {
func readGenericResourcesFromFile(inputFile, namespace string, providerByName map[ProviderName]Provider) ([]client.Object, error) {
objects := make([]client.Object, 0)
stream, err := os.ReadFile(inputFile)
if err != nil {
Expand All @@ -144,6 +144,13 @@ func readGenericResourcesFromFile(inputFile, namespace string) ([]client.Object,
if err != nil {
return nil, fmt.Errorf("failed to extract objects: %w", err)
}

for _, p := range providerByName {
for _, crd := range p.GetCRDs() {
FilteredResources[crd] = struct{}{}
}
}

for _, o := range unstructuredObjects {
if o.GetNamespace() != namespace {
continue
Expand Down
4 changes: 4 additions & 0 deletions pkg/i2gw/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"sync"

networkingv1 "k8s.io/api/networking/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/validation/field"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -55,6 +56,9 @@ type ProviderConf struct {
type Provider interface {
CustomResourceReader
ResourceConverter

// GetCRDs returns the list of CustomResourceDefinitions associated with the Provider.
GetCRDs() []schema.GroupKind
}

type CustomResourceReader interface {
Expand Down
5 changes: 5 additions & 0 deletions pkg/i2gw/providers/apisix/apisix.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"

"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"
)

Expand Down Expand Up @@ -73,3 +74,7 @@ func (p *Provider) ReadResourcesFromFile(_ context.Context, filename string) err
p.storage = storage
return nil
}

func (p *Provider) GetCRDs() []schema.GroupKind {
return nil
}
5 changes: 5 additions & 0 deletions pkg/i2gw/providers/ingressnginx/ingressnginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"

"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"
)

Expand Down Expand Up @@ -73,3 +74,7 @@ func (p *Provider) ReadResourcesFromFile(_ context.Context, filename string) err
p.storage = storage
return nil
}

func (p *Provider) GetCRDs() []schema.GroupKind {
return nil
}
10 changes: 8 additions & 2 deletions pkg/i2gw/providers/istio/istio.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"

"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"
)

Expand All @@ -29,8 +30,6 @@ const ProviderName = "istio"

func init() {
i2gw.ProviderConstructorByName[ProviderName] = NewProvider
i2gw.FilteredResources[gatewayGVK.GroupKind()] = struct{}{}
i2gw.FilteredResources[virtualServiceGVK.GroupKind()] = struct{}{}
}

type Provider struct {
Expand Down Expand Up @@ -72,3 +71,10 @@ func (p *Provider) ReadResourcesFromFile(ctx context.Context, filename string) e
p.storage = storage
return nil
}

func (p *Provider) GetCRDs() []schema.GroupKind {
return []schema.GroupKind{
gatewayGK,
virtualServiceGK,
}
}
6 changes: 3 additions & 3 deletions pkg/i2gw/providers/istio/resource_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (r *reader) readUnstructuredObjects(objects []*unstructured.Unstructured) (
res := newResourcesStorage()

for _, obj := range objects {
if obj.GetAPIVersion() != APIVersion {
if obj.GetAPIVersion() != APIVersion.String() {
log.Printf("%v provider: skipped resource with unsupported APIVersion: %v", ProviderName, obj.GetAPIVersion())
continue
}
Expand Down Expand Up @@ -122,7 +122,7 @@ func (r *reader) readUnstructuredObjects(objects []*unstructured.Unstructured) (

func (r *reader) readGatewaysFromCluster(ctx context.Context) (map[types.NamespacedName]*istiov1beta1.Gateway, error) {
gatewayList := &unstructured.UnstructuredList{}
gatewayList.SetAPIVersion(APIVersion)
gatewayList.SetAPIVersion(APIVersion.String())
gatewayList.SetKind(GatewayKind)

err := r.conf.Client.List(ctx, gatewayList)
Expand Down Expand Up @@ -152,7 +152,7 @@ func (r *reader) readGatewaysFromCluster(ctx context.Context) (map[types.Namespa

func (r *reader) readVirtualServicesFromCluster(ctx context.Context) (map[types.NamespacedName]*istiov1beta1.VirtualService, error) {
virtualServicesList := &unstructured.UnstructuredList{}
virtualServicesList.SetAPIVersion(APIVersion)
virtualServicesList.SetAPIVersion(APIVersion.String())
virtualServicesList.SetKind(VirtualServiceKind)

err := r.conf.Client.List(ctx, virtualServicesList)
Expand Down
10 changes: 7 additions & 3 deletions pkg/i2gw/providers/istio/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@ package istio
import "k8s.io/apimachinery/pkg/runtime/schema"

const (
APIVersion = "networking.istio.io/v1beta1"
GatewayKind = "Gateway"
VirtualServiceKind = "VirtualService"

K8SGatewayClassName = "istio"
)

var (
gatewayGVK = schema.GroupVersionKind{Group: "networking.istio.io", Version: "v1beta1", Kind: "Gateway"}
virtualServiceGVK = schema.GroupVersionKind{Group: "networking.istio.io", Version: "v1beta1", Kind: "VirtualService"}
APIVersion = schema.GroupVersion{
Group: "networking.istio.io",
Version: "v1beta1",
}

gatewayGK = schema.GroupKind{Group: APIVersion.Group, Kind: GatewayKind}
virtualServiceGK = schema.GroupKind{Group: APIVersion.Group, Kind: VirtualServiceKind}
)
8 changes: 7 additions & 1 deletion pkg/i2gw/providers/kong/kong.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package kong
import (
"context"

"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"

"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw"
Expand All @@ -30,7 +31,6 @@ const KongIngressClass = "kong"

func init() {
i2gw.ProviderConstructorByName[Name] = NewProvider
i2gw.FilteredResources[tcpIngressGVK.GroupKind()] = struct{}{}
}

// Provider implements the i2gw.Provider interface.
Expand Down Expand Up @@ -71,3 +71,9 @@ func (p *Provider) ReadResourcesFromFile(_ context.Context, filename string) err
p.storage = storage
return nil
}

func (p *Provider) GetCRDs() []schema.GroupKind {
return []schema.GroupKind{
tcpIngressGVK.GroupKind(),
}
}
5 changes: 5 additions & 0 deletions pkg/i2gw/providers/openapi3/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"

"github.com/getkin/kin-openapi/openapi3"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"

"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw"
Expand Down Expand Up @@ -107,3 +108,7 @@ func readSpecFromFile(ctx context.Context, filename string) (*openapi3.T, error)

return spec, nil
}

func (p *Provider) GetCRDs() []schema.GroupKind {
return nil
}

0 comments on commit 5de027b

Please sign in to comment.