Skip to content

Commit a900076

Browse files
committed
Extend GatewayResources to hold Gateway Extension.
* Create a list of unstructured.Unstructured to hold Gateway Extensions. * Add methods to cast a runtime Object to Unstructured, and print as yaml.
1 parent 0adb24e commit a900076

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

cmd/print.go

+42
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ import (
2525
"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw"
2626
"github.com/samber/lo"
2727
"github.com/spf13/cobra"
28+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
29+
"k8s.io/apimachinery/pkg/runtime"
30+
"k8s.io/apimachinery/pkg/runtime/serializer/json"
2831
"k8s.io/cli-runtime/pkg/genericclioptions"
2932
"k8s.io/cli-runtime/pkg/printers"
3033
"k8s.io/client-go/tools/clientcmd"
@@ -179,6 +182,17 @@ func (pr *PrintRunner) outputResult(gatewayResources []i2gw.GatewayResources) {
179182
}
180183
}
181184

185+
for _, r := range gatewayResources {
186+
resourceCount += len(r.GatewayExtensions)
187+
for _, gatewayExtension := range r.GatewayExtensions {
188+
gatewayExtension := gatewayExtension
189+
fmt.Println("---")
190+
if err := PrintUnstructuredAsYaml(&gatewayExtension); err != nil {
191+
fmt.Printf("# Error printing %s gatewayExtension: %v\n", gatewayExtension.GetName(), err)
192+
}
193+
}
194+
}
195+
182196
if resourceCount == 0 {
183197
msg := "No resources found"
184198
if pr.namespaceFilter != "" {
@@ -188,6 +202,34 @@ func (pr *PrintRunner) outputResult(gatewayResources []i2gw.GatewayResources) {
188202
}
189203
}
190204

205+
func CastToUnstructured(obj runtime.Object) (*unstructured.Unstructured, error) {
206+
// Convert the Kubernetes object to unstructured.Unstructured
207+
unstructuredObj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
208+
if err != nil {
209+
return nil, err
210+
}
211+
212+
return &unstructured.Unstructured{Object: unstructuredObj}, nil
213+
}
214+
215+
func PrintUnstructuredAsYaml(obj *unstructured.Unstructured) error {
216+
// Create a YAML serializer
217+
serializer := json.NewSerializerWithOptions(json.DefaultMetaFactory, nil, nil,
218+
json.SerializerOptions{
219+
Yaml: true,
220+
Pretty: true, // Optional: for better readability
221+
Strict: true,
222+
})
223+
224+
// Encode the unstructured object to YAML
225+
err := serializer.Encode(obj, os.Stdout)
226+
if err != nil {
227+
return err
228+
}
229+
230+
return nil
231+
}
232+
191233
// initializeResourcePrinter assign a specific type of printers.ResourcePrinter
192234
// based on the outputFormat of the printRunner struct.
193235
func (pr *PrintRunner) initializeResourcePrinter() error {

pkg/i2gw/provider.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"sync"
2222

2323
networkingv1 "k8s.io/api/networking/v1"
24+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2425
"k8s.io/apimachinery/pkg/types"
2526
"k8s.io/apimachinery/pkg/util/validation/field"
2627
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -88,7 +89,8 @@ type ProviderImplementationSpecificOptions struct {
8889
ToImplementationSpecificHTTPPathTypeMatch ImplementationSpecificHTTPPathTypeMatchConverter
8990
}
9091

91-
// GatewayResources contains all Gateway-API objects.
92+
// GatewayResources contains all Gateway-API objects and provider Gateway
93+
// extensions.
9294
type GatewayResources struct {
9395
Gateways map[types.NamespacedName]gatewayv1.Gateway
9496
GatewayClasses map[types.NamespacedName]gatewayv1.GatewayClass
@@ -99,6 +101,8 @@ type GatewayResources struct {
99101
UDPRoutes map[types.NamespacedName]gatewayv1alpha2.UDPRoute
100102

101103
ReferenceGrants map[types.NamespacedName]gatewayv1beta1.ReferenceGrant
104+
105+
GatewayExtensions map[types.NamespacedName]unstructured.Unstructured
102106
}
103107

104108
// IR contains all Gateway-API objects, and Intermediate Representation(IR) to

0 commit comments

Comments
 (0)