Skip to content

Commit

Permalink
fix: rv bug
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>
  • Loading branch information
vishal-chdhry committed Jun 27, 2024
1 parent 5a1049e commit 331eee5
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 44 deletions.
30 changes: 19 additions & 11 deletions pkg/api/cephr.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ func (c *cephrStore) List(ctx context.Context, options *metainternalversion.List

cephrList := &reportsv1.ClusterEphemeralReportList{
Items: make([]reportsv1.ClusterEphemeralReport, 0),
ListMeta: metav1.ListMeta{
// TODO: Fix this!!
ResourceVersion: "1",
},
}
for _, cephr := range list.Items {
if cephr.Labels == nil {
Expand Down Expand Up @@ -119,10 +123,12 @@ func (c *cephrStore) Create(ctx context.Context, obj runtime.Object, createValid

klog.Infof("creating cluster ephemeral reports name=%s", cephr.Name)
if !isDryRun {
if err := c.createCephr(cephr); err != nil {
r, err := c.createCephr(cephr)
if err != nil {
return nil, errors.NewBadRequest(fmt.Sprintf("cannot create cluster ephemeral report: %s", err.Error()))
}
if err := c.broadcaster.Action(watch.Added, obj); err != nil {
klog.Info(r.ResourceVersion)
if err := c.broadcaster.Action(watch.Added, r); err != nil {
klog.ErrorS(err, "failed to broadcast event")
}
}
Expand All @@ -144,10 +150,11 @@ func (c *cephrStore) Update(ctx context.Context, name string, objInfo rest.Updat
}
cephr := updatedObject.(*reportsv1.ClusterEphemeralReport)
if forceAllowCreate {
if err := c.updateCephr(cephr, oldObj); err != nil {
r, err := c.updateCephr(cephr, oldObj)
if err != nil {
klog.ErrorS(err, "failed to update resource")
}
if err := c.broadcaster.Action(watch.Added, updatedObject); err != nil {
if err := c.broadcaster.Action(watch.Added, r); err != nil {
klog.ErrorS(err, "failed to broadcast event")
}
return updatedObject, true, nil
Expand All @@ -174,10 +181,11 @@ func (c *cephrStore) Update(ctx context.Context, name string, objInfo rest.Updat

klog.Infof("updating cluster ephemeral reports name=%s", cephr.Name)
if !isDryRun {
if err := c.updateCephr(cephr, oldObj); err != nil {
r, err := c.updateCephr(cephr, oldObj)
if err != nil {
return nil, false, errors.NewBadRequest(fmt.Sprintf("cannot create cluster ephemeral report: %s", err.Error()))
}
if err := c.broadcaster.Action(watch.Modified, updatedObject); err != nil {
if err := c.broadcaster.Action(watch.Modified, r); err != nil {
klog.ErrorS(err, "failed to broadcast event")
}
}
Expand Down Expand Up @@ -302,22 +310,22 @@ func (c *cephrStore) listCephr() (*reportsv1.ClusterEphemeralReportList, error)
return reportList, nil
}

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

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

func (c *cephrStore) updateCephr(report *reportsv1.ClusterEphemeralReport, oldReport *reportsv1.ClusterEphemeralReport) error {
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 errorpkg.Wrapf(err, "could not parse resource version")
return nil, errorpkg.Wrapf(err, "could not parse resource version")
}
report.ResourceVersion = fmt.Sprint(oldRV + 1)

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

func (c *cephrStore) deleteCephr(report *reportsv1.ClusterEphemeralReport) error {
Expand Down
29 changes: 18 additions & 11 deletions pkg/api/cpolr.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ func (c *cpolrStore) List(ctx context.Context, options *metainternalversion.List

cpolrList := &v1alpha2.ClusterPolicyReportList{
Items: make([]v1alpha2.ClusterPolicyReport, 0),
ListMeta: metav1.ListMeta{
// TODO: Fix this!!
ResourceVersion: "1",
},
}
for _, cpolr := range list.Items {
if cpolr.Labels == nil {
Expand Down Expand Up @@ -119,10 +123,11 @@ func (c *cpolrStore) Create(ctx context.Context, obj runtime.Object, createValid

klog.Infof("creating cluster policy report name=%s", cpolr.Name)
if !isDryRun {
if err := c.createCpolr(cpolr); err != nil {
r, err := c.createCpolr(cpolr)
if err != nil {
return nil, errors.NewBadRequest(fmt.Sprintf("cannot create cluster policy report: %s", err.Error()))
}
if err := c.broadcaster.Action(watch.Added, obj); err != nil {
if err := c.broadcaster.Action(watch.Added, r); err != nil {
klog.ErrorS(err, "failed to broadcast event")
}
}
Expand All @@ -144,10 +149,11 @@ func (c *cpolrStore) Update(ctx context.Context, name string, objInfo rest.Updat
}
cpolr := updatedObject.(*v1alpha2.ClusterPolicyReport)
if forceAllowCreate {
if err := c.updateCpolr(cpolr, oldObj); err != nil {
r, err := c.updateCpolr(cpolr, oldObj)
if err != nil {
klog.ErrorS(err, "failed to update resource")
}
if err := c.broadcaster.Action(watch.Added, updatedObject); err != nil {
if err := c.broadcaster.Action(watch.Added, r); err != nil {
klog.ErrorS(err, "failed to broadcast event")
}
return updatedObject, true, nil
Expand All @@ -174,10 +180,11 @@ func (c *cpolrStore) Update(ctx context.Context, name string, objInfo rest.Updat

klog.Infof("updating cluster policy report name=%s", cpolr.Name)
if !isDryRun {
if err := c.updateCpolr(cpolr, oldObj); err != nil {
r, err := c.updateCpolr(cpolr, oldObj)
if err != nil {
return nil, false, errors.NewBadRequest(fmt.Sprintf("cannot create cluster policy report: %s", err.Error()))
}
if err := c.broadcaster.Action(watch.Modified, updatedObject); err != nil {
if err := c.broadcaster.Action(watch.Modified, r); err != nil {
klog.ErrorS(err, "failed to broadcast event")
}
}
Expand Down Expand Up @@ -302,22 +309,22 @@ func (c *cpolrStore) listCpolr() (*v1alpha2.ClusterPolicyReportList, error) {
return reportList, nil
}

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

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

func (c *cpolrStore) updateCpolr(report *v1alpha2.ClusterPolicyReport, oldReport *v1alpha2.ClusterPolicyReport) error {
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 errorpkg.Wrapf(err, "could not parse resource version")
return nil, errorpkg.Wrapf(err, "could not parse resource version")
}
report.ResourceVersion = fmt.Sprint(oldRV + 1)

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

func (c *cpolrStore) deleteCpolr(report *v1alpha2.ClusterPolicyReport) error {
Expand Down
27 changes: 16 additions & 11 deletions pkg/api/ephr.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ func (p *ephrStore) List(ctx context.Context, options *metainternalversion.ListO

ephrList := &reportsv1.EphemeralReportList{
Items: make([]reportsv1.EphemeralReport, 0),
ListMeta: metav1.ListMeta{
// TODO: Fix this!!
ResourceVersion: "1",
},
}
for _, ephr := range list.Items {
if ephr.Labels == nil {
Expand Down Expand Up @@ -130,11 +134,11 @@ func (p *ephrStore) Create(ctx context.Context, obj runtime.Object, createValida

klog.Infof("creating ephemeral reports name=%s namespace=%s", ephr.Name, ephr.Namespace)
if !isDryRun {
err := p.createEphr(ephr)
r, err := p.createEphr(ephr)
if err != nil {
return nil, errors.NewBadRequest(fmt.Sprintf("cannot create ephemeral report: %s", err.Error()))
}
if err := p.broadcaster.Action(watch.Added, obj); err != nil {
if err := p.broadcaster.Action(watch.Added, r); err != nil {
klog.ErrorS(err, "failed to broadcast event")
}
}
Expand All @@ -158,10 +162,11 @@ func (p *ephrStore) Update(ctx context.Context, name string, objInfo rest.Update
ephr := updatedObject.(*reportsv1.EphemeralReport)

if forceAllowCreate {
if err := p.updateEphr(ephr, oldObj); err != nil {
r, err := p.updateEphr(ephr, oldObj)
if err != nil {
klog.ErrorS(err, "failed to update resource")
}
if err := p.broadcaster.Action(watch.Added, updatedObject); err != nil {
if err := p.broadcaster.Action(watch.Added, r); err != nil {
klog.ErrorS(err, "failed to broadcast event")
}
return updatedObject, true, nil
Expand Down Expand Up @@ -192,11 +197,11 @@ func (p *ephrStore) Update(ctx context.Context, name string, objInfo rest.Update

klog.Infof("updating ephemeral reports name=%s namespace=%s", ephr.Name, ephr.Namespace)
if !isDryRun {
err := p.updateEphr(ephr, oldObj)
r, err := p.updateEphr(ephr, oldObj)
if err != nil {
return nil, false, errors.NewBadRequest(fmt.Sprintf("cannot create ephemeral report: %s", err.Error()))
}
if err := p.broadcaster.Action(watch.Modified, updatedObject); err != nil {
if err := p.broadcaster.Action(watch.Modified, r); err != nil {
klog.ErrorS(err, "failed to broadcast event")
}
}
Expand Down Expand Up @@ -324,22 +329,22 @@ func (p *ephrStore) listEphr(namespace string) (*reportsv1.EphemeralReportList,
return reportList, nil
}

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

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

func (p *ephrStore) updateEphr(report *reportsv1.EphemeralReport, oldReport *reportsv1.EphemeralReport) error {
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 errorpkg.Wrapf(err, "could not parse resource version")
return nil, errorpkg.Wrapf(err, "could not parse resource version")
}
report.ResourceVersion = fmt.Sprint(oldRV + 1)

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

func (p *ephrStore) deleteEphr(report *reportsv1.EphemeralReport) error {
Expand Down
28 changes: 17 additions & 11 deletions pkg/api/polr.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ func (p *polrStore) List(ctx context.Context, options *metainternalversion.ListO

polrList := &v1alpha2.PolicyReportList{
Items: make([]v1alpha2.PolicyReport, 0),
ListMeta: metav1.ListMeta{
// TODO: Fix this!!
ResourceVersion: "1",
},
}
for _, polr := range list.Items {
if polr.Labels == nil {
Expand Down Expand Up @@ -130,11 +134,12 @@ func (p *polrStore) Create(ctx context.Context, obj runtime.Object, createValida

klog.Infof("creating policy reports name=%s namespace=%s", polr.Name, polr.Namespace)
if !isDryRun {
err := p.createPolr(polr)
r, err := p.createPolr(polr)
if err != nil {
return nil, errors.NewBadRequest(fmt.Sprintf("cannot create policy report: %s", err.Error()))
}
if err := p.broadcaster.Action(watch.Added, obj); err != nil {
klog.Info(r.ResourceVersion)
if err := p.broadcaster.Action(watch.Added, r); err != nil {
klog.ErrorS(err, "failed to broadcast event")
}
}
Expand All @@ -158,10 +163,11 @@ func (p *polrStore) Update(ctx context.Context, name string, objInfo rest.Update
polr := updatedObject.(*v1alpha2.PolicyReport)

if forceAllowCreate {
if err := p.updatePolr(polr, oldObj); err != nil {
r, err := p.updatePolr(polr, oldObj)
if err != nil {
klog.ErrorS(err, "failed to update resource")
}
if err := p.broadcaster.Action(watch.Added, updatedObject); err != nil {
if err := p.broadcaster.Action(watch.Added, r); err != nil {
klog.ErrorS(err, "failed to broadcast event")
}
return updatedObject, true, nil
Expand Down Expand Up @@ -192,11 +198,11 @@ func (p *polrStore) Update(ctx context.Context, name string, objInfo rest.Update

klog.Infof("updating policy reports name=%s namespace=%s", polr.Name, polr.Namespace)
if !isDryRun {
err := p.updatePolr(polr, oldObj)
r, err := p.updatePolr(polr, oldObj)
if err != nil {
return nil, false, errors.NewBadRequest(fmt.Sprintf("cannot create policy report: %s", err.Error()))
}
if err := p.broadcaster.Action(watch.Modified, updatedObject); err != nil {
if err := p.broadcaster.Action(watch.Modified, r); err != nil {
klog.ErrorS(err, "failed to broadcast event")
}
}
Expand Down Expand Up @@ -324,22 +330,22 @@ func (p *polrStore) listPolr(namespace string) (*v1alpha2.PolicyReportList, erro
return reportList, nil
}

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

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

func (p *polrStore) updatePolr(report *v1alpha2.PolicyReport, oldReport *v1alpha2.PolicyReport) error {
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 errorpkg.Wrapf(err, "could not parse resource version")
return nil, errorpkg.Wrapf(err, "could not parse resource version")
}
report.ResourceVersion = fmt.Sprint(oldRV + 1)

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

func (p *polrStore) deletePolr(report *v1alpha2.PolicyReport) error {
Expand Down

0 comments on commit 331eee5

Please sign in to comment.