Skip to content

Project owner canonicalization to be moved from conversion to REST strategy #14726

@vpnachev

Description

@vpnachev

How to categorize this issue?
/area quality
/kind enhancement

What would you like to be added:
The Project API has an owner field, in practice the owner subject could be also a project member with additional roles assigned. There is some code canonicalizing the resource, but it is placed in the version <-> internal conversion methods

if owner := out.Owner; owner != nil {
outer:
for i, member := range out.Members {
if member.Name == owner.Name && member.APIGroup == owner.APIGroup && member.Kind == owner.Kind {
// add owner role to the current project's owner if not present
for _, role := range member.Roles {
if role == core.ProjectMemberOwner {
continue outer
}
}
out.Members[i].Roles = append(out.Members[i].Roles, core.ProjectMemberOwner)
} else {
// delete owner role from all other members
out.Members[i].Roles = removeRoleFromRoles(member.Roles, ProjectMemberOwner)
}
}
}

if owner := out.Owner; owner != nil {
outer:
for i, member := range out.Members {
if member.Name == owner.Name && member.APIGroup == owner.APIGroup && member.Kind == owner.Kind {
// add owner role to the current project's owner if not present
if member.Role == core.ProjectMemberOwner {
// remove it from owners list if present
out.Members[i].Roles = removeRoleFromRoles(member.Roles, ProjectMemberOwner)
continue outer
}
for _, role := range member.Roles {
if role == ProjectMemberOwner {
continue outer
}
}
if out.Members[i].Role == "" {
out.Members[i].Role = core.ProjectMemberOwner
} else {
out.Members[i].Roles = append(out.Members[i].Roles, core.ProjectMemberOwner)
}
} else {
// delete owner role from all other members
out.Members[i].Roles = removeRoleFromRoles(member.Roles, ProjectMemberOwner)
if member.Role == ProjectMemberOwner {
if len(out.Members[i].Roles) == 0 {
out.Members[i].Role = ""
} else {
out.Members[i].Role = out.Members[i].Roles[0]
if len(out.Members[i].Roles) > 1 {
out.Members[i].Roles = out.Members[i].Roles[1:]
} else {
out.Members[i].Roles = nil
}
}
}
}
}
}

A better place for such code is the REST strategy, i.e. in the Canonicalize method

func (projectStrategy) Canonicalize(_ runtime.Object) {

Why is this needed:
Improve code quality and readability.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/qualityOutput qualification (tests, checks, scans, automation in general, etc.) relatedkind/enhancementEnhancement, improvement, extension

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions