/// <summary> /// Request creation of a Prometheus ServiceMonitor. /// </summary> /// <param name="newServiceMonitor"> /// A <see cref="PrometheusServiceMonitorV1"/> representing the ServiceMonitor to create. /// </param> /// <param name="cancellationToken"> /// An optional <see cref="CancellationToken"/> that can be used to cancel the request. /// </param> /// <returns> /// A <see cref="PrometheusServiceMonitorV1"/> representing the current state for the newly-created ServiceMonitor. /// </returns> public async Task <PrometheusServiceMonitorV1> Create(PrometheusServiceMonitorV1 newServiceMonitor, CancellationToken cancellationToken = default) { if (newServiceMonitor == null) { throw new ArgumentNullException(nameof(newServiceMonitor)); } return(await Http .PostAsJsonAsync( Requests.Collection.WithTemplateParameters(new { Namespace = newServiceMonitor?.Metadata?.Namespace ?? Client.DefaultNamespace }), postBody : newServiceMonitor, cancellationToken : cancellationToken ) .ReadContentAsAsync <PrometheusServiceMonitorV1, StatusV1>()); }
/// <summary> /// Ensure that a ServiceMonitor resource does not exist for the specified database server. /// </summary> public async Task EnsureServiceMonitorAbsent() { RequireCurrentState(); if (State.Kind != DatabaseServerKind.SqlServer) { Log.LogInformation("Skipping monitoring configuration for server {ServerId} (not SQL Server).", State.Id); return; } PrometheusServiceMonitorV1 existingServiceMonitor = await FindServiceMonitor(); if (existingServiceMonitor != null) { Log.LogInformation("Deleting service monitor {ServiceName} for server {ServerId}...", existingServiceMonitor.Metadata.Name, State.Id ); StatusV1 result = await KubeClient.PrometheusServiceMonitorsV1().Delete( name: existingServiceMonitor.Metadata.Name, kubeNamespace: KubeOptions.KubeNamespace ); if (result.Status != "Success" && result.Reason != "NotFound") { Log.LogError("Failed to delete service monitor {ServiceName} for server {ServerId} (Message:{FailureMessage}, Reason:{FailureReason}).", existingServiceMonitor.Metadata.Name, State.Id, result.Message, result.Reason ); } Log.LogInformation("Deleted service monitor {ServiceName} for server {ServerId}.", existingServiceMonitor.Metadata.Name, State.Id ); } }
/// <summary> /// Ensure that a ServiceMonitor resource exists for the specified database server. /// </summary> /// <returns> /// The Service resource, as a <see cref="ServiceV1"/>. /// </returns> public async Task EnsureServiceMonitorPresent() { RequireCurrentState(); if (State.Kind != DatabaseServerKind.SqlServer) { Log.LogInformation("Skipping monitoring configuration for server {ServerId} (not SQL Server).", State.Id); return; } PrometheusServiceMonitorV1 existingServiceMonitor = await FindServiceMonitor(); if (existingServiceMonitor == null) { Log.LogInformation("Creating service monitor for server {ServerId}...", State.Id ); PrometheusServiceMonitorV1 createdService = await KubeClient.PrometheusServiceMonitorsV1().Create( KubeResources.ServiceMonitor(State, kubeNamespace: KubeOptions.KubeNamespace ) ); Log.LogInformation("Successfully created service monitor {ServiceName} for server {ServerId}.", createdService.Metadata.Name, State.Id ); } else { Log.LogInformation("Found existing service monitor {ServiceName} for server {ServerId}.", existingServiceMonitor.Metadata.Name, State.Id ); } }