Skip to content

Commit

Permalink
Update ConfigEntries to be Partition aware (#724)
Browse files Browse the repository at this point in the history
* Add partition support to config entries

* Fail if namespaces are not enabled and admin partitions are enabled
  • Loading branch information
Ashwin Venkatesh committed Sep 16, 2021
1 parent b692dd7 commit 37bdad3
Show file tree
Hide file tree
Showing 16 changed files with 120 additions and 4 deletions.
1 change: 1 addition & 0 deletions charts/consul/templates/connect-inject-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{{- if not (or (and (ne (.Values.client.enabled | toString) "-") .Values.client.enabled) (and (eq (.Values.client.enabled | toString) "-") .Values.global.enabled)) }}{{ fail "clients must be enabled for connect injection" }}{{ end }}
{{- if not .Values.client.grpc }}{{ fail "client.grpc must be true for connect injection" }}{{ end }}
{{- if and .Values.connectInject.consulNamespaces.mirroringK8S (not .Values.global.enableConsulNamespaces) }}{{ fail "global.enableConsulNamespaces must be true if mirroringK8S=true" }}{{ end }}
{{- if and .Values.global.adminPartitions.enabled (not .Values.global.enableConsulNamespaces) }}{{ fail "global.enableConsulNamespaces must be true if global.adminPartitions.enabled=true" }}{{ end }}
{{- if .Values.connectInject.centralConfig }}{{- if eq (toString .Values.connectInject.centralConfig.enabled) "false" }}{{ fail "connectInject.centralConfig.enabled cannot be set to false; to disable, set enable_central_service_config to false in server.extraConfig and client.extraConfig" }}{{ end -}}{{ end -}}
{{- if .Values.connectInject.centralConfig }}{{- if .Values.connectInject.centralConfig.defaultProtocol }}{{ fail "connectInject.centralConfig.defaultProtocol is no longer supported; instead you must migrate to CRDs (see www.consul.io/docs/k8s/crds/upgrade-to-crds)" }}{{ end }}{{ end -}}
{{- if .Values.connectInject.centralConfig }}{{ if .Values.connectInject.centralConfig.proxyDefaults }}{{- if ne (trim .Values.connectInject.centralConfig.proxyDefaults) `{}` }}{{ fail "connectInject.centralConfig.proxyDefaults is no longer supported; instead you must migrate to CRDs (see www.consul.io/docs/k8s/crds/upgrade-to-crds)" }}{{ end }}{{ end }}{{ end -}}
Expand Down
4 changes: 4 additions & 0 deletions charts/consul/templates/controller-deployment.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{{- if .Values.controller.enabled }}
{{- if and .Values.global.adminPartitions.enabled (not .Values.global.enableConsulNamespaces) }}{{ fail "global.enableConsulNamespaces must be true if global.adminPartitions.enabled=true" }}{{ end }}
apiVersion: apps/v1
kind: Deployment
metadata:
Expand Down Expand Up @@ -64,6 +65,9 @@ spec:
-log-json={{ .Values.global.logJSON }} \
-webhook-tls-cert-dir=/tmp/controller-webhook/certs \
-datacenter={{ .Values.global.datacenter }} \
{{- if .Values.global.adminPartitions.enabled }}
-partition={{ .Values.global.adminPartitions.name }} \
{{- end }}
-enable-leader-election \
{{- if .Values.global.enableConsulNamespaces }}
-enable-namespaces=true \
Expand Down
8 changes: 8 additions & 0 deletions charts/consul/templates/crd-servicedefaults.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ spec:
description: Namespace is only accepted within a service-defaults
config entry.
type: string
partition:
description: Partition is only accepted within a service-defaults
config entry.
type: string
passiveHealthCheck:
description: PassiveHealthCheck configuration determines how
upstream proxy instances will be monitored for removal from
Expand Down Expand Up @@ -294,6 +298,10 @@ spec:
description: Namespace is only accepted within a service-defaults
config entry.
type: string
partition:
description: Partition is only accepted within a service-defaults
config entry.
type: string
passiveHealthCheck:
description: PassiveHealthCheck configuration determines
how upstream proxy instances will be monitored for removal
Expand Down
3 changes: 3 additions & 0 deletions charts/consul/templates/crd-serviceintentions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ spec:
namespace:
description: Namespace is the namespace for the Name parameter.
type: string
partition:
description: Partition is the Admin Partition for the Name parameter.
type: string
permissions:
description: Permissions is the list of all additional L7 attributes
that extend the intention match criteria. Permission precedence
Expand Down
13 changes: 13 additions & 0 deletions charts/consul/test/unit/connect-inject-deployment.bats
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,7 @@ EOF
local actual=$(helm template \
-s templates/connect-inject-deployment.yaml \
--set 'connectInject.enabled=true' \
--set 'global.enableConsulNamespaces=true' \
. | tee /dev/stderr |
yq '.spec.template.spec.containers[0].command | any(contains("enable-partitions"))' | tee /dev/stderr)

Expand All @@ -706,6 +707,7 @@ EOF
-s templates/connect-inject-deployment.yaml \
--set 'connectInject.enabled=true' \
--set 'global.adminPartitions.enabled=true' \
--set 'global.enableConsulNamespaces=true' \
. | tee /dev/stderr |
yq '.spec.template.spec.containers[0].command | any(contains("enable-partitions"))' | tee /dev/stderr)

