This is an automated email from the ASF dual-hosted git repository.
pjfanning pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/pekko-management.git
The following commit(s) were added to refs/heads/main by this push:
new 67ed21d1 Do not keep underscores when sanitising Kubernetes resource
paths (#1003)
67ed21d1 is described below
commit 67ed21d1a264f30c632bd17d59c2887f1bdc9a10
Author: PJ Fanning <[email protected]>
AuthorDate: Fri Oct 9 13:25:54 2026 +0100
Do not keep underscores when sanitising Kubernetes resource paths (#1003)
* Do not keep underscores when sanitising Kubernetes resource paths
Motivation:
The lease and PodCost path sanitisers strip characters with the regex
[^\d\w\-\.]. `\w` matches `_`, so underscores survive even though
Kubernetes resource names (RFC 1123 subdomains) do not allow them.
Modification:
Replace `\w` with an explicit `a-zA-Z` class in the precompiled
InvalidLeaseNameChars (lease, used by KubernetesApiImpl and
NativeKubernetesApiImpl pathForLease) and InvalidResourceNameChars
(rolling-update pathForPodCostResource) regexes.
Result:
Underscores are removed from the request path like any other invalid
character. Lease names are already made DNS 1039 compatible before they
reach the API, so this does not change paths for leases. A user-set
PodCost cr-name containing `_` was already rejected by the API server.
Tests:
- Updated lease path tests in KubernetesApiSpec and
NativeKubernetesApiSpec, new PodCost path test in rolling-update
KubernetesApiSpec: fail before the fix, pass after
- sbt lease-kubernetes/test, rolling-update-kubernetes/test: passed
- sbt scalafmtCheckAll: passed
References:
Follow-up from review of #1002
* Tidy Kubernetes resource path regex declarations
Motivation:
Review nits from #1002: the rolling-update InvalidResourceNameChars
had no explicit type, and both path regexes used double-escaped
string literals.
Modification:
Add a Regex type to InvalidResourceNameChars and use triple-quoted
literals for InvalidLeaseNameChars and InvalidResourceNameChars.
Result:
Same patterns, easier to read.
Tests:
- sbt lease-kubernetes/test, rolling-update-kubernetes/test,
scalafmtCheckAll: passed
References:
Follow-up from review of #1002
* Apply suggestions from code review
Co-authored-by: Philippus Baalman <[email protected]>
---------
Co-authored-by: Philippus Baalman <[email protected]>
---
.../lease/kubernetes/internal/AbstractKubernetesApiImpl.scala | 2 +-
.../coordination/lease/kubernetes/KubernetesApiSpec.scala | 4 ++--
.../lease/kubernetes/NativeKubernetesApiSpec.scala | 4 ++--
.../apache/pekko/rollingupdate/kubernetes/KubernetesApi.scala | 3 ++-
.../pekko/rollingupdate/kubernetes/KubernetesApiSpec.scala | 10 ++++++++++
5 files changed, 17 insertions(+), 6 deletions(-)
diff --git
a/lease-kubernetes/src/main/scala/org/apache/pekko/coordination/lease/kubernetes/internal/AbstractKubernetesApiImpl.scala
b/lease-kubernetes/src/main/scala/org/apache/pekko/coordination/lease/kubernetes/internal/AbstractKubernetesApiImpl.scala
index 2e778f5e..2754048b 100644
---
a/lease-kubernetes/src/main/scala/org/apache/pekko/coordination/lease/kubernetes/internal/AbstractKubernetesApiImpl.scala
+++
b/lease-kubernetes/src/main/scala/org/apache/pekko/coordination/lease/kubernetes/internal/AbstractKubernetesApiImpl.scala
@@ -39,7 +39,7 @@ import scala.util.matching.Regex
* INTERNAL API
*/
@InternalApi private[kubernetes] object AbstractKubernetesApiImpl {
- val InvalidLeaseNameChars: Regex = "[^\\d\\w\\-\\.]".r
+ val InvalidLeaseNameChars: Regex = """[^\da-zA-Z\-\.]""".r
}
/**
diff --git
a/lease-kubernetes/src/test/scala/org/apache/pekko/coordination/lease/kubernetes/KubernetesApiSpec.scala
b/lease-kubernetes/src/test/scala/org/apache/pekko/coordination/lease/kubernetes/KubernetesApiSpec.scala
index c7bbe383..1a572084 100644
---
a/lease-kubernetes/src/test/scala/org/apache/pekko/coordination/lease/kubernetes/KubernetesApiSpec.scala
+++
b/lease-kubernetes/src/test/scala/org/apache/pekko/coordination/lease/kubernetes/KubernetesApiSpec.scala
@@ -208,9 +208,9 @@ class KubernetesApiSpec
response shouldEqual Left(LeaseResource(Some(conflictedOwner),
updatedVersion, timestamp))
}
- "strip characters that are not allowed in a resource name from the lease
path" in {
+ "strip characters that are not valid in a resource name from the lease
path" in {
underTest.pathForLease("My_Lease.1@host:7355/x").futureValue.toString
shouldEqual
- "/apis/pekko.apache.org/v1/namespaces/lease/leases/my_lease.1host7355x"
+ "/apis/pekko.apache.org/v1/namespaces/lease/leases/mylease.1host7355x"
}
"remove lease via DELETE" in {
diff --git
a/lease-kubernetes/src/test/scala/org/apache/pekko/coordination/lease/kubernetes/NativeKubernetesApiSpec.scala
b/lease-kubernetes/src/test/scala/org/apache/pekko/coordination/lease/kubernetes/NativeKubernetesApiSpec.scala
index 7b952a29..e2c3c707 100644
---
a/lease-kubernetes/src/test/scala/org/apache/pekko/coordination/lease/kubernetes/NativeKubernetesApiSpec.scala
+++
b/lease-kubernetes/src/test/scala/org/apache/pekko/coordination/lease/kubernetes/NativeKubernetesApiSpec.scala
@@ -189,9 +189,9 @@ class NativeKubernetesApiSpec
fromRFC3339MicroString(timestamp)))
}
- "strip characters that are not allowed in a resource name from the lease
path" in {
+ "strip characters that are not valid in a resource name from the lease
path" in {
underTest.pathForLease("My_Lease.1@host:7355/x").futureValue.toString
shouldEqual
-
"/apis/coordination.k8s.io/v1/namespaces/lease/leases/my_lease.1host7355x"
+ "/apis/coordination.k8s.io/v1/namespaces/lease/leases/mylease.1host7355x"
}
"remove lease via DELETE" in {
diff --git
a/rolling-update-kubernetes/src/main/scala/org/apache/pekko/rollingupdate/kubernetes/KubernetesApi.scala
b/rolling-update-kubernetes/src/main/scala/org/apache/pekko/rollingupdate/kubernetes/KubernetesApi.scala
index 5eddc8d0..c6111761 100644
---
a/rolling-update-kubernetes/src/main/scala/org/apache/pekko/rollingupdate/kubernetes/KubernetesApi.scala
+++
b/rolling-update-kubernetes/src/main/scala/org/apache/pekko/rollingupdate/kubernetes/KubernetesApi.scala
@@ -17,6 +17,7 @@ import java.text.Normalizer
import java.util.Locale
import scala.concurrent.Future
+import scala.util.matching.Regex
import org.apache.pekko
import pekko.Done
@@ -80,7 +81,7 @@ private[pekko] final case class PodCost(podName: String,
cost: Int, address: Str
@InternalApi private[pekko] object KubernetesApi {
private val InvalidDNS1039Chars = "[^-a-z0-9]".r
- private[kubernetes] val InvalidResourceNameChars = "[^\\d\\w\\-\\.]".r
+ private[kubernetes] val InvalidResourceNameChars: Regex =
"""[^\da-zA-Z\-\.]""".r
/**
* Removes from the leading and trailing positions the specified characters.
diff --git
a/rolling-update-kubernetes/src/test/scala/org/apache/pekko/rollingupdate/kubernetes/KubernetesApiSpec.scala
b/rolling-update-kubernetes/src/test/scala/org/apache/pekko/rollingupdate/kubernetes/KubernetesApiSpec.scala
index 18dcd201..410f8745 100644
---
a/rolling-update-kubernetes/src/test/scala/org/apache/pekko/rollingupdate/kubernetes/KubernetesApiSpec.scala
+++
b/rolling-update-kubernetes/src/test/scala/org/apache/pekko/rollingupdate/kubernetes/KubernetesApiSpec.scala
@@ -19,6 +19,8 @@ import com.github.tomakehurst.wiremock.client.MappingBuilder
import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder
import com.github.tomakehurst.wiremock.client.WireMock
import com.github.tomakehurst.wiremock.client.WireMock.aResponse
+import com.github.tomakehurst.wiremock.client.WireMock.delete
+import com.github.tomakehurst.wiremock.client.WireMock.deleteRequestedFor
import com.github.tomakehurst.wiremock.client.WireMock.get
import com.github.tomakehurst.wiremock.client.WireMock.stubFor
import com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo
@@ -27,6 +29,7 @@ import com.github.tomakehurst.wiremock.matching.EqualToPattern
import com.github.tomakehurst.wiremock.stubbing.Scenario
import com.typesafe.config.ConfigFactory
import org.apache.pekko
+import pekko.Done
import pekko.actor.ActorSystem
import pekko.testkit.EventFilter
import pekko.testkit.ImplicitSender
@@ -238,5 +241,12 @@ class KubernetesApiSpec
kubernetesApi.readRevision().futureValue should be("1")
}
}
+
+ "strip characters that are not valid in a resource name from the PodCost
path" in {
+ val path =
urlEqualTo(s"/apis/pekko.apache.org/v1/namespaces/$namespace/podcosts/myapp-cr.1x")
+ stubFor(delete(path).willReturn(aResponse().withStatus(200)))
+ kubernetesApi.removePodCostResource("My_App-CR.1@x").futureValue
shouldEqual Done
+ WireMock.verify(deleteRequestedFor(path))
+ }
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]