diff --git a/pkg/api/cephr.go b/pkg/api/cephr.go index dbe8a99..c23d2a0 100644 --- a/pkg/api/cephr.go +++ b/pkg/api/cephr.go @@ -336,7 +336,7 @@ func (c *cephrStore) getCephr(name string) (*reportsv1.ClusterEphemeralReport, e return nil, errorpkg.Wrapf(err, "could not find cluster ephemeral report in store") } - return val.DeepCopy(), nil + return val, nil } func (c *cephrStore) listCephr() (*reportsv1.ClusterEphemeralReportList, error) { @@ -346,7 +346,11 @@ func (c *cephrStore) listCephr() (*reportsv1.ClusterEphemeralReportList, error) } reportList := &reportsv1.ClusterEphemeralReportList{ - Items: valList, + Items: make([]reportsv1.ClusterEphemeralReport, 0, len(valList)), + } + + for _, v := range valList { + reportList.Items = append(reportList.Items, *v.DeepCopy()) } klog.Infof("value found of length:%d", len(reportList.Items)) @@ -358,12 +362,12 @@ func (c *cephrStore) createCephr(report *reportsv1.ClusterEphemeralReport) (*rep report.UID = uuid.NewUUID() report.CreationTimestamp = metav1.Now() - return report, c.store.ClusterEphemeralReports().Create(context.TODO(), *report) + return report, c.store.ClusterEphemeralReports().Create(context.TODO(), report) } 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) + return report, c.store.ClusterEphemeralReports().Update(context.TODO(), report) } func (c *cephrStore) deleteCephr(report *reportsv1.ClusterEphemeralReport) error { diff --git a/pkg/api/cpolr.go b/pkg/api/cpolr.go index ff0942c..25a0bd2 100644 --- a/pkg/api/cpolr.go +++ b/pkg/api/cpolr.go @@ -335,7 +335,7 @@ func (c *cpolrStore) getCpolr(name string) (*v1alpha2.ClusterPolicyReport, error return nil, errorpkg.Wrapf(err, "could not find cluster policy report in store") } - return val.DeepCopy(), nil + return val, nil } func (c *cpolrStore) listCpolr() (*v1alpha2.ClusterPolicyReportList, error) { @@ -345,7 +345,11 @@ func (c *cpolrStore) listCpolr() (*v1alpha2.ClusterPolicyReportList, error) { } reportList := &v1alpha2.ClusterPolicyReportList{ - Items: valList, + Items: make([]v1alpha2.ClusterPolicyReport, 0, len(valList)), + } + + for _, v := range valList { + reportList.Items = append(reportList.Items, *v.DeepCopy()) } klog.Infof("value found of length:%d", len(reportList.Items)) @@ -357,12 +361,12 @@ func (c *cpolrStore) createCpolr(report *v1alpha2.ClusterPolicyReport) (*v1alpha report.UID = uuid.NewUUID() report.CreationTimestamp = metav1.Now() - return report, c.store.ClusterPolicyReports().Create(context.TODO(), *report) + return report, c.store.ClusterPolicyReports().Create(context.TODO(), report) } 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) + return report, c.store.ClusterPolicyReports().Update(context.TODO(), report) } func (c *cpolrStore) deleteCpolr(report *v1alpha2.ClusterPolicyReport) error { diff --git a/pkg/api/ephr.go b/pkg/api/ephr.go index be9dbac..39e46c8 100644 --- a/pkg/api/ephr.go +++ b/pkg/api/ephr.go @@ -356,7 +356,7 @@ func (p *ephrStore) getEphr(name, namespace string) (*reportsv1.EphemeralReport, return nil, errorpkg.Wrapf(err, "could not find ephemeral report in store") } - return val.DeepCopy(), nil + return val, nil } func (p *ephrStore) listEphr(namespace string) (*reportsv1.EphemeralReportList, error) { @@ -366,7 +366,11 @@ func (p *ephrStore) listEphr(namespace string) (*reportsv1.EphemeralReportList, } reportList := &reportsv1.EphemeralReportList{ - Items: valList, + Items: make([]reportsv1.EphemeralReport, 0, len(valList)), + } + + for _, v := range valList { + reportList.Items = append(reportList.Items, *v.DeepCopy()) } klog.Infof("value found of length:%d", len(reportList.Items)) @@ -378,12 +382,12 @@ func (p *ephrStore) createEphr(report *reportsv1.EphemeralReport) (*reportsv1.Ep report.UID = uuid.NewUUID() report.CreationTimestamp = metav1.Now() - return report, p.store.EphemeralReports().Create(context.TODO(), *report) + return report, p.store.EphemeralReports().Create(context.TODO(), report) } 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) + return report, p.store.EphemeralReports().Update(context.TODO(), report) } func (p *ephrStore) deleteEphr(report *reportsv1.EphemeralReport) error { diff --git a/pkg/api/polr.go b/pkg/api/polr.go index 81d5587..cd5f0c5 100644 --- a/pkg/api/polr.go +++ b/pkg/api/polr.go @@ -356,7 +356,7 @@ func (p *polrStore) getPolr(name, namespace string) (*v1alpha2.PolicyReport, err return nil, errorpkg.Wrapf(err, "could not find policy report in store") } - return val.DeepCopy(), nil + return val, nil } func (p *polrStore) listPolr(namespace string) (*v1alpha2.PolicyReportList, error) { @@ -366,7 +366,11 @@ func (p *polrStore) listPolr(namespace string) (*v1alpha2.PolicyReportList, erro } reportList := &v1alpha2.PolicyReportList{ - Items: valList, + Items: make([]v1alpha2.PolicyReport, 0, len(valList)), + } + + for _, v := range valList { + reportList.Items = append(reportList.Items, *v.DeepCopy()) } klog.Infof("value found of length:%d", len(reportList.Items)) @@ -378,12 +382,12 @@ func (p *polrStore) createPolr(report *v1alpha2.PolicyReport) (*v1alpha2.PolicyR report.UID = uuid.NewUUID() report.CreationTimestamp = metav1.Now() - return report, p.store.PolicyReports().Create(context.TODO(), *report) + return report, p.store.PolicyReports().Create(context.TODO(), report) } 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) + return report, p.store.PolicyReports().Update(context.TODO(), report) } func (p *polrStore) deletePolr(report *v1alpha2.PolicyReport) error { diff --git a/pkg/server/config.go b/pkg/server/config.go index f5280e3..e19d1f3 100644 --- a/pkg/server/config.go +++ b/pkg/server/config.go @@ -118,7 +118,7 @@ func (c Config) migration(store storage.Interface) error { c.Annotations = make(map[string]string) } c.Annotations[api.ServedByReportsServerAnnotation] = api.ServedByReportsServerValue - err := store.ClusterPolicyReports().Create(context.TODO(), c) + err := store.ClusterPolicyReports().Create(context.TODO(), &c) if err != nil { return err } @@ -150,7 +150,7 @@ func (c Config) migration(store storage.Interface) error { cpolr.Annotations = make(map[string]string) } cpolr.Annotations[api.ServedByReportsServerAnnotation] = api.ServedByReportsServerValue - err := store.ClusterPolicyReports().Create(context.TODO(), *cpolr) + err := store.ClusterPolicyReports().Create(context.TODO(), cpolr) if err != nil { klog.Error(err) } @@ -164,7 +164,7 @@ func (c Config) migration(store storage.Interface) error { cpolr.Annotations = make(map[string]string) } cpolr.Annotations[api.ServedByReportsServerAnnotation] = api.ServedByReportsServerValue - err := store.ClusterPolicyReports().Update(context.TODO(), *cpolr) + err := store.ClusterPolicyReports().Update(context.TODO(), cpolr) if err != nil { klog.Error(err) } @@ -199,7 +199,7 @@ func (c Config) migration(store storage.Interface) error { c.Annotations = make(map[string]string) } c.Annotations[api.ServedByReportsServerAnnotation] = api.ServedByReportsServerValue - err := store.PolicyReports().Create(context.TODO(), c) + err := store.PolicyReports().Create(context.TODO(), &c) if err != nil { return err } @@ -231,7 +231,7 @@ func (c Config) migration(store storage.Interface) error { polr.Annotations = make(map[string]string) } polr.Annotations[api.ServedByReportsServerAnnotation] = api.ServedByReportsServerValue - err := store.PolicyReports().Create(context.TODO(), *polr) + err := store.PolicyReports().Create(context.TODO(), polr) if err != nil { klog.Error(err) } @@ -245,7 +245,7 @@ func (c Config) migration(store storage.Interface) error { polr.Annotations = make(map[string]string) } polr.Annotations[api.ServedByReportsServerAnnotation] = api.ServedByReportsServerValue - err := store.PolicyReports().Update(context.TODO(), *polr) + err := store.PolicyReports().Update(context.TODO(), polr) if err != nil { klog.Error(err) } @@ -280,7 +280,7 @@ func (c Config) migration(store storage.Interface) error { c.Annotations = make(map[string]string) } c.Annotations[api.ServedByReportsServerAnnotation] = api.ServedByReportsServerValue - err := store.ClusterEphemeralReports().Create(context.TODO(), c) + err := store.ClusterEphemeralReports().Create(context.TODO(), &c) if err != nil { return err } @@ -311,7 +311,7 @@ func (c Config) migration(store storage.Interface) error { cephr.Annotations = make(map[string]string) } cephr.Annotations[api.ServedByReportsServerAnnotation] = api.ServedByReportsServerValue - err := store.ClusterEphemeralReports().Create(context.TODO(), *cephr) + err := store.ClusterEphemeralReports().Create(context.TODO(), cephr) if err != nil { klog.Error(err) } @@ -325,7 +325,7 @@ func (c Config) migration(store storage.Interface) error { cephr.Annotations = make(map[string]string) } cephr.Annotations[api.ServedByReportsServerAnnotation] = api.ServedByReportsServerValue - err := store.ClusterEphemeralReports().Update(context.TODO(), *cephr) + err := store.ClusterEphemeralReports().Update(context.TODO(), cephr) if err != nil { klog.Error(err) } @@ -359,7 +359,7 @@ func (c Config) migration(store storage.Interface) error { c.Annotations = make(map[string]string) } c.Annotations[api.ServedByReportsServerAnnotation] = api.ServedByReportsServerValue - err := store.EphemeralReports().Create(context.TODO(), c) + err := store.EphemeralReports().Create(context.TODO(), &c) if err != nil { return err } @@ -390,7 +390,7 @@ func (c Config) migration(store storage.Interface) error { ephr.Annotations = make(map[string]string) } ephr.Annotations[api.ServedByReportsServerAnnotation] = api.ServedByReportsServerValue - err := store.EphemeralReports().Create(context.TODO(), *ephr) + err := store.EphemeralReports().Create(context.TODO(), ephr) if err != nil { klog.Error(err) } @@ -404,7 +404,7 @@ func (c Config) migration(store storage.Interface) error { ephr.Annotations = make(map[string]string) } ephr.Annotations[api.ServedByReportsServerAnnotation] = api.ServedByReportsServerValue - err := store.EphemeralReports().Update(context.TODO(), *ephr) + err := store.EphemeralReports().Update(context.TODO(), ephr) if err != nil { klog.Error(err) } diff --git a/pkg/storage/api/interface.go b/pkg/storage/api/interface.go index a3dce2f..4176dee 100644 --- a/pkg/storage/api/interface.go +++ b/pkg/storage/api/interface.go @@ -16,34 +16,34 @@ type Storage interface { } type PolicyReportsInterface interface { - Get(ctx context.Context, name, namespace string) (v1alpha2.PolicyReport, error) - List(ctx context.Context, namespace string) ([]v1alpha2.PolicyReport, error) - Create(ctx context.Context, polr v1alpha2.PolicyReport) error - Update(ctx context.Context, polr v1alpha2.PolicyReport) error + Get(ctx context.Context, name, namespace string) (*v1alpha2.PolicyReport, error) + List(ctx context.Context, namespace string) ([]*v1alpha2.PolicyReport, error) + Create(ctx context.Context, polr *v1alpha2.PolicyReport) error + Update(ctx context.Context, polr *v1alpha2.PolicyReport) error Delete(ctx context.Context, name, namespace string) error } type ClusterPolicyReportsInterface interface { - Get(ctx context.Context, name string) (v1alpha2.ClusterPolicyReport, error) - List(ctx context.Context) ([]v1alpha2.ClusterPolicyReport, error) - Create(ctx context.Context, cpolr v1alpha2.ClusterPolicyReport) error - Update(ctx context.Context, cpolr v1alpha2.ClusterPolicyReport) error + Get(ctx context.Context, name string) (*v1alpha2.ClusterPolicyReport, error) + List(ctx context.Context) ([]*v1alpha2.ClusterPolicyReport, error) + Create(ctx context.Context, cpolr *v1alpha2.ClusterPolicyReport) error + Update(ctx context.Context, cpolr *v1alpha2.ClusterPolicyReport) error Delete(ctx context.Context, name string) error } type EphemeralReportsInterface interface { - Get(ctx context.Context, name, namespace string) (reportsv1.EphemeralReport, error) - List(ctx context.Context, namespace string) ([]reportsv1.EphemeralReport, error) - Create(ctx context.Context, polr reportsv1.EphemeralReport) error - Update(ctx context.Context, polr reportsv1.EphemeralReport) error + Get(ctx context.Context, name, namespace string) (*reportsv1.EphemeralReport, error) + List(ctx context.Context, namespace string) ([]*reportsv1.EphemeralReport, error) + Create(ctx context.Context, polr *reportsv1.EphemeralReport) error + Update(ctx context.Context, polr *reportsv1.EphemeralReport) error Delete(ctx context.Context, name, namespace string) error } type ClusterEphemeralReportsInterface interface { - Get(ctx context.Context, name string) (reportsv1.ClusterEphemeralReport, error) - List(ctx context.Context) ([]reportsv1.ClusterEphemeralReport, error) - Create(ctx context.Context, cephr reportsv1.ClusterEphemeralReport) error - Update(ctx context.Context, cephr reportsv1.ClusterEphemeralReport) error + Get(ctx context.Context, name string) (*reportsv1.ClusterEphemeralReport, error) + List(ctx context.Context) ([]*reportsv1.ClusterEphemeralReport, error) + Create(ctx context.Context, cephr *reportsv1.ClusterEphemeralReport) error + Update(ctx context.Context, cephr *reportsv1.ClusterEphemeralReport) error Delete(ctx context.Context, name string) error } diff --git a/pkg/storage/db/cephr.go b/pkg/storage/db/cephr.go index fe11999..70c5867 100644 --- a/pkg/storage/db/cephr.go +++ b/pkg/storage/db/cephr.go @@ -4,6 +4,7 @@ import ( "context" "database/sql" "encoding/json" + "errors" "fmt" "sync" @@ -27,12 +28,12 @@ func NewClusterEphemeralReportStore(db *sql.DB) (api.ClusterEphemeralReportsInte return &cephr{db: db}, nil } -func (c *cephr) List(ctx context.Context) ([]reportsv1.ClusterEphemeralReport, error) { +func (c *cephr) List(ctx context.Context) ([]*reportsv1.ClusterEphemeralReport, error) { c.Lock() defer c.Unlock() klog.Infof("listing all values") - res := make([]reportsv1.ClusterEphemeralReport, 0) + res := make([]*reportsv1.ClusterEphemeralReport, 0) var jsonb string rows, err := c.db.Query("SELECT report FROM clusterephemeralreports") @@ -52,14 +53,14 @@ func (c *cephr) List(ctx context.Context) ([]reportsv1.ClusterEphemeralReport, e klog.ErrorS(err, "failed to unmarshal clusterephemeralreports") return nil, fmt.Errorf("clusterephemeralreports list: cannot convert jsonb to clusterephemeralreports: %v", err) } - res = append(res, report) + res = append(res, &report) } klog.Infof("list found length: %d", len(res)) return res, nil } -func (c *cephr) Get(ctx context.Context, name string) (reportsv1.ClusterEphemeralReport, error) { +func (c *cephr) Get(ctx context.Context, name string) (*reportsv1.ClusterEphemeralReport, error) { c.Lock() defer c.Unlock() @@ -69,25 +70,29 @@ func (c *cephr) Get(ctx context.Context, name string) (reportsv1.ClusterEphemera if err := row.Scan(&jsonb); err != nil { klog.ErrorS(err, fmt.Sprintf("clusterephemeralreport not found name=%s", name)) if err == sql.ErrNoRows { - return reportsv1.ClusterEphemeralReport{}, fmt.Errorf("clusterephemeralreport get %s: no such ephemeral report", name) + return nil, fmt.Errorf("clusterephemeralreport get %s: no such ephemeral report", name) } - return reportsv1.ClusterEphemeralReport{}, fmt.Errorf("clusterephemeralreport get %s: %v", name, err) + return nil, fmt.Errorf("clusterephemeralreport get %s: %v", name, err) } var report reportsv1.ClusterEphemeralReport err := json.Unmarshal([]byte(jsonb), &report) if err != nil { klog.ErrorS(err, "failed to unmarshal report") - return reportsv1.ClusterEphemeralReport{}, fmt.Errorf("clusterephemeralreport list: cannot convert jsonb to ephemeralreport: %v", err) + return nil, fmt.Errorf("clusterephemeralreport list: cannot convert jsonb to ephemeralreport: %v", err) } - return report, nil + return &report, nil } -func (c *cephr) Create(ctx context.Context, cephr reportsv1.ClusterEphemeralReport) error { +func (c *cephr) Create(ctx context.Context, cephr *reportsv1.ClusterEphemeralReport) error { c.Lock() defer c.Unlock() - klog.Infof("creating entry for key:%s", cephr.Name) - jsonb, err := json.Marshal(cephr) + if cephr == nil { + return errors.New("invalid cluster ephemeral report") + } + + klog.Infof("creating entry for key:%s", cephr.Name) + jsonb, err := json.Marshal(*cephr) if err != nil { klog.ErrorS(err, "failed to unmarshal cephr") return err @@ -101,11 +106,15 @@ func (c *cephr) Create(ctx context.Context, cephr reportsv1.ClusterEphemeralRepo return nil } -func (c *cephr) Update(ctx context.Context, cephr reportsv1.ClusterEphemeralReport) error { +func (c *cephr) Update(ctx context.Context, cephr *reportsv1.ClusterEphemeralReport) error { c.Lock() defer c.Unlock() - jsonb, err := json.Marshal(cephr) + if cephr == nil { + return errors.New("invalid cluster ephemeral report") + } + + jsonb, err := json.Marshal(*cephr) if err != nil { return err } diff --git a/pkg/storage/db/cpolr.go b/pkg/storage/db/cpolr.go index 7b33150..3c0d87a 100644 --- a/pkg/storage/db/cpolr.go +++ b/pkg/storage/db/cpolr.go @@ -4,6 +4,7 @@ import ( "context" "database/sql" "encoding/json" + "errors" "fmt" "sync" @@ -27,12 +28,12 @@ func NewClusterPolicyReportStore(db *sql.DB) (api.ClusterPolicyReportsInterface, return &cpolrdb{db: db}, nil } -func (c *cpolrdb) List(ctx context.Context) ([]v1alpha2.ClusterPolicyReport, error) { +func (c *cpolrdb) List(ctx context.Context) ([]*v1alpha2.ClusterPolicyReport, error) { c.Lock() defer c.Unlock() klog.Infof("listing all values") - res := make([]v1alpha2.ClusterPolicyReport, 0) + res := make([]*v1alpha2.ClusterPolicyReport, 0) var jsonb string rows, err := c.db.Query("SELECT report FROM clusterpolicyreports") @@ -52,14 +53,14 @@ func (c *cpolrdb) List(ctx context.Context) ([]v1alpha2.ClusterPolicyReport, err klog.ErrorS(err, "failed to unmarshal clusterpolicyreport") return nil, fmt.Errorf("clusterpolicyreport list: cannot convert jsonb to clusterpolicyreport: %v", err) } - res = append(res, report) + res = append(res, &report) } klog.Infof("list found length: %d", len(res)) return res, nil } -func (c *cpolrdb) Get(ctx context.Context, name string) (v1alpha2.ClusterPolicyReport, error) { +func (c *cpolrdb) Get(ctx context.Context, name string) (*v1alpha2.ClusterPolicyReport, error) { c.Lock() defer c.Unlock() @@ -69,25 +70,29 @@ func (c *cpolrdb) Get(ctx context.Context, name string) (v1alpha2.ClusterPolicyR if err := row.Scan(&jsonb); err != nil { klog.ErrorS(err, fmt.Sprintf("clusterpolicyreport not found name=%s", name)) if err == sql.ErrNoRows { - return v1alpha2.ClusterPolicyReport{}, fmt.Errorf("clusterpolicyreport get %s: no such policy report", name) + return nil, fmt.Errorf("clusterpolicyreport get %s: no such policy report", name) } - return v1alpha2.ClusterPolicyReport{}, fmt.Errorf("clusterpolicyreport get %s: %v", name, err) + return nil, fmt.Errorf("clusterpolicyreport get %s: %v", name, err) } var report v1alpha2.ClusterPolicyReport err := json.Unmarshal([]byte(jsonb), &report) if err != nil { klog.ErrorS(err, "failed to unmarshal report") - return v1alpha2.ClusterPolicyReport{}, fmt.Errorf("clusterpolicyreport list: cannot convert jsonb to policyreport: %v", err) + return nil, fmt.Errorf("clusterpolicyreport list: cannot convert jsonb to policyreport: %v", err) } - return report, nil + return &report, nil } -func (c *cpolrdb) Create(ctx context.Context, cpolr v1alpha2.ClusterPolicyReport) error { +func (c *cpolrdb) Create(ctx context.Context, cpolr *v1alpha2.ClusterPolicyReport) error { c.Lock() defer c.Unlock() - klog.Infof("creating entry for key:%s", cpolr.Name) - jsonb, err := json.Marshal(cpolr) + if cpolr == nil { + return errors.New("invalid cluster policy report") + } + + klog.Infof("creating entry for key:%s", cpolr.Name) + jsonb, err := json.Marshal(*cpolr) if err != nil { klog.ErrorS(err, "failed to unmarshal cpolr") return err @@ -101,11 +106,15 @@ func (c *cpolrdb) Create(ctx context.Context, cpolr v1alpha2.ClusterPolicyReport return nil } -func (c *cpolrdb) Update(ctx context.Context, cpolr v1alpha2.ClusterPolicyReport) error { +func (c *cpolrdb) Update(ctx context.Context, cpolr *v1alpha2.ClusterPolicyReport) error { c.Lock() defer c.Unlock() - jsonb, err := json.Marshal(cpolr) + if cpolr == nil { + return errors.New("invalid cluster policy report") + } + + jsonb, err := json.Marshal(*cpolr) if err != nil { return err } diff --git a/pkg/storage/db/ephr.go b/pkg/storage/db/ephr.go index 8c6ff69..1ffeb79 100644 --- a/pkg/storage/db/ephr.go +++ b/pkg/storage/db/ephr.go @@ -4,6 +4,7 @@ import ( "context" "database/sql" "encoding/json" + "errors" "fmt" "sync" @@ -32,12 +33,12 @@ func NewEphemeralReportStore(db *sql.DB) (api.EphemeralReportsInterface, error) return &ephrdb{db: db}, nil } -func (p *ephrdb) List(ctx context.Context, namespace string) ([]reportsv1.EphemeralReport, error) { +func (p *ephrdb) List(ctx context.Context, namespace string) ([]*reportsv1.EphemeralReport, error) { p.Lock() defer p.Unlock() klog.Infof("listing all values for namespace:%s", namespace) - res := make([]reportsv1.EphemeralReport, 0) + res := make([]*reportsv1.EphemeralReport, 0) var jsonb string var rows *sql.Rows var err error @@ -63,14 +64,14 @@ func (p *ephrdb) List(ctx context.Context, namespace string) ([]reportsv1.Epheme klog.ErrorS(err, "cannot convert jsonb to ephemeralreport") return nil, fmt.Errorf("ephemeralreport list %q: cannot convert jsonb to ephemeralreport: %v", namespace, err) } - res = append(res, report) + res = append(res, &report) } klog.Infof("list found length: %d", len(res)) return res, nil } -func (p *ephrdb) Get(ctx context.Context, name, namespace string) (reportsv1.EphemeralReport, error) { +func (p *ephrdb) Get(ctx context.Context, name, namespace string) (*reportsv1.EphemeralReport, error) { p.Lock() defer p.Unlock() @@ -80,25 +81,29 @@ func (p *ephrdb) Get(ctx context.Context, name, namespace string) (reportsv1.Eph if err := row.Scan(&jsonb); err != nil { klog.ErrorS(err, fmt.Sprintf("ephemeralreport not found name=%s namespace=%s", name, namespace)) if err == sql.ErrNoRows { - return reportsv1.EphemeralReport{}, fmt.Errorf("ephemeralreport get %s/%s: no such ephemeral report: %v", namespace, name, err) + return nil, fmt.Errorf("ephemeralreport get %s/%s: no such ephemeral report: %v", namespace, name, err) } - return reportsv1.EphemeralReport{}, fmt.Errorf("ephemeralreport get %s/%s: %v", namespace, name, err) + return nil, fmt.Errorf("ephemeralreport get %s/%s: %v", namespace, name, err) } var report reportsv1.EphemeralReport err := json.Unmarshal([]byte(jsonb), &report) if err != nil { klog.ErrorS(err, "cannot convert jsonb to ephemeralreport") - return reportsv1.EphemeralReport{}, fmt.Errorf("ephemeralreport list %q: cannot convert jsonb to ephemeralreport: %v", namespace, err) + return nil, fmt.Errorf("ephemeralreport list %q: cannot convert jsonb to ephemeralreport: %v", namespace, err) } - return report, nil + return &report, nil } -func (p *ephrdb) Create(ctx context.Context, polr reportsv1.EphemeralReport) error { +func (p *ephrdb) Create(ctx context.Context, polr *reportsv1.EphemeralReport) error { p.Lock() defer p.Unlock() - klog.Infof("creating entry for key:%s/%s", polr.Name, polr.Namespace) - jsonb, err := json.Marshal(polr) + if polr == nil { + return errors.New("invalid ephemeral report") + } + + klog.Infof("creating entry for key:%s/%s", polr.Name, polr.Namespace) + jsonb, err := json.Marshal(*polr) if err != nil { return err } @@ -111,11 +116,15 @@ func (p *ephrdb) Create(ctx context.Context, polr reportsv1.EphemeralReport) err return nil } -func (p *ephrdb) Update(ctx context.Context, polr reportsv1.EphemeralReport) error { +func (p *ephrdb) Update(ctx context.Context, polr *reportsv1.EphemeralReport) error { p.Lock() defer p.Unlock() - jsonb, err := json.Marshal(polr) + if polr == nil { + return errors.New("invalid ephemeral report") + } + + jsonb, err := json.Marshal(*polr) if err != nil { return err } diff --git a/pkg/storage/db/polr.go b/pkg/storage/db/polr.go index 79a8c01..ea9168e 100644 --- a/pkg/storage/db/polr.go +++ b/pkg/storage/db/polr.go @@ -4,6 +4,7 @@ import ( "context" "database/sql" "encoding/json" + "errors" "fmt" "sync" @@ -32,12 +33,12 @@ func NewPolicyReportStore(db *sql.DB) (api.PolicyReportsInterface, error) { return &polrdb{db: db}, nil } -func (p *polrdb) List(ctx context.Context, namespace string) ([]v1alpha2.PolicyReport, error) { +func (p *polrdb) List(ctx context.Context, namespace string) ([]*v1alpha2.PolicyReport, error) { p.Lock() defer p.Unlock() klog.Infof("listing all values for namespace:%s", namespace) - res := make([]v1alpha2.PolicyReport, 0) + res := make([]*v1alpha2.PolicyReport, 0) var jsonb string var rows *sql.Rows var err error @@ -64,14 +65,14 @@ func (p *polrdb) List(ctx context.Context, namespace string) ([]v1alpha2.PolicyR klog.ErrorS(err, "cannot convert jsonb to policyreport") return nil, fmt.Errorf("policyreport list %q: cannot convert jsonb to policyreport: %v", namespace, err) } - res = append(res, report) + res = append(res, &report) } klog.Infof("list found length: %d", len(res)) return res, nil } -func (p *polrdb) Get(ctx context.Context, name, namespace string) (v1alpha2.PolicyReport, error) { +func (p *polrdb) Get(ctx context.Context, name, namespace string) (*v1alpha2.PolicyReport, error) { p.Lock() defer p.Unlock() @@ -81,25 +82,29 @@ func (p *polrdb) Get(ctx context.Context, name, namespace string) (v1alpha2.Poli if err := row.Scan(&jsonb); err != nil { klog.ErrorS(err, fmt.Sprintf("policyreport not found name=%s namespace=%s", name, namespace)) if err == sql.ErrNoRows { - return v1alpha2.PolicyReport{}, fmt.Errorf("policyreport get %s/%s: no such policy report: %v", namespace, name, err) + return nil, fmt.Errorf("policyreport get %s/%s: no such policy report: %v", namespace, name, err) } - return v1alpha2.PolicyReport{}, fmt.Errorf("policyreport get %s/%s: %v", namespace, name, err) + return nil, fmt.Errorf("policyreport get %s/%s: %v", namespace, name, err) } var report v1alpha2.PolicyReport err := json.Unmarshal([]byte(jsonb), &report) if err != nil { klog.ErrorS(err, "cannot convert jsonb to policyreport") - return v1alpha2.PolicyReport{}, fmt.Errorf("policyreport list %q: cannot convert jsonb to policyreport: %v", namespace, err) + return nil, fmt.Errorf("policyreport list %q: cannot convert jsonb to policyreport: %v", namespace, err) } - return report, nil + return &report, nil } -func (p *polrdb) Create(ctx context.Context, polr v1alpha2.PolicyReport) error { +func (p *polrdb) Create(ctx context.Context, polr *v1alpha2.PolicyReport) error { p.Lock() defer p.Unlock() - klog.Infof("creating entry for key:%s/%s", polr.Name, polr.Namespace) - jsonb, err := json.Marshal(polr) + if polr == nil { + return errors.New("invalid policy report") + } + + klog.Infof("creating entry for key:%s/%s", polr.Name, polr.Namespace) + jsonb, err := json.Marshal(*polr) if err != nil { return err } @@ -112,11 +117,15 @@ func (p *polrdb) Create(ctx context.Context, polr v1alpha2.PolicyReport) error { return nil } -func (p *polrdb) Update(ctx context.Context, polr v1alpha2.PolicyReport) error { +func (p *polrdb) Update(ctx context.Context, polr *v1alpha2.PolicyReport) error { p.Lock() defer p.Unlock() - jsonb, err := json.Marshal(polr) + if polr == nil { + return errors.New("invalid policy report") + } + + jsonb, err := json.Marshal(*polr) if err != nil { return err } diff --git a/pkg/storage/inmemory/client.go b/pkg/storage/inmemory/client.go index c9c82b8..91f6185 100644 --- a/pkg/storage/inmemory/client.go +++ b/pkg/storage/inmemory/client.go @@ -29,16 +29,16 @@ type ClusterPolicyReportsInterface interface { func New() api.Storage { inMemoryDb := &inMemoryDb{ cpolrdb: &cpolrdb{ - db: make(map[string]v1alpha2.ClusterPolicyReport), + db: make(map[string]*v1alpha2.ClusterPolicyReport), }, polrdb: &polrdb{ - db: make(map[string]v1alpha2.PolicyReport), + db: make(map[string]*v1alpha2.PolicyReport), }, cephrdb: &cephrdb{ - db: make(map[string]reportsv1.ClusterEphemeralReport), + db: make(map[string]*reportsv1.ClusterEphemeralReport), }, ephrdb: &ephrdb{ - db: make(map[string]reportsv1.EphemeralReport), + db: make(map[string]*reportsv1.EphemeralReport), }, } return inMemoryDb diff --git a/pkg/storage/inmemory/cpehr.go b/pkg/storage/inmemory/cpehr.go index 0a70df8..33020c0 100644 --- a/pkg/storage/inmemory/cpehr.go +++ b/pkg/storage/inmemory/cpehr.go @@ -13,20 +13,20 @@ import ( type cephrdb struct { sync.Mutex - db map[string]reportsv1.ClusterEphemeralReport + db map[string]*reportsv1.ClusterEphemeralReport } func (c *cephrdb) key(name string) string { return fmt.Sprintf("cephr/%s", name) } -func (c *cephrdb) List(ctx context.Context) ([]reportsv1.ClusterEphemeralReport, error) { +func (c *cephrdb) List(ctx context.Context) ([]*reportsv1.ClusterEphemeralReport, error) { c.Lock() defer c.Unlock() klog.Infof("listing all values") - res := make([]reportsv1.ClusterEphemeralReport, 0, len(c.db)) + res := make([]*reportsv1.ClusterEphemeralReport, 0, len(c.db)) for _, val := range c.db { res = append(res, val) } @@ -35,7 +35,7 @@ func (c *cephrdb) List(ctx context.Context) ([]reportsv1.ClusterEphemeralReport, return res, nil } -func (c *cephrdb) Get(ctx context.Context, name string) (reportsv1.ClusterEphemeralReport, error) { +func (c *cephrdb) Get(ctx context.Context, name string) (*reportsv1.ClusterEphemeralReport, error) { c.Lock() defer c.Unlock() @@ -46,11 +46,11 @@ func (c *cephrdb) Get(ctx context.Context, name string) (reportsv1.ClusterEpheme return val, nil } else { klog.Errorf("value not found for key:%s", key) - return reportsv1.ClusterEphemeralReport{}, errors.NewNotFound(utils.ClusterEphemeralReportsGR, key) + return nil, errors.NewNotFound(utils.ClusterEphemeralReportsGR, key) } } -func (c *cephrdb) Create(ctx context.Context, cephr reportsv1.ClusterEphemeralReport) error { +func (c *cephrdb) Create(ctx context.Context, cephr *reportsv1.ClusterEphemeralReport) error { c.Lock() defer c.Unlock() @@ -66,7 +66,7 @@ func (c *cephrdb) Create(ctx context.Context, cephr reportsv1.ClusterEphemeralRe } } -func (c *cephrdb) Update(ctx context.Context, cephr reportsv1.ClusterEphemeralReport) error { +func (c *cephrdb) Update(ctx context.Context, cephr *reportsv1.ClusterEphemeralReport) error { c.Lock() defer c.Unlock() diff --git a/pkg/storage/inmemory/cpolr.go b/pkg/storage/inmemory/cpolr.go index 586181a..12c330c 100644 --- a/pkg/storage/inmemory/cpolr.go +++ b/pkg/storage/inmemory/cpolr.go @@ -13,20 +13,20 @@ import ( type cpolrdb struct { sync.Mutex - db map[string]v1alpha2.ClusterPolicyReport + db map[string]*v1alpha2.ClusterPolicyReport } func (c *cpolrdb) key(name string) string { return fmt.Sprintf("cpolr/%s", name) } -func (c *cpolrdb) List(ctx context.Context) ([]v1alpha2.ClusterPolicyReport, error) { +func (c *cpolrdb) List(ctx context.Context) ([]*v1alpha2.ClusterPolicyReport, error) { c.Lock() defer c.Unlock() klog.Infof("listing all values") - res := make([]v1alpha2.ClusterPolicyReport, 0, len(c.db)) + res := make([]*v1alpha2.ClusterPolicyReport, 0, len(c.db)) for _, val := range c.db { res = append(res, val) } @@ -35,7 +35,7 @@ func (c *cpolrdb) List(ctx context.Context) ([]v1alpha2.ClusterPolicyReport, err return res, nil } -func (c *cpolrdb) Get(ctx context.Context, name string) (v1alpha2.ClusterPolicyReport, error) { +func (c *cpolrdb) Get(ctx context.Context, name string) (*v1alpha2.ClusterPolicyReport, error) { c.Lock() defer c.Unlock() @@ -46,11 +46,11 @@ func (c *cpolrdb) Get(ctx context.Context, name string) (v1alpha2.ClusterPolicyR return val, nil } else { klog.Errorf("value not found for key:%s", key) - return v1alpha2.ClusterPolicyReport{}, errors.NewNotFound(utils.ClusterPolicyReportsGR, key) + return nil, errors.NewNotFound(utils.ClusterPolicyReportsGR, key) } } -func (c *cpolrdb) Create(ctx context.Context, cpolr v1alpha2.ClusterPolicyReport) error { +func (c *cpolrdb) Create(ctx context.Context, cpolr *v1alpha2.ClusterPolicyReport) error { c.Lock() defer c.Unlock() @@ -66,7 +66,7 @@ func (c *cpolrdb) Create(ctx context.Context, cpolr v1alpha2.ClusterPolicyReport } } -func (c *cpolrdb) Update(ctx context.Context, cpolr v1alpha2.ClusterPolicyReport) error { +func (c *cpolrdb) Update(ctx context.Context, cpolr *v1alpha2.ClusterPolicyReport) error { c.Lock() defer c.Unlock() diff --git a/pkg/storage/inmemory/ephr.go b/pkg/storage/inmemory/ephr.go index 654bf40..8cd212d 100644 --- a/pkg/storage/inmemory/ephr.go +++ b/pkg/storage/inmemory/ephr.go @@ -14,19 +14,19 @@ import ( type ephrdb struct { sync.Mutex - db map[string]reportsv1.EphemeralReport + db map[string]*reportsv1.EphemeralReport } func (e *ephrdb) key(name, namespace string) string { return fmt.Sprintf("ephr/%s/%s", namespace, name) } -func (e *ephrdb) List(ctx context.Context, namespace string) ([]reportsv1.EphemeralReport, error) { +func (e *ephrdb) List(ctx context.Context, namespace string) ([]*reportsv1.EphemeralReport, error) { e.Lock() defer e.Unlock() klog.Infof("listing all values for namespace:%s", namespace) - res := make([]reportsv1.EphemeralReport, 0) + res := make([]*reportsv1.EphemeralReport, 0) for k, v := range e.db { if namespace == "" || strings.HasPrefix(strings.TrimPrefix(k, "ephr/"), namespace) { @@ -39,7 +39,7 @@ func (e *ephrdb) List(ctx context.Context, namespace string) ([]reportsv1.Epheme return res, nil } -func (e *ephrdb) Get(ctx context.Context, name, namespace string) (reportsv1.EphemeralReport, error) { +func (e *ephrdb) Get(ctx context.Context, name, namespace string) (*reportsv1.EphemeralReport, error) { e.Lock() defer e.Unlock() @@ -50,11 +50,11 @@ func (e *ephrdb) Get(ctx context.Context, name, namespace string) (reportsv1.Eph return val, nil } else { klog.Errorf("value not found for key:%s", key) - return reportsv1.EphemeralReport{}, errors.NewNotFound(utils.EphemeralReportsGR, key) + return nil, errors.NewNotFound(utils.EphemeralReportsGR, key) } } -func (e *ephrdb) Create(ctx context.Context, ephr reportsv1.EphemeralReport) error { +func (e *ephrdb) Create(ctx context.Context, ephr *reportsv1.EphemeralReport) error { e.Lock() defer e.Unlock() @@ -70,7 +70,7 @@ func (e *ephrdb) Create(ctx context.Context, ephr reportsv1.EphemeralReport) err } } -func (e *ephrdb) Update(ctx context.Context, ephr reportsv1.EphemeralReport) error { +func (e *ephrdb) Update(ctx context.Context, ephr *reportsv1.EphemeralReport) error { e.Lock() defer e.Unlock() diff --git a/pkg/storage/inmemory/polr.go b/pkg/storage/inmemory/polr.go index 056e823..b53bac8 100644 --- a/pkg/storage/inmemory/polr.go +++ b/pkg/storage/inmemory/polr.go @@ -14,19 +14,19 @@ import ( type polrdb struct { sync.Mutex - db map[string]v1alpha2.PolicyReport + db map[string]*v1alpha2.PolicyReport } func (p *polrdb) key(name, namespace string) string { return fmt.Sprintf("polr/%s/%s", namespace, name) } -func (p *polrdb) List(ctx context.Context, namespace string) ([]v1alpha2.PolicyReport, error) { +func (p *polrdb) List(ctx context.Context, namespace string) ([]*v1alpha2.PolicyReport, error) { p.Lock() defer p.Unlock() klog.Infof("listing all values for namespace:%s", namespace) - res := make([]v1alpha2.PolicyReport, 0) + res := make([]*v1alpha2.PolicyReport, 0) for k, v := range p.db { if namespace == "" || strings.HasPrefix(strings.TrimPrefix(k, "polr/"), namespace) { @@ -39,7 +39,7 @@ func (p *polrdb) List(ctx context.Context, namespace string) ([]v1alpha2.PolicyR return res, nil } -func (p *polrdb) Get(ctx context.Context, name, namespace string) (v1alpha2.PolicyReport, error) { +func (p *polrdb) Get(ctx context.Context, name, namespace string) (*v1alpha2.PolicyReport, error) { p.Lock() defer p.Unlock() @@ -50,11 +50,11 @@ func (p *polrdb) Get(ctx context.Context, name, namespace string) (v1alpha2.Poli return val, nil } else { klog.Errorf("value not found for key:%s", key) - return v1alpha2.PolicyReport{}, errors.NewNotFound(utils.PolicyReportsGR, key) + return nil, errors.NewNotFound(utils.PolicyReportsGR, key) } } -func (p *polrdb) Create(ctx context.Context, polr v1alpha2.PolicyReport) error { +func (p *polrdb) Create(ctx context.Context, polr *v1alpha2.PolicyReport) error { p.Lock() defer p.Unlock() @@ -70,7 +70,7 @@ func (p *polrdb) Create(ctx context.Context, polr v1alpha2.PolicyReport) error { } } -func (p *polrdb) Update(ctx context.Context, polr v1alpha2.PolicyReport) error { +func (p *polrdb) Update(ctx context.Context, polr *v1alpha2.PolicyReport) error { p.Lock() defer p.Unlock()