Skip to content

Commit

Permalink
feat: add global resource version (kyverno#155)
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>
Signed-off-by: Zach Stone <zach@giantswarm.io>
  • Loading branch information
vishal-chdhry authored and stone-z committed Jul 4, 2024
1 parent 9ef0a00 commit b045574
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 40 deletions.
13 changes: 3 additions & 10 deletions pkg/api/cephr.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"slices"
"strconv"

reportsv1 "github.com/kyverno/kyverno/api/reports/v1"
"github.com/kyverno/reports-server/pkg/storage"
Expand Down Expand Up @@ -135,7 +134,6 @@ func (c *cephrStore) Create(ctx context.Context, obj runtime.Object, createValid
if err != nil {
return nil, errors.NewBadRequest(fmt.Sprintf("cannot create cluster ephemeral report: %s", err.Error()))
}
klog.Info(r.ResourceVersion)
if err := c.broadcaster.Action(watch.Added, r); err != nil {
klog.ErrorS(err, "failed to broadcast event")
}
Expand Down Expand Up @@ -317,20 +315,15 @@ func (c *cephrStore) listCephr() (*reportsv1.ClusterEphemeralReportList, error)
}

func (c *cephrStore) createCephr(report *reportsv1.ClusterEphemeralReport) (*reportsv1.ClusterEphemeralReport, error) {
report.ResourceVersion = fmt.Sprint(1)
report.ResourceVersion = c.store.UseResourceVersion()
report.UID = uuid.NewUUID()
report.CreationTimestamp = metav1.Now()

return report, c.store.ClusterEphemeralReports().Create(context.TODO(), *report)
}

func (c *cephrStore) updateCephr(report *reportsv1.ClusterEphemeralReport, oldReport *reportsv1.ClusterEphemeralReport) (*reportsv1.ClusterEphemeralReport, error) {
oldRV, err := strconv.ParseInt(oldReport.ResourceVersion, 10, 64)
if err != nil {
return nil, errorpkg.Wrapf(err, "could not parse resource version")
}
report.ResourceVersion = fmt.Sprint(oldRV + 1)

func (c *cephrStore) updateCephr(report *reportsv1.ClusterEphemeralReport, _ *reportsv1.ClusterEphemeralReport) (*reportsv1.ClusterEphemeralReport, error) {
report.ResourceVersion = c.store.UseResourceVersion()
return report, c.store.ClusterEphemeralReports().Update(context.TODO(), *report)
}

Expand Down
12 changes: 3 additions & 9 deletions pkg/api/cpolr.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"slices"
"strconv"

"github.com/kyverno/reports-server/pkg/storage"
"github.com/kyverno/reports-server/pkg/utils"
Expand Down Expand Up @@ -315,20 +314,15 @@ func (c *cpolrStore) listCpolr() (*v1alpha2.ClusterPolicyReportList, error) {
}

func (c *cpolrStore) createCpolr(report *v1alpha2.ClusterPolicyReport) (*v1alpha2.ClusterPolicyReport, error) {
report.ResourceVersion = fmt.Sprint(1)
report.ResourceVersion = c.store.UseResourceVersion()
report.UID = uuid.NewUUID()
report.CreationTimestamp = metav1.Now()

return report, c.store.ClusterPolicyReports().Create(context.TODO(), *report)
}

func (c *cpolrStore) updateCpolr(report *v1alpha2.ClusterPolicyReport, oldReport *v1alpha2.ClusterPolicyReport) (*v1alpha2.ClusterPolicyReport, error) {
oldRV, err := strconv.ParseInt(oldReport.ResourceVersion, 10, 64)
if err != nil {
return nil, errorpkg.Wrapf(err, "could not parse resource version")
}
report.ResourceVersion = fmt.Sprint(oldRV + 1)

func (c *cpolrStore) updateCpolr(report *v1alpha2.ClusterPolicyReport, _ *v1alpha2.ClusterPolicyReport) (*v1alpha2.ClusterPolicyReport, error) {
report.ResourceVersion = c.store.UseResourceVersion()
return report, c.store.ClusterPolicyReports().Update(context.TODO(), *report)
}

Expand Down
12 changes: 3 additions & 9 deletions pkg/api/ephr.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"slices"
"strconv"

reportsv1 "github.com/kyverno/kyverno/api/reports/v1"
"github.com/kyverno/reports-server/pkg/storage"
Expand Down Expand Up @@ -336,20 +335,15 @@ func (p *ephrStore) listEphr(namespace string) (*reportsv1.EphemeralReportList,
}

func (p *ephrStore) createEphr(report *reportsv1.EphemeralReport) (*reportsv1.EphemeralReport, error) {
report.ResourceVersion = fmt.Sprint(1)
report.ResourceVersion = p.store.UseResourceVersion()
report.UID = uuid.NewUUID()
report.CreationTimestamp = metav1.Now()

return report, p.store.EphemeralReports().Create(context.TODO(), *report)
}

func (p *ephrStore) updateEphr(report *reportsv1.EphemeralReport, oldReport *reportsv1.EphemeralReport) (*reportsv1.EphemeralReport, error) {
oldRV, err := strconv.ParseInt(oldReport.ResourceVersion, 10, 64)
if err != nil {
return nil, errorpkg.Wrapf(err, "could not parse resource version")
}
report.ResourceVersion = fmt.Sprint(oldRV + 1)

func (p *ephrStore) updateEphr(report *reportsv1.EphemeralReport, _ *reportsv1.EphemeralReport) (*reportsv1.EphemeralReport, error) {
report.ResourceVersion = p.store.UseResourceVersion()
return report, p.store.EphemeralReports().Update(context.TODO(), *report)
}

Expand Down
13 changes: 3 additions & 10 deletions pkg/api/polr.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"slices"
"strconv"

"github.com/kyverno/reports-server/pkg/storage"
"github.com/kyverno/reports-server/pkg/utils"
Expand Down Expand Up @@ -146,7 +145,6 @@ func (p *polrStore) Create(ctx context.Context, obj runtime.Object, createValida
if err != nil {
return nil, errors.NewBadRequest(fmt.Sprintf("cannot create policy report: %s", err.Error()))
}
klog.Info(r.ResourceVersion)
if err := p.broadcaster.Action(watch.Added, r); err != nil {
klog.ErrorS(err, "failed to broadcast event")
}
Expand Down Expand Up @@ -337,20 +335,15 @@ func (p *polrStore) listPolr(namespace string) (*v1alpha2.PolicyReportList, erro
}

func (p *polrStore) createPolr(report *v1alpha2.PolicyReport) (*v1alpha2.PolicyReport, error) {
report.ResourceVersion = fmt.Sprint(1)
report.ResourceVersion = p.store.UseResourceVersion()
report.UID = uuid.NewUUID()
report.CreationTimestamp = metav1.Now()

return report, p.store.PolicyReports().Create(context.TODO(), *report)
}

func (p *polrStore) updatePolr(report *v1alpha2.PolicyReport, oldReport *v1alpha2.PolicyReport) (*v1alpha2.PolicyReport, error) {
oldRV, err := strconv.ParseInt(oldReport.ResourceVersion, 10, 64)
if err != nil {
return nil, errorpkg.Wrapf(err, "could not parse resource version")
}
report.ResourceVersion = fmt.Sprint(oldRV + 1)

func (p *polrStore) updatePolr(report *v1alpha2.PolicyReport, _ *v1alpha2.PolicyReport) (*v1alpha2.PolicyReport, error) {
report.ResourceVersion = p.store.UseResourceVersion()
return report, p.store.PolicyReports().Update(context.TODO(), *report)
}

Expand Down
7 changes: 7 additions & 0 deletions pkg/storage/api/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,10 @@ type ClusterEphemeralReportsInterface interface {
Update(ctx context.Context, cephr reportsv1.ClusterEphemeralReport) error
Delete(ctx context.Context, name string) error
}

type Versioning interface {
// SetResourceVersion sets the resource version to the provided value if its higher
SetResourceVersion(string) error
// UseResourceVersion returns the current resource version and increments the value by one
UseResourceVersion() string
}
49 changes: 47 additions & 2 deletions pkg/storage/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,58 @@ import (
)

type Interface interface {
api.Versioning
api.Storage
}

func New(debug bool, config *db.PostgresConfig) (Interface, error) {
klog.Infof("setting up storage, debug=%v", debug)
if debug {
return inmemory.New(), nil
return &store{
db: inmemory.New(),
versioning: NewVersioning(),
}, nil
}
return db.New(config)

db, err := db.New(config)
if err != nil {
return nil, err
}
return &store{
db: db,
versioning: NewVersioning(),
}, nil
}

type store struct {
db api.Storage
versioning api.Versioning
}

func (s *store) ClusterEphemeralReports() api.ClusterEphemeralReportsInterface {
return s.db.ClusterEphemeralReports()
}

func (s *store) ClusterPolicyReports() api.ClusterPolicyReportsInterface {
return s.db.ClusterPolicyReports()
}

func (s *store) EphemeralReports() api.EphemeralReportsInterface {
return s.db.EphemeralReports()
}

func (s *store) PolicyReports() api.PolicyReportsInterface {
return s.db.PolicyReports()
}

func (s *store) Ready() bool {
return s.db.Ready()
}

func (s *store) SetResourceVersion(val string) error {
return s.versioning.SetResourceVersion(val)
}

func (s *store) UseResourceVersion() string {
return s.versioning.UseResourceVersion()
}
40 changes: 40 additions & 0 deletions pkg/storage/versioning.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package storage

import (
"strconv"
"sync"

"github.com/kyverno/reports-server/pkg/storage/api"
)

type resourceVersion struct {
sync.Mutex
version uint64
}

func NewVersioning() api.Versioning {
return &resourceVersion{
version: 1,
}
}

func (r *resourceVersion) SetResourceVersion(val string) error {
r.Lock()
defer r.Unlock()
number, err := strconv.ParseUint(val, 10, 64)
if err != nil {
return err
}
if number > r.version {
r.version = number
}
return nil
}

func (r *resourceVersion) UseResourceVersion() string {
r.Lock()
defer r.Unlock()
number := strconv.FormatUint(r.version, 10)
r.version += 1
return number
}

0 comments on commit b045574

Please sign in to comment.