Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions control-plane/connect-inject/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"google.golang.org/protobuf/types/known/anypb"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"

"github.com/hashicorp/consul-k8s/control-plane/connect-inject/constants"
pbcatalog "github.com/hashicorp/consul/proto-public/pbcatalog/v1alpha1"
Expand Down Expand Up @@ -160,3 +161,19 @@ func GetPortProtocol(appProtocol *string) pbcatalog.Protocol {
// If unrecognized or empty string, return default
return pbcatalog.Protocol_PROTOCOL_UNSPECIFIED
}

// PortValueFromIntOrString returns the integer port value from the port that can be
// a named port, an integer string (e.g. "80"), or an integer. If the port is a named port,
// this function will attempt to find the value from the containers of the pod.
func PortValueFromIntOrString(pod corev1.Pod, port intstr.IntOrString) (uint32, error) {
if port.Type == intstr.Int {
return uint32(port.IntValue()), nil
}

// Otherwise, find named port or try to parse the string as an int.
portVal, err := PortValue(pod, port.StrVal)
if err != nil {
return 0, err
}
return uint32(portVal), nil
}
11 changes: 11 additions & 0 deletions control-plane/connect-inject/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const (
// DefaultConsulPartition is the default Consul partition name.
DefaultConsulPartition = "default"

// DefaultConsulPeer is the name used to refer to resources that are in the same cluster.
DefaultConsulPeer = "local"

// ProxyDefaultInboundPort is the default inbound port for the proxy.
ProxyDefaultInboundPort = 20000

Expand All @@ -39,4 +42,12 @@ const (

// DefaultGracefulShutdownPath is the default path that consul-dataplane uses for graceful shutdown.
DefaultGracefulShutdownPath = "/graceful_shutdown"

// ConsulKubernetesCheckType is the type of health check in Consul for Kubernetes readiness status.
ConsulKubernetesCheckType = "kubernetes-readiness"

// ConsulKubernetesCheckName is the name of health check in Consul for Kubernetes readiness status.
ConsulKubernetesCheckName = "Kubernetes Readiness Check"

KubernetesSuccessReasonMsg = "Kubernetes health checks passing"
)
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ const (
terminatingGateway = "terminating-gateway"
ingressGateway = "ingress-gateway"

kubernetesSuccessReasonMsg = "Kubernetes health checks passing"
envoyPrometheusBindAddr = "envoy_prometheus_bind_addr"
envoyTelemetryCollectorBindSocketDir = "envoy_telemetry_collector_bind_socket_dir"
defaultNS = "default"
Expand All @@ -57,12 +56,6 @@ const (
// This address does not need to be routable as this node is ephemeral, and we're only providing it because
// Consul's API currently requires node address to be provided when registering a node.
consulNodeAddress = "127.0.0.1"

// consulKubernetesCheckType is the type of health check in Consul for Kubernetes readiness status.
consulKubernetesCheckType = "kubernetes-readiness"

// consulKubernetesCheckName is the name of health check in Consul for Kubernetes readiness status.
consulKubernetesCheckName = "Kubernetes Readiness Check"
)

type Controller struct {
Expand Down Expand Up @@ -469,8 +462,8 @@ func (r *Controller) createServiceRegistrations(pod corev1.Pod, serviceEndpoints
Service: service,
Check: &api.AgentCheck{
CheckID: consulHealthCheckID(pod.Namespace, svcID),
Name: consulKubernetesCheckName,
Type: consulKubernetesCheckType,
Name: constants.ConsulKubernetesCheckName,
Type: constants.ConsulKubernetesCheckType,
Status: healthStatus,
ServiceID: svcID,
Output: getHealthCheckStatusReason(healthStatus, pod.Name, pod.Namespace),
Expand Down Expand Up @@ -664,8 +657,8 @@ func (r *Controller) createServiceRegistrations(pod corev1.Pod, serviceEndpoints
Service: proxyService,
Check: &api.AgentCheck{
CheckID: consulHealthCheckID(pod.Namespace, proxySvcID),
Name: consulKubernetesCheckName,
Type: consulKubernetesCheckType,
Name: constants.ConsulKubernetesCheckName,
Type: constants.ConsulKubernetesCheckType,
Status: healthStatus,
ServiceID: proxySvcID,
Output: getHealthCheckStatusReason(healthStatus, pod.Name, pod.Namespace),
Expand Down Expand Up @@ -807,8 +800,8 @@ func (r *Controller) createGatewayRegistrations(pod corev1.Pod, serviceEndpoints
Service: service,
Check: &api.AgentCheck{
CheckID: consulHealthCheckID(pod.Namespace, pod.Name),
Name: consulKubernetesCheckName,
Type: consulKubernetesCheckType,
Name: constants.ConsulKubernetesCheckName,
Type: constants.ConsulKubernetesCheckType,
Status: healthStatus,
ServiceID: pod.Name,
Namespace: consulNS,
Expand Down Expand Up @@ -900,7 +893,7 @@ func consulHealthCheckID(k8sNS string, serviceID string) string {
// as well as pod name and namespace and returns the reason message.
func getHealthCheckStatusReason(healthCheckStatus, podName, podNamespace string) string {
if healthCheckStatus == api.HealthPassing {
return kubernetesSuccessReasonMsg
return constants.KubernetesSuccessReasonMsg
}

return fmt.Sprintf("Pod \"%s/%s\" is not ready", podNamespace, podName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,40 +172,40 @@ func TestReconcileCreateEndpointWithNamespaces(t *testing.T) {
CheckID: fmt.Sprintf("%s/pod1-service-created", testCase.SourceKubeNS),
ServiceName: "service-created",
ServiceID: "pod1-service-created",
Name: consulKubernetesCheckName,
Name: constants.ConsulKubernetesCheckName,
Status: api.HealthPassing,
Output: kubernetesSuccessReasonMsg,
Type: consulKubernetesCheckType,
Output: constants.KubernetesSuccessReasonMsg,
Type: constants.ConsulKubernetesCheckType,
Namespace: testCase.ExpConsulNS,
},
{
CheckID: fmt.Sprintf("%s/pod1-service-created-sidecar-proxy", testCase.SourceKubeNS),
ServiceName: "service-created-sidecar-proxy",
ServiceID: "pod1-service-created-sidecar-proxy",
Name: consulKubernetesCheckName,
Name: constants.ConsulKubernetesCheckName,
Status: api.HealthPassing,
Output: kubernetesSuccessReasonMsg,
Type: consulKubernetesCheckType,
Output: constants.KubernetesSuccessReasonMsg,
Type: constants.ConsulKubernetesCheckType,
Namespace: testCase.ExpConsulNS,
},
{
CheckID: fmt.Sprintf("%s/pod2-service-created", testCase.SourceKubeNS),
ServiceName: "service-created",
ServiceID: "pod2-service-created",
Name: consulKubernetesCheckName,
Name: constants.ConsulKubernetesCheckName,
Status: api.HealthPassing,
Output: kubernetesSuccessReasonMsg,
Type: consulKubernetesCheckType,
Output: constants.KubernetesSuccessReasonMsg,
Type: constants.ConsulKubernetesCheckType,
Namespace: testCase.ExpConsulNS,
},
{
CheckID: fmt.Sprintf("%s/pod2-service-created-sidecar-proxy", testCase.SourceKubeNS),
ServiceName: "service-created-sidecar-proxy",
ServiceID: "pod2-service-created-sidecar-proxy",
Name: consulKubernetesCheckName,
Name: constants.ConsulKubernetesCheckName,
Status: api.HealthPassing,
Output: kubernetesSuccessReasonMsg,
Type: consulKubernetesCheckType,
Output: constants.KubernetesSuccessReasonMsg,
Type: constants.ConsulKubernetesCheckType,
Namespace: testCase.ExpConsulNS,
},
},
Expand Down Expand Up @@ -446,30 +446,30 @@ func TestReconcileCreateGatewayWithNamespaces(t *testing.T) {
CheckID: "default/mesh-gateway",
ServiceName: "mesh-gateway",
ServiceID: "mesh-gateway",
Name: consulKubernetesCheckName,
Name: constants.ConsulKubernetesCheckName,
Status: api.HealthPassing,
Output: kubernetesSuccessReasonMsg,
Type: consulKubernetesCheckType,
Output: constants.KubernetesSuccessReasonMsg,
Type: constants.ConsulKubernetesCheckType,
Namespace: "default",
},
{
CheckID: "default/terminating-gateway",
ServiceName: "terminating-gateway",
ServiceID: "terminating-gateway",
Name: consulKubernetesCheckName,
Name: constants.ConsulKubernetesCheckName,
Status: api.HealthPassing,
Output: kubernetesSuccessReasonMsg,
Type: consulKubernetesCheckType,
Output: constants.KubernetesSuccessReasonMsg,
Type: constants.ConsulKubernetesCheckType,
Namespace: testCase.ConsulNS,
},
{
CheckID: "default/ingress-gateway",
ServiceName: "ingress-gateway",
ServiceID: "ingress-gateway",
Name: consulKubernetesCheckName,
Name: constants.ConsulKubernetesCheckName,
Status: api.HealthPassing,
Output: kubernetesSuccessReasonMsg,
Type: consulKubernetesCheckType,
Output: constants.KubernetesSuccessReasonMsg,
Type: constants.ConsulKubernetesCheckType,
Namespace: testCase.ConsulNS,
},
},
Expand Down
Loading