Skip to content

Commit

Permalink
NETOBSERV-1893: fix netobserv-mertics-reader role creation [1.7 backp…
Browse files Browse the repository at this point in the history
…ort] (#800)

* NETOBSERV-1893: fix netobserv-mertics-reader role creation

Prevent undesired mutation of the role by copying it before use.

* Change ClusterRoles to be provided as functions

This is to avoid keeping reference to old / potentially mutated objects.
  • Loading branch information
jotak authored Oct 3, 2024
1 parent 0de6d49 commit 017d213
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 31 deletions.
3 changes: 2 additions & 1 deletion controllers/flp/flp_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,8 @@ func reconcileDataAccessRoles(ctx context.Context, r *reconcilers.Common, b *bui
}
}
// Install netobserv-metrics-reader role
return r.ReconcileClusterRole(ctx, &resources.PromReaderCR)
cr := resources.PromReaderCR()
return r.ReconcileClusterRole(ctx, &cr)
}

func (r *Reconciler) getOpenShiftSubnets(ctx context.Context) ([]flowslatest.SubnetLabel, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/loki/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func ClusterRoles(appName, saName, namespace string) ([]rbacv1.ClusterRole, []rbacv1.ClusterRoleBinding) {
crb := writerBinding(appName, saName, namespace)
return []rbacv1.ClusterRole{resources.LokiWriterCR, resources.LokiReaderCR}, []rbacv1.ClusterRoleBinding{*crb}
return []rbacv1.ClusterRole{resources.LokiWriterCR(), resources.LokiReaderCR()}, []rbacv1.ClusterRoleBinding{*crb}
}

func writerBinding(appName, saName, namespace string) *rbacv1.ClusterRoleBinding {
Expand Down
64 changes: 35 additions & 29 deletions pkg/resources/static_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,43 @@ import (
"github.com/netobserv/network-observability-operator/controllers/constants"
)

var LokiWriterCR = rbacv1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{
Name: constants.LokiCRWriter,
},
Rules: []rbacv1.PolicyRule{{
APIGroups: []string{"loki.grafana.com"},
Resources: []string{"network"},
ResourceNames: []string{"logs"},
Verbs: []string{"create"},
}},
func LokiWriterCR() rbacv1.ClusterRole {
return rbacv1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{
Name: constants.LokiCRWriter,
},
Rules: []rbacv1.PolicyRule{{
APIGroups: []string{"loki.grafana.com"},
Resources: []string{"network"},
ResourceNames: []string{"logs"},
Verbs: []string{"create"},
}},
}
}

var LokiReaderCR = rbacv1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{
Name: constants.LokiCRReader,
},
Rules: []rbacv1.PolicyRule{{
APIGroups: []string{"loki.grafana.com"},
Resources: []string{"network"},
ResourceNames: []string{"logs"},
Verbs: []string{"get"},
}},
func LokiReaderCR() rbacv1.ClusterRole {
return rbacv1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{
Name: constants.LokiCRReader,
},
Rules: []rbacv1.PolicyRule{{
APIGroups: []string{"loki.grafana.com"},
Resources: []string{"network"},
ResourceNames: []string{"logs"},
Verbs: []string{"get"},
}},
}
}

var PromReaderCR = rbacv1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{
Name: constants.PromCRReader,
},
Rules: []rbacv1.PolicyRule{{
APIGroups: []string{"metrics.k8s.io"},
Resources: []string{"pods"},
Verbs: []string{"create"},
}},
func PromReaderCR() rbacv1.ClusterRole {
return rbacv1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{
Name: constants.PromCRReader,
},
Rules: []rbacv1.PolicyRule{{
APIGroups: []string{"metrics.k8s.io"},
Resources: []string{"pods"},
Verbs: []string{"create"},
}},
}
}

0 comments on commit 017d213

Please sign in to comment.