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

syntax = "proto3";

package hashicorp.consul.mesh.v2beta1;

import "pbmesh/v2beta1/common.proto";
import "pbresource/annotations.proto";

// NOTE: this should align to the GAMMA/gateway-api version, or at least be
// easily translatable.
//
// https://gateway-api.sigs.k8s.io/references/spec/#gateway.networking.k8s.io/v1alpha2.TCPRoute
//
// This is a Resource type.
message TCPRoute {
  option (hashicorp.consul.resource.spec) = {scope: SCOPE_NAMESPACE};

  // ParentRefs references the resources (usually Services) that a Route wants
  // to be attached to.
  //
  // It is invalid to reference an identical parent more than once. It is valid
  // to reference multiple distinct sections within the same parent resource.
  repeated ParentReference parent_refs = 1;

  // Rules are a list of TCP matchers and actions.
  repeated TCPRouteRule rules = 2;
}

message TCPRouteRule {
  // BackendRefs defines the backend(s) where matching requests should be sent.
  // If unspecified or invalid (refers to a non-existent resource or a Service
  // with no endpoints), the underlying implementation MUST actively reject
  // connection attempts to this backend. Connection rejections must respect
  // weight; if an invalid backend is requested to have 80% of connections,
  // then 80% of connections must be rejected instead.
  repeated TCPBackendRef backend_refs = 1;
}

message TCPBackendRef {
  BackendReference backend_ref = 1;

  // Weight specifies the proportion of requests forwarded to the referenced
  // backend. This is computed as weight/(sum of all weights in this
  // BackendRefs list). For non-zero values, there may be some epsilon from the
  // exact proportion defined here depending on the precision an implementation
  // supports. Weight is not a percentage and the sum of weights does not need
  // to equal 100.
  //
  // If only one backend is specified and it has a weight greater than 0, 100%
  // of the traffic is forwarded to that backend. If weight is set to 0, no
  // traffic should be forwarded for this entry. If unspecified, weight defaults
  // to 1.
  uint32 weight = 2;
}
