Skip to content

Commit

Permalink
✨ Support image spec 1.1 referrers api (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
tosone authored Oct 27, 2023
1 parent eea6e90 commit 1dd621d
Show file tree
Hide file tree
Showing 16 changed files with 306 additions and 7 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ require (
github.com/moby/buildkit v0.12.2
github.com/opencontainers/distribution-spec/specs-go v0.0.0-20231016131659-3940529fe6c0
github.com/opencontainers/go-digest v1.0.0
github.com/opencontainers/image-spec v1.1.0-rc5
github.com/redis/go-redis/v9 v9.2.1
github.com/robfig/cron/v3 v3.0.1
github.com/rs/zerolog v1.31.0
Expand Down Expand Up @@ -218,7 +219,6 @@ require (
github.com/mozillazg/go-httpheader v0.4.0 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/nwaples/rardecode v1.1.3 // indirect
github.com/opencontainers/image-spec v1.1.0-rc5 // indirect
github.com/opencontainers/runc v1.1.7 // indirect
github.com/opencontainers/runtime-spec v1.1.0-rc.2 // indirect
github.com/opencontainers/selinux v1.11.0 // indirect
Expand Down
16 changes: 16 additions & 0 deletions pkg/dal/dao/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ type ArtifactService interface {
GetNamespaceSize(ctx context.Context, namespaceID int64) (int64, error)
// GetRepositorySize get the specific repository size
GetRepositorySize(ctx context.Context, repositoryID int64) (int64, error)
// GetReferrers ...
GetReferrers(ctx context.Context, repositoryID int64, digest string, artifactTypes []string) ([]*models.Artifact, error)
}

type artifactService struct {
Expand Down Expand Up @@ -364,3 +366,17 @@ func (s *artifactService) GetRepositorySize(ctx context.Context, repositoryID in
}
return result.Size, nil
}

// GetReferrers ...
func (s *artifactService) GetReferrers(ctx context.Context, repositoryID int64, digest string, artifactTypes []string) ([]*models.Artifact, error) {
artifactObj, err := s.tx.Artifact.WithContext(ctx).Where(s.tx.Artifact.RepositoryID.Eq(repositoryID)).
Where(s.tx.Artifact.Digest.Eq(digest)).First()
if err != nil {
return nil, err
}
query := s.tx.Artifact.WithContext(ctx).Where(s.tx.Artifact.RepositoryID.Eq(repositoryID))
if len(artifactTypes) > 0 {
query = query.Where(s.tx.Artifact.ConfigMediaType.In(artifactTypes...))
}
return query.Where(s.tx.Artifact.ReferrerID.Eq(artifactObj.ID)).Find()
}
15 changes: 15 additions & 0 deletions pkg/dal/dao/mocks/artifact.go

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

2 changes: 2 additions & 0 deletions pkg/dal/migrations/mysql/0001_initialize.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,12 @@ CREATE TABLE IF NOT EXISTS `artifacts` (
`pushed_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`last_pull` timestamp,
`pull_times` bigint NOT NULL DEFAULT 0,
`referrer_id` bigint,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`deleted_at` bigint NOT NULL DEFAULT 0,
FOREIGN KEY (`repository_id`) REFERENCES `repositories` (`id`),
FOREIGN KEY (`referrer_id`) REFERENCES `artifacts` (`id`),
CONSTRAINT `artifacts_unique_with_repo` UNIQUE (`repository_id`, `digest`, `deleted_at`)
);

Expand Down
2 changes: 2 additions & 0 deletions pkg/dal/migrations/postgresql/0001_initialize.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,12 @@ CREATE TABLE IF NOT EXISTS "artifacts" (
"pushed_at" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
"last_pull" timestamp,
"pull_times" bigint NOT NULL DEFAULT 0,
"referrer_id" bigint,
"created_at" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
"deleted_at" bigint NOT NULL DEFAULT 0,
FOREIGN KEY ("repository_id") REFERENCES "repositories" ("id"),
FOREIGN KEY ("referrer_id") REFERENCES "artifacts" ("id"),
CONSTRAINT "artifacts_unique_with_repo" UNIQUE ("repository_id", "digest", "deleted_at")
);

Expand Down
2 changes: 2 additions & 0 deletions pkg/dal/migrations/sqlite3/0001_initialize.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,13 @@ CREATE TABLE IF NOT EXISTS `artifacts` (
`type` text CHECK (`type` IN ('image', 'imageIndex', 'chart', 'cnab', 'wasm', 'provenance', 'cosign', 'unknown')) NOT NULL DEFAULT 'unknown',
`pushed_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`last_pull` timestamp,
`referrer_id` integer,
`pull_times` bigint NOT NULL DEFAULT 0,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`deleted_at` bigint NOT NULL DEFAULT 0,
FOREIGN KEY (`repository_id`) REFERENCES `repositories` (`id`),
FOREIGN KEY (`referrer_id`) REFERENCES `artifacts` (`id`),
CONSTRAINT `artifacts_unique_with_repo` UNIQUE (`repository_id`, `digest`, `deleted_at`)
);

Expand Down
3 changes: 3 additions & 0 deletions pkg/dal/models/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ type Artifact struct {
Vulnerability ArtifactVulnerability `gorm:"foreignKey:ArtifactID;"`
Sbom ArtifactSbom `gorm:"foreignKey:ArtifactID;"`

ReferrerID *int64
Referrer *Artifact

ArtifactIndexes []*Artifact `gorm:"many2many:artifact_artifacts;"`
Blobs []*Blob `gorm:"many2many:artifact_blobs;"`
Tags []*Tag `gorm:"foreignKey:ArtifactID;"`
Expand Down
8 changes: 8 additions & 0 deletions pkg/dal/query/artifact_sboms.gen.go

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

8 changes: 8 additions & 0 deletions pkg/dal/query/artifact_vulnerabilities.gen.go

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

96 changes: 95 additions & 1 deletion pkg/dal/query/artifacts.gen.go

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

8 changes: 8 additions & 0 deletions pkg/dal/query/blobs.gen.go

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

8 changes: 8 additions & 0 deletions pkg/dal/query/tags.gen.go

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

Loading

0 comments on commit 1dd621d

Please sign in to comment.