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: enhance the replication webhook payload #19433

Merged
merged 1 commit into from
Oct 23, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@
Type: task.ResourceType,
Status: task.Status,
NameAndTag: nameAndTag,
References: strings.Split(task.References, ","),

Check warning on line 194 in src/controller/event/handler/webhook/artifact/replication.go

View check run for this annotation

Codecov / codecov/patch

src/controller/event/handler/webhook/artifact/replication.go#L194

Added line #L194 was not covered by tests
}
payload.EventData.Replication.SuccessfulArtifact = []*ctlModel.ArtifactInfo{succeedArtifact}
}
Expand All @@ -199,6 +200,7 @@
Type: task.ResourceType,
Status: task.Status,
NameAndTag: nameAndTag,
References: strings.Split(task.References, ","),

Check warning on line 203 in src/controller/event/handler/webhook/artifact/replication.go

View check run for this annotation

Codecov / codecov/patch

src/controller/event/handler/webhook/artifact/replication.go#L203

Added line #L203 was not covered by tests
}
payload.EventData.Replication.FailedArtifact = []*ctlModel.ArtifactInfo{failedArtifact}
}
Expand Down
9 changes: 5 additions & 4 deletions src/controller/event/model/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ type Replication struct {

// ArtifactInfo describe info of artifact
type ArtifactInfo struct {
Type string `json:"type"`
Status string `json:"status"`
NameAndTag string `json:"name_tag"`
FailReason string `json:"fail_reason,omitempty"`
Type string `json:"type"`
Status string `json:"status"`
NameAndTag string `json:"name_tag"`
References []string `json:"references"`
FailReason string `json:"fail_reason,omitempty"`
}

// ReplicationResource describes replication resource info
Expand Down
1 change: 1 addition & 0 deletions src/controller/replication/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ func convertTask(task *task.Task) *Task {
ResourceType: task.GetStringFromExtraAttrs("resource_type"),
SourceResource: task.GetStringFromExtraAttrs("source_resource"),
DestinationResource: task.GetStringFromExtraAttrs("destination_resource"),
References: task.GetStringFromExtraAttrs("references"),
Operation: task.GetStringFromExtraAttrs("operation"),
JobID: task.JobID,
CreationTime: task.CreationTime,
Expand Down
4 changes: 4 additions & 0 deletions src/controller/replication/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ func (r *replicationTestSuite) TestListTasks() {
"resource_type": "artifact",
"source_resource": "library/hello-world",
"destination_resource": "library/hello-world",
"references": "v1,v2,v3",
"operation": "copy",
},
},
Expand All @@ -211,6 +212,7 @@ func (r *replicationTestSuite) TestListTasks() {
r.Equal("artifact", tasks[0].ResourceType)
r.Equal("library/hello-world", tasks[0].SourceResource)
r.Equal("library/hello-world", tasks[0].DestinationResource)
r.Equal("v1,v2,v3", tasks[0].References)
r.Equal("copy", tasks[0].Operation)
r.taskMgr.AssertExpectations(r.T())
}
Expand All @@ -225,6 +227,7 @@ func (r *replicationTestSuite) TestGetTask() {
"resource_type": "artifact",
"source_resource": "library/hello-world",
"destination_resource": "library/hello-world",
"references": "v1,v2,v3",
"operation": "copy",
},
},
Expand All @@ -236,6 +239,7 @@ func (r *replicationTestSuite) TestGetTask() {
r.Equal("artifact", task.ResourceType)
r.Equal("library/hello-world", task.SourceResource)
r.Equal("library/hello-world", task.DestinationResource)
r.Equal("v1,v2,v3", task.References)
r.Equal("copy", task.Operation)
r.taskMgr.AssertExpectations(r.T())
}
Expand Down
3 changes: 2 additions & 1 deletion src/controller/replication/flow/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ func (c *copyFlow) createTasks(ctx context.Context, srcResources, dstResources [
"operation": "copy",
"resource_type": string(srcResource.Type),
"source_resource": getResourceName(srcResource),
"destination_resource": getResourceName(dstResource)}); err != nil {
"destination_resource": getResourceName(dstResource),
"references": getResourceReferences(dstResource)}); err != nil {
return err
}

Expand Down
3 changes: 2 additions & 1 deletion src/controller/replication/flow/deletion.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ func (d *deletionFlow) createTasks(ctx context.Context, srcResources, dstResourc
"operation": operation,
"resource_type": string(resource.Type),
"source_resource": getResourceName(resource),
"destination_resource": getResourceName(dstResources[i])}); err != nil {
"destination_resource": getResourceName(dstResources[i]),
"references": getResourceReferences(dstResources[i])}); err != nil {
return err
}
}
Expand Down
31 changes: 31 additions & 0 deletions src/controller/replication/flow/stage.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,37 @@
return fmt.Sprintf("%s [%d item(s) in total]", meta.Repository.Name, n)
}

// getResourceReferences gets the string lists of the resource reference, use tag name first or digest if no tag
// e.g v1,v2,dev,sha256:9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08
func getResourceReferences(res *model.Resource) string {
if res == nil {
return ""
}

Check warning on line 160 in src/controller/replication/flow/stage.go

View check run for this annotation

Codecov / codecov/patch

src/controller/replication/flow/stage.go#L159-L160

Added lines #L159 - L160 were not covered by tests
meta := res.Metadata
if meta == nil {
return ""
}

Check warning on line 164 in src/controller/replication/flow/stage.go

View check run for this annotation

Codecov / codecov/patch

src/controller/replication/flow/stage.go#L163-L164

Added lines #L163 - L164 were not covered by tests

references := make([]string, 0)
if len(meta.Artifacts) > 0 {
for _, artifact := range meta.Artifacts {
// contains tags
if len(artifact.Tags) > 0 {
references = append(references, artifact.Tags...)
continue
}
// contains no tag, use digest
if len(artifact.Digest) > 0 {
references = append(references, artifact.Digest)
}

Check warning on line 177 in src/controller/replication/flow/stage.go

View check run for this annotation

Codecov / codecov/patch

src/controller/replication/flow/stage.go#L175-L177

Added lines #L175 - L177 were not covered by tests
}
} else {
references = append(references, meta.Vtags...)
}

return strings.Join(references, ",")
}

// repository:a/b/c/image namespace:n replaceCount: -1 -> n/image
// repository:a/b/c/image namespace:n replaceCount: 0 -> n/a/b/c/image
// repository:a/b/c/image namespace:n replaceCount: 1 -> n/b/c/image
Expand Down
1 change: 1 addition & 0 deletions src/controller/replication/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type Task struct {
ResourceType string
SourceResource string
DestinationResource string
References string
Operation string
JobID string
CreationTime time.Time
Expand Down
Loading