Skip to content

Commit

Permalink
Add support for setting authLabels.
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasBergmeier6176 committed Jun 26, 2023
1 parent 2cb1ba9 commit 5d5748f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 11 deletions.
7 changes: 4 additions & 3 deletions api/v1/users_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ type UserSpec struct {
}

type User struct {
Name string `json:"name,omitempty"`
Login string `json:"login"`
Email string `json:"email,omitempty"`
Name string `json:"name,omitempty"`
Login string `json:"login"`
Email string `json:"email,omitempty"`
AuthLabels []string `json:"authLabels,omitempty"`
}

// UserStatus defines the observed state of User
Expand Down
9 changes: 8 additions & 1 deletion api/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions config/crd/bases/grafana.abergmeier.github.io_users.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ spec:
users:
items:
properties:
authLabels:
items:
type: string
type: array
email:
type: string
login:
Expand Down
36 changes: 29 additions & 7 deletions internal/controller/users_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"math/rand"
"sort"
"sync"
"time"

Expand Down Expand Up @@ -271,10 +272,11 @@ func calculateUserBuckets(client *gapi.Client, adminLogin string, desired map[em
continue
}
currentUserMap[email(ou.Email)] = gapi.User{
ID: ou.ID,
Email: ou.Email,
Name: ou.Name,
Login: ou.Login,
ID: ou.ID,
Email: ou.Email,
Name: ou.Name,
Login: ou.Login,
AuthLabels: ou.AuthLabels,
}
obsoleteIds[ou.ID] = struct{}{}
}
Expand All @@ -283,9 +285,10 @@ func calculateUserBuckets(client *gapi.Client, adminLogin string, desired map[em
alreadyPresentUser, ok := currentUserMap[email]
if !ok {
users.missing = append(users.missing, gapi.User{
Email: string(email),
Name: uc.Name,
Login: uc.Login,
Email: string(email),
Name: uc.Name,
Login: uc.Login,
AuthLabels: uc.AuthLabels,
})
continue
}
Expand All @@ -297,6 +300,7 @@ func calculateUserBuckets(client *gapi.Client, adminLogin string, desired map[em
changed.Email = uc.Email
changed.Name = uc.Name
changed.Login = uc.Login
changed.AuthLabels = uc.AuthLabels
users.change = append(users.change, changed)
}
}
Expand All @@ -309,6 +313,24 @@ func calculateUserBuckets(client *gapi.Client, adminLogin string, desired map[em
}

func needsChange(lhs *grafanav1.User, current *gapi.User) bool {
if len(lhs.AuthLabels) != len(current.AuthLabels) {
return true
}

if len(lhs.AuthLabels) == 0 {
sort.Strings(lhs.AuthLabels)
}

if len(current.AuthLabels) == 0 {
sort.Strings(current.AuthLabels)
}

for i := 0; i != len(lhs.AuthLabels); i++ {
if lhs.AuthLabels[i] != current.AuthLabels[i] {
return true
}
}

return lhs.Email != current.Email || lhs.Login != current.Login || lhs.Name != current.Name
}

Expand Down

0 comments on commit 5d5748f

Please sign in to comment.