From 5c15476a2a4f5a4424d587bfa5747a3e26814136 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=B6wel?= Date: Wed, 24 May 2023 13:09:41 +0200 Subject: [PATCH] Apply review suggestions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Christian Löwel --- .../v1beta1/harborproject_types.go | 1 + controllers/goharbor/project/memberships.go | 21 ++++++++++--------- pkg/rest/v2/project.go | 21 ++++++++----------- 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/apis/goharbor.io/v1beta1/harborproject_types.go b/apis/goharbor.io/v1beta1/harborproject_types.go index b664a6ebf..0d9365b80 100644 --- a/apis/goharbor.io/v1beta1/harborproject_types.go +++ b/apis/goharbor.io/v1beta1/harborproject_types.go @@ -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." diff --git a/controllers/goharbor/project/memberships.go b/controllers/goharbor/project/memberships.go index c1ba28ebb..7621fcbcb 100644 --- a/controllers/goharbor/project/memberships.go +++ b/controllers/goharbor/project/memberships.go @@ -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 @@ -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 @@ -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 diff --git a/pkg/rest/v2/project.go b/pkg/rest/v2/project.go index c427a0594..eafe46815 100644 --- a/pkg/rest/v2/project.go +++ b/pkg/rest/v2/project.go @@ -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") @@ -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{ @@ -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) @@ -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 }