@@ -45,15 +45,23 @@ func NewClusterMonitoringOperatorTask(
4545}
4646
4747func (t * ClusterMonitoringOperatorTask ) Run (ctx context.Context ) error {
48- for name , crf := range map [string ]func () (* rbacv1.ClusterRole , error ){
48+ optionalMonitoringEnabled , err := t .client .HasOptionalMonitoringCapability (ctx )
49+ if err != nil {
50+ return fmt .Errorf ("checking for optional monitoring capability failed: %w" , err )
51+ }
52+
53+ crfs := map [string ]func () (* rbacv1.ClusterRole , error ){
4954 "cluster-monitoring-view" : t .factory .ClusterMonitoringClusterRoleView ,
5055 "system:aggregated-metrics-reader" : t .factory .ClusterMonitoringClusterRoleAggregatedMetricsReader ,
5156 "pod-metrics-reader" : t .factory .ClusterMonitoringClusterRolePodMetricsReader ,
5257 "monitoring-rules-edit" : t .factory .ClusterMonitoringRulesEditClusterRole ,
5358 "monitoring-rules-view" : t .factory .ClusterMonitoringRulesViewClusterRole ,
5459 "monitoring-edit" : t .factory .ClusterMonitoringEditClusterRole ,
55- "alert-routing-edit" : t .factory .ClusterMonitoringAlertingEditClusterRole ,
56- } {
60+ }
61+ if optionalMonitoringEnabled {
62+ crfs ["alert-routing-edit" ] = t .factory .ClusterMonitoringAlertingEditClusterRole
63+ }
64+ for name , crf := range crfs {
5765 cr , err := crf ()
5866 if err != nil {
5967 return fmt .Errorf ("initializing %s ClusterRole failed: %w" , name , err )
@@ -65,34 +73,35 @@ func (t *ClusterMonitoringOperatorTask) Run(ctx context.Context) error {
6573 }
6674 }
6775
68- uwcr , err := t .factory .ClusterMonitoringEditUserWorkloadConfigRole ()
69- if err != nil {
70- return fmt .Errorf ("initializing UserWorkloadConfigEdit Role failed: %w" , err )
71- }
72-
73- err = t .client .CreateOrUpdateRole (ctx , uwcr )
74- if err != nil {
75- return fmt .Errorf ("reconciling UserWorkloadConfigEdit Role failed: %w" , err )
76- }
76+ if optionalMonitoringEnabled {
77+ uwcr , err := t .factory .ClusterMonitoringEditUserWorkloadConfigRole ()
78+ if err != nil {
79+ return fmt .Errorf ("initializing UserWorkloadConfigEdit Role failed: %w" , err )
80+ }
7781
78- uwar , err := t .factory .ClusterMonitoringEditUserWorkloadAlertmanagerApiReader ()
79- if err != nil {
80- return fmt .Errorf ("initializing UserWorkloadAlertmanagerApiReader Role failed: %w" , err )
81- }
82+ err = t .client .CreateOrUpdateRole (ctx , uwcr )
83+ if err != nil {
84+ return fmt .Errorf ("reconciling UserWorkloadConfigEdit Role failed: %w" , err )
85+ }
86+ uwar , err := t .factory .ClusterMonitoringEditUserWorkloadAlertmanagerApiReader ()
87+ if err != nil {
88+ return fmt .Errorf ("initializing UserWorkloadAlertmanagerApiReader Role failed: %w" , err )
89+ }
8290
83- err = t .client .CreateOrUpdateRole (ctx , uwar )
84- if err != nil {
85- return fmt .Errorf ("reconciling UserWorkloadAlertmanagerApiReader Role failed: %w" , err )
86- }
91+ err = t .client .CreateOrUpdateRole (ctx , uwar )
92+ if err != nil {
93+ return fmt .Errorf ("reconciling UserWorkloadAlertmanagerApiReader Role failed: %w" , err )
94+ }
8795
88- uwaw , err := t .factory .ClusterMonitoringEditUserWorkloadAlertmanagerApiWriter ()
89- if err != nil {
90- return fmt .Errorf ("initializing UserWorkloadAlertmanagerApiWriter Role failed: %w" , err )
91- }
96+ uwaw , err := t .factory .ClusterMonitoringEditUserWorkloadAlertmanagerApiWriter ()
97+ if err != nil {
98+ return fmt .Errorf ("initializing UserWorkloadAlertmanagerApiWriter Role failed: %w" , err )
99+ }
92100
93- err = t .client .CreateOrUpdateRole (ctx , uwaw )
94- if err != nil {
95- return fmt .Errorf ("reconciling UserWorkloadAlertmanagerApiWriter Role failed: %w" , err )
101+ err = t .client .CreateOrUpdateRole (ctx , uwaw )
102+ if err != nil {
103+ return fmt .Errorf ("reconciling UserWorkloadAlertmanagerApiWriter Role failed: %w" , err )
104+ }
96105 }
97106
98107 amrr , err := t .factory .ClusterMonitoringAlertManagerViewRole ()
@@ -104,8 +113,7 @@ func (t *ClusterMonitoringOperatorTask) Run(ctx context.Context) error {
104113 if err != nil {
105114 return fmt .Errorf ("initializing AlertmanagerWrite Role failed: %w" , err )
106115 }
107-
108- if t .config .ClusterMonitoringConfiguration .AlertmanagerMainConfig .IsEnabled () {
116+ if t .config .ClusterMonitoringConfiguration .AlertmanagerMainConfig .IsEnabled () && optionalMonitoringEnabled {
109117 if err = t .client .CreateOrUpdateRole (ctx , amwr ); err != nil {
110118 return fmt .Errorf ("reconciling AlertmanagerWrite Role failed: %w" , err )
111119 }
0 commit comments