Skip to content

Commit

Permalink
return readers back
Browse files Browse the repository at this point in the history
  • Loading branch information
dpasiukevich committed Dec 1, 2023
1 parent 483422d commit 7dd1150
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 59 deletions.
22 changes: 11 additions & 11 deletions PROVIDER.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ In this section, we will walk through a demo of how to add support for the `exam
├── examplegateway
└── ingressnginx
```
2. Create a struct named `resourceFetcher` which implements the `CustomResourceFetcher` interface in a file named
2. Create a struct named `resourceReader` which implements the `CustomResourceReader` interface in a file named
`resource_converter.go`.
```go
package examplegateway
Expand All @@ -37,30 +37,30 @@ import (
"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw"
)

// converter implements the i2gw.CustomResourceFetcher interface.
type resourceFetcher struct {
// converter implements the i2gw.CustomResourceReader interface.
type resourceReader struct {
conf *i2gw.ProviderConf
}

// newResourceFetcher returns a resourceFetcherinstance.
func newResourceFetcher(conf *i2gw.ProviderConf) *resourceFetcher {
return &resourceFetcher{
// newResourceReader returns a resourceReader instance.
func newResourceReader(conf *i2gw.ProviderConf) *resourceReader {
return &resourceReader{
conf: conf,
}
}

func (r *resourceFetcher) FetchResourcesFromCluster(ctx context.Context) error {
func (r *resourceReader) ReadResourcesFromCluster(ctx context.Context) error {
// read example-gateway related resources from the cluster.
return nil
}

func (r *resourceFetcher) FetchResourcesFromFiles(ctx context.Context, filename string) error {
func (r *resourceReader) ReadResourcesFromFiles(ctx context.Context, filename string) error {
// read example-gateway related resources from the file.
return nil
}
```

These methods are used by providers to fetch and store additional resources they may need during conversion.
These methods are used by providers to read and store additional resources they may need during conversion.

