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

feat: embed templates into operator plugin #5

Merged
merged 19 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from 13 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
117 changes: 80 additions & 37 deletions pkg/kfapp/ossm/ossm_manifests.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,53 @@
package ossm

import (
"embed"
"fmt"
kftypesv3 "github.com/opendatahub-io/opendatahub-operator/apis/apps"
configtypes "github.com/opendatahub-io/opendatahub-operator/apis/config"
"github.com/opendatahub-io/opendatahub-operator/pkg/kfconfig/ossmplugin"
"github.com/opendatahub-io/opendatahub-operator/pkg/secret"
"github.com/pkg/errors"
"io/fs"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/rest"
"os"
"path"
"path/filepath"
"strings"
)

//go:embed templates
var embeddedFiles embed.FS

type applier func(config *rest.Config, filename string, elems ...configtypes.NameValue) error

func (o *OssmInstaller) applyManifests() error {
var apply applier

for _, m := range o.manifests {
targetPath, err := m.targetPath()
if err != nil {
log.Error(err, "Error generating target path")
return err
}
if m.patch {
apply = func(config *rest.Config, filename string, elems ...configtypes.NameValue) error {
log.Info("patching using manifest", "name", m.name, "path", m.targetPath())
log.Info("patching using manifest", "name", m.name, "path", targetPath)
return o.PatchResourceFromFile(filename, elems...)
}
} else {
apply = func(config *rest.Config, filename string, elems ...configtypes.NameValue) error {
log.Info("applying manifest", "name", m.name, "path", m.targetPath())
log.Info("applying manifest", "name", m.name, "path", targetPath)
return o.CreateResourceFromFile(filename, elems...)
}
}

err := apply(
err = apply(
o.config,
m.targetPath(),
targetPath,
)

if err != nil {
log.Error(err, "failed to create resource", "name", m.name, "path", m.targetPath())
log.Error(err, "failed to create resource", "name", m.name, "path", targetPath)
return err
}
}
Expand All @@ -50,21 +59,24 @@ func (o *OssmInstaller) processManifests() error {
if err := o.SyncCache(); err != nil {
return internalError(err)
}

const (
ControlPlaneDir = "templates/control-plane"
AuthDir = "templates/authorino"
)

Choose a reason for hiding this comment

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

I would move them next to //go:embed, otherwise they're local per func invocation.

Copy link
Author

Choose a reason for hiding this comment

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

moved it with the const baseOutputDir in types.go 22c5bd0

// TODO warn when file is not present instead of throwing an error
// IMPORTANT: Order of locations from where we load manifests/templates to process is significant
err := o.loadManifestsFrom(
path.Join("control-plane", "base"),
path.Join("control-plane", "filters"),
path.Join("control-plane", "oauth"),
path.Join("control-plane", "smm.tmpl"),
path.Join("control-plane", "namespace.patch.tmpl"),

path.Join("authorino", "namespace.tmpl"),
path.Join("authorino", "smm.tmpl"),
path.Join("authorino", "base"),
path.Join("authorino", "rbac"),
path.Join("authorino", "mesh-authz-ext-provider.patch.tmpl"),
path.Join(ControlPlaneDir, "base"),
path.Join(ControlPlaneDir, "filters"),
path.Join(ControlPlaneDir, "oauth"),
path.Join(ControlPlaneDir, "smm.tmpl"),
path.Join(ControlPlaneDir, "namespace.patch.tmpl"),

path.Join(AuthDir, "namespace.tmpl"),
path.Join(AuthDir, "auth-smm.tmpl"),
path.Join(AuthDir, "base"),
path.Join(AuthDir, "rbac"),
path.Join(AuthDir, "mesh-authz-ext-provider.patch.tmpl"),
)
if err != nil {
return internalError(errors.WithStack(err))
Expand All @@ -74,6 +86,9 @@ func (o *OssmInstaller) processManifests() error {
if err != nil {
return internalError(errors.WithStack(err))
}
if copyFsErr := copyEmbeddedFS(embeddedFiles, "templates", filepath.Join(baseOutputDir, o.Namespace, o.Name)); copyFsErr != nil {
cam-garrison marked this conversation as resolved.
Show resolved Hide resolved
return internalError(errors.WithStack(err))
}

for i, m := range o.manifests {
if err := m.processTemplate(data); err != nil {
Expand All @@ -87,15 +102,12 @@ func (o *OssmInstaller) processManifests() error {
}

func (o *OssmInstaller) loadManifestsFrom(paths ...string) error {
manifestRepo, ok := o.GetRepoCache(kftypesv3.ManifestsRepoName)
if !ok {
return internalError(errors.New("manifests repo is not defined."))
}

var err error
var manifests []manifest
for i := range paths {
manifests, err = loadManifestsFrom(manifests, path.Join(manifestRepo.LocalPath, TMPL_LOCAL_PATH, paths[i]))
var manifestRepo = embeddedFiles
var rootDir = filepath.Join(baseOutputDir, o.Namespace, o.Name)
for _, p := range paths {
manifests, err = loadManifestsFrom(manifestRepo, manifests, p, rootDir)
cam-garrison marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return internalError(errors.WithStack(err))
}
Expand All @@ -106,25 +118,56 @@ func (o *OssmInstaller) loadManifestsFrom(paths ...string) error {
return nil
}

func loadManifestsFrom(manifests []manifest, dir string) ([]manifest, error) {
func loadManifestsFrom(manifestRepo fs.FS, manifests []manifest, path string, rootDir string) ([]manifest, error) {
f, err := manifestRepo.Open(path)
if err != nil {
return nil, internalError(errors.WithStack(err))
}
defer f.Close()

if err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
info, err := f.Stat()
if err != nil {
return nil, internalError(errors.WithStack(err))
}

if info.IsDir() {
// It's a directory, so walk it
dirFS, err := fs.Sub(manifestRepo, path)
if err != nil {
return err
return nil, internalError(errors.WithStack(err))
}
if info.IsDir() {

err = fs.WalkDir(dirFS, ".", func(relativePath string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
fullPath := filepath.Join(path, relativePath)
basePath := filepath.Base(relativePath)
manifests = append(manifests, manifest{
name: basePath,
path: fullPath,
patch: strings.Contains(basePath, ".patch"),
template: filepath.Ext(relativePath) == ".tmpl",
templateDir: rootDir,
})
return nil
})
if err != nil {
return nil, internalError(errors.WithStack(err))
}
} else {
// It's a file, so handle it directly
basePath := filepath.Base(path)
manifests = append(manifests, manifest{
name: basePath,
path: path,
patch: strings.Contains(basePath, ".patch"),
template: filepath.Ext(path) == ".tmpl",
name: basePath,
path: path,
patch: strings.Contains(basePath, ".patch"),
template: filepath.Ext(path) == ".tmpl",
templateDir: rootDir,
})
return nil
}); err != nil {
return nil, internalError(errors.WithStack(err))
}

return manifests, nil
Expand Down
10 changes: 10 additions & 0 deletions pkg/kfapp/ossm/templates/authorino/auth-smm.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: maistra.io/v1
kind: ServiceMeshMember
metadata:
name: default
namespace: {{ .Auth.Namespace }}
spec:
controlPlaneRef:
namespace: {{ .Mesh.Namespace }}
name: {{ .Mesh.Name }}

44 changes: 44 additions & 0 deletions pkg/kfapp/ossm/templates/authorino/base/authconfig.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
apiVersion: authorino.kuadrant.io/v1beta1
kind: AuthConfig
metadata:
name: odh-dashboard-protection
namespace: {{ .AppNamespace }}
labels:
{{ ReplaceChar .Auth.Authorino.Label "=" ": " }}
spec:
hosts:
- {{.AppNamespace}}.{{ .Domain }}
identity:
- name: kubernetes-users
kubernetes:
audiences:
- "https://kubernetes.default.svc"
authorization:
- name: k8s-rbac-only-service-viewers
kubernetes:
user:
valueFrom: { authJSON: auth.identity.username }
resourceAttributes:
namespace:
value: {{ .AppNamespace }}
group:
value: ""
resource:
value: services
name:
value: odh-dashboard
verb:
value: get
response:
- name: x-auth-data
json:
properties:
- name: username
valueFrom: { authJSON: auth.identity.username }
denyWith:
unauthenticated:
message:
value: "Access denied"
unauthorized:
message:
value: "Unauthorized"
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: operator.authorino.kuadrant.io/v1beta1
kind: Authorino
metadata:
name: {{ .Auth.Authorino.Name }}
namespace: {{ .Auth.Namespace }}
spec:
image: {{ .Auth.Authorino.Image }}
authConfigLabelSelectors: {{ .Auth.Authorino.Label }}
clusterWide: true
listener:
tls:
enabled: false
oidcServer:
tls:
enabled: false
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: maistra.io/v2
kind: ServiceMeshControlPlane
metadata:
name: {{ .Mesh.Name }}
namespace: {{ .Mesh.Namespace }}
spec:
techPreview:
meshConfig:
extensionProviders:
- name: {{ .AppNamespace }}-odh-auth-provider
envoyExtAuthzGrpc:
service: {{ .Auth.Authorino.Name }}-authorino-authorization.{{ .Auth.Namespace }}.svc.cluster.local
port: 50051
6 changes: 6 additions & 0 deletions pkg/kfapp/ossm/templates/authorino/namespace.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: v1
kind: Namespace
metadata:
name: {{ .Auth.Namespace }}
labels:
control-plane: authorino-operator
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: auth-service-monitoring
namespace: {{ .Auth.Namespace }}
subjects:
- kind: ServiceAccount
name: auth-service
namespace: {{ .Auth.Namespace }}
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-monitoring-view
13 changes: 13 additions & 0 deletions pkg/kfapp/ossm/templates/authorino/rbac/cluster-role-binding.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: auth-service
namespace: {{ .Auth.Namespace }}
subjects:
- kind: ServiceAccount
name: auth-service
namespace: {{ .Auth.Namespace }}
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: auth-service
Loading