Skip to content

Commit

Permalink
chore: enable receiver-naming from revive
Browse files Browse the repository at this point in the history
Signed-off-by: Matthieu MOREL <[email protected]>
  • Loading branch information
mmorel-35 committed Jan 4, 2025
1 parent e66068c commit a5a3791
Show file tree
Hide file tree
Showing 8 changed files with 428 additions and 429 deletions.
1 change: 0 additions & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ linters-settings:
- name: range
# receiver names in a method should reflect the struct name (p for Person, for example)
- name: receiver-naming
disabled: true
# redefining built in names (true, false, append, make) can lead to bugs very difficult to detect.
- name: redefines-builtin-id
disabled: true
Expand Down
24 changes: 12 additions & 12 deletions controller/sharding/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,20 @@ func NewClusterSharding(_ db.ArgoDB, shard, replicas int, shardingAlgorithm stri
}

// IsManagedCluster returns whether or not the cluster should be processed by a given shard.
func (s *ClusterSharding) IsManagedCluster(c *v1alpha1.Cluster) bool {
s.lock.RLock()
defer s.lock.RUnlock()
func (sharding *ClusterSharding) IsManagedCluster(c *v1alpha1.Cluster) bool {
sharding.lock.RLock()
defer sharding.lock.RUnlock()
if c == nil { // nil cluster (in-cluster) is always managed by current clusterShard
return true
}
clusterShard := 0
if shard, ok := s.Shards[c.Server]; ok {
if shard, ok := sharding.Shards[c.Server]; ok {
clusterShard = shard
} else {
log.Warnf("The cluster %s has no assigned shard.", c.Server)
}
log.Debugf("Checking if cluster %s with clusterShard %d should be processed by shard %d", c.Server, clusterShard, s.Shard)
return clusterShard == s.Shard
log.Debugf("Checking if cluster %s with clusterShard %d should be processed by shard %d", c.Server, clusterShard, sharding.Shard)
return clusterShard == sharding.Shard
}

func (sharding *ClusterSharding) Init(clusters *v1alpha1.ClusterList, apps *v1alpha1.ApplicationList) {
Expand Down Expand Up @@ -187,22 +187,22 @@ func hasShardingUpdates(old, new *v1alpha1.Cluster) bool {
}

// A read lock should be acquired before calling getClusterAccessor.
func (d *ClusterSharding) getClusterAccessor() clusterAccessor {
func (sharding *ClusterSharding) getClusterAccessor() clusterAccessor {
return func() []*v1alpha1.Cluster {
// no need to lock, as this is only called from the updateDistribution function
clusters := make([]*v1alpha1.Cluster, 0, len(d.Clusters))
for _, c := range d.Clusters {
clusters := make([]*v1alpha1.Cluster, 0, len(sharding.Clusters))
for _, c := range sharding.Clusters {
clusters = append(clusters, c)
}
return clusters
}
}

// A read lock should be acquired before calling getAppAccessor.
func (d *ClusterSharding) getAppAccessor() appAccessor {
func (sharding *ClusterSharding) getAppAccessor() appAccessor {
return func() []*v1alpha1.Application {
apps := make([]*v1alpha1.Application, 0, len(d.Apps))
for _, a := range d.Apps {
apps := make([]*v1alpha1.Application, 0, len(sharding.Apps))
for _, a := range sharding.Apps {
apps = append(apps, a)
}
return apps
Expand Down
18 changes: 9 additions & 9 deletions hack/gen-resources/generators/application_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func NewApplicationGenerator(argoClientSet *appclientset.Clientset, clientSet *k
return &ApplicationGenerator{argoClientSet, clientSet, db}
}

func (pg *ApplicationGenerator) buildRandomSource(repositories []*v1alpha1.Repository) (*v1alpha1.ApplicationSource, error) {
func (ag *ApplicationGenerator) buildRandomSource(repositories []*v1alpha1.Repository) (*v1alpha1.ApplicationSource, error) {
seed := rand.New(rand.NewSource(time.Now().Unix()))
repoNumber := seed.Int() % len(repositories)
return &v1alpha1.ApplicationSource{
Expand All @@ -47,7 +47,7 @@ func (ag *ApplicationGenerator) buildSource(opts *util.GenerateOpts, repositorie
return ag.buildRandomSource(repositories)
}

func (pg *ApplicationGenerator) buildRandomDestination(opts *util.GenerateOpts, clusters []v1alpha1.Cluster) (*v1alpha1.ApplicationDestination, error) {
func (ag *ApplicationGenerator) buildRandomDestination(opts *util.GenerateOpts, clusters []v1alpha1.Cluster) (*v1alpha1.ApplicationDestination, error) {
seed := rand.New(rand.NewSource(time.Now().Unix()))
clusterNumber := seed.Int() % len(clusters)
return &v1alpha1.ApplicationDestination{
Expand All @@ -64,25 +64,25 @@ func (ag *ApplicationGenerator) buildDestination(opts *util.GenerateOpts, cluste
return ag.buildRandomDestination(opts, clusters)
}

func (pg *ApplicationGenerator) Generate(opts *util.GenerateOpts) error {
settingsMgr := settings.NewSettingsManager(context.TODO(), pg.clientSet, opts.Namespace)
repositories, err := db.NewDB(opts.Namespace, settingsMgr, pg.clientSet).ListRepositories(context.TODO())
func (ag *ApplicationGenerator) Generate(opts *util.GenerateOpts) error {
settingsMgr := settings.NewSettingsManager(context.TODO(), ag.clientSet, opts.Namespace)
repositories, err := db.NewDB(opts.Namespace, settingsMgr, ag.clientSet).ListRepositories(context.TODO())
if err != nil {
return err
}
clusters, err := db.NewDB(opts.Namespace, settingsMgr, pg.clientSet).ListClusters(context.TODO())
clusters, err := db.NewDB(opts.Namespace, settingsMgr, ag.clientSet).ListClusters(context.TODO())
if err != nil {
return err
}
applications := pg.argoClientSet.ArgoprojV1alpha1().Applications(opts.Namespace)
applications := ag.argoClientSet.ArgoprojV1alpha1().Applications(opts.Namespace)
for i := 0; i < opts.ApplicationOpts.Samples; i++ {
log.Printf("Generate application #%v", i)
source, err := pg.buildSource(opts, repositories)
source, err := ag.buildSource(opts, repositories)
if err != nil {
return err
}
log.Printf("Pick source %q", source)
destination, err := pg.buildDestination(opts, clusters.Items)
destination, err := ag.buildDestination(opts, clusters.Items)
if err != nil {
return err
}
Expand Down
84 changes: 42 additions & 42 deletions pkg/apis/application/v1alpha1/app_project_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,19 @@ type AppProjectStatus struct {
}

// GetRoleByName returns the role in a project by the name with its index
func (p *AppProject) GetRoleByName(name string) (*ProjectRole, int, error) {
for i, role := range p.Spec.Roles {
func (proj *AppProject) GetRoleByName(name string) (*ProjectRole, int, error) {
for i, role := range proj.Spec.Roles {
if name == role.Name {
return &role, i, nil
}
}
return nil, -1, fmt.Errorf("role '%s' does not exist in project '%s'", name, p.Name)
return nil, -1, fmt.Errorf("role '%s' does not exist in project '%s'", name, proj.Name)
}

// GetJWTTokenFromSpec looks up the index of a JWTToken in a project by id (new token), if not then by the issue at time (old token)
func (p *AppProject) GetJWTTokenFromSpec(roleName string, issuedAt int64, id string) (*JWTToken, int, error) {
func (proj *AppProject) GetJWTTokenFromSpec(roleName string, issuedAt int64, id string) (*JWTToken, int, error) {
// This is for backward compatibility. In the oder version, JWTTokens are stored under spec.role
role, _, err := p.GetRoleByName(roleName)
role, _, err := proj.GetRoleByName(roleName)
if err != nil {
return nil, -1, err
}
Expand All @@ -107,46 +107,46 @@ func (p *AppProject) GetJWTTokenFromSpec(roleName string, issuedAt int64, id str
}
}

return nil, -1, fmt.Errorf("JWT token for role '%s' issued at '%d' does not exist in project '%s'", role.Name, issuedAt, p.Name)
return nil, -1, fmt.Errorf("JWT token for role '%s' issued at '%d' does not exist in project '%s'", role.Name, issuedAt, proj.Name)
}

// GetJWTToken looks up the index of a JWTToken in a project by id (new token), if not then by the issue at time (old token)
func (p *AppProject) GetJWTToken(roleName string, issuedAt int64, id string) (*JWTToken, int, error) {
func (proj *AppProject) GetJWTToken(roleName string, issuedAt int64, id string) (*JWTToken, int, error) {
// This is for newer version, JWTTokens are stored under status
if id != "" {
for i, token := range p.Status.JWTTokensByRole[roleName].Items {
for i, token := range proj.Status.JWTTokensByRole[roleName].Items {
if id == token.ID {
return &token, i, nil
}
}
}

if issuedAt != -1 {
for i, token := range p.Status.JWTTokensByRole[roleName].Items {
for i, token := range proj.Status.JWTTokensByRole[roleName].Items {
if issuedAt == token.IssuedAt {
return &token, i, nil
}
}
}

return nil, -1, fmt.Errorf("JWT token for role '%s' issued at '%d' does not exist in project '%s'", roleName, issuedAt, p.Name)
return nil, -1, fmt.Errorf("JWT token for role '%s' issued at '%d' does not exist in project '%s'", roleName, issuedAt, proj.Name)
}

// RemoveJWTToken removes the specified JWT from an AppProject
func (p AppProject) RemoveJWTToken(roleIndex int, issuedAt int64, id string) error {
roleName := p.Spec.Roles[roleIndex].Name
func (proj AppProject) RemoveJWTToken(roleIndex int, issuedAt int64, id string) error {
roleName := proj.Spec.Roles[roleIndex].Name
// For backward compatibility
_, jwtTokenIndex, err1 := p.GetJWTTokenFromSpec(roleName, issuedAt, id)
_, jwtTokenIndex, err1 := proj.GetJWTTokenFromSpec(roleName, issuedAt, id)
if err1 == nil {
p.Spec.Roles[roleIndex].JWTTokens[jwtTokenIndex] = p.Spec.Roles[roleIndex].JWTTokens[len(p.Spec.Roles[roleIndex].JWTTokens)-1]
p.Spec.Roles[roleIndex].JWTTokens = p.Spec.Roles[roleIndex].JWTTokens[:len(p.Spec.Roles[roleIndex].JWTTokens)-1]
proj.Spec.Roles[roleIndex].JWTTokens[jwtTokenIndex] = proj.Spec.Roles[roleIndex].JWTTokens[len(proj.Spec.Roles[roleIndex].JWTTokens)-1]
proj.Spec.Roles[roleIndex].JWTTokens = proj.Spec.Roles[roleIndex].JWTTokens[:len(proj.Spec.Roles[roleIndex].JWTTokens)-1]
}

// New location for storing JWTToken
_, jwtTokenIndex, err2 := p.GetJWTToken(roleName, issuedAt, id)
_, jwtTokenIndex, err2 := proj.GetJWTToken(roleName, issuedAt, id)
if err2 == nil {
p.Status.JWTTokensByRole[roleName].Items[jwtTokenIndex] = p.Status.JWTTokensByRole[roleName].Items[len(p.Status.JWTTokensByRole[roleName].Items)-1]
p.Status.JWTTokensByRole[roleName] = JWTTokens{Items: p.Status.JWTTokensByRole[roleName].Items[:len(p.Status.JWTTokensByRole[roleName].Items)-1]}
proj.Status.JWTTokensByRole[roleName].Items[jwtTokenIndex] = proj.Status.JWTTokensByRole[roleName].Items[len(proj.Status.JWTTokensByRole[roleName].Items)-1]
proj.Status.JWTTokensByRole[roleName] = JWTTokens{Items: proj.Status.JWTTokensByRole[roleName].Items[:len(proj.Status.JWTTokensByRole[roleName].Items)-1]}
}

if err1 == nil || err2 == nil {
Expand All @@ -159,8 +159,8 @@ func (p AppProject) RemoveJWTToken(roleIndex int, issuedAt int64, id string) err
}

// TODO: document this method
func (p *AppProject) ValidateJWTTokenID(roleName string, id string) error {
role, _, err := p.GetRoleByName(roleName)
func (proj *AppProject) ValidateJWTTokenID(roleName string, id string) error {
role, _, err := proj.GetRoleByName(roleName)
if err != nil {
return err
}
Expand All @@ -175,9 +175,9 @@ func (p *AppProject) ValidateJWTTokenID(roleName string, id string) error {
return nil
}

func (p *AppProject) ValidateProject() error {
func (proj *AppProject) ValidateProject() error {
destKeys := make(map[string]bool)
for _, dest := range p.Spec.Destinations {
for _, dest := range proj.Spec.Destinations {
if dest.Name == "!*" {
return status.Errorf(codes.InvalidArgument, "name has an invalid format, '!*'")
}
Expand All @@ -202,15 +202,15 @@ func (p *AppProject) ValidateProject() error {
}

srcNamespaces := make(map[string]bool)
for _, ns := range p.Spec.SourceNamespaces {
for _, ns := range proj.Spec.SourceNamespaces {
if _, ok := srcNamespaces[ns]; ok {
return status.Errorf(codes.InvalidArgument, "source namespace '%s' already added", ns)
}
srcNamespaces[ns] = true
}

srcRepos := make(map[string]bool)
for _, src := range p.Spec.SourceRepos {
for _, src := range proj.Spec.SourceRepos {
if src == "!*" {
return status.Errorf(codes.InvalidArgument, "source repository has an invalid format, '!*'")
}
Expand All @@ -222,7 +222,7 @@ func (p *AppProject) ValidateProject() error {
}

roleNames := make(map[string]bool)
for _, role := range p.Spec.Roles {
for _, role := range proj.Spec.Roles {
if _, ok := roleNames[role.Name]; ok {
return status.Errorf(codes.AlreadyExists, "role '%s' already exists", role.Name)
}
Expand All @@ -234,7 +234,7 @@ func (p *AppProject) ValidateProject() error {
if _, ok := existingPolicies[policy]; ok {
return status.Errorf(codes.AlreadyExists, "policy '%s' already exists for role '%s'", policy, role.Name)
}
if err := validatePolicy(p.Name, role.Name, policy); err != nil {
if err := validatePolicy(proj.Name, role.Name, policy); err != nil {
return err
}
existingPolicies[policy] = true
Expand All @@ -252,9 +252,9 @@ func (p *AppProject) ValidateProject() error {
roleNames[role.Name] = true
}

if p.Spec.SyncWindows.HasWindows() {
if proj.Spec.SyncWindows.HasWindows() {
existingWindows := make(map[string]bool)
for _, window := range p.Spec.SyncWindows {
for _, window := range proj.Spec.SyncWindows {
if window == nil {
continue
}
Expand All @@ -273,7 +273,7 @@ func (p *AppProject) ValidateProject() error {
}

destServiceAccts := make(map[string]bool)
for _, destServiceAcct := range p.Spec.DestinationServiceAccounts {
for _, destServiceAcct := range proj.Spec.DestinationServiceAccounts {
if strings.Contains(destServiceAcct.Server, "!") {
return status.Errorf(codes.InvalidArgument, "server has an invalid format, '%s'", destServiceAcct.Server)
}
Expand Down Expand Up @@ -308,8 +308,8 @@ func (p *AppProject) ValidateProject() error {
}

// AddGroupToRole adds an OIDC group to a role
func (p *AppProject) AddGroupToRole(roleName, group string) (bool, error) {
role, roleIndex, err := p.GetRoleByName(roleName)
func (proj *AppProject) AddGroupToRole(roleName, group string) (bool, error) {
role, roleIndex, err := proj.GetRoleByName(roleName)
if err != nil {
return false, err
}
Expand All @@ -319,38 +319,38 @@ func (p *AppProject) AddGroupToRole(roleName, group string) (bool, error) {
}
}
role.Groups = append(role.Groups, group)
p.Spec.Roles[roleIndex] = *role
proj.Spec.Roles[roleIndex] = *role
return true, nil
}

// RemoveGroupFromRole removes an OIDC group from a role
func (p *AppProject) RemoveGroupFromRole(roleName, group string) (bool, error) {
role, roleIndex, err := p.GetRoleByName(roleName)
func (proj *AppProject) RemoveGroupFromRole(roleName, group string) (bool, error) {
role, roleIndex, err := proj.GetRoleByName(roleName)
if err != nil {
return false, err
}
for i, roleGroup := range role.Groups {
if group == roleGroup {
role.Groups = append(role.Groups[:i], role.Groups[i+1:]...)
p.Spec.Roles[roleIndex] = *role
proj.Spec.Roles[roleIndex] = *role
return true, nil
}
}
return false, nil
}

// NormalizePolicies normalizes the policies in the project
func (p *AppProject) NormalizePolicies() {
for i, role := range p.Spec.Roles {
func (proj *AppProject) NormalizePolicies() {
for i, role := range proj.Spec.Roles {
var normalizedPolicies []string
for _, policy := range role.Policies {
normalizedPolicies = append(normalizedPolicies, p.normalizePolicy(policy))
normalizedPolicies = append(normalizedPolicies, proj.normalizePolicy(policy))
}
p.Spec.Roles[i].Policies = normalizedPolicies
proj.Spec.Roles[i].Policies = normalizedPolicies
}
}

func (p *AppProject) normalizePolicy(policy string) string {
func (proj *AppProject) normalizePolicy(policy string) string {
policyComponents := strings.Split(policy, ",")
normalizedPolicy := ""
for _, component := range policyComponents {
Expand Down Expand Up @@ -597,10 +597,10 @@ func jwtTokensCombine(tokens1 []JWTToken, tokens2 []JWTToken) []JWTToken {
// Applications in the installation namespace are always permitted. Also, at
// application creation time, its namespace may yet be empty to indicate that
// the application will be created in the controller's namespace.
func (p AppProject) IsAppNamespacePermitted(app *Application, controllerNs string) bool {
func (proj AppProject) IsAppNamespacePermitted(app *Application, controllerNs string) bool {
if app.Namespace == "" || app.Namespace == controllerNs {
return true
}

return glob.MatchStringInList(p.Spec.SourceNamespaces, app.Namespace, glob.REGEXP)
return glob.MatchStringInList(proj.Spec.SourceNamespaces, app.Namespace, glob.REGEXP)
}
Loading

0 comments on commit a5a3791

Please sign in to comment.