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

feat: move Image to storage and implement field selectors #25

Merged
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 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
2 changes: 2 additions & 0 deletions Tiltfile
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ local_resource(
"go.sum",
"cmd/worker",
"api",
"internal/messaging",
"internal/handlers",
],
)

Expand Down
19 changes: 19 additions & 0 deletions api/storage/v1alpha1/image_metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package v1alpha1

// ImageMetadata contains the metadata details of an image.
type ImageMetadata struct {
fabriziosestito marked this conversation as resolved.
Show resolved Hide resolved
// Registry specifies the name of the Registry object in the same namespace where the image is stored.
Registry string `json:"registry"`
// Repository specifies the repository path of the image. Example: "rancher/sbombastic".
Repository string `json:"repository"`
// Tag specifies the tag of the image. Example: "latest".
Tag string `json:"tag"`
// Platform specifies the platform of the image. Example "linux/amd64".
Platform string `json:"platform"`
// Digest specifies the sha256 digest of the image.
Digest string `json:"digest"`
}

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.platform`
// +kubebuilder:selectablefield:JSONPath=`.spec.imageMetadata.digest`

// 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
}
46 changes: 42 additions & 4 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 All @@ -40,21 +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("Image"), imageMetadataFieldSelectorConversion)
if err != nil {
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 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.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.platform":
return label, value, nil
case "spec.imageMetadata.digest":
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.imageMetadata.*")
}
}
12 changes: 11 additions & 1 deletion 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.platform`
// +kubebuilder:selectablefield:JSONPath=`.spec.imageMetadata.digest`

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

// SBOMSpec defines the desired state of a SBOM
type SBOMSpec struct {
Data runtime.RawExtension `json:"data"`
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