Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Consul namespaces for service-intentions #362

Merged
merged 4 commits into from
Oct 30, 2020
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
2 changes: 1 addition & 1 deletion api/v1alpha1/servicedefaults_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func (e ExposeConfig) validate(path *field.Path) []*field.Error {
pathCfg.Path,
`must begin with a '/'`))
}
if !sliceContains(protocols, pathCfg.Protocol) {
if pathCfg.Protocol != "" && !sliceContains(protocols, pathCfg.Protocol) {
errs = append(errs, field.Invalid(
indexPath.Child("protocol"),
pathCfg.Protocol,
Expand Down
11 changes: 11 additions & 0 deletions api/v1alpha1/servicedefaults_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,17 @@ func TestServiceDefaults_Validate(t *testing.T) {
MeshGateway: MeshGatewayConfig{
Mode: "remote",
},
Expose: ExposeConfig{
Checks: false,
Paths: []ExposePath{
{
ListenerPort: 100,
Path: "/bar",
LocalPathPort: 1000,
Protocol: "",
},
},
},
},
},
expectedErrMsg: "",
Expand Down
52 changes: 41 additions & 11 deletions api/v1alpha1/serviceintentions_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/hashicorp/consul-k8s/api/common"
"github.com/hashicorp/consul-k8s/namespaces"
"github.com/hashicorp/consul/api"
capi "github.com/hashicorp/consul/api"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -153,10 +154,11 @@ func (in *ServiceIntentions) SyncedConditionStatus() corev1.ConditionStatus {

func (in *ServiceIntentions) ToConsul(datacenter string) api.ConfigEntry {
return &capi.ServiceIntentionsConfigEntry{
Kind: in.ConsulKind(),
Name: in.Spec.Destination.Name,
Sources: in.Spec.Sources.toConsul(),
Meta: meta(datacenter),
Kind: in.ConsulKind(),
Name: in.Spec.Destination.Name,
Namespace: in.Spec.Destination.Namespace,
Sources: in.Spec.Sources.toConsul(),
Meta: meta(datacenter),
}
}

Expand Down Expand Up @@ -249,6 +251,9 @@ func (in *ServiceIntentions) MatchesConsul(candidate api.ConfigEntry) bool {
func (in *ServiceIntentions) Validate() error {
var errs field.ErrorList
path := field.NewPath("spec")
if len(in.Spec.Sources) == 0 {
errs = append(errs, field.Required(path.Child("sources"), `at least one source must be specified`))
}
for i, source := range in.Spec.Sources {
if len(source.Permissions) > 0 && source.Action != "" {
asJSON, _ := json.Marshal(source)
Expand Down Expand Up @@ -297,16 +302,41 @@ func (in *IntentionHTTPPermission) validate(path *field.Path) field.ErrorList {
return errs
}

// Default sets zero value fields on this object to their defaults.
func (in *ServiceIntentions) Default() {
if in.Spec.Destination.Namespace == "" {
in.Spec.Destination.Namespace = in.Namespace
// Default sets the namespace field on spec.destination to their default values if namespaces are enabled.
func (in *ServiceIntentions) Default(consulNamespacesEnabled bool, destinationNamespace string, mirroring bool, prefix string) {
// If namespaces are enabled we want to set the destination namespace field to it's
// default. If namespaces are not enabled (i.e. OSS) we don't set the
// namespace fields because this would cause errors
// making API calls (because namespace fields can't be set in OSS).
if consulNamespacesEnabled {
namespace := namespaces.ConsulNamespace(in.Namespace, consulNamespacesEnabled, destinationNamespace, mirroring, prefix)
if in.Spec.Destination.Namespace == "" {
in.Spec.Destination.Namespace = namespace
}
}
for _, source := range in.Spec.Sources {
if source.Namespace == "" {
source.Namespace = in.Namespace
}

// ValidateNamespaces returns an error if spec.destination.namespace or spec.sources[i].namespace
// is set but namespaces are disabled.
func (in *ServiceIntentions) ValidateNamespaces(namespacesEnabled bool) error {
var errs field.ErrorList
path := field.NewPath("spec")
if !namespacesEnabled {
if in.Spec.Destination.Namespace != "" {
errs = append(errs, field.Invalid(path.Child("destination").Child("namespace"), in.Spec.Destination.Namespace, `consul namespaces must be enabled to set destination.namespace`))
}
for i, source := range in.Spec.Sources {
if source.Namespace != "" {
errs = append(errs, field.Invalid(path.Child("sources").Index(i).Child("namespace"), source.Namespace, `consul namespaces must be enabled to set source.namespace`))
}
}
}
if len(errs) > 0 {
return apierrors.NewInvalid(
schema.GroupKind{Group: ConsulHashicorpGroup, Kind: common.ServiceIntentions},
in.KubernetesName(), errs)
}
return nil
}

func (in IntentionAction) validate(path *field.Path) *field.Error {
Expand Down
Loading