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

Fix sanitization of DC label (fixes #622) #623

Merged
merged 3 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -13,6 +13,7 @@ Changelog for Cass Operator, new PRs should update the `main / unreleased` secti

* [CHANGE] [#618](https://github.com/k8ssandra/cass-operator/issues/618) Update dependencies to support controller-runtime 0.17.2, modify required parts.
* [ENHANCEMENT] [#532](https://github.com/k8ssandra/cass-operator/issues/532) Instead of rejecting updates/creates with deprecated fields, return kubectl warnings.
* [BUGFIX] [#622](https://github.com/k8ssandra/cass-operator/issues/622) Fix sanitization of DC label

## v1.19.0

Expand Down
2 changes: 1 addition & 1 deletion apis/cassandra/v1beta1/cassandradatacenter_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ func (dc *CassandraDatacenter) SetCondition(condition DatacenterCondition) {
// GetDatacenterLabels ...
func (dc *CassandraDatacenter) GetDatacenterLabels() map[string]string {
labels := dc.GetClusterLabels()
labels[DatacenterLabel] = dc.SanitizedName()
labels[DatacenterLabel] = CleanLabelValue(dc.DatacenterName())
return labels
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/reconciliation/construct_statefulset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ func Test_newStatefulSetForCassandraDatacenter_dcNameOverride(t *testing.T) {
oplabels.NameLabel: oplabels.NameLabelValue,
oplabels.CreatedByLabel: oplabels.CreatedByLabelValue,
oplabels.VersionLabel: "4.0.1",
api.DatacenterLabel: "mysuperdc",
api.DatacenterLabel: "MySuperDC",
api.ClusterLabel: "piclem",
api.RackLabel: dc.Spec.Racks[0].Name,
}
Expand All @@ -648,7 +648,7 @@ func Test_newStatefulSetForCassandraDatacenter_dcNameOverride(t *testing.T) {
oplabels.NameLabel: oplabels.NameLabelValue,
oplabels.CreatedByLabel: oplabels.CreatedByLabelValue,
oplabels.VersionLabel: "4.0.1",
api.DatacenterLabel: "mysuperdc",
api.DatacenterLabel: "MySuperDC",
api.ClusterLabel: "piclem",
api.RackLabel: dc.Spec.Racks[0].Name,
api.CassNodeState: stateReadyToStart,
Expand Down
2 changes: 1 addition & 1 deletion pkg/reconciliation/reconcile_datacenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (rc *ReconciliationContext) listPVCs() (*corev1.PersistentVolumeClaimList,
rc.ReqLogger.Info("reconciler::listPVCs")

selector := map[string]string{
api.DatacenterLabel: rc.Datacenter.Name,
api.DatacenterLabel: api.CleanLabelValue(rc.Datacenter.DatacenterName()),
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 was probably a bug.

}

listOptions := &client.ListOptions{
Expand Down
2 changes: 1 addition & 1 deletion tests/decommission_dc/decommission_dc_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var (
dc2Name = "dc2"
dc1Yaml = "../testdata/default-two-rack-two-node-dc.yaml"
dc2Yaml = "../testdata/default-two-rack-two-node-dc2.yaml"
dc1Label = fmt.Sprintf("cassandra.datastax.com/datacenter=%s", api.CleanupForKubernetes(dc1OverrideName))
dc1Label = fmt.Sprintf("cassandra.datastax.com/datacenter=%s", api.CleanLabelValue(dc1OverrideName))
dc2Label = fmt.Sprintf("cassandra.datastax.com/datacenter=%s", dc2Name)
seedLabel = "cassandra.datastax.com/seed-node=true"
taskYaml = "../testdata/tasks/rebuild_task.yaml"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ var _ = Describe(testName, func() {
step = "get ready pods"
json = "jsonpath={.items[*].status.containerStatuses[0].ready}"
k = kubectl.Get("pods").
WithLabel(fmt.Sprintf("cassandra.datastax.com/datacenter=%s", api.CleanupForKubernetes(dcNameOverride))).
WithLabel(fmt.Sprintf("cassandra.datastax.com/datacenter=%s", api.CleanLabelValue(dcNameOverride))).
WithFlag("field-selector", "status.phase=Running").
FormatOutput(json)

Expand All @@ -105,7 +105,7 @@ var _ = Describe(testName, func() {
// Verify each pod does have the annotation..
json := `jsonpath={.items[0].metadata.annotations.control\.k8ssandra\.io/restartedAt}`
k = kubectl.Get("pods").
WithLabel(fmt.Sprintf("cassandra.datastax.com/datacenter=%s", api.CleanupForKubernetes(dcNameOverride))).
WithLabel(fmt.Sprintf("cassandra.datastax.com/datacenter=%s", api.CleanLabelValue(dcNameOverride))).
WithFlag("field-selector", "status.phase=Running").
FormatOutput(json)
ns.WaitForOutputPatternAndLog(step, k, `^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$`, 360)
Expand Down
10 changes: 5 additions & 5 deletions tests/test_all_the_things/test_all_the_things_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var (
dcNameOverride = "My_Super_Dc"
dcYaml = "../testdata/default-two-rack-two-node-dc.yaml"
dcResource = fmt.Sprintf("CassandraDatacenter/%s", dcName)
dcLabel = fmt.Sprintf("cassandra.datastax.com/datacenter=%s", api.CleanupForKubernetes(dcNameOverride))
dcLabel = fmt.Sprintf("cassandra.datastax.com/datacenter=%s", api.CleanLabelValue(dcNameOverride))
ns = ginkgo_util.NewWrapper(testName, namespace)
)

Expand Down Expand Up @@ -89,7 +89,7 @@ var _ = Describe(testName, func() {
ns.WaitForDatacenterOperatorProgress(dcName, "Updating", 60)
ns.WaitForDatacenterConditionWithTimeout(dcName, "ScalingUp", string(corev1.ConditionFalse), 1200)
// Ensure that when 'ScaleUp' becomes 'false' that our pods are in fact up and running
Expect(len(ns.GetDatacenterReadyPodNames(api.CleanupForKubernetes(dcNameOverride)))).To(Equal(4))
Expect(len(ns.GetDatacenterReadyPodNames(api.CleanLabelValue(dcNameOverride)))).To(Equal(4))

ns.ExpectDoneReconciling(dcName)
ns.WaitForDatacenterReady(dcName)
Expand All @@ -114,7 +114,7 @@ var _ = Describe(testName, func() {
FormatOutput(json)
ns.WaitForOutputAndLog(step, k, "4", 20)

ns.WaitForDatacenterToHaveNoPods(api.CleanupForKubernetes(dcNameOverride))
ns.WaitForDatacenterToHaveNoPods(api.CleanLabelValue(dcNameOverride))

step = "resume the dc"
json = "{\"spec\": {\"stopped\": false}}"
Expand All @@ -133,7 +133,7 @@ var _ = Describe(testName, func() {
wg.Add(1)
go func() {
k = kubectl.Logs("-f").
WithLabel(fmt.Sprintf("statefulset.kubernetes.io/pod-name=cluster1-%s-r1-sts-0", api.CleanupForKubernetes(dcNameOverride))).
WithLabel(fmt.Sprintf("statefulset.kubernetes.io/pod-name=cluster1-%s-r1-sts-0", api.CleanLabelValue(dcNameOverride))).
Copy link
Contributor

Choose a reason for hiding this comment

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

issue: This is wrong and the test shouldn't pass if we mess up naming of the StatefulSet. The StatefulSet name must adhere to the CleanupForKubernetes process (minimum), not CleanLabelValue as those values are not allowed in the StatefulSet's name.

Although we're polling for label value here, the DNS name of the pod matters. The name defined must match what's in here:

https://github.com/k8ssandra/cass-operator/blob/master/pkg/reconciliation/construct_statefulset.go#L29

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, my bad, I saw a label and just changed the function, not realizing that the value was actually referencing a resource name. I'll revert this.

WithFlag("container", "cassandra")
output, err := ns.Output(k)
Expect(err).ToNot(HaveOccurred())
Expand All @@ -148,7 +148,7 @@ var _ = Describe(testName, func() {

found, err := regexp.MatchString("node/drain status=200 OK", logOutput)
if err == nil && !found {
ns.Log(fmt.Sprintf("logOutput, pod: statefulset.kubernetes.io/pod-name=cluster1-%s-r1-sts-0 => %s", api.CleanupForKubernetes(dcNameOverride), logOutput))
ns.Log(fmt.Sprintf("logOutput, pod: statefulset.kubernetes.io/pod-name=cluster1-%s-r1-sts-0 => %s", api.CleanLabelValue(dcNameOverride), logOutput))
}
if err != nil {
ns.Log(fmt.Sprintf("Regexp parsing failed: %v", err))
Expand Down
4 changes: 2 additions & 2 deletions tests/util/ginkgo/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ func (ns *NsWrapper) WaitForDatacenterReadyPodCountWithTimeout(dcName string, co
step := "waiting for the node to become ready"
json := "jsonpath={.items[*].status.containerStatuses[0].ready}"
k := kubectl.Get("pods").
WithLabel(fmt.Sprintf("cassandra.datastax.com/datacenter=%s", api.CleanupForKubernetes(dcName))).
WithLabel(fmt.Sprintf("cassandra.datastax.com/datacenter=%s", api.CleanLabelValue(dcName))).
WithFlag("field-selector", "status.phase=Running").
FormatOutput(json)
ns.WaitForOutputAndLog(step, k, duplicate("true", count), timeout)
Expand Down Expand Up @@ -516,7 +516,7 @@ func (ns *NsWrapper) GetDatacenterPodNames(dcName string) []string {
func (ns *NsWrapper) GetDatacenterReadyPodNames(dcName string) []string {
json := "jsonpath={.items[?(@.status.containerStatuses[0].ready==true)].metadata.name}"
k := kubectl.Get("pods").
WithFlag("selector", fmt.Sprintf("cassandra.datastax.com/datacenter=%s", api.CleanupForKubernetes(dcName))).
WithFlag("selector", fmt.Sprintf("cassandra.datastax.com/datacenter=%s", api.CleanLabelValue(dcName))).
FormatOutput(json)

output := ns.OutputPanic(k)
Expand Down
Loading