Skip to content

Commit

Permalink
pass namespace in ProviderConf and make read from file account for na…
Browse files Browse the repository at this point in the history
…mepsaces
  • Loading branch information
LiorLieberman committed Dec 28, 2023
1 parent 654e76f commit 1d31753
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 30 deletions.
14 changes: 8 additions & 6 deletions pkg/i2gw/ingress2gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ func ToGatewayAPIResources(ctx context.Context, namespace string, inputFile stri
var ingresses networkingv1.IngressList

providerByName, err := constructProviders(&ProviderConf{
Client: cl,
Client: cl,
Namespace: namespace,
}, providers)
if err != nil {
return nil, err
Expand Down Expand Up @@ -136,7 +137,8 @@ func constructProviders(conf *ProviderConf, providers []string) (map[ProviderNam
// ExtractObjectsFromReader extracts all objects from a reader,
// which is created from YAML or JSON input files.
// It retrieves all objects, including nested ones if they are contained within a list.
func ExtractObjectsFromReader(reader io.Reader) ([]*unstructured.Unstructured, error) {
// The function takes a namespace parameter to optionally return only namespaced resources.
func ExtractObjectsFromReader(reader io.Reader, namespace string) ([]*unstructured.Unstructured, error) {
d := kubeyaml.NewYAMLOrJSONDecoder(reader, 4096)
var objs []*unstructured.Unstructured
for {
Expand All @@ -150,6 +152,9 @@ func ExtractObjectsFromReader(reader io.Reader) ([]*unstructured.Unstructured, e
if u == nil {
continue
}
if namespace != "" && u.GetNamespace() != namespace {
continue
}
objs = append(objs, u)
}

Expand Down Expand Up @@ -187,15 +192,12 @@ func ConstructIngressesFromFile(l *networkingv1.IngressList, inputFile string, n
}

reader := bytes.NewReader(stream)
objs, err := ExtractObjectsFromReader(reader)
objs, err := ExtractObjectsFromReader(reader, namespace)
if err != nil {
return err
}

for _, f := range objs {
if namespace != "" && f.GetNamespace() != namespace {
continue
}
if !f.GroupVersionKind().Empty() && f.GroupVersionKind().Kind == "Ingress" {
var i networkingv1.Ingress
err = runtime.DefaultUnstructuredConverter.
Expand Down
3 changes: 2 additions & 1 deletion pkg/i2gw/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ type ProviderConstructor func(conf *ProviderConf) Provider
// ProviderConf contains all the configuration required for every concrete
// Provider implementation.
type ProviderConf struct {
Client client.Client
Client client.Client
Namespace string
}

// The Provider interface specifies the required functionality which needs to be
Expand Down
21 changes: 4 additions & 17 deletions pkg/i2gw/providers/istio/istio.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ limitations under the License.
package istio

import (
"bytes"
"context"
"fmt"
"os"

"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw"
"k8s.io/apimachinery/pkg/util/validation/field"
Expand All @@ -43,7 +41,7 @@ type Provider struct {
func NewProvider(conf *i2gw.ProviderConf) i2gw.Provider {
return &Provider{
storage: newResourcesStorage(),
reader: newResourceReader(conf.Client),
reader: newResourceReader(conf),
converter: newConverter(),
}
}
Expand All @@ -66,22 +64,11 @@ func (p *Provider) ReadResourcesFromCluster(ctx context.Context) error {
return nil
}

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

unstructuredObjects, err := i2gw.ExtractObjectsFromReader(bytes.NewReader(stream))
if err != nil {
return fmt.Errorf("failed to extract objects: %w", err)
}

storage, err := p.reader.readUnstructuredObjects(unstructuredObjects)
if err != nil {
return fmt.Errorf("failed to read unstructured objects: %w", err)
}

p.storage = *storage
return nil
}
33 changes: 27 additions & 6 deletions pkg/i2gw/providers/istio/resource_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,26 @@ limitations under the License.
package istio

import (
"bytes"
"context"
"fmt"
"log"
"os"

"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw"
istiov1beta1 "istio.io/client-go/pkg/apis/networking/v1beta1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)

type reader struct {
k8sClient client.Client
conf *i2gw.ProviderConf
}

func newResourceReader(k8sClient client.Client) reader {
func newResourceReader(conf *i2gw.ProviderConf) reader {
return reader{
k8sClient: k8sClient,
conf: conf,
}
}

Expand All @@ -58,6 +60,25 @@ func (r *reader) readResourcesFromCluster(ctx context.Context) (*storage, error)
return &res, nil
}

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

unstructuredObjects, err := i2gw.ExtractObjectsFromReader(bytes.NewReader(stream), r.conf.Namespace)
if err != nil {
return nil, fmt.Errorf("failed to extract objects: %w", err)
}

storage, err := r.readUnstructuredObjects(unstructuredObjects)
if err != nil {
return nil, fmt.Errorf("failed to read unstructured objects: %w", err)
}

return storage, nil
}

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

Expand Down Expand Up @@ -102,7 +123,7 @@ func (r *reader) readGatewaysFromCluster(ctx context.Context) (map[types.Namespa
gatewayList.SetAPIVersion(APIVersion)
gatewayList.SetKind(GatewayKind)

err := r.k8sClient.List(ctx, gatewayList)
err := r.conf.Client.List(ctx, gatewayList)
if err != nil {
return nil, fmt.Errorf("failed to list istio gateways: %w", err)
}
Expand All @@ -128,7 +149,7 @@ func (r *reader) readVirtualServicesFromCluster(ctx context.Context) (map[types.
virtualServicesList.SetAPIVersion(APIVersion)
virtualServicesList.SetKind(VirtualServiceKind)

err := r.k8sClient.List(ctx, virtualServicesList)
err := r.conf.Client.List(ctx, virtualServicesList)
if err != nil {
return nil, fmt.Errorf("failed to list istio virtual services: %w", err)
}
Expand Down

0 comments on commit 1d31753

Please sign in to comment.