@@ -10,14 +10,12 @@ import (
1010)
1111
1212type GrantAccessResult struct {
13- Added bool
14- Namespace string
15- Name string
16- Kind string
17- Detail string
13+ AlreadyAdded bool
14+ Namespace string
15+ Name string
1816}
1917
20- func GrantAccessToTopic (ctx context.Context , namespace , topicName string , acl nais_kafka.TopicACL ) (* GrantAccessResult , error ) {
18+ func GrantAccessToTopic (ctx context.Context , namespace , topicName string , newAcl nais_kafka.TopicACL ) (* GrantAccessResult , error ) {
2119 client := k8s .SetupControllerRuntimeClient ()
2220
2321 if err := validateNamespace (ctx , client , namespace ); err != nil {
@@ -26,37 +24,26 @@ func GrantAccessToTopic(ctx context.Context, namespace, topicName string, acl na
2624
2725 var topic nais_kafka.Topic
2826 if err := client .Get (ctx , ctrl.ObjectKey {Name : topicName , Namespace : namespace }, & topic ); err != nil {
29- return nil , fmt .Errorf ("validate topic: %w" , err )
27+ return nil , fmt .Errorf ("get topic: %w" , err )
3028 }
3129
32- // Default to read access if not specified
33- if acl .Access == "" {
34- acl .Access = "read"
35- }
36-
37- newACLs , added := ensureTopicACL (topic .Spec .ACL , acl )
38- if ! added {
30+ if checkIfAclInList (topic .Spec .ACL , newAcl ) {
3931 return & GrantAccessResult {
40- Added : false ,
41- Namespace : namespace ,
42- Name : topic .Name ,
43- Kind : topic .Kind ,
44- Detail : acl .Access ,
32+ AlreadyAdded : true ,
33+ Namespace : namespace ,
34+ Name : topicName ,
4535 }, nil
4636 }
47-
48- topic .Spec .ACL = newACLs
37+ topic .Spec .ACL = append (topic .Spec .ACL , newAcl )
4938
5039 if err := client .Update (ctx , & topic ); err != nil {
5140 return nil , fmt .Errorf ("update topic: %w" , err )
5241 }
5342
5443 return & GrantAccessResult {
55- Added : true ,
56- Namespace : namespace ,
57- Name : topic .Name ,
58- Kind : topic .Kind ,
59- Detail : acl .Access ,
44+ AlreadyAdded : false ,
45+ Namespace : namespace ,
46+ Name : topicName ,
6047 }, nil
6148}
6249
@@ -69,49 +56,43 @@ func GrantAccessToStream(ctx context.Context, namespace, streamName, userName st
6956
7057 var stream nais_kafka.Stream
7158 if err := client .Get (ctx , ctrl.ObjectKey {Name : streamName , Namespace : namespace }, & stream ); err != nil {
72- return nil , fmt .Errorf ("validate stream: %w" , err )
59+ return nil , fmt .Errorf ("get stream: %w" , err )
7360 }
7461
75- newUsers , added := ensureAdditionalStreamUser (stream .Spec .AdditionalUsers , userName )
76- if ! added {
62+ if checkIfUserInList (stream .Spec .AdditionalUsers , userName ) {
7763 return & GrantAccessResult {
78- Added : false ,
79- Namespace : namespace ,
80- Name : stream .Name ,
81- Kind : stream .Kind ,
82- Detail : userName ,
64+ AlreadyAdded : true ,
65+ Namespace : namespace ,
66+ Name : streamName ,
8367 }, nil
8468 }
85-
86- stream .Spec .AdditionalUsers = newUsers
69+ stream .Spec .AdditionalUsers = append (stream .Spec .AdditionalUsers , nais_kafka.AdditionalStreamUser {Username : userName })
8770
8871 if err := client .Update (ctx , & stream ); err != nil {
8972 return nil , fmt .Errorf ("update stream: %w" , err )
9073 }
9174
9275 return & GrantAccessResult {
93- Added : true ,
94- Namespace : namespace ,
95- Name : stream .Name ,
96- Kind : stream .Kind ,
97- Detail : userName ,
76+ AlreadyAdded : false ,
77+ Namespace : namespace ,
78+ Name : streamName ,
9879 }, nil
9980}
10081
101- func ensureTopicACL (existing []nais_kafka.TopicACL , wanted nais_kafka.TopicACL ) ([]nais_kafka. TopicACL , bool ) {
82+ func checkIfAclInList (existing []nais_kafka.TopicACL , wanted nais_kafka.TopicACL ) bool {
10283 for _ , e := range existing {
10384 if e .Team == wanted .Team && e .Application == wanted .Application && e .Access == wanted .Access {
104- return existing , false
85+ return true
10586 }
10687 }
107- return append ( existing , wanted ), true
88+ return false
10889}
10990
110- func ensureAdditionalStreamUser (existing []nais_kafka.AdditionalStreamUser , userName string ) ([]nais_kafka. AdditionalStreamUser , bool ) {
91+ func checkIfUserInList (existing []nais_kafka.AdditionalStreamUser , userName string ) bool {
11192 for _ , u := range existing {
11293 if u .Username == userName {
113- return existing , false
94+ return true
11495 }
11596 }
116- return append ( existing , nais_kafka. AdditionalStreamUser { Username : userName }), true
97+ return false
11798}
0 commit comments