|
1 | 1 | package com.redhat; |
2 | 2 |
|
3 | 3 | import java.util.Map; |
| 4 | +import java.util.concurrent.atomic.AtomicBoolean; |
4 | 5 |
|
5 | 6 | import org.eclipse.microprofile.config.inject.ConfigProperty; |
6 | 7 | import org.jboss.logging.Logger; |
@@ -108,80 +109,135 @@ public UpdateControl<Meter> createOrUpdateResource(Meter resource, Context<Meter |
108 | 109 | } |
109 | 110 |
|
110 | 111 | void createServiceMonitor() { |
111 | | - // Create Role |
112 | | - Role role = new RoleBuilder() |
113 | | - .withNewMetadata() |
114 | | - .withName(ROLE_NAME) |
115 | | - .endMetadata() |
116 | | - .addNewRule() |
117 | | - .addNewApiGroup("") |
118 | | - .withResources("pods", "services", "endpoints") |
119 | | - .withVerbs("get", "list", "watch") |
120 | | - .endRule() |
121 | | - .build(); |
122 | | - client.roles().createOrReplace(role); |
123 | | - |
124 | | - // Create RoleBinding |
125 | | - RoleBinding roleBinding = new RoleBindingBuilder() |
126 | | - .withNewMetadata() |
127 | | - .withName(ROLE_BINDING_NAME) |
128 | | - .endMetadata() |
129 | | - .addNewSubject() |
130 | | - .withKind("ServiceAccount") |
131 | | - .withName("prometheus-k8s") |
132 | | - .withNamespace(OPENSHIFT_MONITORING_NAMESPACE) |
133 | | - .endSubject() |
134 | | - .withNewRoleRef() |
135 | | - .withKind("Role") |
136 | | - .withName(ROLE_NAME) |
137 | | - .endRoleRef() |
138 | | - .build(); |
139 | | - client.roleBindings().createOrReplace(roleBinding); |
140 | | - |
141 | | - // Create Prometheus Rule |
142 | | - PrometheusRule promRule = new PrometheusRuleBuilder() |
143 | | - .withNewMetadata() |
144 | | - .withName(PROMETHEUS_RULES_NAME) |
145 | | - .withNamespace(OPENSHIFT_MONITORING_NAMESPACE) |
146 | | - .endMetadata() |
147 | | - .withNewSpec() |
148 | | - .addNewGroup() |
149 | | - .withName("application-services.rules") |
150 | | - .addNewRule() |
151 | | - .withNewExpr("sum by (prod_name) (appsvcs_cpu_usage_cores)") |
152 | | - .withRecord("appsvcs:cores_by_product:sum") |
153 | | - .endRule() |
154 | | - .endGroup() |
155 | | - .endSpec() |
156 | | - .build(); |
157 | | - client.monitoring().prometheusRules().inNamespace(OPENSHIFT_MONITORING_NAMESPACE).createOrReplace(promRule); |
158 | | - |
159 | | - // Create ServiceMonitor |
160 | | - ServiceMonitor monitor = new ServiceMonitorBuilder() |
161 | | - .withNewMetadata() |
162 | | - .withName(SERVICE_MONITOR_NAME) |
163 | | - .withNamespace(OPENSHIFT_MONITORING_NAMESPACE) |
164 | | - .endMetadata() |
165 | | - .withNewSpec() |
166 | | - .addNewEndpoint() |
167 | | - .withScheme("http") |
168 | | - .withPort("http") |
169 | | - .withPath("/q/metrics") |
170 | | - .withInterval(config.scrapeInterval()) |
171 | | - .endEndpoint() |
172 | | - .withNewNamespaceSelector() |
173 | | - .withMatchNames(client.getNamespace()) |
174 | | - .endNamespaceSelector() |
175 | | - .withNewSelector() |
176 | | - .withMatchLabels(Map.of("app.kubernetes.io/name", applicationName)) |
177 | | - .endSelector() |
178 | | - .endSpec() |
179 | | - .build(); |
180 | | - client.monitoring().serviceMonitors().inNamespace(OPENSHIFT_MONITORING_NAMESPACE).createOrReplace(monitor); |
181 | | - |
182 | | - LOG.info("ServiceMonitor " + SERVICE_MONITOR_NAME + " installed."); |
| 112 | + if (serviceMonitor().get() != null) { |
| 113 | + System.out.println("SKIPPED INSTALL"); |
| 114 | + // Don't repeat the install |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + try { |
| 119 | + // Create Role |
| 120 | + Role role = new RoleBuilder() |
| 121 | + .withNewMetadata() |
| 122 | + .withName(ROLE_NAME) |
| 123 | + .endMetadata() |
| 124 | + .addNewRule() |
| 125 | + .addNewApiGroup("") |
| 126 | + .withResources("pods", "services", "endpoints") |
| 127 | + .withVerbs("get", "list", "watch") |
| 128 | + .endRule() |
| 129 | + .build(); |
| 130 | + role = client.roles().createOrReplace(role); |
| 131 | + |
| 132 | + // TODO remove |
| 133 | + System.out.println("ROLE RESOURCE VERSION: " + role.getMetadata().getResourceVersion()); |
| 134 | + if (role.getMetadata().getResourceVersion() == null) { |
| 135 | + // No exception, but Role not created properly |
| 136 | + return; |
| 137 | + } |
| 138 | + } catch (Exception e) { |
| 139 | + LOG.error("Failed to create Role", e); |
| 140 | + //TODO Uncomment when permissions are fixed |
| 141 | +// return; |
| 142 | + } |
| 143 | + |
| 144 | + try { |
| 145 | + // Create RoleBinding |
| 146 | + RoleBinding roleBinding = new RoleBindingBuilder() |
| 147 | + .withNewMetadata() |
| 148 | + .withName(ROLE_BINDING_NAME) |
| 149 | + .endMetadata() |
| 150 | + .addNewSubject() |
| 151 | + .withKind("ServiceAccount") |
| 152 | + .withName("prometheus-k8s") |
| 153 | + .withNamespace(OPENSHIFT_MONITORING_NAMESPACE) |
| 154 | + .endSubject() |
| 155 | + .withNewRoleRef() |
| 156 | + .withKind("Role") |
| 157 | + .withName(ROLE_NAME) |
| 158 | + .endRoleRef() |
| 159 | + .build(); |
| 160 | + roleBinding = client.roleBindings().createOrReplace(roleBinding); |
| 161 | + |
| 162 | + // TODO remove |
| 163 | + System.out.println("ROLEBINDING RESOURCE VERSION: " + roleBinding.getMetadata().getResourceVersion()); |
| 164 | + |
| 165 | + if (roleBinding.getMetadata().getResourceVersion() == null) { |
| 166 | + // No exception, but RoleBinding not created properly |
| 167 | + return; |
| 168 | + } |
| 169 | + } catch (Exception e) { |
| 170 | + LOG.error("Failed to create RoleBinding", e); |
| 171 | + //TODO Uncomment when permissions are fixed |
| 172 | +// return; |
| 173 | + } |
| 174 | + |
| 175 | + try { |
| 176 | + // Create Prometheus Rule |
| 177 | + PrometheusRule promRule = new PrometheusRuleBuilder() |
| 178 | + .withNewMetadata() |
| 179 | + .withName(PROMETHEUS_RULES_NAME) |
| 180 | + .withNamespace(OPENSHIFT_MONITORING_NAMESPACE) |
| 181 | + .endMetadata() |
| 182 | + .withNewSpec() |
| 183 | + .addNewGroup() |
| 184 | + .withName("application-services.rules") |
| 185 | + .addNewRule() |
| 186 | + .withNewExpr("sum by (prod_name) (appsvcs_cpu_usage_cores)") |
| 187 | + .withRecord("appsvcs:cores_by_product:sum") |
| 188 | + .endRule() |
| 189 | + .endGroup() |
| 190 | + .endSpec() |
| 191 | + .build(); |
| 192 | + promRule = client.monitoring().prometheusRules().inNamespace(OPENSHIFT_MONITORING_NAMESPACE).createOrReplace(promRule); |
| 193 | + |
| 194 | + // TODO remove |
| 195 | + System.out.println("PROMETHEUSRULE RESOURCE VERSION: " + promRule.getMetadata().getResourceVersion()); |
| 196 | + if (promRule.getMetadata().getResourceVersion() == null) { |
| 197 | + // No exception, but PrometheusRule not created properly |
| 198 | + return; |
| 199 | + } |
| 200 | + } catch (Exception e) { |
| 201 | + LOG.error("Failed to create PrometheusRule", e); |
| 202 | + //TODO Uncomment when permissions are fixed |
| 203 | + // return; |
| 204 | + } |
| 205 | + |
| 206 | + try { |
| 207 | + // Create ServiceMonitor |
| 208 | + ServiceMonitor monitor = new ServiceMonitorBuilder() |
| 209 | + .withNewMetadata() |
| 210 | + .withName(SERVICE_MONITOR_NAME) |
| 211 | + .withNamespace(OPENSHIFT_MONITORING_NAMESPACE) |
| 212 | + .endMetadata() |
| 213 | + .withNewSpec() |
| 214 | + .addNewEndpoint() |
| 215 | + .withScheme("http") |
| 216 | + .withPort("http") |
| 217 | + .withPath("/q/metrics") |
| 218 | + .withInterval(config.scrapeInterval()) |
| 219 | + .endEndpoint() |
| 220 | + .withNewNamespaceSelector() |
| 221 | + .withMatchNames(client.getNamespace()) |
| 222 | + .endNamespaceSelector() |
| 223 | + .withNewSelector() |
| 224 | + .withMatchLabels(Map.of("app.kubernetes.io/name", applicationName)) |
| 225 | + .endSelector() |
| 226 | + .endSpec() |
| 227 | + .build(); |
| 228 | + monitor = client.monitoring().serviceMonitors().inNamespace(OPENSHIFT_MONITORING_NAMESPACE).createOrReplace(monitor); |
| 229 | + // TODO remove |
| 230 | + System.out.println("SERVICEMONITOR RESOURCE VERSION: " + monitor.getMetadata().getResourceVersion()); |
| 231 | + |
| 232 | + if (!monitor.getMetadata().getResourceVersion().isEmpty()) { |
| 233 | + LOG.info("ServiceMonitor " + SERVICE_MONITOR_NAME + " installed."); |
| 234 | + } |
| 235 | + } catch (Exception e) { |
| 236 | + LOG.error("Failed to create ServiceMonitor", e); |
| 237 | + } |
183 | 238 | } |
184 | 239 |
|
| 240 | + // These are safe to execute even if all of them are not there |
185 | 241 | void deleteServiceMonitor() { |
186 | 242 | // Delete ServiceMonitor |
187 | 243 | Resource<ServiceMonitor> serviceMonitorResource = serviceMonitor(); |
|
0 commit comments