Skip to content

Commit

Permalink
fix: annotate migrated reports
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 Sep 16, 2024
1 parent 0605bcf commit 0b61224
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pkg/api/cephr.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (c *cephrStore) Create(ctx context.Context, obj runtime.Object, createValid
}
if cephr.Name == "" {
if cephr.GenerateName == "" {
return nil, errors.NewConflict(utils.ClusterEphemeralReportsGR, cephr.Name, fmt.Errorf("name and generate name not provided"))
return nil, errors.NewAlreadyExists(utils.ClusterEphemeralReportsGR, cephr.Name)
}
cephr.Name = nameGenerator.GenerateName(cephr.GenerateName)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/cpolr.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (c *cpolrStore) Create(ctx context.Context, obj runtime.Object, createValid
}
if cpolr.Name == "" {
if cpolr.GenerateName == "" {
return nil, errors.NewConflict(utils.ClusterPolicyReportsGR, cpolr.Name, fmt.Errorf("name and generate name not provided"))
return nil, errors.NewAlreadyExists(utils.ClusterPolicyReportsGR, cpolr.Name)
}
cpolr.Name = nameGenerator.GenerateName(cpolr.GenerateName)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/ephr.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func (p *ephrStore) Create(ctx context.Context, obj runtime.Object, createValida
if !isDryRun {
r, err := p.createEphr(ephr)
if err != nil {
return nil, errors.NewBadRequest(fmt.Sprintf("cannot create ephemeral report: %s", err.Error()))
return nil, errors.NewAlreadyExists(utils.EphemeralReportsGR, ephr.Name)
}
if err := p.broadcaster.Action(watch.Added, r); err != nil {
klog.ErrorS(err, "failed to broadcast event")
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/polr.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func (p *polrStore) Create(ctx context.Context, obj runtime.Object, createValida
if !isDryRun {
r, err := p.createPolr(polr)
if err != nil {
return nil, errors.NewBadRequest(fmt.Sprintf("cannot create policy report: %s", err.Error()))
return nil, errors.NewAlreadyExists(utils.PolicyReportsGR, polr.Name)
}
if err := p.broadcaster.Action(watch.Added, r); err != nil {
klog.ErrorS(err, "failed to broadcast event")
Expand Down
68 changes: 68 additions & 0 deletions pkg/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ func (c Config) migration(store storage.Interface) error {
return nil
}
for _, c := range cpolrs.Items {
if c.Annotations != nil {
if _, ok := c.Annotations[api.ServedByReportsServerAnnotation]; ok {
continue
}
} else {
c.Annotations = make(map[string]string)
}
c.Annotations[api.ServedByReportsServerAnnotation] = api.ServedByReportsServerValue
err := store.ClusterPolicyReports().Create(context.TODO(), c)
if err != nil {
return err
Expand Down Expand Up @@ -138,7 +146,10 @@ func (c Config) migration(store storage.Interface) error {
if _, ok := cpolr.Annotations[api.ServedByReportsServerAnnotation]; ok {
return
}
} else {
cpolr.Annotations = make(map[string]string)
}
cpolr.Annotations[api.ServedByReportsServerAnnotation] = api.ServedByReportsServerValue
err := store.ClusterPolicyReports().Create(context.TODO(), *cpolr)
if err != nil {
klog.Error(err)
Expand All @@ -149,7 +160,10 @@ func (c Config) migration(store storage.Interface) error {
if _, ok := cpolr.Annotations[api.ServedByReportsServerAnnotation]; ok {
return
}
} else {
cpolr.Annotations = make(map[string]string)
}
cpolr.Annotations[api.ServedByReportsServerAnnotation] = api.ServedByReportsServerValue
err := store.ClusterPolicyReports().Update(context.TODO(), *cpolr)
if err != nil {
klog.Error(err)
Expand All @@ -160,7 +174,10 @@ func (c Config) migration(store storage.Interface) error {
if _, ok := cpolr.Annotations[api.ServedByReportsServerAnnotation]; ok {
return
}
} else {
cpolr.Annotations = make(map[string]string)
}
cpolr.Annotations[api.ServedByReportsServerAnnotation] = api.ServedByReportsServerValue
err := store.ClusterPolicyReports().Delete(context.TODO(), cpolr.Name)
if err != nil {
klog.Error(err)
Expand All @@ -174,6 +191,14 @@ func (c Config) migration(store storage.Interface) error {
return nil
}
for _, c := range polrs.Items {
if c.Annotations != nil {
if _, ok := c.Annotations[api.ServedByReportsServerAnnotation]; ok {
continue
}
} else {
c.Annotations = make(map[string]string)
}
c.Annotations[api.ServedByReportsServerAnnotation] = api.ServedByReportsServerValue
err := store.PolicyReports().Create(context.TODO(), c)
if err != nil {
return err
Expand Down Expand Up @@ -202,7 +227,10 @@ func (c Config) migration(store storage.Interface) error {
if _, ok := polr.Annotations[api.ServedByReportsServerAnnotation]; ok {
return
}
} else {
polr.Annotations = make(map[string]string)
}
polr.Annotations[api.ServedByReportsServerAnnotation] = api.ServedByReportsServerValue
err := store.PolicyReports().Create(context.TODO(), *polr)
if err != nil {
klog.Error(err)
Expand All @@ -213,7 +241,10 @@ func (c Config) migration(store storage.Interface) error {
if _, ok := polr.Annotations[api.ServedByReportsServerAnnotation]; ok {
return
}
} else {
polr.Annotations = make(map[string]string)
}
polr.Annotations[api.ServedByReportsServerAnnotation] = api.ServedByReportsServerValue
err := store.PolicyReports().Update(context.TODO(), *polr)
if err != nil {
klog.Error(err)
Expand All @@ -224,7 +255,10 @@ func (c Config) migration(store storage.Interface) error {
if _, ok := polr.Annotations[api.ServedByReportsServerAnnotation]; ok {
return
}
} else {
polr.Annotations = make(map[string]string)
}
polr.Annotations[api.ServedByReportsServerAnnotation] = api.ServedByReportsServerValue
err := store.PolicyReports().Delete(context.TODO(), polr.Name, polr.Namespace)
if err != nil {
klog.Error(err)
Expand All @@ -238,6 +272,14 @@ func (c Config) migration(store storage.Interface) error {
return nil
}
for _, c := range cephrs.Items {
if c.Annotations != nil {
if _, ok := c.Annotations[api.ServedByReportsServerAnnotation]; ok {
continue
}
} else {
c.Annotations = make(map[string]string)
}
c.Annotations[api.ServedByReportsServerAnnotation] = api.ServedByReportsServerValue
err := store.ClusterEphemeralReports().Create(context.TODO(), c)
if err != nil {
return err
Expand Down Expand Up @@ -265,7 +307,10 @@ func (c Config) migration(store storage.Interface) error {
if _, ok := cephr.Annotations[api.ServedByReportsServerAnnotation]; ok {
return
}
} else {
cephr.Annotations = make(map[string]string)
}
cephr.Annotations[api.ServedByReportsServerAnnotation] = api.ServedByReportsServerValue
err := store.ClusterEphemeralReports().Create(context.TODO(), *cephr)
if err != nil {
klog.Error(err)
Expand All @@ -276,7 +321,10 @@ func (c Config) migration(store storage.Interface) error {
if _, ok := cephr.Annotations[api.ServedByReportsServerAnnotation]; ok {
return
}
} else {
cephr.Annotations = make(map[string]string)
}
cephr.Annotations[api.ServedByReportsServerAnnotation] = api.ServedByReportsServerValue
err := store.ClusterEphemeralReports().Update(context.TODO(), *cephr)
if err != nil {
klog.Error(err)
Expand All @@ -287,7 +335,10 @@ func (c Config) migration(store storage.Interface) error {
if _, ok := cephr.Annotations[api.ServedByReportsServerAnnotation]; ok {
return
}
} else {
cephr.Annotations = make(map[string]string)
}
cephr.Annotations[api.ServedByReportsServerAnnotation] = api.ServedByReportsServerValue
err := store.ClusterEphemeralReports().Delete(context.TODO(), cephr.Name)
if err != nil {
klog.Error(err)
Expand All @@ -300,6 +351,14 @@ func (c Config) migration(store storage.Interface) error {
return nil
}
for _, c := range ephrs.Items {
if c.Annotations != nil {
if _, ok := c.Annotations[api.ServedByReportsServerAnnotation]; ok {
continue
}
} else {
c.Annotations = make(map[string]string)
}
c.Annotations[api.ServedByReportsServerAnnotation] = api.ServedByReportsServerValue
err := store.EphemeralReports().Create(context.TODO(), c)
if err != nil {
return err
Expand Down Expand Up @@ -327,7 +386,10 @@ func (c Config) migration(store storage.Interface) error {
if _, ok := ephr.Annotations[api.ServedByReportsServerAnnotation]; ok {
return
}
} else {
ephr.Annotations = make(map[string]string)
}
ephr.Annotations[api.ServedByReportsServerAnnotation] = api.ServedByReportsServerValue
err := store.EphemeralReports().Create(context.TODO(), *ephr)
if err != nil {
klog.Error(err)
Expand All @@ -338,7 +400,10 @@ func (c Config) migration(store storage.Interface) error {
if _, ok := ephr.Annotations[api.ServedByReportsServerAnnotation]; ok {
return
}
} else {
ephr.Annotations = make(map[string]string)
}
ephr.Annotations[api.ServedByReportsServerAnnotation] = api.ServedByReportsServerValue
err := store.EphemeralReports().Update(context.TODO(), *ephr)
if err != nil {
klog.Error(err)
Expand All @@ -349,7 +414,10 @@ func (c Config) migration(store storage.Interface) error {
if _, ok := ephr.Annotations[api.ServedByReportsServerAnnotation]; ok {
return
}
} else {
ephr.Annotations = make(map[string]string)
}
ephr.Annotations[api.ServedByReportsServerAnnotation] = api.ServedByReportsServerValue
err := store.EphemeralReports().Delete(context.TODO(), ephr.Name, ephr.Namespace)
if err != nil {
klog.Error(err)
Expand Down

0 comments on commit 0b61224

Please sign in to comment.