Skip to content

Commit

Permalink
chore: simplifies resource loading (maistra#68)
Browse files Browse the repository at this point in the history
Route resources loader has many unused parameters being part of the
interface definition.

This change streamlines the abstraction to only rely on a route data
structure and type based on which particular resources are loaded.

Data structure holding routing information has been renamed to
RoutingData and new constructor func has been introduced to encapsulate
logic of creating service-related fields.
  • Loading branch information
bartoszmajsak authored Aug 29, 2024
1 parent df91330 commit f05c502
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 41 deletions.
16 changes: 3 additions & 13 deletions controllers/routingctrl/reconcile_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/opendatahub-io/odh-platform/pkg/unstruct"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)

Expand Down Expand Up @@ -69,22 +68,13 @@ func (r *Controller) createRoutingResources(ctx context.Context, target *unstruc
func (r *Controller) exportService(ctx context.Context, target *unstructured.Unstructured, exportedSvc *corev1.Service, domain string) error {
exportModes := r.extractExportModes(target)

templateData := spi.RoutingTemplateData{
PlatformRoutingConfiguration: r.config,
PublicServiceName: exportedSvc.GetName() + "-" + exportedSvc.GetNamespace(),
ServiceName: exportedSvc.GetName(),
ServiceNamespace: exportedSvc.GetNamespace(),
ServiceTargetPort: exportedSvc.Spec.Ports[0].TargetPort.String(),
Domain: domain,
}
templateData := spi.NewRoutingData(r.config, exportedSvc, domain)

// To establish ownership for watched component
ownershipLabels := append(labels.AsOwner(target), labels.AppManagedBy("odh-routing-controller"))

targetKey := client.ObjectKeyFromObject(target)

for _, exportMode := range exportModes {
resources, err := r.templateLoader.Load(ctx, exportMode, targetKey, templateData)
resources, err := r.templateLoader.Load(templateData, exportMode)
if err != nil {
return fmt.Errorf("could not load templates for type %s: %w", exportMode, err)
}
Expand All @@ -98,7 +88,7 @@ func (r *Controller) exportService(ctx context.Context, target *unstructured.Uns
return r.propagateHostsToWatchedCR(target, templateData)
}

func (r *Controller) propagateHostsToWatchedCR(target *unstructured.Unstructured, data spi.RoutingTemplateData) error {
func (r *Controller) propagateHostsToWatchedCR(target *unstructured.Unstructured, data *spi.RoutingData) error {
exportModes := r.extractExportModes(target)

// Remove all existing routing addresses
Expand Down
7 changes: 6 additions & 1 deletion pkg/metadata/labels/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ func StandardLabelsFrom(source metav1.Object) []metadata.Option {
// AppendStandardLabelsFrom appends standard labels found in source object but only
// when they are not already present in the target object.
func AppendStandardLabelsFrom(source metav1.Object) *LabelAppender {
return &LabelAppender{labels: standardLabelsFrom(source)}
return AppendLabels(standardLabelsFrom(source)...)
}

// AppendLabels appends provided labels to the target object but only when they are not already present.
func AppendLabels(labels ...Label) *LabelAppender {
return &LabelAppender{labels: labels}
}

// MatchingLabels returns a client.MatchingLabels selector for the provided labels.
Expand Down
6 changes: 2 additions & 4 deletions pkg/routing/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package routing

import (
"bytes"
"context"
_ "embed" // needed for go:embed directive
"fmt"
"strings"
Expand All @@ -11,7 +10,6 @@ import (
"github.com/opendatahub-io/odh-platform/pkg/schema"
"github.com/opendatahub-io/odh-platform/pkg/spi"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
)

//go:embed template/routing_public.yaml
Expand All @@ -27,7 +25,7 @@ func NewStaticTemplateLoader() spi.RoutingTemplateLoader {
return &staticTemplateLoader{}
}

func (s *staticTemplateLoader) Load(_ context.Context, routeType spi.RouteType, key types.NamespacedName, data spi.RoutingTemplateData) ([]*unstructured.Unstructured, error) {
func (s *staticTemplateLoader) Load(data *spi.RoutingData, routeType spi.RouteType) ([]*unstructured.Unstructured, error) {
var resources []*unstructured.Unstructured

var templateContent []byte
Expand Down Expand Up @@ -60,7 +58,7 @@ func (s *staticTemplateLoader) Load(_ context.Context, routeType spi.RouteType,
return resources, nil
}

func (s *staticTemplateLoader) resolveTemplate(tmpl []byte, data spi.RoutingTemplateData) ([]byte, error) {
func (s *staticTemplateLoader) resolveTemplate(tmpl []byte, data *spi.RoutingData) ([]byte, error) {
engine, err := template.New("routing").Parse(string(tmpl))
if err != nil {
return []byte{}, fmt.Errorf("could not create template engine: %w", err)
Expand Down
45 changes: 29 additions & 16 deletions pkg/routing/routing_test.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,53 @@
package routing_test

import (
"context"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/opendatahub-io/odh-platform/pkg/routing"
"github.com/opendatahub-io/odh-platform/pkg/spi"
"github.com/opendatahub-io/odh-platform/test"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/ptr"
)

var _ = Describe("Resource functions", test.Unit(), func() {

Context("Template Loader", func() {

data := spi.RoutingTemplateData{
PlatformRoutingConfiguration: spi.PlatformRoutingConfiguration{
GatewayNamespace: "opendatahub",
IngressSelectorLabel: "istio",
IngressSelectorValue: "rhoai-gateway",
IngressService: "rhoai-router-ingress",
},
PublicServiceName: "registry-office",
ServiceName: "registry",
ServiceNamespace: "office",
Domain: "app-crc.testing",
config := spi.PlatformRoutingConfiguration{
GatewayNamespace: "opendatahub",
IngressSelectorLabel: "istio",
IngressSelectorValue: "rhoai-gateway",
IngressService: "rhoai-router-ingress",
}

data := spi.NewRoutingData(config,
&corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "registry",
Namespace: "office",
},
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{
Name: "http-api",
Port: 80,
AppProtocol: ptr.To("http"),
},
},
},
},
"app-crc.testing",
)

It("should load public resources", func() {
// given
// data^

// when
res, err := routing.NewStaticTemplateLoader().Load(context.Background(), spi.PublicRoute, types.NamespacedName{}, data)
res, err := routing.NewStaticTemplateLoader().Load(data, spi.PublicRoute)

// then
Expect(err).ShouldNot(HaveOccurred())
Expand All @@ -46,7 +59,7 @@ var _ = Describe("Resource functions", test.Unit(), func() {
// data^

// when
res, err := routing.NewStaticTemplateLoader().Load(context.Background(), spi.ExternalRoute, types.NamespacedName{}, data)
res, err := routing.NewStaticTemplateLoader().Load(data, spi.ExternalRoute)

// then
Expect(err).ShouldNot(HaveOccurred())
Expand Down
22 changes: 15 additions & 7 deletions pkg/spi/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

authorinov1beta2 "github.com/kuadrant/authorino/api/v1beta2"
"github.com/opendatahub-io/odh-platform/pkg/platform"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
)
Expand Down Expand Up @@ -130,8 +131,7 @@ type PlatformRoutingConfiguration struct {
GatewayNamespace string
}

// TODO(mvp) revise the stuct name - is it only for templates?
type RoutingTemplateData struct {
type RoutingData struct {
PlatformRoutingConfiguration

PublicServiceName string // [service-name]-[service-namespace]
Expand All @@ -143,10 +143,18 @@ type RoutingTemplateData struct {
Domain string
}

// RoutingTemplateLoader provides a way to differentiate the Route template used based on
// - RouteType
// - Namespace / Resource name
// - Loader source
func NewRoutingData(config PlatformRoutingConfiguration, svc *corev1.Service, domain string) *RoutingData {
return &RoutingData{
PlatformRoutingConfiguration: config,
PublicServiceName: svc.GetName() + "-" + svc.GetNamespace(),
ServiceName: svc.GetName(),
ServiceNamespace: svc.GetNamespace(),
ServiceTargetPort: svc.Spec.Ports[0].TargetPort.String(),
Domain: domain,
}
}

// RoutingTemplateLoader provides a way to differentiate the Route resource templates used based on its types.
type RoutingTemplateLoader interface {
Load(ctx context.Context, routeType RouteType, key types.NamespacedName, data RoutingTemplateData) ([]*unstructured.Unstructured, error)
Load(data *RoutingData, routeType RouteType) ([]*unstructured.Unstructured, error)
}

0 comments on commit f05c502

Please sign in to comment.