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

Process SM related templates in memory #15

Merged
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
8 changes: 1 addition & 7 deletions components/dashboard/servicemesh_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package dashboard

import (
"path"
"path/filepath"

operatorv1 "github.com/openshift/api/operator/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -43,15 +42,10 @@ func (d *Dashboard) configureServiceMesh(cli client.Client, owner metav1.Object,

func (d *Dashboard) defineServiceMeshFeatures(dscispec *dsci.DSCInitializationSpec) feature.DefinedFeatures {
return func(s *feature.FeaturesInitializer) error {
var rootDir = filepath.Join(feature.BaseOutputDir, dscispec.ApplicationsNamespace)
if err := feature.CopyEmbeddedFiles("templates", rootDir); err != nil {
return err
}

createMeshResources, err := feature.CreateFeature("dashboard-create-service-mesh-routing-resources").
For(dscispec).
Manifests(
path.Join(rootDir, feature.ControlPlaneDir, "components", d.GetComponentName()),
path.Join(feature.ControlPlaneDir, "components", d.GetComponentName()),
).
WithResources(servicemesh.EnabledInDashboard).
WithData(
Expand Down
10 changes: 2 additions & 8 deletions components/kserve/serverless_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package kserve

import (
"path"
"path/filepath"

"github.com/opendatahub-io/opendatahub-operator/v2/pkg/feature"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/feature/serverless"
Expand All @@ -16,15 +15,10 @@ const (
)

func (k *Kserve) configureServerlessFeatures(s *feature.FeaturesInitializer) error {
var rootDir = filepath.Join(feature.BaseOutputDir, s.DSCInitializationSpec.ApplicationsNamespace)
if err := feature.CopyEmbeddedFiles(templatesDir, rootDir); err != nil {
return err
}

servingDeployment, err := feature.CreateFeature("serverless-serving-deployment").
For(s.DSCInitializationSpec).
Manifests(
path.Join(rootDir, templatesDir, "serving-install"),
path.Join(templatesDir, "serving-install"),
).
WithData(PopulateComponentSettings(k)).
PreConditions(
Expand Down Expand Up @@ -55,7 +49,7 @@ func (k *Kserve) configureServerlessFeatures(s *feature.FeaturesInitializer) err
).
WithResources(serverless.ServingCertificateResource).
Manifests(
path.Join(rootDir, templatesDir, "serving-istio-gateways"),
path.Join(templatesDir, "serving-istio-gateways"),
).
Load()
if err != nil {
Expand Down
32 changes: 13 additions & 19 deletions controllers/dscinitialization/servicemesh_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package dscinitialization

import (
"path"
"path/filepath"

operatorv1 "github.com/openshift/api/operator/v1"
corev1 "k8s.io/api/core/v1"
Expand All @@ -14,17 +13,12 @@ import (
)

func defineServiceMeshFeatures(f *feature.FeaturesInitializer) error {
var rootDir = filepath.Join(feature.BaseOutputDir, f.DSCInitializationSpec.ApplicationsNamespace)
if err := feature.CopyEmbeddedFiles("templates", rootDir); err != nil {
return err
}

serviceMeshSpec := f.ServiceMesh

smcpCreation, errSmcp := feature.CreateFeature("service-mesh-control-plane-creation").
For(f.DSCInitializationSpec).
Manifests(
path.Join(rootDir, feature.ControlPlaneDir, "base", "control-plane.tmpl"),
path.Join(feature.ControlPlaneDir, "base", "control-plane.tmpl"),
).
PreConditions(
servicemesh.EnsureServiceMeshOperatorInstalled,
Expand All @@ -43,7 +37,7 @@ func defineServiceMeshFeatures(f *feature.FeaturesInitializer) error {
metricsCollection, errMetrics := feature.CreateFeature("service-mesh-monitoring").
For(f.DSCInitializationSpec).
Manifests(
path.Join(rootDir, feature.MonitoringDir),
path.Join(feature.MonitoringDir),
).
PreConditions(
servicemesh.EnsureServiceMeshInstalled,
Expand All @@ -59,9 +53,9 @@ func defineServiceMeshFeatures(f *feature.FeaturesInitializer) error {
if oauth, err := feature.CreateFeature("service-mesh-control-plane-configure-oauth").
For(f.DSCInitializationSpec).
Manifests(
path.Join(rootDir, feature.ControlPlaneDir, "base"),
path.Join(rootDir, feature.ControlPlaneDir, "oauth"),
path.Join(rootDir, feature.ControlPlaneDir, "filters"),
path.Join(feature.ControlPlaneDir, "base"),
path.Join(feature.ControlPlaneDir, "oauth"),
path.Join(feature.ControlPlaneDir, "filters"),
).
WithResources(
servicemesh.DefaultValues,
Expand Down Expand Up @@ -96,8 +90,8 @@ func defineServiceMeshFeatures(f *feature.FeaturesInitializer) error {
if serviceMesh, err := feature.CreateFeature("app-add-namespace-to-service-mesh").
For(f.DSCInitializationSpec).
Manifests(
path.Join(rootDir, feature.ControlPlaneDir, "smm.tmpl"),
path.Join(rootDir, feature.ControlPlaneDir, "namespace.patch.tmpl"),
path.Join(feature.ControlPlaneDir, "smm.tmpl"),
path.Join(feature.ControlPlaneDir, "namespace.patch.tmpl"),
).
WithData(servicemesh.ClusterDetails).
Load(); err != nil {
Expand All @@ -109,7 +103,7 @@ func defineServiceMeshFeatures(f *feature.FeaturesInitializer) error {
if gatewayRoute, err := feature.CreateFeature("service-mesh-create-gateway-route").
For(f.DSCInitializationSpec).
Manifests(
path.Join(rootDir, feature.ControlPlaneDir, "routing"),
path.Join(feature.ControlPlaneDir, "routing"),
).
WithData(servicemesh.ClusterDetails).
PostConditions(
Expand All @@ -133,10 +127,10 @@ func defineServiceMeshFeatures(f *feature.FeaturesInitializer) error {
if extAuthz, err := feature.CreateFeature("service-mesh-control-plane-setup-external-authorization").
For(f.DSCInitializationSpec).
Manifests(
path.Join(rootDir, feature.AuthDir, "auth-smm.tmpl"),
path.Join(rootDir, feature.AuthDir, "base"),
path.Join(rootDir, feature.AuthDir, "rbac"),
path.Join(rootDir, feature.AuthDir, "mesh-authz-ext-provider.patch.tmpl"),
path.Join(feature.AuthDir, "auth-smm.tmpl"),
path.Join(feature.AuthDir, "base"),
path.Join(feature.AuthDir, "rbac"),
path.Join(feature.AuthDir, "mesh-authz-ext-provider.patch.tmpl"),
).
WithData(servicemesh.ClusterDetails).
PreConditions(
Expand All @@ -153,7 +147,7 @@ func defineServiceMeshFeatures(f *feature.FeaturesInitializer) error {
//
// To make it part of Service Mesh we have to patch it with injection
// enabled instead, otherwise it will not have proxy pod injected.
return f.ApplyManifest(path.Join(rootDir, feature.AuthDir, "deployment.injection.patch.tmpl"))
return f.ApplyManifest(path.Join(feature.AuthDir, "deployment.injection.patch.tmpl"))
},
).
OnDelete(servicemesh.RemoveExtensionProvider).
Expand Down
5 changes: 2 additions & 3 deletions pkg/cluster/cluster_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import (
"errors"
"fmt"
"io"
"k8s.io/client-go/rest"
"net/http"
"net/url"
"os"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"strconv"
"strings"

"github.com/bitly/go-simplejson"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client/config"

"github.com/opendatahub-io/opendatahub-operator/v2/pkg/gvr"
)
Expand Down Expand Up @@ -46,7 +46,6 @@ func GetOAuthServerDetails() (*simplejson.Json, error) {
}

func request(method string, url string) ([]byte, error) {

restCfg, err := config.GetConfig()
if err != nil {
return nil, err
Expand Down
17 changes: 16 additions & 1 deletion pkg/feature/builder.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package feature

import (
"io/fs"

"github.com/pkg/errors"
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/client-go/dynamic"
Expand Down Expand Up @@ -79,7 +81,7 @@ func (fb *featureBuilder) Manifests(paths ...string) *featureBuilder {
var manifests []manifest

for _, path := range paths {
manifests, err = loadManifestsFrom(path)
manifests, err = loadManifestsFrom(f.fsys, path)
if err != nil {
return errors.WithStack(err)
}
Expand Down Expand Up @@ -156,6 +158,7 @@ func (fb *featureBuilder) Load() (*Feature, error) {
feature := &Feature{
Name: fb.name,
Enabled: true,
fsys: embeddedFiles,
}

for i := range fb.builders {
Expand Down Expand Up @@ -185,3 +188,15 @@ func (fb *featureBuilder) Load() (*Feature, error) {

return feature, nil
}

// ManifestSource sets the root file system (fs.FS) from which manifest paths are loaded
// If ManifestSource is not called in the builder chain, the default source will be the embeddedFiles.
func (fb *featureBuilder) ManifestSource(fsys fs.FS) *featureBuilder {
fb.builders = append(fb.builders, func(f *Feature) error {
f.fsys = fsys

return nil
})

return fb
}
34 changes: 17 additions & 17 deletions pkg/feature/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package feature

import (
"context"
"io/fs"

"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
Expand Down Expand Up @@ -31,7 +32,9 @@ type Feature struct {
DynamicClient dynamic.Interface
Client client.Client

manifests []manifest
fsys fs.FS
manifests []manifest

cleanups []Action
resources []Action
preconditions []Action
Expand Down Expand Up @@ -67,35 +70,32 @@ func (f *Feature) Apply() error {
return multiErr.ErrorOrNil()
}

// create or update resources
// Create or update resources
for _, resource := range f.resources {
if err := resource(f); err != nil {
return err
}
}

// Process and apply manifests
for _, m := range f.manifests {
if err := m.processTemplate(f.Spec); err != nil {
for i, m := range f.manifests {
if err := m.processTemplate(f.fsys, f.Spec); err != nil {
return errors.WithStack(err)
}

log.Info("converted template to manifest", "feature", f.Name, "path", m.targetPath())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think as a follow-up PR should change log levels from info to debug in most places. It's too much meaningless output in the logs now just to see that stuff is happening

f.manifests[i] = m
}

if err := f.applyManifests(); err != nil {
return err
}

// Check all postconditions and collect errors
for _, postcondition := range f.postconditions {
multiErr = multierror.Append(multiErr, postcondition(f))
}

if multiErr.ErrorOrNil() != nil {
return multiErr.ErrorOrNil()
}

return nil
return multiErr.ErrorOrNil()
}

func (f *Feature) Cleanup() error {
Expand Down Expand Up @@ -159,34 +159,34 @@ func (f *Feature) addCleanup(cleanupFuncs ...Action) {

func (f *Feature) ApplyManifest(filename string) error {
m := loadManifestFrom(filename)
if err := m.processTemplate(f.Spec); err != nil {
if err := m.processTemplate(f.fsys, f.Spec); err != nil {
return err
}

return f.apply(m)
}

type apply func(filename string) error
type apply func(data string) error

func (f *Feature) apply(m manifest) error {
var applier apply
targetPath := m.targetPath()

if m.patch {
applier = func(filename string) error {
applier = func(data string) error {
log.Info("patching using manifest", "feature", f.Name, "name", m.name, "path", targetPath)

return f.patchResourceFromFile(filename)
return f.patchResources(data)
}
} else {
applier = func(filename string) error {
applier = func(data string) error {
log.Info("applying manifest", "feature", f.Name, "name", m.name, "path", targetPath)

return f.createResourceFromFile(filename)
return f.createResources(data)
}
}

if err := applier(targetPath); err != nil {
if err := applier(m.processedContent); err != nil {
log.Error(err, "failed to create resource", "feature", f.Name, "name", m.name, "path", targetPath)

return err
Expand Down
Loading
Loading