Skip to content

Commit

Permalink
fix: simplify watch
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 Jul 7, 2024
1 parent 3dd1083 commit 339ecd3
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 92 deletions.
47 changes: 24 additions & 23 deletions pkg/api/cephr.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,29 +274,30 @@ func (c *cephrStore) DeleteCollection(ctx context.Context, deleteValidation rest

func (c *cephrStore) Watch(ctx context.Context, options *metainternalversion.ListOptions) (watch.Interface, error) {
klog.Infof("watching cluster ephemeral reports rv=%s", options.ResourceVersion)
switch options.ResourceVersion {
case "", "0":
return c.broadcaster.Watch()
default:
break
}
items, err := c.List(ctx, options)
if err != nil {
return nil, err
}
list, ok := items.(*reportsv1.ClusterEphemeralReportList)
if !ok {
return nil, fmt.Errorf("failed to convert runtime object into cluster ephemeral report list")
}
events := make([]watch.Event, len(list.Items))
for i, pol := range list.Items {
report := pol.DeepCopy()
events[i] = watch.Event{
Type: watch.Added,
Object: report,
}
}
return c.broadcaster.WatchWithPrefix(events)
return c.broadcaster.Watch()
// switch options.ResourceVersion {
// case "", "0":
// return c.broadcaster.Watch()
// default:
// break
// }
// items, err := c.List(ctx, options)
// if err != nil {
// return nil, err
// }
// list, ok := items.(*reportsv1.ClusterEphemeralReportList)
// if !ok {
// return nil, fmt.Errorf("failed to convert runtime object into cluster ephemeral report list")
// }
// events := make([]watch.Event, len(list.Items))
// for i, pol := range list.Items {
// report := pol.DeepCopy()
// events[i] = watch.Event{
// Type: watch.Added,
// Object: report,
// }
// }
// return c.broadcaster.WatchWithPrefix(events)
}

func (c *cephrStore) ConvertToTable(ctx context.Context, object runtime.Object, tableOptions runtime.Object) (*metav1beta1.Table, error) {
Expand Down
47 changes: 24 additions & 23 deletions pkg/api/cpolr.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,29 +273,30 @@ func (c *cpolrStore) DeleteCollection(ctx context.Context, deleteValidation rest

func (c *cpolrStore) Watch(ctx context.Context, options *metainternalversion.ListOptions) (watch.Interface, error) {
klog.Infof("watching cluster policy reports rv=%s", options.ResourceVersion)
switch options.ResourceVersion {
case "", "0":
return c.broadcaster.Watch()
default:
break
}
items, err := c.List(ctx, options)
if err != nil {
return nil, err
}
list, ok := items.(*v1alpha2.ClusterPolicyReportList)
if !ok {
return nil, fmt.Errorf("failed to convert runtime object into cluster policy report list")
}
events := make([]watch.Event, len(list.Items))
for i, pol := range list.Items {
report := pol.DeepCopy()
events[i] = watch.Event{
Type: watch.Added,
Object: report,
}
}
return c.broadcaster.WatchWithPrefix(events)
return c.broadcaster.Watch()
// switch options.ResourceVersion {
// case "", "0":
// return c.broadcaster.Watch()
// default:
// break
// }
// items, err := c.List(ctx, options)
// if err != nil {
// return nil, err
// }
// list, ok := items.(*v1alpha2.ClusterPolicyReportList)
// if !ok {
// return nil, fmt.Errorf("failed to convert runtime object into cluster policy report list")
// }
// events := make([]watch.Event, len(list.Items))
// for i, pol := range list.Items {
// report := pol.DeepCopy()
// events[i] = watch.Event{
// Type: watch.Added,
// Object: report,
// }
// }
// return c.broadcaster.WatchWithPrefix(events)
}

func (c *cpolrStore) ConvertToTable(ctx context.Context, object runtime.Object, tableOptions runtime.Object) (*metav1beta1.Table, error) {
Expand Down
47 changes: 24 additions & 23 deletions pkg/api/ephr.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,29 +294,30 @@ func (p *ephrStore) DeleteCollection(ctx context.Context, deleteValidation rest.

func (p *ephrStore) Watch(ctx context.Context, options *metainternalversion.ListOptions) (watch.Interface, error) {
klog.Infof("watching ephemeral reports rv=%s", options.ResourceVersion)
switch options.ResourceVersion {
case "", "0":
return p.broadcaster.Watch()
default:
break
}
items, err := p.List(ctx, options)
if err != nil {
return nil, err
}
list, ok := items.(*reportsv1.EphemeralReportList)
if !ok {
return nil, fmt.Errorf("failed to convert runtime object into ephemeral report list")
}
events := make([]watch.Event, len(list.Items))
for i, pol := range list.Items {
report := pol.DeepCopy()
events[i] = watch.Event{
Type: watch.Added,
Object: report,
}
}
return p.broadcaster.WatchWithPrefix(events)
return p.broadcaster.Watch()
// switch options.ResourceVersion {
// case "", "0":
// return p.broadcaster.Watch()
// default:
// break
// }
// items, err := p.List(ctx, options)
// if err != nil {
// return nil, err
// }
// list, ok := items.(*reportsv1.EphemeralReportList)
// if !ok {
// return nil, fmt.Errorf("failed to convert runtime object into ephemeral report list")
// }
// events := make([]watch.Event, len(list.Items))
// for i, pol := range list.Items {
// report := pol.DeepCopy()
// events[i] = watch.Event{
// Type: watch.Added,
// Object: report,
// }
// }
// return p.broadcaster.WatchWithPrefix(events)
}

func (p *ephrStore) ConvertToTable(ctx context.Context, object runtime.Object, tableOptions runtime.Object) (*metav1beta1.Table, error) {
Expand Down
47 changes: 24 additions & 23 deletions pkg/api/polr.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,29 +294,30 @@ func (p *polrStore) DeleteCollection(ctx context.Context, deleteValidation rest.

func (p *polrStore) Watch(ctx context.Context, options *metainternalversion.ListOptions) (watch.Interface, error) {
klog.Infof("watching policy reports rv=%s", options.ResourceVersion)
switch options.ResourceVersion {
case "", "0":
return p.broadcaster.Watch()
default:
break
}
items, err := p.List(ctx, options)
if err != nil {
return nil, err
}
list, ok := items.(*v1alpha2.PolicyReportList)
if !ok {
return nil, fmt.Errorf("failed to convert runtime object into policy report list")
}
events := make([]watch.Event, len(list.Items))
for i, pol := range list.Items {
report := pol.DeepCopy()
events[i] = watch.Event{
Type: watch.Added,
Object: report,
}
}
return p.broadcaster.WatchWithPrefix(events)
return p.broadcaster.Watch()
// switch options.ResourceVersion {
// case "", "0":
// return p.broadcaster.Watch()
// default:
// break
// }
// items, err := p.List(ctx, options)
// if err != nil {
// return nil, err
// }
// list, ok := items.(*v1alpha2.PolicyReportList)
// if !ok {
// return nil, fmt.Errorf("failed to convert runtime object into policy report list")
// }
// events := make([]watch.Event, len(list.Items))
// for i, pol := range list.Items {
// report := pol.DeepCopy()
// events[i] = watch.Event{
// Type: watch.Added,
// Object: report,
// }
// }
// return p.broadcaster.WatchWithPrefix(events)
}

func (p *polrStore) ConvertToTable(ctx context.Context, object runtime.Object, tableOptions runtime.Object) (*metav1beta1.Table, error) {
Expand Down

0 comments on commit 339ecd3

Please sign in to comment.