Skip to content

Commit 87deacc

Browse files
committed
refactor: Trim Comments To Non-Obvious Rationale
Signed-off-by: Vad1mo <vadim@8gears.com>
1 parent cc0db78 commit 87deacc

5 files changed

Lines changed: 26 additions & 49 deletions

File tree

src/controller/artifact/abstractor_test.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -387,12 +387,11 @@ var (
387387

388388
type abstractorTestSuite struct {
389389
suite.Suite
390-
argMgr *tart.Manager
391-
blobMgr *tblob.Manager
392-
regCli *registry.Client
393-
abstractor *abstractor
394-
processor *tpro.Processor
395-
// the classifier chain in place before the suite replaced it
390+
argMgr *tart.Manager
391+
blobMgr *tblob.Manager
392+
regCli *registry.Client
393+
abstractor *abstractor
394+
processor *tpro.Processor
396395
classifiers []manifest.ChildClassifier
397396
}
398397

@@ -409,9 +408,7 @@ func (a *abstractorTestSuite) SetupTest() {
409408
// clear all registered processors
410409
processor.Registry = map[string]processor.Processor{}
411410
processor.Registry[schema2.MediaTypeImageConfig] = a.processor
412-
// replace the classifier chain with a mock-backed instance so index children
413-
// are classified against the suite's managers instead of the globals wired
414-
// up in the manifest package's init()
411+
// classify against the suite's managers, not the globals wired up by init()
415412
a.classifiers = manifest.ChildClassifiers
416413
manifest.ChildClassifiers = []manifest.ChildClassifier{
417414
manifest.NewInTotoAttestationClassifier(a.argMgr, a.regCli),

src/controller/artifact/manifest/attestation.go

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,8 @@ import (
3232
"github.com/goharbor/harbor/src/pkg/registry"
3333
)
3434

35-
// Attestation manifests are stored inside the index next to the platform images
36-
// they describe, linked by these annotations rather than by a distinct manifest
37-
// media type. See
35+
// Attestations are linked to the images they describe by these annotations
36+
// rather than by a distinct manifest media type.
3837
// https://github.com/moby/buildkit/blob/master/docs/attestations/attestation-storage.md
3938
const (
4039
referenceTypeAnnotation = "vnd.docker.reference.type"
@@ -57,25 +56,22 @@ type inTotoSubject struct {
5756
Digest map[string]string `json:"digest"`
5857
}
5958

60-
// InTotoAttestationClassifier classifies in-toto attestation manifests carried
61-
// inside an index as accessories of the platform image they attest. BuildKit,
62-
// Buildah and Podman all emit this layout, so the classifier is named after the
63-
// attestation format rather than after any single producer.
59+
// InTotoAttestationClassifier classifies in-toto attestation manifests as
60+
// accessories of the image they attest. Named after the attestation format, not
61+
// its producer: BuildKit, Buildah and Podman all emit this layout.
6462
type InTotoAttestationClassifier struct {
6563
artMgr artifact.Manager
6664
regCli registry.Client
6765
}
6866

69-
// NewInTotoAttestationClassifier returns a classifier for in-toto attestation
70-
// manifests.
67+
// NewInTotoAttestationClassifier returns a classifier for in-toto attestations.
7168
func NewInTotoAttestationClassifier(artMgr artifact.Manager, regCli registry.Client) *InTotoAttestationClassifier {
7269
return &InTotoAttestationClassifier{
7370
artMgr: artMgr,
7471
regCli: regCli,
7572
}
7673
}
7774

78-
// Classify implements ChildClassifier.
7975
func (c *InTotoAttestationClassifier) Classify(ctx context.Context, repository string, descriptor v1.Descriptor, siblings []v1.Descriptor) (*artifact.AccessoryCandidate, error) {
8076
if !isAttestationDescriptor(descriptor) {
8177
//nolint:nilnil // descriptor is not an attestation
@@ -223,11 +219,9 @@ func digestInIndex(siblings []v1.Descriptor, digestRef string) bool {
223219
return false
224220
}
225221

226-
// uniqueDigestInIndex returns the one sibling digest the subject matches. A
227-
// subject names a single artifact under several algorithms, so matching more
228-
// than one sibling means the payload disagrees with the index and is not safe
229-
// to attach. Rejecting the ambiguity also keeps the result independent of the
230-
// map iteration order behind subjectDigests.
222+
// A subject names one artifact under several algorithms, so matching more than
223+
// one sibling means the payload disagrees with the index. Rejecting that also
224+
// keeps the result independent of subjectDigests' map iteration order.
231225
func uniqueDigestInIndex(siblings []v1.Descriptor, digestRefs []string) string {
232226
match := ""
233227
for _, digestRef := range digestRefs {

src/controller/artifact/manifest/attestation_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,7 @@ func TestResolveAttestationSubject(t *testing.T) {
368368
})
369369
}
370370

371-
// A subject names one artifact under several algorithms, so the digest map is
372-
// iterated in unspecified order. Resolution must not depend on that order.
371+
// Resolution must not depend on the digest map's iteration order.
373372
func TestResolveAttestationSubjectAmbiguousDigests(t *testing.T) {
374373
amd64Encoded := "cad250bb95ea402adf4f687cc7d6747ecf0de875e6d6117f74437893964903df"
375374
sha256Ref := "sha256:" + amd64Encoded

src/controller/artifact/manifest/classifier.go

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,18 @@ import (
2323
"github.com/goharbor/harbor/src/pkg/artifact"
2424
)
2525

26-
// ChildClassifier inspects a child descriptor of an OCI index or a Docker
27-
// manifest list and decides whether it is a platform child of the index or an
28-
// accessory of one of its siblings.
26+
// ChildClassifier classifies a child descriptor of an OCI index or Docker manifest list.
2927
type ChildClassifier interface {
30-
// Classify returns a non-nil candidate when the descriptor is an accessory of
31-
// a sibling. Returning a nil candidate and a nil error means "not mine" and
32-
// lets the next classifier decide.
28+
// Classify returns a nil candidate and a nil error when the descriptor is not
29+
// its concern, leaving it to the next classifier.
3330
Classify(ctx context.Context, repository string, descriptor v1.Descriptor, siblings []v1.Descriptor) (*artifact.AccessoryCandidate, error)
3431
}
3532

36-
// ChildClassifiers holds the registered classifiers in registration order.
37-
// Exported so that tests can replace the chain with mock-backed instances, the
38-
// same way tests replace processor.Registry.
33+
// ChildClassifiers holds the registered classifiers, in registration order.
3934
var ChildClassifiers []ChildClassifier
4035

41-
// RegisterChildClassifier appends a classifier to the chain. A nil classifier is
42-
// dropped rather than stored: it would otherwise panic on every index push,
43-
// long after the faulty registration.
36+
// RegisterChildClassifier appends a classifier to the chain. A nil would panic on
37+
// every index push, far from the faulty registration, so it is dropped here.
4438
func RegisterChildClassifier(classifier ChildClassifier) {
4539
if classifier == nil {
4640
log.Errorf("refusing to register a nil child classifier")
@@ -49,9 +43,8 @@ func RegisterChildClassifier(classifier ChildClassifier) {
4943
ChildClassifiers = append(ChildClassifiers, classifier)
5044
}
5145

52-
// ClassifyChild walks the registered classifiers and returns the first accessory
53-
// candidate produced for the descriptor. A nil candidate means no classifier
54-
// claimed it, so the descriptor is an ordinary platform child of the index.
46+
// ClassifyChild returns the first accessory candidate claimed for the descriptor.
47+
// A nil candidate means it is an ordinary platform child of the index.
5548
func ClassifyChild(ctx context.Context, repository string, descriptor v1.Descriptor, siblings []v1.Descriptor) (*artifact.AccessoryCandidate, error) {
5649
for _, classifier := range ChildClassifiers {
5750
candidate, err := classifier.Classify(ctx, repository, descriptor, siblings)

src/controller/artifact/manifest/classifier_test.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,14 @@ func (s stubClassifier) Classify(_ context.Context, _ string, _ v1.Descriptor, _
3939
return s.candidate, s.err
4040
}
4141

42-
// withClassifiers swaps the package chain for the duration of a test.
4342
func withClassifiers(t *testing.T, classifiers ...ChildClassifier) {
4443
t.Helper()
4544
original := ChildClassifiers
4645
ChildClassifiers = classifiers
4746
t.Cleanup(func() { ChildClassifiers = original })
4847
}
4948

50-
// TestDefaultChainIsRegistered pins the bootstrap. An empty chain means index
51-
// children are never classified as accessories, which is silent data loss
52-
// rather than a visible failure.
49+
// An empty chain is silent data loss, not a visible failure, so pin the bootstrap.
5350
func TestDefaultChainIsRegistered(t *testing.T) {
5451
require.NotEmpty(t, ChildClassifiers, "the package init() must register at least one classifier")
5552

@@ -62,9 +59,7 @@ func TestDefaultChainIsRegistered(t *testing.T) {
6259
}
6360
require.NotNil(t, attestation, "the in-toto attestation classifier must be registered by default")
6461

65-
// init() captures pkg.ArtifactMgr and registry.Cli. Go initialises imported
66-
// packages first, so both must already be set; a nil here would mean silent
67-
// nil-pointer panics on every index push.
62+
// init() captures these globals, so it relies on their packages initialising first
6863
assert.NotNil(t, attestation.artMgr, "pkg.ArtifactMgr must be initialised before this package's init()")
6964
assert.NotNil(t, attestation.regCli, "registry.Cli must be initialised before this package's init()")
7065
}
@@ -130,7 +125,6 @@ func TestRegisterChildClassifierDropsNil(t *testing.T) {
130125
RegisterChildClassifier(nil)
131126
assert.Empty(t, ChildClassifiers, "a nil classifier would panic on every index push")
132127

133-
// the chain stays usable
134128
candidate, err := ClassifyChild(context.Background(), "library/hello", v1.Descriptor{}, nil)
135129
require.NoError(t, err)
136130
assert.Nil(t, candidate)

0 commit comments

Comments
 (0)