/// <summary>
        ///     Find the server's associated ServiceMonitor (if it exists).
        /// </summary>
        /// <returns>
        ///     The ServiceMonitor, or <c>null</c> if it was not found.
        /// </returns>
        public async Task <PrometheusServiceMonitorV1> FindServiceMonitor()
        {
            RequireCurrentState();

            List <PrometheusServiceMonitorV1> matchingServices = await KubeClient.PrometheusServiceMonitorsV1().List(
                labelSelector: $"cloud.dimensiondata.daas.server-id = {State.Id},cloud.dimensiondata.daas.monitor-type = database-server",
                kubeNamespace: KubeOptions.KubeNamespace
                );

            if (matchingServices.Count == 0)
            {
                return(null);
            }

            return(matchingServices[matchingServices.Count - 1]);
        }
        /// <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
                                   );
            }
        }