Expand All @@ -724,6 +726,17 @@ EOF
[ "${actual}" = "true" ]
}

@test "connectInject/Deployment: fails if namespaces are disabled and .global.adminPartitions.enabled=true" {
cd `chart_dir`
run helm template \
-s templates/connect-inject-deployment.yaml \
--set 'global.adminPartitions.enabled=true' \
--set 'global.enableConsulNamespaces=false' \
--set 'connectInject.enabled=true' .
[ "$status" -eq 1 ]
[[ "$output" =~ "global.enableConsulNamespaces must be true if global.adminPartitions.enabled=true" ]]
}

#--------------------------------------------------------------------
# namespaces

Expand Down
38 changes: 38 additions & 0 deletions charts/consul/test/unit/controller-deployment.bats
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,44 @@ load _helpers
[ "${actual}" = "" ]
}

#--------------------------------------------------------------------
# partitions

@test "controller/Deployment: partitions options disabled by default" {
cd `chart_dir`
local actual=$(helm template \
-s templates/controller-deployment.yaml \
--set 'controller.enabled=true' \
--set 'global.enableConsulNamespaces=true' \
. | tee /dev/stderr |
yq '.spec.template.spec.containers[0].command | any(contains("partition"))' | tee /dev/stderr)

[ "${actual}" = "false" ]
}

@test "controller/Deployment: partition name set with .global.adminPartitions.enabled=true" {
cd `chart_dir`
local actual=$(helm template \
-s templates/controller-deployment.yaml \
--set 'controller.enabled=true' \
--set 'global.adminPartitions.enabled=true' \
--set 'global.enableConsulNamespaces=true' \
. | tee /dev/stderr |
yq '.spec.template.spec.containers[0].command | any(contains("partition=default"))' | tee /dev/stderr)

[ "${actual}" = "true" ]
}

@test "controller/Deployment: fails if namespaces are disabled and .global.adminPartitions.enabled=true" {
cd `chart_dir`
run helm template \
-s templates/controller-deployment.yaml \
--set 'global.adminPartitions.enabled=true' \
--set 'global.enableConsulNamespaces=false' \
--set 'controller.enabled=true' .
[ "$status" -eq 1 ]
[[ "$output" =~ "global.enableConsulNamespaces must be true if global.adminPartitions.enabled=true" ]]
}
#--------------------------------------------------------------------
# namespaces

