Skip to content

Commit

Permalink
[1.1.x] Normalize partitions during CRD camparison.
Browse files Browse the repository at this point in the history
  • Loading branch information
thisisnotashwin committed Dec 1, 2023
1 parent 24cfa5d commit 2d6534f
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 24 deletions.
3 changes: 3 additions & 0 deletions .changelog/3296.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug-fix
control-plane: normalize the `partition` and `namespace` fields in V1 CRDs when comparing with saved version of the config-entry.
```
12 changes: 10 additions & 2 deletions control-plane/api/v1alpha1/exportedservices_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/hashicorp/consul-k8s/control-plane/api/common"
"github.com/hashicorp/consul/api"
capi "github.com/hashicorp/consul/api"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"

"github.com/hashicorp/consul-k8s/control-plane/api/common"
)

const ExportedServicesKubeKind = "exportedservices"
Expand Down Expand Up @@ -182,8 +183,15 @@ func (in *ExportedServices) MatchesConsul(candidate api.ConfigEntry) bool {
if !ok {
return false
}

specialEquality := cmp.Options{
cmp.FilterPath(func(path cmp.Path) bool {
return path.String() == "Services.Consumers.Partition"
}, cmp.Transformer("NormalizePartition", normalizeEmptyToDefault)),
}

// No datacenter is passed to ToConsul as we ignore the Meta field when checking for equality.
return cmp.Equal(in.ToConsul(""), configEntry, cmpopts.IgnoreFields(capi.ExportedServicesConfigEntry{}, "Partition", "Meta", "ModifyIndex", "CreateIndex"), cmpopts.IgnoreUnexported(), cmpopts.EquateEmpty())
return cmp.Equal(in.ToConsul(""), configEntry, cmpopts.IgnoreFields(capi.ExportedServicesConfigEntry{}, "Partition", "Meta", "ModifyIndex", "CreateIndex"), cmpopts.IgnoreUnexported(), cmpopts.EquateEmpty(), specialEquality)

}

Expand Down
6 changes: 4 additions & 2 deletions control-plane/api/v1alpha1/exportedservices_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"testing"
"time"

"github.com/hashicorp/consul-k8s/control-plane/api/common"
capi "github.com/hashicorp/consul/api"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/hashicorp/consul-k8s/control-plane/api/common"
)

// Test MatchesConsul for cases that should return true.
Expand Down Expand Up @@ -105,7 +106,8 @@ func TestExportedServices_MatchesConsul(t *testing.T) {
Partition: "fifth",
},
{
Peer: "third-peer",
Peer: "third-peer",
Partition: "default",
},
},
},
Expand Down
18 changes: 15 additions & 3 deletions control-plane/api/v1alpha1/ingressgateway_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ package v1alpha1
import (
"encoding/json"
"fmt"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/hashicorp/consul-k8s/control-plane/api/common"
"github.com/hashicorp/consul-k8s/control-plane/namespaces"
capi "github.com/hashicorp/consul/api"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"

"github.com/hashicorp/consul-k8s/control-plane/api/common"
"github.com/hashicorp/consul-k8s/control-plane/namespaces"
)

const (
Expand Down Expand Up @@ -265,8 +267,18 @@ func (in *IngressGateway) MatchesConsul(candidate capi.ConfigEntry) bool {
if !ok {
return false
}

specialEquality := cmp.Options{
cmp.FilterPath(func(path cmp.Path) bool {
return path.String() == "Listeners.Services.Namespace"
}, cmp.Transformer("NormalizeNamespace", normalizeEmptyToDefault)),
cmp.FilterPath(func(path cmp.Path) bool {
return path.String() == "Listeners.Services.Partition"
}, cmp.Transformer("NormalizePartition", normalizeEmptyToDefault)),
}

// No datacenter is passed to ToConsul as we ignore the Meta field when checking for equality.
return cmp.Equal(in.ToConsul(""), configEntry, cmpopts.IgnoreFields(capi.IngressGatewayConfigEntry{}, "Partition", "Namespace", "Meta", "ModifyIndex", "CreateIndex"), cmpopts.IgnoreUnexported(), cmpopts.EquateEmpty())
return cmp.Equal(in.ToConsul(""), configEntry, cmpopts.IgnoreFields(capi.IngressGatewayConfigEntry{}, "Partition", "Namespace", "Meta", "ModifyIndex", "CreateIndex"), cmpopts.IgnoreUnexported(), cmpopts.EquateEmpty(), specialEquality)
}

func (in *IngressGateway) Validate(consulMeta common.ConsulMeta) error {
Expand Down
4 changes: 2 additions & 2 deletions control-plane/api/v1alpha1/ingressgateway_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import (
"time"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/consul-k8s/control-plane/api/common"
capi "github.com/hashicorp/consul/api"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"

"github.com/hashicorp/consul-k8s/control-plane/api/common"
)

func TestIngressGateway_MatchesConsul(t *testing.T) {
Expand Down Expand Up @@ -99,7 +100,6 @@ func TestIngressGateway_MatchesConsul(t *testing.T) {
Name: "name1",
Hosts: []string{"host1_1", "host1_2"},
Namespace: "ns1",
Partition: "default",
IngressServiceConfig: IngressServiceConfig{
MaxConnections: &maxConnections,
MaxPendingRequests: &maxPendingRequests,
Expand Down
17 changes: 14 additions & 3 deletions control-plane/api/v1alpha1/servicedefaults_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/hashicorp/consul-k8s/control-plane/api/common"
capi "github.com/hashicorp/consul/api"
"github.com/miekg/dns"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"

"github.com/hashicorp/consul-k8s/control-plane/api/common"
)

const (
Expand Down Expand Up @@ -534,9 +535,19 @@ func (in *ServiceDefaults) MatchesConsul(candidate capi.ConfigEntry) bool {
if !ok {
return false
}

specialEquality := cmp.Options{
cmp.FilterPath(func(path cmp.Path) bool {
return path.String() == "UpstreamConfig.Overrides.Namespace"
}, cmp.Transformer("NormalizeNamespace", normalizeEmptyToDefault)),
cmp.FilterPath(func(path cmp.Path) bool {
return path.String() == "UpstreamConfig.Overrides.Partition"
}, cmp.Transformer("NormalizePartition", normalizeEmptyToDefault)),
cmp.Comparer(transparentProxyConfigComparer),
}

// No datacenter is passed to ToConsul as we ignore the Meta field when checking for equality.
return cmp.Equal(in.ToConsul(""), configEntry, cmpopts.IgnoreFields(capi.ServiceConfigEntry{}, "Partition", "Namespace", "Meta", "ModifyIndex", "CreateIndex"), cmpopts.IgnoreUnexported(), cmpopts.EquateEmpty(),
cmp.Comparer(transparentProxyConfigComparer))
return cmp.Equal(in.ToConsul(""), configEntry, cmpopts.IgnoreFields(capi.ServiceConfigEntry{}, "Partition", "Namespace", "Meta", "ModifyIndex", "CreateIndex"), cmpopts.IgnoreUnexported(), cmpopts.EquateEmpty(), specialEquality)
}

func (in *ServiceDefaults) ConsulGlobalResource() bool {
Expand Down
4 changes: 2 additions & 2 deletions control-plane/api/v1alpha1/servicedefaults_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,6 @@ func TestServiceDefaults_MatchesConsul(t *testing.T) {
},
{
Name: "upstream-default",
Namespace: "ns",
EnvoyListenerJSON: `{"key": "value"}`,
EnvoyClusterJSON: `{"key": "value"}`,
Protocol: "http2",
Expand Down Expand Up @@ -649,7 +648,8 @@ func TestServiceDefaults_MatchesConsul(t *testing.T) {
},
{
Name: "upstream-default",
Namespace: "ns",
Namespace: "default",
Partition: "default",
EnvoyListenerJSON: `{"key": "value"}`,
EnvoyClusterJSON: `{"key": "value"}`,
Protocol: "http2",
Expand Down
21 changes: 19 additions & 2 deletions control-plane/api/v1alpha1/serviceresolver_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ package v1alpha1

import (
"encoding/json"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/hashicorp/consul-k8s/control-plane/api/common"
capi "github.com/hashicorp/consul/api"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"

"github.com/hashicorp/consul-k8s/control-plane/api/common"
)

const ServiceResolverKubeKind string = "serviceresolver"
Expand Down Expand Up @@ -311,8 +313,23 @@ func (in *ServiceResolver) MatchesConsul(candidate capi.ConfigEntry) bool {
if !ok {
return false
}

specialEquality := cmp.Options{
cmp.FilterPath(func(path cmp.Path) bool {
return path.String() == "Redirect.Namespace"
}, cmp.Transformer("NormalizeNamespace", normalizeEmptyToDefault)),
cmp.FilterPath(func(path cmp.Path) bool {
return path.String() == "Redirect.Partition"
}, cmp.Transformer("NormalizePartition", normalizeEmptyToDefault)),
cmp.FilterPath(func(path cmp.Path) bool {
return path.String() == "Failover.Targets.Namespace"
}, cmp.Transformer("NormalizeNamespace", normalizeEmptyToDefault)),
cmp.FilterPath(func(path cmp.Path) bool {
return path.String() == "Failover.Targets.Partition"
}, cmp.Transformer("NormalizePartition", normalizeEmptyToDefault)),
}
// No datacenter is passed to ToConsul as we ignore the Meta field when checking for equality.
return cmp.Equal(in.ToConsul(""), configEntry, cmpopts.IgnoreFields(capi.ServiceResolverConfigEntry{}, "Partition", "Namespace", "Meta", "ModifyIndex", "CreateIndex"), cmpopts.IgnoreUnexported(), cmpopts.EquateEmpty())
return cmp.Equal(in.ToConsul(""), configEntry, cmpopts.IgnoreFields(capi.ServiceResolverConfigEntry{}, "Partition", "Namespace", "Meta", "ModifyIndex", "CreateIndex"), cmpopts.IgnoreUnexported(), cmpopts.EquateEmpty(), specialEquality)
}

func (in *ServiceResolver) ConsulGlobalResource() bool {
Expand Down
6 changes: 5 additions & 1 deletion control-plane/api/v1alpha1/serviceresolver_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"testing"
"time"

"github.com/hashicorp/consul-k8s/control-plane/api/common"
capi "github.com/hashicorp/consul/api"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/hashicorp/consul-k8s/control-plane/api/common"
)

func TestServiceResolver_MatchesConsul(t *testing.T) {
Expand Down Expand Up @@ -78,6 +79,7 @@ func TestServiceResolver_MatchesConsul(t *testing.T) {
Targets: []ServiceResolverFailoverTarget{
{Peer: "failover_peer3"},
{Partition: "failover_partition3", Namespace: "failover_namespace3"},
{Peer: "failover_peer4"},
},
},
},
Expand Down Expand Up @@ -126,6 +128,7 @@ func TestServiceResolver_MatchesConsul(t *testing.T) {
Service: "redirect",
ServiceSubset: "redirect_subset",
Namespace: "redirect_namespace",
Partition: "default",
Datacenter: "redirect_datacenter",
Peer: "redirect_peer",
},
Expand All @@ -146,6 +149,7 @@ func TestServiceResolver_MatchesConsul(t *testing.T) {
Targets: []capi.ServiceResolverFailoverTarget{
{Peer: "failover_peer3"},
{Partition: "failover_partition3", Namespace: "failover_namespace3"},
{Partition: "default", Peer: "failover_peer4"},
},
},
},
Expand Down
17 changes: 14 additions & 3 deletions control-plane/api/v1alpha1/servicerouter_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/hashicorp/consul-k8s/control-plane/api/common"
"github.com/hashicorp/consul-k8s/control-plane/namespaces"
capi "github.com/hashicorp/consul/api"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"

"github.com/hashicorp/consul-k8s/control-plane/api/common"
"github.com/hashicorp/consul-k8s/control-plane/namespaces"
)

func init() {
Expand Down Expand Up @@ -250,8 +251,18 @@ func (in *ServiceRouter) MatchesConsul(candidate capi.ConfigEntry) bool {
if !ok {
return false
}

specialEquality := cmp.Options{
cmp.FilterPath(func(path cmp.Path) bool {
return path.String() == "Routes.Destination.Namespace"
}, cmp.Transformer("NormalizeNamespace", normalizeEmptyToDefault)),
cmp.FilterPath(func(path cmp.Path) bool {
return path.String() == "Routes.Destination.Partition"
}, cmp.Transformer("NormalizePartition", normalizeEmptyToDefault)),
}

// No datacenter is passed to ToConsul as we ignore the Meta field when checking for equality.
return cmp.Equal(in.ToConsul(""), configEntry, cmpopts.IgnoreFields(capi.ServiceRouterConfigEntry{}, "Partition", "Namespace", "Meta", "ModifyIndex", "CreateIndex"), cmpopts.IgnoreUnexported(), cmpopts.EquateEmpty())
return cmp.Equal(in.ToConsul(""), configEntry, cmpopts.IgnoreFields(capi.ServiceRouterConfigEntry{}, "Partition", "Namespace", "Meta", "ModifyIndex", "CreateIndex"), cmpopts.IgnoreUnexported(), cmpopts.EquateEmpty(), specialEquality)
}

func (in *ServiceRouter) Validate(consulMeta common.ConsulMeta) error {
Expand Down
4 changes: 3 additions & 1 deletion control-plane/api/v1alpha1/servicerouter_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"time"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/consul-k8s/control-plane/api/common"
capi "github.com/hashicorp/consul/api"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/hashicorp/consul-k8s/control-plane/api/common"
)

// Test MatchesConsul.
Expand Down Expand Up @@ -155,6 +156,7 @@ func TestServiceRouter_MatchesConsul(t *testing.T) {
Service: "service",
ServiceSubset: "serviceSubset",
Namespace: "namespace",
Partition: "default",
PrefixRewrite: "prefixRewrite",
IdleTimeout: 1 * time.Second,
RequestTimeout: 1 * time.Second,
Expand Down
15 changes: 13 additions & 2 deletions control-plane/api/v1alpha1/servicesplitter_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/hashicorp/consul-k8s/control-plane/api/common"
capi "github.com/hashicorp/consul/api"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"

"github.com/hashicorp/consul-k8s/control-plane/api/common"
)

func init() {
Expand Down Expand Up @@ -165,8 +166,18 @@ func (in *ServiceSplitter) MatchesConsul(candidate capi.ConfigEntry) bool {
if !ok {
return false
}

specialEquality := cmp.Options{
cmp.FilterPath(func(path cmp.Path) bool {
return path.String() == "Splits.Namespace"
}, cmp.Transformer("NormalizeNamespace", normalizeEmptyToDefault)),
cmp.FilterPath(func(path cmp.Path) bool {
return path.String() == "Splits.Partition"
}, cmp.Transformer("NormalizePartition", normalizeEmptyToDefault)),
}

// No datacenter is passed to ToConsul as we ignore the Meta field when checking for equality.
return cmp.Equal(in.ToConsul(""), configEntry, cmpopts.IgnoreFields(capi.ServiceSplitterConfigEntry{}, "Partition", "Namespace", "Meta", "ModifyIndex", "CreateIndex"), cmpopts.IgnoreUnexported(), cmpopts.EquateEmpty())
return cmp.Equal(in.ToConsul(""), configEntry, cmpopts.IgnoreFields(capi.ServiceSplitterConfigEntry{}, "Partition", "Namespace", "Meta", "ModifyIndex", "CreateIndex"), cmpopts.IgnoreUnexported(), cmpopts.EquateEmpty(), specialEquality)
}

func (in *ServiceSplitter) Validate(consulMeta common.ConsulMeta) error {
Expand Down
4 changes: 3 additions & 1 deletion control-plane/api/v1alpha1/servicesplitter_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"testing"
"time"

"github.com/hashicorp/consul-k8s/control-plane/api/common"
capi "github.com/hashicorp/consul/api"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/hashicorp/consul-k8s/control-plane/api/common"
)

// Test MatchesConsul.
Expand Down Expand Up @@ -93,6 +94,7 @@ func TestServiceSplitter_MatchesConsul(t *testing.T) {
Service: "foo",
ServiceSubset: "bar",
Namespace: "baz",
Partition: "default",
RequestHeaders: &capi.HTTPHeaderModifiers{
Add: map[string]string{
"foo": "bar",
Expand Down

0 comments on commit 2d6534f

Please sign in to comment.