3. Create a struct named `converter` which implements the `ResourceConverter` interface in a file named `converter.go`.
The implemented `ToGatewayAPI` function should simply call every registered `featureParser` function, one by one.
Expand Down Expand Up @@ -109,15 +109,15 @@ import (
type Provider struct {
conf *i2gw.ProviderConf

*resourceFetcher
*resourceRead
*converter
}

// NewProvider constructs and returns the example-gateway implementation of i2gw.Provider.
func NewProvider(conf *i2gw.ProviderConf) i2gw.Provider {
return &Provider{
conf: conf,
resourceFetcher: newResourceFetcher(conf),
resourceReader: newResourceReader(conf),
converter: newConverter(conf),
}
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/i2gw/ingress2gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ func ToGatewayAPIResources(ctx context.Context, namespace string, inputFile stri
return nil, nil, fmt.Errorf("failed to read ingresses from file: %w", err)
}
resources.Ingresses = ingresses.Items
if err = fetchProviderResourcesFromFile(ctx, providerByName, inputFile); err != nil {
if err = readProviderResourcesFromFile(ctx, providerByName, inputFile); err != nil {
return nil, nil, err
}
} else {
if err = ConstructIngressesFromCluster(ctx, cl, &ingresses); err != nil {
return nil, nil, fmt.Errorf("failed to read ingresses from cluster: %w", err)
}
resources.Ingresses = ingresses.Items
if err = fetchProviderResourcesFromCluster(ctx, providerByName); err != nil {
if err = readProviderResourcesFromCluster(ctx, providerByName); err != nil {
return nil, nil, err
}
}
Expand All @@ -95,18 +95,18 @@ func ToGatewayAPIResources(ctx context.Context, namespace string, inputFile stri
return httpRoutes, gateways, nil
}

func fetchProviderResourcesFromFile(ctx context.Context, providerByName map[ProviderName]Provider, inputFile string) error {
func readProviderResourcesFromFile(ctx context.Context, providerByName map[ProviderName]Provider, inputFile string) error {
for name, provider := range providerByName {
if err := provider.FetchResourcesFromFile(ctx, inputFile); err != nil {
if err := provider.ReadResourcesFromFile(ctx, inputFile); err != nil {
return fmt.Errorf("failed to read %s resources from file: %w", name, err)
}
}
return nil
}

func fetchProviderResourcesFromCluster(ctx context.Context, providerByName map[ProviderName]Provider) error {
func readProviderResourcesFromCluster(ctx context.Context, providerByName map[ProviderName]Provider) error {
for name, provider := range providerByName {
if err := provider.FetchResourcesFromCluster(ctx); err != nil {
if err := provider.ReadResourcesFromCluster(ctx); err != nil {
return fmt.Errorf("failed to read %s resources from the cluster: %w", name, err)
}
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/i2gw/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,19 @@ type ProviderConf struct {
// implemented by every concrete Ingress/Gateway-API provider, in order for it to
// be used.
type Provider interface {
CustomResourceFetcher
CustomResourceReader
ResourceConverter
}

type CustomResourceFetcher interface {
type CustomResourceReader interface {

// FetchResourcesFromCluster fetches custom resources associated with
// ReadResourcesFromCluster reads custom resources associated with
// the underlying Provider implementation from the kubernetes cluster.
FetchResourcesFromCluster(ctx context.Context) error
ReadResourcesFromCluster(ctx context.Context) error

// FetchResourcesFromFile reads custom resources associated with
// ReadResourcesFromFile reads custom resources associated with
// the underlying Provider implementation from the file.
FetchResourcesFromFile(ctx context.Context, filename string) error
ReadResourcesFromFile(ctx context.Context, filename string) error
}

// The ResourceConverter interface specifies all the implemented Gateway API resource
Expand Down
8 changes: 4 additions & 4 deletions pkg/i2gw/providers/ingressnginx/ingressnginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ func init() {
type Provider struct {
conf *i2gw.ProviderConf

*resourceFetcher
*resourceReader
*converter
}

// NewProvider constructs and returns the ingress-nginx implementation of i2gw.Provider.
func NewProvider(conf *i2gw.ProviderConf) i2gw.Provider {
return &Provider{
conf: conf,
resourceFetcher: newResourceFetcher(conf),
converter: newConverter(conf),
conf: conf,
resourceReader: newResourceReader(conf),
converter: newConverter(conf),
}
}
14 changes: 7 additions & 7 deletions pkg/i2gw/providers/ingressnginx/resource_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,24 @@ import (
"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw"
)

// converter implements the i2gw.CustomResourceFetcher interface.
type resourceFetcher struct {
// converter implements the i2gw.CustomResourceReader interface.
type resourceReader struct {
conf *i2gw.ProviderConf
}

// newResourceFetcher returns a resourceFetcher instance.
func newResourceFetcher(conf *i2gw.ProviderConf) *resourceFetcher {
return &resourceFetcher{
// newResourceReader returns a resourceReader instance.
func newResourceReader(conf *i2gw.ProviderConf) *resourceReader {
return &resourceReader{
conf: conf,
}
}

func (r *resourceFetcher) FetchResourcesFromCluster(_ context.Context) error {
func (r *resourceReader) ReadResourcesFromCluster(_ context.Context) error {
// ingress-nginx does not have any CRDs.
return nil
}

func (r *resourceFetcher) FetchResourcesFromFile(_ context.Context, _ string) error {
func (r *resourceReader) ReadResourcesFromFile(_ context.Context, _ string) error {
// ingress-nginx does not have any CRDs.
return nil
}
13 changes: 6 additions & 7 deletions pkg/i2gw/providers/istio/istio.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ func init() {

type Provider struct {
storage storage
reader fetcher
reader reader
converter converter
}

// NewProvider returns the istio implementation of i2gw.Provider.
func NewProvider(conf *i2gw.ProviderConf) i2gw.Provider {
return &Provider{
storage: newResourcesStorage(),
reader: newResourceFetcher(conf.Client),
reader: newResourceReader(conf.Client),
converter: newConverter(),
}
}
Expand All @@ -56,8 +56,8 @@ func (p *Provider) ToGatewayAPI(_ i2gw.InputResources) (i2gw.GatewayResources, f
return i2gw.GatewayResources{}, field.ErrorList{field.Forbidden(field.NewPath(""), "conversion is WIP")}
}

func (p *Provider) FetchResourcesFromCluster(ctx context.Context) error {
storage, err := p.reader.fetchResourcesFromCluster(ctx)
func (p *Provider) ReadResourcesFromCluster(ctx context.Context) error {
storage, err := p.reader.readResourcesFromCluster(ctx)
if err != nil {
return fmt.Errorf("failed to read resources from cluster: %w", err)
}
Expand All @@ -66,14 +66,13 @@ func (p *Provider) FetchResourcesFromCluster(ctx context.Context) error {
return nil
}

func (p *Provider) FetchResourcesFromFile(_ context.Context, filename string) error {
func (p *Provider) ReadResourcesFromFile(_ context.Context, filename string) error {
stream, err := os.ReadFile(filename)
if err != nil {
return fmt.Errorf("failed to read file %v: %w", filename, err)
}

reader := bytes.NewReader(stream)
unstructuredObjects, err := i2gw.ExtractObjectsFromReader(reader)
unstructuredObjects, err := i2gw.ExtractObjectsFromReader(bytes.NewReader(stream))
if err != nil {
return fmt.Errorf("failed to extract objects: %w", err)
}
Expand Down
14 changes: 7 additions & 7 deletions pkg/i2gw/providers/istio/resource_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

type fetcher struct {
type reader struct {
k8sClient client.Client
}

func newResourceFetcher(k8sClient client.Client) fetcher {
return fetcher{
func newResourceReader(k8sClient client.Client) reader {
return reader{
k8sClient: k8sClient,
}
}

func (r *fetcher) fetchResourcesFromCluster(ctx context.Context) (*storage, error) {
func (r *reader) readResourcesFromCluster(ctx context.Context) (*storage, error) {
res := newResourcesStorage()

gateways, err := r.readGatewaysFromCluster(ctx)
Expand All @@ -58,7 +58,7 @@ func (r *fetcher) fetchResourcesFromCluster(ctx context.Context) (*storage, erro
return &res, nil
}

func (r *fetcher) readUnstructuredObjects(objects []*unstructured.Unstructured) (*storage, error) {
func (r *reader) readUnstructuredObjects(objects []*unstructured.Unstructured) (*storage, error) {
res := newResourcesStorage()

for _, obj := range objects {
Expand Down Expand Up @@ -97,7 +97,7 @@ func (r *fetcher) readUnstructuredObjects(objects []*unstructured.Unstructured)
return &res, nil
}

func (r *fetcher) readGatewaysFromCluster(ctx context.Context) (map[types.NamespacedName]*istiov1beta1.Gateway, error) {
func (r *reader) readGatewaysFromCluster(ctx context.Context) (map[types.NamespacedName]*istiov1beta1.Gateway, error) {
gatewayList := &unstructured.UnstructuredList{}
gatewayList.SetAPIVersion(APIVersion)
gatewayList.SetKind(GatewayKind)
Expand All @@ -123,7 +123,7 @@ func (r *fetcher) readGatewaysFromCluster(ctx context.Context) (map[types.Namesp
return res, nil
}

func (r *fetcher) readVirtualServicesFromCluster(ctx context.Context) (map[types.NamespacedName]*istiov1beta1.VirtualService, error) {
func (r *reader) readVirtualServicesFromCluster(ctx context.Context) (map[types.NamespacedName]*istiov1beta1.VirtualService, error) {
virtualServicesList := &unstructured.UnstructuredList{}
virtualServicesList.SetAPIVersion(APIVersion)
virtualServicesList.SetKind(VirtualServiceKind)
Expand Down
8 changes: 4 additions & 4 deletions pkg/i2gw/providers/kong/kong.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ func init() {
type Provider struct {
conf *i2gw.ProviderConf

*resourceFetcher
*resourceReader
*converter
}

// NewProvider constructs and returns the kong implementation of i2gw.Provider.
func NewProvider(conf *i2gw.ProviderConf) i2gw.Provider {
return &Provider{
conf: conf,
resourceFetcher: newResourceFetcher(conf),
converter: newConverter(conf),
conf: conf,
resourceReader: newResourceReader(conf),
converter: newConverter(conf),
}
}
14 changes: 7 additions & 7 deletions pkg/i2gw/providers/kong/resource_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,22 @@ import (
"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw"
)

// converter implements the i2gw.CustomResourceFetcher interface.
type resourceFetcher struct {
// converter implements the i2gw.CustomResourceReader interface.
type resourceReader struct {
conf *i2gw.ProviderConf
}

// newResourceFetcher returns a resourceFetcher instance.
func newResourceFetcher(conf *i2gw.ProviderConf) *resourceFetcher {
return &resourceFetcher{
// newResourceReader returns a resourceReader instance.
func newResourceReader(conf *i2gw.ProviderConf) *resourceReader {
return &resourceReader{
conf: conf,
}
}

func (r *resourceFetcher) FetchResourcesFromCluster(_ context.Context) error {
func (r *resourceReader) ReadResourcesFromCluster(_ context.Context) error {
return nil
}

func (r *resourceFetcher) FetchResourcesFromFile(_ context.Context, _ string) error {
func (r *resourceReader) ReadResourcesFromFile(_ context.Context, _ string) error {
return nil
}

0 comments on commit 7dd1150

Please sign in to comment.