Skip to content

Commit

Permalink
Apply review suggestions
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Löwel <[email protected]>
  • Loading branch information
ChristianLoewel committed May 24, 2023
1 parent ffefb50 commit 5c15476
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
1 change: 1 addition & 0 deletions apis/goharbor.io/v1beta1/harborproject_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
// +resource:path=harborproject
// +kubebuilder:subresource:status
// +kubebuilder:resource:categories="goharbor",shortName="hp"
// +kubebuilder:printcolumn:name="ProjectName",type=string,JSONPath=`.spec.projectName`,description="Project name in Harbor"
// +kubebuilder:printcolumn:name="HarborServerConfig",type=string,JSONPath=`.spec.harborServerConfig`,description="HarborServerConfiguration name"
// +kubebuilder:printcolumn:name="Status",type=string,JSONPath=`.status.status`,description="HarborProject status"
// +kubebuilder:printcolumn:name="Age",type=date,JSONPath=`.metadata.creationTimestamp`,description="Timestamp representing the server time when this object was created. It is not guaranteed to be set in happens-before order across separate operations. Clients may not set this value. It is represented in RFC3339 form and is in UTC."
Expand Down
21 changes: 11 additions & 10 deletions controllers/goharbor/project/memberships.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ type memberDifferences struct {
}

const (
harborAPIProjectAdmin int = 1
harborAPIDeveloper int = 2
harborAPIGuest int = 3
harborAPIMaintainer int = 4
harborAPIProjectAdminRole int = 1
harborAPIDeveloperRole int = 2
harborAPIGuestRole int = 3
harborAPIMaintainerRole int = 4
)

// map string role mappings from CRD to int for Harbor API.
var memberRoleMapping = map[string]int{
"projectAdmin": harborAPIProjectAdmin,
"developer": harborAPIDeveloper,
"guest": harborAPIGuest,
"maintainer": harborAPIMaintainer,
"projectAdmin": harborAPIProjectAdminRole,
"developer": harborAPIDeveloperRole,
"guest": harborAPIGuestRole,
"maintainer": harborAPIMaintainerRole,
}

func (r *Reconciler) reconcileMembership(hp *goharborv1.HarborProject, log logr.Logger) (err error) { //nolint:funlen
Expand Down Expand Up @@ -120,7 +120,7 @@ func findDifferences(currentMemberships []*models.ProjectMemberEntity, desiredMe
return getProjectMemberName(&desiredMemberships[i]) < getProjectMemberName(&desiredMemberships[j])
})

// search all currentMembers in desiredMembers. If found, mark for update if necessary, if not mark for deletion.
// search all currentMembers in desiredMembers. If found, mark for update or deletion if necessary.
for _, currentMember := range currentMemberships {
idx := sort.Search(desiredMembershipsCnt, func(i int) bool {
return getProjectMemberName(&desiredMemberships[i]) >= currentMember.EntityName
Expand Down Expand Up @@ -230,7 +230,8 @@ func createDesiredMemberships(definedMemberships []*goharborv1.HarborProjectMemb
return desiredMembers, nil
}

// marshal all objects into json and hash it.
// marshal all current and desired memberships into json and hash them.
// this hash is used to efficiently find differences later on.
func generateHash(currentMemberships []*models.ProjectMemberEntity, desiredMemberships []*goharborv1.HarborProjectMember) (string, error) {
type membershipComp struct {
CurrentMemberships []*models.ProjectMemberEntity
Expand Down
21 changes: 9 additions & 12 deletions pkg/rest/v2/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,8 @@ func (c *Client) EnsureProject(name string) (int64, error) {
return int64(p.ProjectID), nil
}

if err != nil {
if !strings.Contains(err.Error(), "no project with name") {
return 0, errors.Errorf("error when getting project %s: %s", name, err)
}
if !strings.Contains(err.Error(), "no project with name") {
return 0, errors.Errorf("error when getting project %s: %s", name, err)
}

fmt.Println("creating project since target project doesn't exist")
Expand Down Expand Up @@ -330,7 +328,7 @@ func (c *Client) GetProjectRequest(hp *goharborv1beta1.HarborProject) (*models.P
hp.Spec.HarborProjectMetadata = &goharborv1beta1.HarborProjectMetadata{}
}

model := &models.ProjectReq{
projectReq := &models.ProjectReq{
ProjectName: hp.Spec.ProjectName,
CVEAllowlist: &models.CVEAllowlist{},
Metadata: &models.ProjectMetadata{
Expand All @@ -339,24 +337,23 @@ func (c *Client) GetProjectRequest(hp *goharborv1beta1.HarborProject) (*models.P
EnableContentTrustCosign: utilstring.Bool2Str(hp.Spec.HarborProjectMetadata.EnableContentTrustCosign),
PreventVul: utilstring.Bool2Str(hp.Spec.HarborProjectMetadata.PreventVulnerable),
Public: *utilstring.Bool2Str(hp.Spec.HarborProjectMetadata.Public),
Severity: &hp.Spec.HarborProjectMetadata.Severity,
},
}

// create objects for Harbor API from CVE List in Custom Resource
for _, cve := range hp.Spec.CveAllowList {
model.CVEAllowlist.Items = append(model.CVEAllowlist.Items, &models.CVEAllowlistItem{CVEID: cve})
projectReq.CVEAllowlist.Items = append(projectReq.CVEAllowlist.Items, &models.CVEAllowlistItem{CVEID: cve})
}

// if ReuseSysCveAllowlist is not explicitly set, set it depending on if project cve allow list is configured
if hp.Spec.HarborProjectMetadata.ReuseSysCveAllowlist == nil {
reuse := len(hp.Spec.CveAllowList) == 0
model.Metadata.ReuseSysCVEAllowlist = utilstring.Bool2Str(&reuse)
projectReq.Metadata.ReuseSysCVEAllowlist = utilstring.Bool2Str(&reuse)
} else {
model.Metadata.ReuseSysCVEAllowlist = utilstring.Bool2Str(hp.Spec.HarborProjectMetadata.ReuseSysCveAllowlist)
projectReq.Metadata.ReuseSysCVEAllowlist = utilstring.Bool2Str(hp.Spec.HarborProjectMetadata.ReuseSysCveAllowlist)
}

model.Metadata.Severity = &hp.Spec.HarborProjectMetadata.Severity

// if set, parse human readable storage quota (e.g. "10Gi") into byte int64 for Harbor API
if hp.Spec.StorageQuota != "" {
parsedQuota, err := resource.ParseQuantity(hp.Spec.StorageQuota)
Expand All @@ -365,8 +362,8 @@ func (c *Client) GetProjectRequest(hp *goharborv1beta1.HarborProject) (*models.P
}

value := parsedQuota.Value()
model.StorageLimit = &value
projectReq.StorageLimit = &value
}

return model, nil
return projectReq, nil
}

0 comments on commit 5c15476

Please sign in to comment.