Skip to content

Commit

Permalink
wip field selector
Browse files Browse the repository at this point in the history
Signed-off-by: Fabrizio Sestito <fabrizio.sestito@suse.com>
  • Loading branch information
fabriziosestito committed Nov 7, 2024
1 parent b049bd7 commit e15cfc5
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 3 deletions.
4 changes: 4 additions & 0 deletions api/storage/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
)

func init() {
}

// GroupName is the group name used in this package
const GroupName = "storage.sbombastic.rancher.io"

Expand Down Expand Up @@ -35,5 +38,6 @@ func addKnownTypes(scheme *runtime.Scheme) error {
&v1alpha1.SBOM{},
&v1alpha1.SBOMList{},
)

return nil
}
20 changes: 20 additions & 0 deletions api/storage/v1alpha1/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package v1alpha1

import (
"fmt"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand Down Expand Up @@ -56,5 +58,23 @@ func addKnownTypes(scheme *runtime.Scheme) error {
&metav1.CreateOptions{},
&metav1.ListOptions{},
)

err := scheme.AddFieldLabelConversionFunc(SchemeGroupVersion.WithKind("SBOM"), SBOMFieldSelectorConversion)
if err != nil {
return err

Check failure on line 64 in api/storage/v1alpha1/register.go

View workflow job for this annotation

GitHub Actions / Golangci-lint

error returned from external package is unwrapped: sig: func (*k8s.io/apimachinery/pkg/runtime.Scheme).AddFieldLabelConversionFunc(gvk k8s.io/apimachinery/pkg/runtime/schema.GroupVersionKind, conversionFunc k8s.io/apimachinery/pkg/runtime.FieldLabelConversionFunc) error (wrapcheck)
}
return nil
}

func SBOMFieldSelectorConversion(label, value string) (string, string, error) {
switch label {
case "metadata.name":
return label, value, nil
case "metadata.namespace":
return label, value, nil
case "spec.registry":
return label, value, nil
default:
return "", "", fmt.Errorf("%q is not a known field selector: only %q, %q, %q", label, "metadata.name", "metadata.namespace", "spec.registry")
}
}
3 changes: 2 additions & 1 deletion api/storage/v1alpha1/sbom_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ type SBOM struct {

// SBOMSpec defines the desired state of a SBOM
type SBOMSpec struct {
Data runtime.RawExtension `json:"data"`
Data runtime.RawExtension `json:"data"`
Registry string `json:"registry,omitempty"`
}

// SBOMStatus defines the observed state of a SBOM
Expand Down
1 change: 1 addition & 0 deletions examples/sbom.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ metadata:
spec:
data:
foo: bar
registry: "foo"
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,12 @@ require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/k0kubun/pp v3.0.1+incompatible // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/minio/highwayhash v1.0.3 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8Hm
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/k0kubun/pp v3.0.1+incompatible h1:3tqvf7QgUnZ5tXO6pNAZlrvHgl6DvifjDrd9g2S9Z40=
github.com/k0kubun/pp v3.0.1+incompatible/go.mod h1:GWse8YhT0p8pT4ir3ZgBbfZild3tgzSScAn6HmfYukg=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
Expand All @@ -139,6 +141,9 @@ github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
Expand Down Expand Up @@ -296,6 +301,7 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220906165534-d0df966e6959/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
Expand Down
4 changes: 3 additions & 1 deletion internal/storage/sbom_strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ func MatchSBOM(label labels.Selector, field fields.Selector) storage.SelectionPr

// SelectableFields returns a field set that represents the object.
func SelectableFields(obj *v1alpha1.SBOM) fields.Set {
return generic.ObjectMetaFieldsSet(&obj.ObjectMeta, true)
return generic.MergeFieldsSets(generic.ObjectMetaFieldsSet(&obj.ObjectMeta, false), fields.Set{
"spec.registry": obj.Spec.Registry,
})
}

type sbomStrategy struct {
Expand Down
3 changes: 3 additions & 0 deletions internal/storage/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

sq "github.com/Masterminds/squirrel"
"github.com/jmoiron/sqlx"
"github.com/k0kubun/pp"
)

// objectSchema is the schema of an object in the database.
Expand Down Expand Up @@ -247,6 +248,8 @@ func (s *store) Get(ctx context.Context, key string, opts storage.GetOptions, ob
// The returned contents may be delayed, but it is guaranteed that they will
// match 'opts.ResourceVersion' according 'opts.ResourceVersionMatch'.
func (s *store) GetList(ctx context.Context, key string, opts storage.ListOptions, listObj runtime.Object) error {
pp.Println("GetList", opts)

queryBuilder := sq.Select("*").From(s.table)
namespace := extractNamespace(key)
if namespace != "" {
Expand Down
11 changes: 10 additions & 1 deletion pkg/generated/applyconfiguration/storage/v1alpha1/sbomspec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions pkg/generated/openapi/zz_generated.openapi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions test/crd/storage.sbombastic.rancher.io_sboms.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ spec:
data:
type: object
x-kubernetes-preserve-unknown-fields: true
registry:
type: string
required:
- data
type: object
Expand Down

0 comments on commit e15cfc5

Please sign in to comment.