Expand Down
3 changes: 3 additions & 0 deletions control-plane/api/v1alpha1/servicedefaults_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ type Upstream struct {
Name string `json:"name,omitempty"`
// Namespace is only accepted within a service-defaults config entry.
Namespace string `json:"namespace,omitempty"`
// Partition is only accepted within a service-defaults config entry.
Partition string `json:"partition,omitempty"`
// EnvoyListenerJSON is a complete override ("escape hatch") for the upstream's
// listener.
// Note: This escape hatch is NOT compatible with the discovery chain and
Expand Down Expand Up @@ -322,6 +324,7 @@ func (in *Upstream) toConsul() *capi.UpstreamConfig {
return &capi.UpstreamConfig{
Name: in.Name,
Namespace: in.Namespace,
Partition: in.Partition,
EnvoyListenerJSON: in.EnvoyListenerJSON,
EnvoyClusterJSON: in.EnvoyClusterJSON,
Protocol: in.Protocol,
Expand Down
6 changes: 6 additions & 0 deletions control-plane/api/v1alpha1/servicedefaults_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func TestServiceDefaults_ToConsul(t *testing.T) {
Defaults: &Upstream{
Name: "upstream-default",
Namespace: "ns",
Partition: "part",
EnvoyListenerJSON: `{"key": "value"}`,
EnvoyClusterJSON: `{"key": "value"}`,
Protocol: "http2",
Expand All @@ -91,6 +92,7 @@ func TestServiceDefaults_ToConsul(t *testing.T) {
{
Name: "upstream-override-1",
Namespace: "ns",
Partition: "part",
EnvoyListenerJSON: `{"key": "value"}`,
EnvoyClusterJSON: `{"key": "value"}`,
Protocol: "http2",
Expand All @@ -113,6 +115,7 @@ func TestServiceDefaults_ToConsul(t *testing.T) {
{
Name: "upstream-default",
Namespace: "ns",
Partition: "part",
EnvoyListenerJSON: `{"key": "value"}`,
EnvoyClusterJSON: `{"key": "value"}`,
Protocol: "http2",
Expand Down Expand Up @@ -169,6 +172,7 @@ func TestServiceDefaults_ToConsul(t *testing.T) {
Defaults: &capi.UpstreamConfig{
Name: "upstream-default",
Namespace: "ns",
Partition: "part",
EnvoyListenerJSON: `{"key": "value"}`,
EnvoyClusterJSON: `{"key": "value"}`,
Protocol: "http2",
Expand All @@ -190,6 +194,7 @@ func TestServiceDefaults_ToConsul(t *testing.T) {
{
Name: "upstream-override-1",
Namespace: "ns",
Partition: "part",
EnvoyListenerJSON: `{"key": "value"}`,
EnvoyClusterJSON: `{"key": "value"}`,
Protocol: "http2",
Expand All @@ -210,6 +215,7 @@ func TestServiceDefaults_ToConsul(t *testing.T) {
{
Name: "upstream-default",
Namespace: "ns",
Partition: "part",
EnvoyListenerJSON: `{"key": "value"}`,
EnvoyClusterJSON: `{"key": "value"}`,
Protocol: "http2",
Expand Down
3 changes: 3 additions & 0 deletions control-plane/api/v1alpha1/serviceintentions_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ type SourceIntention struct {
Name string `json:"name,omitempty"`
// Namespace is the namespace for the Name parameter.
Namespace string `json:"namespace,omitempty"`
// Partition is the Admin Partition for the Name parameter.
Partition string `json:"partition,omitempty"`
// Action is required for an L4 intention, and should be set to one of
// "allow" or "deny" for the action that should be taken if this intention matches a request.
Action IntentionAction `json:"action,omitempty"`
Expand Down Expand Up @@ -306,6 +308,7 @@ func (in *SourceIntention) toConsul() *capi.SourceIntention {
return &capi.SourceIntention{
Name: in.Name,
Namespace: in.Namespace,
Partition: in.Partition,
Action: in.Action.toConsul(),
Permissions: in.Permissions.toConsul(),
Description: in.Description,
Expand Down
15 changes: 15 additions & 0 deletions control-plane/api/v1alpha1/serviceintentions_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,21 @@ func TestServiceIntentions_MatchesConsul(t *testing.T) {
{
Name: "svc1",
Namespace: "test",
Partition: "test",
Action: "allow",
Description: "allow access from svc1",
},
{
Name: "*",
Namespace: "not-test",
Partition: "not-test",
Action: "deny",
Description: "disallow access from namespace not-test",
},
{
Name: "svc-2",
Namespace: "bar",
Partition: "bar",
Permissions: IntentionPermissions{
{
Action: "allow",
Expand Down Expand Up @@ -101,20 +104,23 @@ func TestServiceIntentions_MatchesConsul(t *testing.T) {
{
Name: "svc1",
Namespace: "test",
Partition: "test",
Action: "allow",
Precedence: 0,
Description: "allow access from svc1",
},
{
Name: "*",
Namespace: "not-test",
Partition: "not-test",
Action: "deny",
Precedence: 1,
Description: "disallow access from namespace not-test",
},
{
Name: "svc-2",
Namespace: "bar",
Partition: "bar",
Permissions: []*capi.IntentionPermission{
{
Action: "allow",
Expand Down Expand Up @@ -249,18 +255,21 @@ func TestServiceIntentions_ToConsul(t *testing.T) {
{
Name: "svc1",
Namespace: "test",
Partition: "test",
Action: "allow",
Description: "allow access from svc1",
},
{
Name: "*",
Namespace: "not-test",
Partition: "not-test",
Action: "deny",
Description: "disallow access from namespace not-test",
},
{
Name: "svc-2",
Namespace: "bar",
Partition: "bar",
Permissions: IntentionPermissions{
{
Action: "allow",
Expand Down Expand Up @@ -299,18 +308,21 @@ func TestServiceIntentions_ToConsul(t *testing.T) {
{
Name: "svc1",
Namespace: "test",
Partition: "test",
Action: "allow",
Description: "allow access from svc1",
},
{
Name: "*",
Namespace: "not-test",
Partition: "not-test",
Action: "deny",
Description: "disallow access from namespace not-test",
},
{
Name: "svc-2",
Namespace: "bar",
Partition: "bar",
Permissions: []*capi.IntentionPermission{
{
Action: "allow",
Expand Down Expand Up @@ -601,16 +613,19 @@ func TestServiceIntentions_Validate(t *testing.T) {
{
Name: "web",
Namespace: "web",
Partition: "web",
Action: "allow",
},
{
Name: "db",
Namespace: "db",
Partition: "db",
Action: "deny",
},
{
Name: "bar",
Namespace: "bar",
Partition: "bar",
Permissions: IntentionPermissions{
{
Action: "allow",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ spec:
description: Namespace is only accepted within a service-defaults
config entry.
type: string
partition:
description: Partition is only accepted within a service-defaults
config entry.
type: string
passiveHealthCheck:
description: PassiveHealthCheck configuration determines how
upstream proxy instances will be monitored for removal from
Expand Down Expand Up @@ -288,6 +292,10 @@ spec:
description: Namespace is only accepted within a service-defaults
config entry.
type: string
partition:
description: Partition is only accepted within a service-defaults
config entry.
type: string
passiveHealthCheck:
description: PassiveHealthCheck configuration determines
how upstream proxy instances will be monitored for removal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ spec:
namespace:
description: Namespace is the namespace for the Name parameter.
type: string
partition:
description: Partition is the Admin Partition for the Name parameter.
type: string
permissions:
description: Permissions is the list of all additional L7 attributes
that extend the intention match criteria. Permission precedence
Expand Down
2 changes: 1 addition & 1 deletion control-plane/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/google/go-cmp v0.5.6
github.com/google/go-querystring v1.0.0 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/hashicorp/consul/api v1.4.1-0.20210827004034-d2e50fd130ae
github.com/hashicorp/consul/api v1.10.1-0.20210913215352-5b658d2f392d
github.com/hashicorp/consul/sdk v0.8.0
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-discover v0.0.0-20200812215701-c4b85f6ed31f
Expand Down
4 changes: 2 additions & 2 deletions control-plane/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,8 @@ github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgf
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/grpc-ecosystem/grpc-gateway v1.9.5/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/hashicorp/consul/api v1.1.0/go.mod h1:VmuI/Lkw1nC05EYQWNKwWGbkg+FbDBtguAZLlVdkD9Q=
github.com/hashicorp/consul/api v1.4.1-0.20210827004034-d2e50fd130ae h1:eWcikXQgBN6yrsbANCTieR1uT+a7WoYYvGUFj8enPow=
github.com/hashicorp/consul/api v1.4.1-0.20210827004034-d2e50fd130ae/go.mod h1:sDjTOq0yUyv5G4h+BqSea7Fn6BU+XbolEz1952UB+mk=
github.com/hashicorp/consul/api v1.10.1-0.20210913215352-5b658d2f392d h1:IBMYvG34CbxQqM55tBk8aVtmIQxvcczI0BqyxmbQDBs=
github.com/hashicorp/consul/api v1.10.1-0.20210913215352-5b658d2f392d/go.mod h1:sDjTOq0yUyv5G4h+BqSea7Fn6BU+XbolEz1952UB+mk=
github.com/hashicorp/consul/sdk v0.1.1/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyNV1vwHyQBF0x8=
github.com/hashicorp/consul/sdk v0.7.0/go.mod h1:fY08Y9z5SvJqevyZNy6WWPXiG3KwBPAvlcdx16zZ0fM=
github.com/hashicorp/consul/sdk v0.8.0 h1:OJtKBtEjboEZvG6AOUdh4Z1Zbyu0WcxQ0qatRrZHTVU=
Expand Down
Loading

0 comments on commit 37bdad3

Please sign in to comment.