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 a Rdbms prototype repository for Porch #124

Open
wants to merge 48 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
cbf9f6c
Rebased onto new branch
liamfallon Oct 9, 2024
8fba8df
Added config for db tutorial
liamfallon Oct 10, 2024
6670e9f
Added package resource perisistence
liamfallon Oct 10, 2024
a175c64
porchctl pull/push supported
liamfallon Oct 13, 2024
e9b6970
Fix push deleting resources on package on no change push
liamfallon Oct 13, 2024
1a0faa1
Allow all mutations to cary on in the case of more than one
liamfallon Oct 14, 2024
9da733f
Propose delete now working
liamfallon Oct 14, 2024
0feef02
Add namespace to db creation Yaml file
liamfallon Oct 16, 2024
e2689d1
Update fox for resource deletion
liamfallon Oct 22, 2024
6c7fadd
Remove namespace from package names
liamfallon Oct 23, 2024
72a1cfe
Return pointer rather than instance on read of PackageRevision from db
liamfallon Oct 29, 2024
804d636
Fix up comments after rebase
liamfallon Nov 5, 2024
e64c038
Fix up comments after rebase
liamfallon Nov 5, 2024
9ef6fba
Update db repo due to rebases
liamfallon Nov 6, 2024
3cf87a4
Rebased against main
liamfallon Nov 27, 2024
f302d04
Added config for db tutorial
liamfallon Oct 10, 2024
150c30d
Added package resource perisistence
liamfallon Oct 10, 2024
b709c5a
porchctl pull/push supported
liamfallon Oct 13, 2024
941532a
Fix push deleting resources on package on no change push
liamfallon Oct 13, 2024
de5b756
Propose delete now working
liamfallon Oct 14, 2024
e1fbf5d
Add namespace to db creation Yaml file
liamfallon Oct 16, 2024
b24ddd5
Remove namespace from package names
liamfallon Oct 23, 2024
f456ca4
Return pointer rather than instance on read of PackageRevision from db
liamfallon Oct 29, 2024
1cb41f2
Fix up comments after rebase
liamfallon Nov 5, 2024
ceaa33d
Fix up comments after rebase
liamfallon Nov 5, 2024
31b5a4e
Update db repo due to rebases
liamfallon Nov 6, 2024
6086c2a
Use a RDBMS as a cache in Porch
liamfallon Nov 27, 2024
df3b66b
Rebase against main
liamfallon Nov 28, 2024
55ef1cb
rebase
liamfallon Nov 28, 2024
499a30d
Latest update
liamfallon Dec 16, 2024
d05efe5
Tutorial update
liamfallon Dec 16, 2024
4318af0
Correct trace statement
liamfallon Dec 16, 2024
eca436f
Added handling for resources
liamfallon Jan 9, 2025
ea31b74
Updated to handle subpackages
liamfallon Jan 9, 2025
259b357
Clean up lint flags
liamfallon Jan 9, 2025
883ed32
Refactored code to put repository implementations and cache implement…
liamfallon Jan 15, 2025
cffb7e2
Cache moved behind interfce
liamfallon Jan 21, 2025
1f581a7
Rebasing
liamfallon Jan 21, 2025
1d07598
Up[date go modules
liamfallon Feb 4, 2025
a6a0c63
Refactoring to make db cache optional
liamfallon Feb 12, 2025
46b2ca1
Updated to fix 323 tests
liamfallon Feb 17, 2025
d5a7374
Fix linting warnings
liamfallon Feb 17, 2025
ede953c
Add command line options to enable db cache
liamfallon Feb 18, 2025
023cea1
Correct the names of some files and fix commetns in CR Cache
liamfallon Feb 18, 2025
6aabb03
Fix mutex locking on package
liamfallon Feb 18, 2025
be3fa2a
Restore e2e tests commented out in error
liamfallon Feb 18, 2025
7975236
Updated db cache to handle connection pooling correctly
liamfallon Feb 19, 2025
75ea664
Changes and fixes to get more e2e tests passing
liamfallon Feb 24, 2025
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
2 changes: 1 addition & 1 deletion api/generated/openapi/zz_generated.openapi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions deployments/local/kind_porch_test_cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ nodes:
hostPort: 3000
- containerPort: 30001 # function-runner NodePort
hostPort: 30001
extraMounts:
- containerPath: /mnt/postgresql
hostPath: /tmp/porch/postgresql
67 changes: 67 additions & 0 deletions examples/tutorials/database-cache/porch-db.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
DROP TABLE IF EXISTS resources;
DROP TABLE IF EXISTS package_revisions;
DROP TABLE IF EXISTS packages;
DROP TABLE IF EXISTS repositories;

DROP TYPE IF EXISTS package_rev_lifecycle;

CREATE TABLE IF NOT EXISTS repositories (
name_space TEXT NOT NULL,
repo_name TEXT NOT NULL,
meta TEXT NOT NULL,
spec TEXT NOT NULL,
updated TIMESTAMPTZ,
updatedby TEXT NOT NULL,
deployment BOOLEAN,
PRIMARY KEY (name_space, repo_name)
);

CREATE TABLE IF NOT EXISTS packages (
name_space TEXT NOT NULL,
repo_name TEXT NOT NULL,
package_name TEXT NOT NULL,
meta TEXT NOT NULL,
spec TEXT NOT NULL,
updated TIMESTAMPTZ NOT NULL,
updatedby TEXT NOT NULL,
PRIMARY KEY (name_space, repo_name, package_name),
CONSTRAINT fk_repository
FOREIGN KEY (name_space, repo_name)
REFERENCES repositories (name_space, repo_name)
ON DELETE CASCADE
);

CREATE TYPE package_rev_lifecycle AS ENUM ('Draft', 'Proposed', 'Published', 'DeletionProposed');

CREATE TABLE IF NOT EXISTS package_revisions (
name_space TEXT NOT NULL,
repo_name TEXT NOT NULL,
package_name TEXT NOT NULL,
package_rev TEXT NOT NULL,
workspace_name TEXT NOT NULL,
meta TEXT NOT NULL,
spec TEXT NOT NULL,
updated TIMESTAMPTZ NOT NULL,
updatedby TEXT NOT NULL,
lifecycle package_rev_lifecycle NOT NULL,
PRIMARY KEY (name_space, repo_name, package_name, package_rev, workspace_name),
CONSTRAINT fk_package
FOREIGN KEY (name_space, repo_name, package_name)
REFERENCES packages (name_space, repo_name, package_name)
ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS resources (
name_space TEXT NOT NULL,
repo_name TEXT NOT NULL,
package_name TEXT NOT NULL,
package_rev TEXT NOT NULL,
workspace_name TEXT NOT NULL,
resource_key TEXT NOT NULL,
resource_value TEXT NOT NULL,
PRIMARY KEY (name_space, repo_name, package_name, package_rev, workspace_name, resource_key),
CONSTRAINT fk_package
FOREIGN KEY (name_space, repo_name, package_name, package_rev, workspace_name)
REFERENCES package_revisions (name_space, repo_name, package_name, package_rev, workspace_name)
ON DELETE CASCADE
);
128 changes: 128 additions & 0 deletions examples/tutorials/database-cache/postgres.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
apiVersion: v1
kind: Namespace
metadata:
name: porch-db
labels:
name: porch-db

---

# See:
# https://www.digitalocean.com/community/tutorials/how-to-deploy-postgres-to-kubernetes-cluster#create-a-persistentvolume-pv-and-a-persistentvolumeclaim-pvc

apiVersion: apps/v1
kind: Deployment

metadata:
name: postgresql
namespace: porch-db
labels:
app.kubernetes.io/name: postgresql
app.kubernetes.io/part-of: porch

spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/name: postgresql
app.kubernetes.io/part-of: porch
template:
metadata:
labels:
app.kubernetes.io/name: postgresql
app.kubernetes.io/part-of: porch
spec:
containers:
- name: postgresql
image: 'postgres:17'
imagePullPolicy: IfNotPresent
resources:
requests:
memory: 2Gi
cpu: "2"
env:
- name: PGPORT
value: "55432"
- name: POSTGRES_DB
value: porch
- name: POSTGRES_USER
value: porch
- name: POSTGRES_PASSWORD
value: porch
- name: POSTGRES_INITDB_ARGS
value: --no-instructions --no-sync
volumeMounts:
- name: postgresql-data
mountPath: /var/lib/postgresql/data
ports:
- name: postgresql
containerPort: 55432
volumes:
- name: postgresql-data
persistentVolumeClaim:
claimName: postgresql-data
---

apiVersion: v1
kind: Service

metadata:
name: postgresql
namespace: porch-db
labels:
app.kubernetes.io/name: postgresql
app.kubernetes.io/part-of: porch

spec:
type: LoadBalancer
selector:
app.kubernetes.io/name: postgresql
app.kubernetes.io/part-of: porch
ports:
- name: postgresql
port: 55432
targetPort: 55432

---

apiVersion: v1
kind: PersistentVolumeClaim

metadata:
name: postgresql-data
namespace: porch-db
labels:
app.kubernetes.io/name: postgresql
app.kubernetes.io/part-of: porch

spec:
storageClassName: manual
#storageClassName: standard # this is the rancher.io/local-path provisioner in Kind
accessModes:
- ReadWriteMany # rancher.io/local-path only supports ReadWriteOnce
resources:
requests:
storage: 5Gi

---

kind: PersistentVolume
apiVersion: v1

metadata:
name: postgresql-data
labels:
app.kubernetes.io/name: postgresql
app.kubernetes.io/part-of: porch

spec:
claimRef:
name: postgresql-data
namespace: porch-db
storageClassName: manual
capacity:
storage: 5Gi
accessModes:
- ReadWriteMany
hostPath:
path: /mnt/postgresql
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ require (
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/google/uuid v1.6.0
github.com/hexops/gotextdiff v1.0.3
github.com/jackc/pgx/v5 v5.7.2
github.com/otiai10/copy v1.14.0
github.com/philopon/go-toposort v0.0.0-20170620085441-9be86dbd762f
github.com/prep/wasmexec v0.0.0-20220807105708-6554945c1dec
Expand Down Expand Up @@ -123,6 +124,9 @@ require (
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,14 @@ github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.7.2 h1:mLoDLV6sonKlvjIEsV56SkWNCnuNv531l94GaIzO+XI=
github.com/jackc/pgx/v5 v5.7.2/go.mod h1:ncY89UGWxg82EykZUwSpUKEfccBGGYq1xjrOpsbsfGQ=
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
github.com/jonboulle/clockwork v0.2.2 h1:UOGuzwb1PwsrDAObMuhUnj0p5ULPj8V/xJ7Kx9qUBdQ=
Expand Down
43 changes: 19 additions & 24 deletions pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ import (
internalapi "github.com/nephio-project/porch/internal/api/porchinternal/v1alpha1"
"github.com/nephio-project/porch/internal/kpt/fnruntime"
"github.com/nephio-project/porch/pkg/cache"
memorycache "github.com/nephio-project/porch/pkg/cache/memory"
cachetypes "github.com/nephio-project/porch/pkg/cache/types"
"github.com/nephio-project/porch/pkg/engine"
"github.com/nephio-project/porch/pkg/meta"
"github.com/nephio-project/porch/pkg/registry/porch"
"google.golang.org/api/option"
"google.golang.org/api/sts/v1"
Expand Down Expand Up @@ -75,13 +74,9 @@ func init() {

// ExtraConfig holds custom apiserver config
type ExtraConfig struct {
CoreAPIKubeconfigPath string
CacheDirectory string
FunctionRunnerAddress string
DefaultImagePrefix string
RepoSyncFrequency time.Duration
UseUserDefinedCaBundle bool
MaxGrpcMessageSize int
CoreAPIKubeconfigPath string
GRPCRuntimeOptions engine.GRPCRuntimeOptions
CacheOptions cachetypes.CacheOptions
}

// Config defines the config for the apiserver
Expand All @@ -94,7 +89,7 @@ type Config struct {
type PorchServer struct {
GenericAPIServer *genericapiserver.GenericAPIServer
coreClient client.WithWatch
cache cache.Cache
cache cachetypes.Cache
PeriodicRepoSyncFrequency time.Duration
}

Expand Down Expand Up @@ -220,40 +215,40 @@ func (c completedConfig) New() (*PorchServer, error) {
porch.NewGcloudWIResolver(coreV1Client, stsClient),
}

metadataStore := meta.NewCrdMetadataStore(coreClient)

credentialResolver := porch.NewCredentialResolver(coreClient, resolverChain)
referenceResolver := porch.NewReferenceResolver(coreClient)
userInfoProvider := &porch.ApiserverUserInfoProvider{}

watcherMgr := engine.NewWatcherManager()

memoryCache := memorycache.NewCache(c.ExtraConfig.CacheDirectory, c.ExtraConfig.RepoSyncFrequency, c.ExtraConfig.UseUserDefinedCaBundle, memorycache.CacheOptions{
CredentialResolver: credentialResolver,
UserInfoProvider: userInfoProvider,
MetadataStore: metadataStore,
ObjectNotifier: watcherMgr,
})
c.ExtraConfig.CacheOptions.CoreClient = coreClient
c.ExtraConfig.CacheOptions.RepoPRChangeNotifier = watcherMgr
c.ExtraConfig.CacheOptions.ExternalRepoOptions.CredentialResolver = credentialResolver
c.ExtraConfig.CacheOptions.ExternalRepoOptions.UserInfoProvider = userInfoProvider

cacheImpl, err := cache.CreateCacheImpl(context.TODO(), c.ExtraConfig.CacheOptions)
if err != nil {
return nil, fmt.Errorf("failed to create repository cache: %w", err)
}

runnerOptionsResolver := func(namespace string) fnruntime.RunnerOptions {
runnerOptions := fnruntime.RunnerOptions{}
runnerOptions.InitDefaults(c.ExtraConfig.DefaultImagePrefix)
runnerOptions.InitDefaults(c.ExtraConfig.GRPCRuntimeOptions.DefaultImagePrefix)

return runnerOptions
}

cad, err := engine.NewCaDEngine(
engine.WithCache(memoryCache),
engine.WithCache(cacheImpl),
// The order of registering the function runtimes matters here. When
// evaluating a function, the runtimes will be tried in the same
// order as they are registered.
engine.WithBuiltinFunctionRuntime(),
engine.WithGRPCFunctionRuntime(c.ExtraConfig.FunctionRunnerAddress, c.ExtraConfig.MaxGrpcMessageSize),
engine.WithGRPCFunctionRuntime(c.ExtraConfig.GRPCRuntimeOptions),
engine.WithCredentialResolver(credentialResolver),
engine.WithRunnerOptionsResolver(runnerOptionsResolver),
engine.WithReferenceResolver(referenceResolver),
engine.WithUserInfoProvider(userInfoProvider),
engine.WithMetadataStore(metadataStore),
engine.WithWatcherManager(watcherMgr),
)
if err != nil {
Expand All @@ -268,9 +263,9 @@ func (c completedConfig) New() (*PorchServer, error) {
s := &PorchServer{
GenericAPIServer: genericServer,
coreClient: coreClient,
cache: memoryCache,
cache: cacheImpl,
// Set background job periodic frequency the same as repo sync frequency.
PeriodicRepoSyncFrequency: c.ExtraConfig.RepoSyncFrequency,
PeriodicRepoSyncFrequency: c.ExtraConfig.CacheOptions.RepoSyncFrequency,
}

// Install the groups.
Expand Down
33 changes: 27 additions & 6 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 The Nephio Authors
// Copyright 2024-2025 The Nephio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -16,12 +16,33 @@ package cache

import (
"context"
"fmt"

configapi "github.com/nephio-project/porch/api/porchconfig/v1alpha1"
"github.com/nephio-project/porch/pkg/repository"
crcache "github.com/nephio-project/porch/pkg/cache/crcache"
"github.com/nephio-project/porch/pkg/cache/dbcache"
cachetypes "github.com/nephio-project/porch/pkg/cache/types"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/trace"
)

type Cache interface {
OpenRepository(ctx context.Context, repositorySpec *configapi.Repository) (repository.Repository, error)
CloseRepository(ctx context.Context, repositorySpec *configapi.Repository, allRepos []configapi.Repository) error
var tracer = otel.Tracer("cache")

func CreateCacheImpl(ctx context.Context, options cachetypes.CacheOptions) (cachetypes.Cache, error) {
ctx, span := tracer.Start(ctx, "Repository::RepositoryFactory", trace.WithAttributes())
defer span.End()

var cacheFactory cachetypes.CacheFactory

switch cacheType := options.CacheType; cacheType {
case cachetypes.CRCacheType:
cacheFactory = new(crcache.CrCacheFactory)

case cachetypes.DBCacheType:
cacheFactory = new(dbcache.DbCacheFactory)

default:
return nil, fmt.Errorf("type %q not supported", cacheType)
}

return cacheFactory.NewCache(ctx, options)
}
Loading
Loading