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

Expose the new metrics port in the all-pods service #489

Merged
merged 1 commit into from
Jan 27, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Changelog for Cass Operator, new PRs should update the `main / unreleased` secti
* [ENHANCEMENT] [#315](https://github.com/k8ssandra/cass-operator/issues/351) PodTemplateSpec allows setting Affinities, which are merged with the current rules. PodAntiAffinity behavior has changed, if allowMultipleWorkers is set to true the PodTemplateSpec antiAffinity rules are copied as is, otherwise merged with current restrictions. Prevent usage of deprecated rack.Zone (use topology.kubernetes.io/zone label instead), but allow removal of Zone.
* [BUGFIX] [#410](https://github.com/k8ssandra/cass-operator/issues/410) Fix installation in IPv6 only environment
* [BUGFIX] [#455](https://github.com/k8ssandra/cass-operator/issues/455) After task had completed, the running state would still say true
* [BUGFIX] [#488](https://github.com/k8ssandra/cass-operator/issues/488) Expose the new metrics port in services

## v1.13.1

Expand Down
11 changes: 10 additions & 1 deletion pkg/reconciliation/construct_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package reconciliation
// This file defines constructors for k8s service-related objects
import (
"net"
"strings"

api "github.com/k8ssandra/cass-operator/apis/cassandra/v1beta1"
"github.com/k8ssandra/cass-operator/pkg/oplabels"
Expand All @@ -32,7 +33,12 @@ func newServiceForCassandraDatacenter(dc *api.CassandraDatacenter) *corev1.Servi
namedServicePort("tls-native", 9142, 9142),
namedServicePort("mgmt-api", 8080, 8080),
namedServicePort("prometheus", 9103, 9103),
namedServicePort("thrift", 9160, 9160),
namedServicePort("metrics", 9000, 9000),
}

if strings.HasPrefix(dc.Spec.ServerVersion, "3.") || dc.Spec.ServerType == "dse" {
ports = append(ports,
namedServicePort("thrift", 9160, 9160))
}

if dc.Spec.DseWorkloads != nil {
Expand Down Expand Up @@ -258,6 +264,9 @@ func newAllPodsServiceForCassandraDatacenter(dc *api.CassandraDatacenter) *corev
{
Name: "prometheus", Port: 9103, TargetPort: intstr.FromInt(9103),
},
{
Name: "metrics", Port: 9000, TargetPort: intstr.FromInt(9000),
},
}

addAdditionalOptions(service, &dc.Spec.AdditionalServiceConfig.AllPodsService)
Expand Down
97 changes: 97 additions & 0 deletions pkg/reconciliation/construct_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

api "github.com/k8ssandra/cass-operator/apis/cassandra/v1beta1"
Expand Down Expand Up @@ -407,3 +408,99 @@ func TestAddingAdditionalLabels(t *testing.T) {
t.Errorf("service labels = %v, want %v", service.Labels, expected)
}
}

func TestServicePorts(t *testing.T) {
tests := []struct {
name string
dc *api.CassandraDatacenter
dcServicePorts []int32
allPodsServicePorts []int32
}{
{
name: "Cassandra 3.11.14",
dc: &api.CassandraDatacenter{
Spec: api.CassandraDatacenterSpec{
ClusterName: "bob",
ServerType: "cassandra",
ServerVersion: "3.11.14",
},
},
dcServicePorts: []int32{8080, 9000, 9042, 9103, 9142, 9160},
allPodsServicePorts: []int32{8080, 9000, 9042, 9103},
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder: shouldn't all-pods service expose the same ports as the dc service? But I didn't want to change that in this PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it depends what the all-ports service is used for.
If no one expressed any requirement around that, I agree we shouldn't change it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I understand the all-pods service is identical to the dc service, but also includes pods not ready. So it's not suitable to be used as a contact point for the drivers, but it is useful for metrics for instance. Maybe that's the reason why it doesn't expose so many ports. That said, following this logic it shouldn't expose 9042 either. Anyways 🤷‍♂️

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cass-operator sets allPods as the ServiceName for all pods to ensure that they get DNS working correctly. Using only the dcPods would make the DNS names fail in certain cases:

#18

},
{
name: "Cassandra 4.0.7",
dc: &api.CassandraDatacenter{
Spec: api.CassandraDatacenterSpec{
ClusterName: "bob",
ServerType: "cassandra",
ServerVersion: "4.0.7",
},
},
dcServicePorts: []int32{8080, 9000, 9042, 9103, 9142},
allPodsServicePorts: []int32{8080, 9000, 9042, 9103},
},
{
name: "DSE 6.8.31",
dc: &api.CassandraDatacenter{
Spec: api.CassandraDatacenterSpec{
ClusterName: "bob",
ServerType: "dse",
ServerVersion: "6.8.31",
},
},
dcServicePorts: []int32{8080, 9000, 9042, 9103, 9142, 9160},
allPodsServicePorts: []int32{8080, 9000, 9042, 9103},
},
{
name: "Cassandra 4.0.7 with custom ports",
dc: &api.CassandraDatacenter{
Spec: api.CassandraDatacenterSpec{
ClusterName: "bob",
ServerType: "cassandra",
ServerVersion: "4.0.7",
PodTemplateSpec: &corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "cassandra",
Ports: []corev1.ContainerPort{
{
Name: "metrics",
ContainerPort: 9004,
},
},
},
},
},
},
},
},
// FIXME: 9004 should be in the list of open ports
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is interesting: I copied this test from TestPorts, but I realized that if the user changes a managed port at container level, the change is not reflected at service level.

dcServicePorts: []int32{8080, 9000, 9042, 9103, 9142},
allPodsServicePorts: []int32{8080, 9000, 9042, 9103},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var getServicePorts = func(svc *corev1.Service) []int32 {
servicePorts := make([]int32, len(svc.Spec.Ports))
for i := 0; i < len(svc.Spec.Ports); i++ {
servicePorts[i] = svc.Spec.Ports[i].Port
}
return servicePorts
}
t.Run("dc service", func(t *testing.T) {
svc := newServiceForCassandraDatacenter(test.dc)
servicePorts := getServicePorts(svc)
assert.ElementsMatch(t, servicePorts, test.dcServicePorts)
})
t.Run("all pods service", func(t *testing.T) {
svc := newAllPodsServiceForCassandraDatacenter(test.dc)
servicePorts := getServicePorts(svc)
assert.ElementsMatch(t, servicePorts, test.allPodsServicePorts)
})
})
}
}