Skip to content

Commit

Permalink
wip
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 11, 2024
1 parent e15cfc5 commit 50dc09d
Show file tree
Hide file tree
Showing 45 changed files with 1,809 additions and 324 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
GOLANGCI_LINT_VERSION := v1.61.0
CONTROLLER_TOOLS_VERSION := v0.16.1
CONTROLLER_TOOLS_VERSION := v0.16.5
ENVTEST_VERSION := release-0.19
ENVTEST_K8S_VERSION := 1.31.0

Expand Down
8 changes: 2 additions & 6 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,10 @@ resources:
kind: Registry
path: github.com/rancher/sbombastic/api/v1alpha1
version: v1alpha1
- api:
crdVersion: v1
namespaced: true
controller: true
- controller: true
domain: sbombastic.rancher.io
group: sbombastic
group: storage.sbombastic.rancher.io
kind: Image
path: github.com/rancher/sbombastic/api/v1alpha1
version: v1alpha1
- controller: true
domain: sbombastic.rancher.io
Expand Down
13 changes: 13 additions & 0 deletions api/storage/v1alpha1/image_metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package v1alpha1

type ImageMetadata struct {
Registry string `json:"registry"`
Repository string `json:"repository"`
Tag string `json:"tag"`
Digest string `json:"digest"`
Platform string `json:"platform"`
}

type ImageMetadataAccessor interface {
GetImageMetadata() ImageMetadata
}
61 changes: 30 additions & 31 deletions api/v1alpha1/image_types.go → api/storage/v1alpha1/image_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,37 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
ImageRegistryLabel = "sbombastic.rancher.io/registry"
ImageRepositoryLabel = "sbombastic.rancher.io/repository"
ImageTagLabel = "sbombastic.rancher.io/tag"
ImageDigestLabel = "sbombastic.rancher.io/digest"
ImagePlatformLabel = "sbombastic.rancher.io/platform"
)
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// ImageList contains a list of Image
type ImageList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []Image `json:"items"`
}

// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +kubebuilder:selectablefield:JSONPath=`.spec.imageMetadata.registry`
// +kubebuilder:selectablefield:JSONPath=`.spec.imageMetadata.repository`
// +kubebuilder:selectablefield:JSONPath=`.spec.imageMetadata.tag`
// +kubebuilder:selectablefield:JSONPath=`.spec.imageMetadata.digest`
// +kubebuilder:selectablefield:JSONPath=`.spec.imageMetadata.platform`

// Image is the Schema for the images API
type Image struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec ImageSpec `json:"spec,omitempty"`
Status ImageStatus `json:"status,omitempty"`
}

// ImageSpec defines the desired state of Image
type ImageSpec struct {
// list of the layers that make the image
// Metadata of the image
ImageMetadata `json:"imageMetadata"`
// List of the layers that make the image
Layers []ImageLayer `json:"layers,omitempty"`
}

Expand All @@ -51,27 +71,6 @@ type ImageStatus struct {
// Important: Run "make" to regenerate code after modifying this file
}

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status

// Image is the Schema for the images API
type Image struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec ImageSpec `json:"spec,omitempty"`
Status ImageStatus `json:"status,omitempty"`
}

// +kubebuilder:object:root=true

// ImageList contains a list of Image
type ImageList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []Image `json:"items"`
}

func init() {
SchemeBuilder.Register(&Image{}, &ImageList{})
func (i *Image) GetImageMetadata() ImageMetadata {
return i.Spec.ImageMetadata
}
36 changes: 27 additions & 9 deletions api/storage/v1alpha1/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,39 +42,57 @@ func Resource(resource string) schema.GroupResource {

var (
// SchemeBuilder is the scheme builder with scheme init functions to run for this API package
SchemeBuilder = runtime.NewSchemeBuilder(addKnownTypes)
SchemeBuilder = runtime.NewSchemeBuilder(AddKnownTypes)
// AddToScheme is a common registration function for mapping packaged scoped group & version keys to a scheme
AddToScheme = SchemeBuilder.AddToScheme
)

// Adds the list of known types to the given scheme.
func addKnownTypes(scheme *runtime.Scheme) error {
func AddKnownTypes(scheme *runtime.Scheme) error {
scheme.AddKnownTypes(SchemeGroupVersion,
&ScanResult{},
&ScanResultList{},
&Image{},
&ImageList{},

&SBOM{},
&SBOMList{},

&ScanResult{},
&ScanResultList{},

&metav1.GetOptions{},
&metav1.CreateOptions{},
&metav1.ListOptions{},
)

err := scheme.AddFieldLabelConversionFunc(SchemeGroupVersion.WithKind("SBOM"), SBOMFieldSelectorConversion)
err := scheme.AddFieldLabelConversionFunc(SchemeGroupVersion.WithKind("Image"), imageMetadataFieldSelectorConversion)
if err != nil {
return err
return fmt.Errorf("unable to add field selector conversion function to Image: %w", err)
}

err = scheme.AddFieldLabelConversionFunc(SchemeGroupVersion.WithKind("SBOM"), imageMetadataFieldSelectorConversion)
if err != nil {
return fmt.Errorf("unable to add field selector conversion function to SBOM: %w", err)
}
return nil
}

func SBOMFieldSelectorConversion(label, value string) (string, string, error) {
func imageMetadataFieldSelectorConversion(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":
case "spec.imageMetadata.registry":
return label, value, nil
case "spec.imageMetadata.repository":
return label, value, nil
case "spec.imageMetadata.tag":
return label, value, nil
case "spec.imageMetadata.digest":
return label, value, nil
case "spec.imageMetadata.platform":
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")
return "", "", fmt.Errorf("%q is not a known field selector: only %q, %q, %q", label, "metadata.name", "metadata.namespace", "spec.imageMetadata.*")
}
}
13 changes: 11 additions & 2 deletions api/storage/v1alpha1/sbom_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ type SBOMList struct {

// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +kubebuilder:selectablefield:JSONPath=`.spec.imageMetadata.registry`
// +kubebuilder:selectablefield:JSONPath=`.spec.imageMetadata.repository`
// +kubebuilder:selectablefield:JSONPath=`.spec.imageMetadata.tag`
// +kubebuilder:selectablefield:JSONPath=`.spec.imageMetadata.digest`
// +kubebuilder:selectablefield:JSONPath=`.spec.imageMetadata.platform`

// SBOM represents a Software Bill of Materials of an OCI artifact
type SBOM struct {
Expand All @@ -44,12 +49,16 @@ type SBOM struct {

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

// SBOMStatus defines the observed state of a SBOM
type SBOMStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
}

func (s *SBOM) GetImageMetadata() ImageMetadata {
return s.Spec.ImageMetadata
}
132 changes: 132 additions & 0 deletions api/storage/v1alpha1/zz_generated.deepcopy.go

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

Loading

0 comments on commit 50dc09d

Please sign in to comment.