Skip to content

Commit

Permalink
Added handling for resources
Browse files Browse the repository at this point in the history
  • Loading branch information
liamfallon committed Jan 9, 2025
1 parent c53afaf commit 65e1735
Show file tree
Hide file tree
Showing 9 changed files with 359 additions and 107 deletions.
46 changes: 22 additions & 24 deletions examples/tutorials/database-cache/porch-db.sql
Original file line number Diff line number Diff line change
@@ -1,69 +1,67 @@
DROP TABLE IF EXISTS resources;
DROP TABLE IF EXISTS package_revisions;
DROP TABLE IF EXISTS packages;
DROP TABLE IF EXISTS repositories;
DROP TABLE IF EXISTS resources;

DROP TYPE IF EXISTS package_rev_lifecycle;

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

CREATE TABLE IF NOT EXISTS packages (
namespace TEXT NOT NULL,
name_space TEXT NOT NULL,
repo_name TEXT NOT NULL,
package_name TEXT NOT NULL,
meta JSONB NOT NULL,
spec JSONB NOT NULL,
meta TEXT NOT NULL,
spec TEXT NOT NULL,
updated TIMESTAMPTZ NOT NULL,
updatedby TEXT NOT NULL,
PRIMARY KEY (namespace, repo_name, package_name),
PRIMARY KEY (name_space, repo_name, package_name),
CONSTRAINT fk_repository
FOREIGN KEY (namespace, repo_name)
REFERENCES repositories (namespace, repo_name)
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 (
namespace TEXT NOT NULL,
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 JSONB NOT NULL,
spec JSONB 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 (namespace, repo_name, package_name, package_rev),
PRIMARY KEY (name_space, repo_name, package_name, package_rev, workspace_name),
CONSTRAINT fk_package
FOREIGN KEY (namespace, repo_name, package_name)
REFERENCES packages (namespace, repo_name, package_name)
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 (
namespace TEXT NOT NULL,
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 JSONB NOT NULL,
lifecycle package_rev_lifecycle NOT NULL,
resources BYTEA,
PRIMARY KEY (namespace, repo_name, package_name, package_rev),
resource_value TEXT NOT NULL,
PRIMARY KEY (name_space, repo_name, package_name, package_rev, workspace_name, resource_key),
CONSTRAINT fk_package
FOREIGN KEY (namespace, repo_name, package_name)
REFERENCES packages (namespace, repo_name, package_name)
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
);
27 changes: 24 additions & 3 deletions pkg/cache/dbcache/dbpackage.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package dbcache
import (
"context"
"database/sql"
"encoding/json"
"time"

"github.com/nephio-project/porch/api/porch/v1alpha1"
Expand Down Expand Up @@ -51,7 +52,7 @@ func (p *dbPackage) Key() repository.PackageKey {
return p.pkgKey
}

func (p *dbPackage) createPackage() (*dbPackage, error) {
func (p *dbPackage) savePackage() (*dbPackage, error) {
_, err := pkgReadFromDB(p.Key())
if err == nil {
return p, pkgUpdateDB(p)
Expand Down Expand Up @@ -99,8 +100,8 @@ func (p *dbPackage) GetPackage() *v1alpha1.PorchPackage {
}
}

func (p *dbPackage) createPackageRevision(d *dbPackageRevision) (*dbPackageRevision, error) {
return d.createPackageRevision()
func (p *dbPackage) savePackageRevision(d *dbPackageRevision) (*dbPackageRevision, error) {
return d.savePackageRevision()
}

func (p *dbPackage) DeletePackageRevision(ctx context.Context, old repository.PackageRevision) error {
Expand Down Expand Up @@ -138,3 +139,23 @@ func (p *dbPackage) Delete() error {

return pkgDeleteFromDB(p.Key())
}

func (p *dbPackage) metaAsJson() string {
jsonMeta, _ := json.Marshal(p.meta)
return string(jsonMeta)
}

func (p *dbPackage) setMetaFromJson(jsonMeta string) {
p.meta = metav1.ObjectMeta{}
json.Unmarshal([]byte(jsonMeta), p.meta)

Check failure on line 150 in pkg/cache/dbcache/dbpackage.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `json.Unmarshal` is not checked (errcheck)
}

func (p *dbPackage) specAsJson() string {
jsonSpec, _ := json.Marshal(p.spec)
return string(jsonSpec)
}

func (p *dbPackage) setSpecFromJson(jsonSpec string) {
p.spec = v1alpha1.PackageSpec{}
json.Unmarshal([]byte(jsonSpec), p.spec)

Check failure on line 160 in pkg/cache/dbcache/dbpackage.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `json.Unmarshal` is not checked (errcheck)
}
35 changes: 28 additions & 7 deletions pkg/cache/dbcache/dbpackagerevision.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package dbcache
import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
"strings"
Expand Down Expand Up @@ -61,7 +62,7 @@ func (pr *dbPackageRevision) Key() repository.PackageRevisionKey {
return pr.pkgRevKey
}

func (pr *dbPackageRevision) createPackageRevision() (*dbPackageRevision, error) {
func (pr *dbPackageRevision) savePackageRevision() (*dbPackageRevision, error) {
_, err := pkgRevReadFromDB(pr.Key())
if err == nil {
return pr, pkgRevUpdateDB(pr)
Expand Down Expand Up @@ -191,13 +192,13 @@ func (pr *dbPackageRevision) GetUpstreamLock(context.Context) (kptfile.Upstream,
return kptfile.Upstream{}, kptfile.UpstreamLock{}, nil
}

func (p *dbPackageRevision) ToMainPackageRevision() repository.PackageRevision {
func (pr *dbPackageRevision) ToMainPackageRevision() repository.PackageRevision {
return &dbPackageRevision{
pkgRevKey: p.pkgRevKey,
updated: p.updated,
updatedBy: p.updatedBy,
lifecycle: p.lifecycle,
resources: p.resources,
pkgRevKey: pr.pkgRevKey,
updated: pr.updated,
updatedBy: pr.updatedBy,
lifecycle: pr.lifecycle,
resources: pr.resources,
}
}

Expand Down Expand Up @@ -257,3 +258,23 @@ func (pr *dbPackageRevision) UpdateResources(ctx context.Context, new *v1alpha1.
func (pr *dbPackageRevision) GetName() string {
return pr.pkgRevKey.Package
}

func (pr *dbPackageRevision) metaAsJson() string {
jsonMeta, _ := json.Marshal(pr.meta)
return string(jsonMeta)
}

func (pr *dbPackageRevision) setMetaFromJson(jsonMeta string) {
pr.meta = metav1.ObjectMeta{}
json.Unmarshal([]byte(jsonMeta), pr.meta)

Check failure on line 269 in pkg/cache/dbcache/dbpackagerevision.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `json.Unmarshal` is not checked (errcheck)
}

func (pr *dbPackageRevision) specAsJson() string {
jsonSpec, _ := json.Marshal(pr.spec)
return string(jsonSpec)
}

func (pr *dbPackageRevision) setSpecFromJson(jsonSpec string) {
pr.spec = v1alpha1.PackageRevisionSpec{}
json.Unmarshal([]byte(jsonSpec), pr.spec)
}
149 changes: 149 additions & 0 deletions pkg/cache/dbcache/dbpackagerevisionresourcessql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
// Copyright 2024 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.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package dbcache

import (
"database/sql"

"github.com/nephio-project/porch/pkg/repository"
"k8s.io/klog/v2"
)

func pkgRevResourceReadFromDB(prk repository.PackageRevisionKey, resKey string) (string, string, error) {

Check failure on line 24 in pkg/cache/dbcache/dbpackagerevisionresourcessql.go

View workflow job for this annotation

GitHub Actions / lint

func `pkgRevResourceReadFromDB` is unused (unused)
klog.Infof("pkgRevResourceReadFromDB: reading package revision resource %q:%s", prk, resKey)

sqlStatement := `SELECT resource_value FROM resources
WHERE name_space=$1 AND repo_name=$2 AND package_name=$3 AND package_rev=$4 AND workspace_name=$5 AND key=$6`

var resVal string

err := GetDBConnection().db.QueryRow(
sqlStatement, prk.Namespace, prk.Repository, prk.Package, prk.Revision, prk.WorkspaceName).Scan(
&resVal)

if err != nil {
if err == sql.ErrNoRows {
klog.Infof("pkgRevResourceReadFromDB: package revision resource not found in db %q:%s", prk, resKey)
} else {
klog.Infof("pkgRevResourceReadFromDB: reading package revision resource %q:%s returned err: %q", prk, resKey, err)
}
return resKey, "", err
}

klog.Infof("pkgRevResourceReadFromDB: reading package succeeded %q:%s", prk, resKey)

return resKey, resVal, err
}

func pkgRevResourcesReadFromDB(prk repository.PackageRevisionKey) (map[string]string, error) {
sqlStatement := `SELECT * FROM resources WHERE name_space=$1 AND repo_name=$2 AND package_name=$3 AND package_rev=$4 AND workspace_name=$5`

resources := make(map[string]string)

klog.Infof("pkgRevResourcesReadFromDB: running query [%q] on %q", sqlStatement, prk)

rows, err := GetDBConnection().db.Query(sqlStatement, prk.Namespace, prk.Repository, prk.Package, prk.Revision, prk.WorkspaceName)
if err != nil {
klog.Infof("pkgRevResourcesReadFromDB: query failed for %q: %q", prk, err)
return nil, err
}

klog.Infof("pkgRevResourcesReadFromDB: query succeeded for %q", prk)

defer rows.Close()

for rows.Next() {
var resKey, resVal string

rows.Scan(

Check failure on line 70 in pkg/cache/dbcache/dbpackagerevisionresourcessql.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `rows.Scan` is not checked (errcheck)
&resKey,
&resVal)

resources[resKey] = resVal
}

return resources, nil
}

func pkgRevResourcesWriteToDB(pr *dbPackageRevision) error {
klog.Infof("pkgRevResourcesWriteToDB: writing package revision resources for %q", pr.Key())

for k, v := range pr.resources {
if err := pkgRevResourceWriteToDB(pr, k, v); err != nil {
return err
}
}

klog.Infof("pkgRevResourcesWriteToDB: succeeded, row created")
return nil
}

func pkgRevResourceWriteToDB(pr *dbPackageRevision, resKey string, resVal string) error {
klog.Infof("pkgRevResourceWriteToDB: writing package revision resource %q:%s", pr.Key(), resKey)

sqlStatement := `
INSERT INTO resources (name_space, repo_name, package_name, package_rev, workspace_name, resource_key, resource_value)
VALUES ($1, $2, $3, $4, $5, $6, $7)
ON CONFLICT (name_space, repo_name, package_name, package_rev, workspace_name, resource_key)
DO UPDATE SET resource_value = EXCLUDED.resource_value`

klog.Infof("pkgRevResourceWriteToDB: running query [%q] on repository (%#v)", sqlStatement, pr)

prk := pr.Key()
if returnedVal := GetDBConnection().db.QueryRow(
sqlStatement,
prk.Namespace, prk.Repository, prk.Package, prk.Revision, prk.WorkspaceName,
resKey, resVal); returnedVal.Err() == nil {
klog.Infof("pkgRevResourceWriteToDB: query succeeded, row created")
return nil
} else {
klog.Infof("pkgRevResourceWriteToDB: query failed %q", returnedVal.Err())
return returnedVal.Err()
}
}

func pkgRevResourcesDeleteFromDB(prk repository.PackageRevisionKey) error {
klog.Infof("pkgRevResourcesDeleteFromDB: deleting package revision %q", prk)

sqlStatement := `DELETE FROM resources WHERE name_space=$1 AND repo_name=$2 AND package_name=$3 AND package_rev=$4 AND workspace_name=$5`

returnedVal := GetDBConnection().db.QueryRow(sqlStatement,
prk.Namespace, prk.Repository, prk.Package, prk.Revision, prk.WorkspaceName)

if returnedVal.Err() == nil {
klog.Infof("pkgRevResourcesDeleteFromDB: deleted package revision %q", prk)
} else {
klog.Infof("pkgRevResourcesDeleteFromDB: deletion of package revision %q failed: %q", prk, returnedVal.Err())
}

return returnedVal.Err()
}

func pkgRevResourceDeleteFromDB(prk repository.PackageRevisionKey, resKey string) error {
klog.Infof("pkgRevResourceDeleteFromDB: deleting package revision %q", prk)

sqlStatement := `DELETE FROM resources WHERE name_space=$1 AND repo_name=$2 AND package_name=$3 AND package_rev=$4 AND workspace_name=$5 AND presource_key=$6`

returnedVal := GetDBConnection().db.QueryRow(sqlStatement,
prk.Namespace, prk.Repository, prk.Package, prk.Revision, prk.WorkspaceName, resKey)

if returnedVal.Err() == nil {
klog.Infof("pkgRevResourceDeleteFromDB: deleted package revision %q", prk)
} else {
klog.Infof("pkgRevResourceDeleteFromDB: deletion of package revision %q failed: %q", prk, returnedVal.Err())
}

return returnedVal.Err()
}
Loading

0 comments on commit 65e1735

Please sign in to comment.