// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

syntax = "proto3";

package hashicorp.consul.catalog.v2beta1;

import "google/protobuf/duration.proto";
import "pbcatalog/v2beta1/selector.proto";
import "pbresource/annotations.proto";

// This resource will belong to a workload and will have an ownership relationship.
message HealthStatus {
  option (hashicorp.consul.resource.spec) = {scope: SCOPE_NAMESPACE};

  // Type is the type of this health check, such as http, tcp, or kubernetes-readiness
  string type = 1;
  // Health is the status. This maps to existing health check statuses.
  Health status = 2;
  // Description is the description for this status.
  string description = 3;
  // Output is the output from running the check that resulted in this status
  string output = 4;
}

// +kubebuilder:validation:Enum=HEALTH_ANY;HEALTH_PASSING;HEALTH_WARNING;HEALTH_CRITICAL;HEALTH_MAINTENANCE
// +kubebuilder:validation:Type=string
enum Health {
  // buf:lint:ignore ENUM_ZERO_VALUE_SUFFIX
  HEALTH_ANY = 0;
  HEALTH_PASSING = 1;
  HEALTH_WARNING = 2;
  HEALTH_CRITICAL = 3;
  HEALTH_MAINTENANCE = 4;
}

message HealthChecks {
  option (hashicorp.consul.resource.spec) = {scope: SCOPE_NAMESPACE};

  WorkloadSelector workloads = 1;
  repeated HealthCheck health_checks = 2;
}

message HealthCheck {
  string name = 1;
  oneof definition {
    HTTPCheck http = 2;
    TCPCheck tcp = 3;
    UDPCheck udp = 4;
    GRPCCheck grpc = 5;
    OSServiceCheck os_service = 6;
  }
  // +kubebuilder:validation:Format=duration
  google.protobuf.Duration interval = 7;
  // +kubebuilder:validation:Format=duration
  google.protobuf.Duration timeout = 8;
  // +kubebuilder:validation:Format=duration
  google.protobuf.Duration deregister_critical_after = 9;
}

message HTTPCheck {
  string url = 1;
  map<string, string> header = 2;
  string method = 3;
  string body = 4;
  bool disable_redirects = 5;
  CheckTLSConfig tls = 6;
}

message TCPCheck {
  string address = 1;
}

message UDPCheck {
  string address = 1;
}

message GRPCCheck {
  string address = 1;
  CheckTLSConfig tls = 2;
}

message OSServiceCheck {
  string address = 1;
}

message CheckTLSConfig {
  string tls_server_name = 1;
  bool tls_skip_verify = 2;
  bool use_tls = 3;
}

// This resource will belong to a node and will have an ownership relationship.
message NodeHealthStatus {
  option (hashicorp.consul.resource.spec) = {scope: SCOPE_PARTITION};

  // Type is the type of this health check, such as http, tcp, or kubernetes-readiness
  string type = 1;
  // Health is the status. This maps to existing health check statuses.
  Health status = 2;
  // Description is the description for this status.
  string description = 3;
  // Output is the output from running the check that resulted in this status
  string output = 4;
}
