Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement template rendering action #1364

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion controllers/components/dashboard/dashboard_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (

componentsv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/components/v1"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/deploy"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/render"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/render/kustomize"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/security"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/updatestatus"
Expand Down Expand Up @@ -76,7 +77,7 @@ func NewComponentReconciler(ctx context.Context, mgr ctrl.Manager) error {
WithAction(configureDependencies).
WithAction(security.NewUpdatePodSecurityRoleBindingAction(serviceAccounts)).
WithAction(kustomize.NewAction(
kustomize.WithCache(kustomize.DefaultCachingKeyFn),
kustomize.WithCache(render.DefaultCachingKeyFn),
// Those are the default labels added by the legacy deploy method
// and should be preserved as the original plugin were affecting
// deployment selectors that are immutable once created, so it won't
Expand Down
3 changes: 2 additions & 1 deletion controllers/components/kueue/kueue_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (

componentsv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/components/v1"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/deploy"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/render"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/render/kustomize"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/updatestatus"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/predicates/resources"
Expand Down Expand Up @@ -64,7 +65,7 @@ func NewComponentReconciler(ctx context.Context, mgr ctrl.Manager) error {
WithAction(initialize).
WithAction(devFlags).
WithAction(kustomize.NewAction(
kustomize.WithCache(kustomize.DefaultCachingKeyFn),
kustomize.WithCache(render.DefaultCachingKeyFn),
kustomize.WithLabel(labels.ODH.Component(ComponentName), "true"),
kustomize.WithLabel(labels.K8SCommon.PartOf, ComponentName),
)).
Expand Down
28 changes: 17 additions & 11 deletions controllers/components/modelregistry/modelregistry_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ import (

componentsv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/components/v1"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/deploy"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/render"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/render/kustomize"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/render/template"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/updatestatus"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/handlers"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/predicates/resources"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/reconciler"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/metadata/labels"
Expand All @@ -53,7 +54,10 @@ func NewComponentReconciler(ctx context.Context, mgr ctrl.Manager) error {
Owns(&rbacv1.ClusterRoleBinding{}).
Owns(&corev1.Service{}).
Owns(&corev1.ServiceAccount{}).
Owns(&appsv1.Deployment{}, builder.WithPredicates(resources.NewDeploymentPredicate())).
Owns(
&appsv1.Deployment{},
builder.WithPredicates(resources.NewDeploymentPredicate()),
).
Owns(&admissionregistrationv1.MutatingWebhookConfiguration{}).
Owns(&admissionregistrationv1.ValidatingWebhookConfiguration{}).
Watches(&corev1.Namespace{}).
Expand All @@ -62,20 +66,22 @@ func NewComponentReconciler(ctx context.Context, mgr ctrl.Manager) error {
// the operator (overlays/odh/extras), so in order to properly keep them
// in sync with the manifests, we should also create an additional watcher
Watches(&rbacv1.ClusterRole{}).
// This component copies a secret referenced by the default IngressController
// to the Istio namespace, hence we need to watch for the related secret
// placed in the openshift-ingress namespace and with name defined in the
// IngressController on the .spec.defaultCertificate.name path
WatchesH(
&corev1.Secret{},
handlers.ToNamed(componentsv1.ModelRegistryInstanceName),
builder.WithPredicates(ingressSecret(ctx, mgr.GetClient()))).
// This component adds a ServiceMeshMember resource to the registries
// namespaces that must be left even if the component is removed, hence
// we can't own.
//
// TODO: add dynamic watching for gvk.ServiceMeshMember if it make sense
// https://issues.redhat.com/browse/RHOAIENG-15170
//
// actions
WithAction(checkPreConditions).
WithAction(initialize).
WithAction(configureDependencies).
WithAction(template.NewAction(
template.WithCache(render.DefaultCachingKeyFn),
)).
WithAction(kustomize.NewAction(
kustomize.WithCache(kustomize.DefaultCachingKeyFn),
kustomize.WithCache(render.DefaultCachingKeyFn),
kustomize.WithLabel(labels.ODH.Component(ComponentName), "true"),
kustomize.WithLabel(labels.K8SCommon.PartOf, ComponentName),
)).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ func initialize(ctx context.Context, rr *odhtypes.ReconciliationRequest) error {
baseManifestInfo(BaseManifestsSourcePath),
extraManifestInfo(BaseManifestsSourcePath),
}
rr.Templates = []odhtypes.TemplateInfo{{
FS: resourcesFS,
Path: ServiceMeshMemberTemplate,
}}

df := mr.GetDevFlags()

Expand Down Expand Up @@ -123,17 +127,6 @@ func configureDependencies(ctx context.Context, rr *odhtypes.ReconciliationReque
return fmt.Errorf("failed to add default ingress secret for model registry: %w", err)
}

// Service Mesh

smm, err := createServiceMeshMember(rr.DSCI, mr.Spec.RegistriesNamespace)
if err != nil {
return fmt.Errorf("failed to create ServiceMesh Member: %w", err)
}

if err := rr.AddResource(smm); err != nil {
return fmt.Errorf("failed to add ServiceMesh Member: %w", err)
}

return nil
}

Expand Down
78 changes: 4 additions & 74 deletions controllers/components/modelregistry/modelregistry_support.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,20 @@
package modelregistry

import (
"context"
"fmt"
"embed"
"path"
"strings"
"text/template"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"

componentsv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/components/v1"
dsciv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/dscinitialization/v1"
infrav1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/infrastructure/v1"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster"
odhtypes "github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/types"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/conversion"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/deploy"

_ "embed"
)

const (
ComponentName = componentsv1.ModelRegistryComponentName
DefaultModelRegistriesNamespace = "odh-model-registries"
DefaultModelRegistryCert = "default-modelregistry-cert"
BaseManifestsSourcePath = "overlays/odh"
ServiceMeshMemberTemplate = "resources/servicemesh-member.tmpl.yaml"
)

var (
Expand All @@ -42,8 +29,8 @@ var (
}
)

//go:embed resources/servicemesh-member.tmpl.yaml
var smmTemplate string
//go:embed resources
var resourcesFS embed.FS

func baseManifestInfo(sourcePath string) odhtypes.ManifestInfo {
return odhtypes.ManifestInfo{
Expand All @@ -60,60 +47,3 @@ func extraManifestInfo(sourcePath string) odhtypes.ManifestInfo {
SourcePath: path.Join(sourcePath, "extras"),
}
}

func createServiceMeshMember(dsci *dsciv1.DSCInitialization, namespace string) (*unstructured.Unstructured, error) {
tmpl, err := template.New("servicemeshmember").Parse(smmTemplate)
if err != nil {
return nil, fmt.Errorf("error parsing servicemeshmember template: %w", err)
}

controlPlaneData := struct {
Namespace string
ControlPlane *infrav1.ControlPlaneSpec
}{
Namespace: namespace,
ControlPlane: &dsci.Spec.ServiceMesh.ControlPlane,
}

builder := strings.Builder{}
if err = tmpl.Execute(&builder, controlPlaneData); err != nil {
return nil, fmt.Errorf("error executing servicemeshmember template: %w", err)
}

obj, err := conversion.StrToUnstructured(builder.String())
if err != nil || len(obj) != 1 {
return nil, fmt.Errorf("error converting servicemeshmember template: %w", err)
}

return obj[0], nil
}

func ingressSecret(ctx context.Context, cli client.Client) predicate.Funcs {
f := func(obj client.Object) bool {
ic, err := cluster.FindAvailableIngressController(ctx, cli)
if err != nil {
return false
}
if ic.Spec.DefaultCertificate == nil {
return false
}

return obj.GetName() == ic.Spec.DefaultCertificate.Name &&
obj.GetNamespace() == cluster.IngressNamespace
}

return predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool {
return false
},
GenericFunc: func(e event.GenericEvent) bool {
return false
},
DeleteFunc: func(e event.DeleteEvent) bool {
return f(e.Object)
},
UpdateFunc: func(e event.UpdateEvent) bool {
return f(e.ObjectNew)
},
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ apiVersion: maistra.io/v1
kind: ServiceMeshMember
metadata:
name: default
namespace: {{.Namespace}}
namespace: {{.Component.Spec.RegistriesNamespace}}
spec:
controlPlaneRef:
namespace: {{ .ControlPlane.Namespace }}
name: {{ .ControlPlane.Name }}
namespace: {{ .DSCI.Spec.ServiceMesh.ControlPlane.Namespace }}
name: {{ .DSCI.Spec.ServiceMesh.ControlPlane.Name }}
3 changes: 2 additions & 1 deletion controllers/components/ray/ray_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (

componentsv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/components/v1"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/deploy"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/render"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/render/kustomize"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/updatestatus"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/predicates/resources"
Expand Down Expand Up @@ -57,7 +58,7 @@ func NewComponentReconciler(ctx context.Context, mgr ctrl.Manager) error {
WithAction(initialize).
WithAction(devFlags).
WithAction(kustomize.NewAction(
kustomize.WithCache(kustomize.DefaultCachingKeyFn),
kustomize.WithCache(render.DefaultCachingKeyFn),
kustomize.WithLabel(labels.ODH.Component(ComponentName), "true"),
kustomize.WithLabel(labels.K8SCommon.PartOf, ComponentName),
)).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (

componentsv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/components/v1"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/deploy"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/render"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/render/kustomize"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/updatestatus"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/predicates/resources"
Expand All @@ -54,7 +55,7 @@ func NewComponentReconciler(ctx context.Context, mgr ctrl.Manager) error {
WithAction(initialize).
WithAction(devFlags).
WithAction(kustomize.NewAction(
kustomize.WithCache(kustomize.DefaultCachingKeyFn),
kustomize.WithCache(render.DefaultCachingKeyFn),
kustomize.WithLabel(labels.ODH.Component(ComponentName), "true"),
kustomize.WithLabel(labels.K8SCommon.PartOf, ComponentName),
)).
Expand Down
3 changes: 2 additions & 1 deletion controllers/components/trustyai/trustyai_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

componentsv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/components/v1"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/deploy"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/render"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/render/kustomize"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/updatestatus"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/predicates/resources"
Expand Down Expand Up @@ -55,7 +56,7 @@ func NewComponentReconciler(ctx context.Context, mgr ctrl.Manager) error {
WithAction(initialize).
WithAction(devFlags).
WithAction(kustomize.NewAction(
kustomize.WithCache(kustomize.DefaultCachingKeyFn),
kustomize.WithCache(render.DefaultCachingKeyFn),
kustomize.WithLabel(labels.ODH.Component(ComponentName), "true"),
kustomize.WithLabel(labels.K8SCommon.PartOf, ComponentName),
)).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@ import (

const RendererEngine = "kustomize"

type CachingKeyFn func(_ context.Context, rr *types.ReconciliationRequest) ([]byte, error)

// Action takes a set of manifest locations and render them as Unstructured resources for
// further processing. The Action can eventually cache the results in memory to avoid doing
// a full manifest rendering when not needed.
type Action struct {
keOpts []kustomize.EngineOptsFn
ke *kustomize.Engine

cachingKeyFn CachingKeyFn
cachingKeyFn render.CachingKeyFn
cachingKey []byte
cachedResources resources.UnstructuredList
}
Expand Down Expand Up @@ -70,7 +68,7 @@ func WithManifestsOptions(values ...kustomize.EngineOptsFn) ActionOpts {
}
}

func WithCache(value CachingKeyFn) ActionOpts {
func WithCache(value render.CachingKeyFn) ActionOpts {
return func(action *Action) {
action.cachingKeyFn = value
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func TestRenderResourcesWithCacheAction(t *testing.T) {
g.Expect(err).ShouldNot(HaveOccurred())

action := kustomize.NewAction(
kustomize.WithCache(kustomize.DefaultCachingKeyFn),
kustomize.WithCache(render.DefaultCachingKeyFn),
kustomize.WithLabel(labels.ComponentPartOf, "foo"),
kustomize.WithLabel("platform.opendatahub.io/namespace", ns),
kustomize.WithAnnotation("platform.opendatahub.io/release", "1.2.3"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kustomize
package render

import (
"context"
Expand All @@ -9,13 +9,21 @@ import (
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/types"
)

type CachingKeyFn func(_ context.Context, rr *types.ReconciliationRequest) ([]byte, error)

func DefaultCachingKeyFn(_ context.Context, rr *types.ReconciliationRequest) ([]byte, error) {
hash := sha256.New()

generation := make([]byte, binary.MaxVarintLen64)
binary.PutVarint(generation, rr.Instance.GetGeneration())
dsciGeneration := make([]byte, binary.MaxVarintLen64)
binary.PutVarint(dsciGeneration, rr.DSCI.GetGeneration())

instanceGeneration := make([]byte, binary.MaxVarintLen64)
binary.PutVarint(instanceGeneration, rr.Instance.GetGeneration())

if _, err := hash.Write(generation); err != nil {
if _, err := hash.Write(dsciGeneration); err != nil {
return nil, fmt.Errorf("unable to calculate checksum of reconciliation object: %w", err)
}
if _, err := hash.Write(instanceGeneration); err != nil {
return nil, fmt.Errorf("unable to calculate checksum of reconciliation object: %w", err)
}
if _, err := hash.Write([]byte(rr.Release.Name)); err != nil {
Expand All @@ -30,6 +38,11 @@ func DefaultCachingKeyFn(_ context.Context, rr *types.ReconciliationRequest) ([]
return nil, fmt.Errorf("unable to calculate checksum of reconciliation object: %w", err)
}
}
for i := range rr.Templates {
if _, err := hash.Write([]byte(rr.Templates[i].Path)); err != nil {
return nil, fmt.Errorf("unable to calculate checksum of reconciliation object: %w", err)
}
}

return hash.Sum(nil), nil
}
Loading