Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use resource version while listing #156

Merged
merged 4 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/actions/setup-build-env/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ runs:
git fetch --prune --unshallow
- uses: actions/setup-go@6edd4406fa81c3da01a34fa6f6343087c207a568 # v3.5.0
with:
go-version: ~1.21.3
go-version: ~1.22.2
- shell: bash
run: |
go mod download
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/conformance-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5.0.0
with:
go-version: ~1.21.1
go-version: ~1.22.2
- name: Install helm
id: helm
uses: azure/setup-helm@5119fcb9089d432beecbf79bb2c7915207344b78 # v3.5
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/migration-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5.0.0
with:
go-version: ~1.21.1
go-version: ~1.22.2
- name: Install helm
id: helm
uses: azure/setup-helm@5119fcb9089d432beecbf79bb2c7915207344b78 # v3.5
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
ARG ARCH
FROM golang:1.21.5 as build
FROM golang:1.22.2 as build

WORKDIR /
COPY . ./
Expand Down
6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
module github.com/kyverno/reports-server

go 1.21.4

toolchain go1.21.5
go 1.22.2

require (
github.com/kyverno/kyverno v1.12.0-alpha.1
github.com/lib/pq v1.10.9
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.8.0
k8s.io/api v0.29.1
k8s.io/apimachinery v0.29.2
k8s.io/apiserver v0.29.1
k8s.io/client-go v0.29.1
Expand Down Expand Up @@ -98,7 +97,6 @@ require (
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.29.1 // indirect
k8s.io/kms v0.29.1 // indirect
k8s.io/utils v0.0.0-20240102154912-e7106e64919e // indirect
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.29.0 // indirect
Expand Down
27 changes: 21 additions & 6 deletions pkg/api/cephr.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"context"
"fmt"
"slices"
"strconv"

reportsv1 "github.com/kyverno/kyverno/api/reports/v1"
"github.com/kyverno/reports-server/pkg/storage"
Expand Down Expand Up @@ -51,7 +52,7 @@
}

func (c *cephrStore) List(ctx context.Context, options *metainternalversion.ListOptions) (runtime.Object, error) {
labelSelector := labels.Everything()
var labelSelector labels.Selector

Check warning on line 55 in pkg/api/cephr.go

View check run for this annotation

Codecov / codecov/patch

pkg/api/cephr.go#L55

Added line #L55 was not covered by tests
// fieldSelector := fields.Everything() // TODO: Field selectors
if options != nil {
if options.LabelSelector != nil {
Expand All @@ -74,19 +75,33 @@
cephrList := &reportsv1.ClusterEphemeralReportList{
Items: make([]reportsv1.ClusterEphemeralReport, 0),
ListMeta: metav1.ListMeta{
// TODO: Fix this!!
ResourceVersion: "1",
},
}
var desiredRv uint64
if len(options.ResourceVersion) == 0 {
desiredRv = 1
} else {
desiredRv, err = strconv.ParseUint(options.ResourceVersion, 10, 64)
if err != nil {
return nil, err
}

Check warning on line 88 in pkg/api/cephr.go

View check run for this annotation

Codecov / codecov/patch

pkg/api/cephr.go#L81-L88

Added lines #L81 - L88 were not covered by tests
}
var resourceVersion uint64
resourceVersion = 1

Check warning on line 91 in pkg/api/cephr.go

View check run for this annotation

Codecov / codecov/patch

pkg/api/cephr.go#L90-L91

Added lines #L90 - L91 were not covered by tests
for _, cephr := range list.Items {
if cephr.Labels == nil {
return list, nil
allow, rv, err := allowObjectListWatch(cephr.ObjectMeta, labelSelector, desiredRv, options.ResourceVersionMatch)
if err != nil {
return nil, err

Check warning on line 95 in pkg/api/cephr.go

View check run for this annotation

Codecov / codecov/patch

pkg/api/cephr.go#L93-L95

Added lines #L93 - L95 were not covered by tests
}
if labelSelector.Matches(labels.Set(cephr.Labels)) {
if rv > resourceVersion {
resourceVersion = rv
}
if allow {

Check warning on line 100 in pkg/api/cephr.go

View check run for this annotation

Codecov / codecov/patch

pkg/api/cephr.go#L97-L100

Added lines #L97 - L100 were not covered by tests
cephrList.Items = append(cephrList.Items, cephr)
}
}

cephrList.ListMeta.ResourceVersion = strconv.FormatUint(resourceVersion, 10)

Check warning on line 104 in pkg/api/cephr.go

View check run for this annotation

Codecov / codecov/patch

pkg/api/cephr.go#L104

Added line #L104 was not covered by tests
return cephrList, nil
}

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

"github.com/kyverno/reports-server/pkg/storage"
"github.com/kyverno/reports-server/pkg/utils"
Expand Down Expand Up @@ -51,7 +52,7 @@
}

func (c *cpolrStore) List(ctx context.Context, options *metainternalversion.ListOptions) (runtime.Object, error) {
labelSelector := labels.Everything()
var labelSelector labels.Selector

Check warning on line 55 in pkg/api/cpolr.go

View check run for this annotation

Codecov / codecov/patch

pkg/api/cpolr.go#L55

Added line #L55 was not covered by tests
// fieldSelector := fields.Everything() // TODO: Field selectors
if options != nil {
if options.LabelSelector != nil {
Expand All @@ -74,19 +75,33 @@
cpolrList := &v1alpha2.ClusterPolicyReportList{
Items: make([]v1alpha2.ClusterPolicyReport, 0),
ListMeta: metav1.ListMeta{
// TODO: Fix this!!
ResourceVersion: "1",
},
}
var desiredRv uint64
if len(options.ResourceVersion) == 0 {
desiredRv = 1
} else {
desiredRv, err = strconv.ParseUint(options.ResourceVersion, 10, 64)
if err != nil {
return nil, err
}

Check warning on line 88 in pkg/api/cpolr.go

View check run for this annotation

Codecov / codecov/patch

pkg/api/cpolr.go#L81-L88

Added lines #L81 - L88 were not covered by tests
}
var resourceVersion uint64
resourceVersion = 1

Check warning on line 91 in pkg/api/cpolr.go

View check run for this annotation

Codecov / codecov/patch

pkg/api/cpolr.go#L90-L91

Added lines #L90 - L91 were not covered by tests
for _, cpolr := range list.Items {
if cpolr.Labels == nil {
return list, nil
allow, rv, err := allowObjectListWatch(cpolr.ObjectMeta, labelSelector, desiredRv, options.ResourceVersionMatch)
if err != nil {
return nil, err

Check warning on line 95 in pkg/api/cpolr.go

View check run for this annotation

Codecov / codecov/patch

pkg/api/cpolr.go#L93-L95

Added lines #L93 - L95 were not covered by tests
}
if labelSelector.Matches(labels.Set(cpolr.Labels)) {
if rv > resourceVersion {
resourceVersion = rv
}
if allow {

Check warning on line 100 in pkg/api/cpolr.go

View check run for this annotation

Codecov / codecov/patch

pkg/api/cpolr.go#L97-L100

Added lines #L97 - L100 were not covered by tests
cpolrList.Items = append(cpolrList.Items, cpolr)
}
}

cpolrList.ListMeta.ResourceVersion = strconv.FormatUint(resourceVersion, 10)

Check warning on line 104 in pkg/api/cpolr.go

View check run for this annotation

Codecov / codecov/patch

pkg/api/cpolr.go#L104

Added line #L104 was not covered by tests
return cpolrList, nil
}

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

reportsv1 "github.com/kyverno/kyverno/api/reports/v1"
"github.com/kyverno/reports-server/pkg/storage"
Expand Down Expand Up @@ -52,7 +53,7 @@
}

func (p *ephrStore) List(ctx context.Context, options *metainternalversion.ListOptions) (runtime.Object, error) {
labelSelector := labels.Everything()
var labelSelector labels.Selector

Check warning on line 56 in pkg/api/ephr.go

View check run for this annotation

Codecov / codecov/patch

pkg/api/ephr.go#L56

Added line #L56 was not covered by tests
// fieldSelector := fields.Everything() // TODO: Field selectors
if options != nil {
if options.LabelSelector != nil {
Expand All @@ -77,19 +78,33 @@
ephrList := &reportsv1.EphemeralReportList{
Items: make([]reportsv1.EphemeralReport, 0),
ListMeta: metav1.ListMeta{
// TODO: Fix this!!
ResourceVersion: "1",
},
}
var desiredRv uint64
if len(options.ResourceVersion) == 0 {
desiredRv = 1
} else {
desiredRv, err = strconv.ParseUint(options.ResourceVersion, 10, 64)
if err != nil {
return nil, err
}

Check warning on line 91 in pkg/api/ephr.go

View check run for this annotation

Codecov / codecov/patch

pkg/api/ephr.go#L84-L91

Added lines #L84 - L91 were not covered by tests
}
var resourceVersion uint64
resourceVersion = 1

Check warning on line 94 in pkg/api/ephr.go

View check run for this annotation

Codecov / codecov/patch

pkg/api/ephr.go#L93-L94

Added lines #L93 - L94 were not covered by tests
for _, ephr := range list.Items {
if ephr.Labels == nil {
return list, nil
allow, rv, err := allowObjectListWatch(ephr.ObjectMeta, labelSelector, desiredRv, options.ResourceVersionMatch)
if err != nil {
return nil, err

Check warning on line 98 in pkg/api/ephr.go

View check run for this annotation

Codecov / codecov/patch

pkg/api/ephr.go#L96-L98

Added lines #L96 - L98 were not covered by tests
}
if labelSelector.Matches(labels.Set(ephr.Labels)) {
if rv > resourceVersion {
resourceVersion = rv
}
if allow {

Check warning on line 103 in pkg/api/ephr.go

View check run for this annotation

Codecov / codecov/patch

pkg/api/ephr.go#L100-L103

Added lines #L100 - L103 were not covered by tests
ephrList.Items = append(ephrList.Items, ephr)
}
}

ephrList.ListMeta.ResourceVersion = strconv.FormatUint(resourceVersion, 10)

Check warning on line 107 in pkg/api/ephr.go

View check run for this annotation

Codecov / codecov/patch

pkg/api/ephr.go#L107

Added line #L107 was not covered by tests
return ephrList, nil
}

Expand Down
36 changes: 36 additions & 0 deletions pkg/api/filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package api

import (
"strconv"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
)

func allowObjectListWatch(object metav1.ObjectMeta, labelSelector labels.Selector, desiredRv uint64, rvmatch metav1.ResourceVersionMatch) (bool, uint64, error) {
rv, err := strconv.ParseUint(object.ResourceVersion, 10, 64)
if err != nil {
return false, 0, err
}

Check warning on line 14 in pkg/api/filter.go

View check run for this annotation

Codecov / codecov/patch

pkg/api/filter.go#L10-L14

Added lines #L10 - L14 were not covered by tests

switch rvmatch {
case metav1.ResourceVersionMatchNotOlderThan:
if rv < desiredRv {
return false, 0, nil
}
case metav1.ResourceVersionMatchExact:
if rv != desiredRv {
return false, 0, nil
}

Check warning on line 24 in pkg/api/filter.go

View check run for this annotation

Codecov / codecov/patch

pkg/api/filter.go#L16-L24

Added lines #L16 - L24 were not covered by tests
}

if labelSelector == nil {
return true, rv, nil
}

Check warning on line 29 in pkg/api/filter.go

View check run for this annotation

Codecov / codecov/patch

pkg/api/filter.go#L27-L29

Added lines #L27 - L29 were not covered by tests

if labelSelector.Matches(labels.Set(object.Labels)) {
return true, rv, nil
} else {
return false, 0, nil
}

Check warning on line 35 in pkg/api/filter.go

View check run for this annotation

Codecov / codecov/patch

pkg/api/filter.go#L31-L35

Added lines #L31 - L35 were not covered by tests
}
27 changes: 21 additions & 6 deletions pkg/api/polr.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"context"
"fmt"
"slices"
"strconv"

"github.com/kyverno/reports-server/pkg/storage"
"github.com/kyverno/reports-server/pkg/utils"
Expand Down Expand Up @@ -52,7 +53,7 @@
}

func (p *polrStore) List(ctx context.Context, options *metainternalversion.ListOptions) (runtime.Object, error) {
labelSelector := labels.Everything()
var labelSelector labels.Selector

Check warning on line 56 in pkg/api/polr.go

View check run for this annotation

Codecov / codecov/patch

pkg/api/polr.go#L56

Added line #L56 was not covered by tests
// fieldSelector := fields.Everything() // TODO: Field selectors
if options != nil {
if options.LabelSelector != nil {
Expand All @@ -77,19 +78,33 @@
polrList := &v1alpha2.PolicyReportList{
Items: make([]v1alpha2.PolicyReport, 0),
ListMeta: metav1.ListMeta{
// TODO: Fix this!!
ResourceVersion: "1",
},
}
var desiredRv uint64
if len(options.ResourceVersion) == 0 {
desiredRv = 1
} else {
desiredRv, err = strconv.ParseUint(options.ResourceVersion, 10, 64)
if err != nil {
return nil, err
}

Check warning on line 91 in pkg/api/polr.go

View check run for this annotation

Codecov / codecov/patch

pkg/api/polr.go#L84-L91

Added lines #L84 - L91 were not covered by tests
}
var resourceVersion uint64
resourceVersion = 1

Check warning on line 94 in pkg/api/polr.go

View check run for this annotation

Codecov / codecov/patch

pkg/api/polr.go#L93-L94

Added lines #L93 - L94 were not covered by tests
for _, polr := range list.Items {
if polr.Labels == nil {
return list, nil
allow, rv, err := allowObjectListWatch(polr.ObjectMeta, labelSelector, desiredRv, options.ResourceVersionMatch)
if err != nil {
return nil, err

Check warning on line 98 in pkg/api/polr.go

View check run for this annotation

Codecov / codecov/patch

pkg/api/polr.go#L96-L98

Added lines #L96 - L98 were not covered by tests
}
if labelSelector.Matches(labels.Set(polr.Labels)) {
if rv > resourceVersion {
resourceVersion = rv
}
if allow {

Check warning on line 103 in pkg/api/polr.go

View check run for this annotation

Codecov / codecov/patch

pkg/api/polr.go#L100-L103

Added lines #L100 - L103 were not covered by tests
polrList.Items = append(polrList.Items, polr)
}
}

polrList.ListMeta.ResourceVersion = strconv.FormatUint(resourceVersion, 10)

Check warning on line 107 in pkg/api/polr.go

View check run for this annotation

Codecov / codecov/patch

pkg/api/polr.go#L107

Added line #L107 was not covered by tests
return polrList, nil
}

Expand Down
2 changes: 1 addition & 1 deletion tools/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/kyverno/reports-server/tools

go 1.21
go 1.22.2

require (
github.com/google/addlicense v1.1.1
Expand Down
Loading