diff --git a/cli/src/deploy.rs b/cli/src/deploy.rs index 6312678..c9f7342 100644 --- a/cli/src/deploy.rs +++ b/cli/src/deploy.rs @@ -183,7 +183,12 @@ pub fn deploy(options: Options<'_>, host: &Host) -> Result { service: project.name.clone(), revision, image, - url: crate::lifecycle::service_status_url(&observed, &installed)?, + url: crate::lifecycle::service_status_url( + &observed, + &project.name, + options.namespace, + &installed, + )?, exposed, }) } @@ -333,15 +338,20 @@ fn wait_for_deployment( }; Ok((deployment_ready(&service, &revision, revision_name) && ready(&route) - && visibility_ready(&service, &route, exposed)?) + && visibility_ready(&service, &route, service_name, exposed)?) .then_some(service)) }, ) } -fn visibility_ready(service: &Value, route: &Value, exposed: bool) -> Result { +fn visibility_ready( + service: &Value, + route: &Value, + service_name: &str, + exposed: bool, +) -> Result { for resource in [service, route] { - let url = crate::lifecycle::observed_service_url(resource)?; + let url = crate::lifecycle::observed_service_url(resource, service_name, "default")?; let internal = url .host_str() .is_some_and(|host| host.ends_with(".svc.cluster.local")); @@ -357,10 +367,7 @@ fn visibility_ready(service: &Value, route: &Value, exposed: bool) -> Result bool { - let generation = service["metadata"]["generation"].as_i64(); - let observed = service["status"]["observedGeneration"].as_i64(); - generation.is_some() - && observed.is_some_and(|observed| Some(observed) >= generation) + kube::observation_current(service) && service["status"]["latestCreatedRevisionName"] == revision_name && service["status"]["latestReadyRevisionName"] == revision_name && ready(service) @@ -395,20 +402,33 @@ mod tests { }, "status":{"url":"http://demo.default.svc.cluster.local"} }); - assert!(visibility_ready(&public, &public, true).unwrap()); - assert!(visibility_ready(&internal, &internal, false).unwrap()); - assert!(!visibility_ready(&public, &internal, true).unwrap()); - assert!(!visibility_ready(&internal, &public, false).unwrap()); + assert!(visibility_ready(&public, &public, "demo", true).unwrap()); + assert!(visibility_ready(&internal, &internal, "demo", false).unwrap()); + assert!(!visibility_ready(&public, &internal, "demo", true).unwrap()); + assert!(!visibility_ready(&internal, &public, "demo", false).unwrap()); let mut stale = internal.clone(); stale["metadata"]["labels"] = json!({}); - assert!(!visibility_ready(&stale, &stale, true).unwrap()); + assert!(!visibility_ready(&stale, &stale, "demo", true).unwrap()); let mut stale = public.clone(); stale["metadata"]["labels"] = internal["metadata"]["labels"].clone(); - assert!(!visibility_ready(&stale, &stale, false).unwrap()); - assert!(visibility_ready(&json!({}), &public, true).is_err()); + assert!(!visibility_ready(&stale, &stale, "demo", false).unwrap()); + assert!(visibility_ready(&json!({}), &public, "demo", true).is_err()); let mut secure_internal = internal.clone(); secure_internal["status"]["url"] = json!("https://demo.default.svc.cluster.local"); - assert!(visibility_ready(&secure_internal, &secure_internal, false).is_err()); + assert!(visibility_ready(&secure_internal, &secure_internal, "demo", false).is_err()); + for (field, observed) in [("name", "other"), ("namespace", "other")] { + let mut unrelated = public.clone(); + unrelated["metadata"][field] = json!(observed); + unrelated["status"]["url"] = json!(format!( + "http://{}.{}.example.com", + unrelated["metadata"]["name"].as_str().unwrap(), + unrelated["metadata"]["namespace"].as_str().unwrap() + )); + assert!(visibility_ready(&public, &unrelated, "demo", true).is_err()); + } + let mut custom = public.clone(); + custom["status"]["url"] = json!("https://api.customer.example"); + assert!(visibility_ready(&custom, &custom, "demo", true).unwrap()); } #[test] @@ -442,6 +462,20 @@ mod tests { &revision, "demo-00002" )); + let mut weighted = service(2, "demo-00002"); + weighted["spec"]["traffic"] = json!([ + {"revisionName":"demo-00001","percent":25}, + {"revisionName":"demo-00002","percent":75} + ]); + weighted["status"]["traffic"] = weighted["spec"]["traffic"].clone(); + for _ in 0..2 { + assert!(deployment_ready(&weighted, &revision, "demo-00002")); + } + weighted["status"]["traffic"] = json!([ + {"revisionName":"demo-00001","percent":100}, + {"revisionName":"demo-00002","percent":0} + ]); + assert!(deployment_ready(&weighted, &revision, "demo-00002")); } #[test] diff --git a/cli/src/event.rs b/cli/src/event.rs index a168ff8..616a62d 100644 --- a/cli/src/event.rs +++ b/cli/src/event.rs @@ -66,7 +66,12 @@ pub fn publish(options: Options<'_>, host: &Host) -> Result { !crate::kube::cluster_local(&service), "service is cluster-local; pass --url with a reachable endpoint, such as a port-forward URL" ); - let url = crate::lifecycle::service_status_url(&service, &kube.installation()?)?; + let url = crate::lifecycle::service_status_url( + &service, + &project.name, + "default", + &kube.installation()?, + )?; ensure!( !url.is_empty(), "cluster has no public service domain; pass --url or configure the provider's domain" diff --git a/cli/src/kube.rs b/cli/src/kube.rs index dc7a6f5..f8623e3 100644 --- a/cli/src/kube.rs +++ b/cli/src/kube.rs @@ -21,6 +21,12 @@ pub fn cluster_local(service: &Value) -> bool { service["metadata"]["labels"][VISIBILITY_LABEL].as_str() == Some("cluster-local") } +pub(crate) fn observation_current(service: &Value) -> bool { + let generation = service["metadata"]["generation"].as_i64(); + let observed = service["status"]["observedGeneration"].as_i64(); + generation.is_some() && observed.is_some_and(|observed| Some(observed) >= generation) +} + fn normalized_kubeconfig(paths: std::ffi::OsString) -> Result { let paths = std::env::split_paths(&paths) .map(|path| { diff --git a/cli/src/lifecycle.rs b/cli/src/lifecycle.rs index acb3be2..12948b3 100644 --- a/cli/src/lifecycle.rs +++ b/cli/src/lifecycle.rs @@ -70,12 +70,17 @@ pub fn status(directory: &Path, context: Option<&str>, host: &Host) -> Result, host: &Host) -> Result Result> { Ok(active) } -pub(crate) fn observed_service_url(service: &Value) -> Result { - let name = service - .pointer("/metadata/name") - .and_then(Value::as_str) - .context("Knative Service has no name")?; - let namespace = service - .pointer("/metadata/namespace") - .and_then(Value::as_str) - .context("Knative Service has no namespace")?; +pub(crate) fn observed_service_url(service: &Value, name: &str, namespace: &str) -> Result { + ensure!( + service.pointer("/metadata/name").and_then(Value::as_str) == Some(name) + && service + .pointer("/metadata/namespace") + .and_then(Value::as_str) + == Some(namespace), + "Knative observation does not match Service {namespace}/{name}" + ); let observed = service .pointer("/status/url") .and_then(Value::as_str) @@ -330,15 +335,23 @@ pub(crate) fn observed_service_url(service: &Value) -> Result { "Knative Service URL must not contain a path, query or fragment" ); let host = url.host_str().context("Knative Service URL has no host")?; - ensure!( - host.starts_with(&format!("{name}.{namespace}.")), - "Knative Service URL does not match the Service" - ); + // Public hosts can use custom domains. Internal hosts encode the Service identity. + if host.ends_with(".svc.cluster.local") { + ensure!( + host == format!("{name}.{namespace}.svc.cluster.local"), + "Knative Service URL does not match Service {namespace}/{name}" + ); + } Ok(url) } -pub(crate) fn service_status_url(service: &Value, installation: &Installation) -> Result { - let mut url = observed_service_url(service)?; +pub(crate) fn service_status_url( + service: &Value, + name: &str, + namespace: &str, + installation: &Installation, +) -> Result { + let mut url = observed_service_url(service, name, namespace)?; if kube::cluster_local(service) { ensure!( url.scheme() == "http" @@ -356,9 +369,6 @@ pub(crate) fn service_status_url(service: &Value, installation: &Installation) - .is_some_and(|host| host.ends_with(".svc.cluster.local")), "public Knative Service URL is cluster-local" ); - if installation.domain.ends_with(".invalid") { - return Ok(String::new()); - } url.set_scheme("https") .map_err(|()| anyhow::anyhow!("Knative Service URL cannot use HTTPS"))?; match installation.https_port.as_deref() { @@ -553,7 +563,7 @@ mod tests { #[test] fn status_url_preserves_observed_host_and_cluster_visibility() { - let installation = Installation { + let mut installation = Installation { data: BTreeMap::new(), domain: "localhost".to_owned(), https_port: Some("8443".to_owned()), @@ -563,7 +573,7 @@ mod tests { "status":{"url":"http://custom.default.example.com"} }); assert_eq!( - service_status_url(&external, &installation).unwrap(), + service_status_url(&external, "custom", "default", &installation).unwrap(), "https://custom.default.example.com:8443" ); let internal = json!({ @@ -575,7 +585,7 @@ mod tests { "status":{"url":"http://demo.default.svc.cluster.local"} }); assert_eq!( - service_status_url(&internal, &installation).unwrap(), + service_status_url(&internal, "demo", "default", &installation).unwrap(), "http://demo.default.svc.cluster.local" ); let unrelated = json!({ @@ -586,12 +596,12 @@ mod tests { }, "status":{"url":"ftp://other.other.svc.cluster.local"} }); - assert!(service_status_url(&unrelated, &installation).is_err()); + assert!(service_status_url(&unrelated, "demo", "default", &installation).is_err()); let wrong_service = json!({ - "metadata":{"name":"demo","namespace":"default"}, + "metadata":{"name":"other","namespace":"other"}, "status":{"url":"http://other.other.example.com"} }); - assert!(service_status_url(&wrong_service, &installation).is_err()); + assert!(service_status_url(&wrong_service, "demo", "default", &installation).is_err()); for observed in [ "https://user@demo.default.example.com", "http://demo.default.example.com/path", @@ -601,13 +611,13 @@ mod tests { "metadata":{"name":"demo","namespace":"default"}, "status":{"url":observed} }); - assert!(service_status_url(&invalid, &installation).is_err()); + assert!(service_status_url(&invalid, "demo", "default", &installation).is_err()); } let public_internal = json!({ "metadata":{"name":"demo","namespace":"default"}, "status":{"url":"http://demo.default.svc.cluster.local"} }); - assert!(service_status_url(&public_internal, &installation).is_err()); + assert!(service_status_url(&public_internal, "demo", "default", &installation).is_err()); let secure_internal = json!({ "metadata":{ "name":"demo", @@ -616,6 +626,38 @@ mod tests { }, "status":{"url":"https://demo.default.svc.cluster.local"} }); - assert!(service_status_url(&secure_internal, &installation).is_err()); + assert!(service_status_url(&secure_internal, "demo", "default", &installation).is_err()); + installation.domain = "unconfigured.invalid".to_owned(); + assert_eq!( + service_status_url(&external, "custom", "default", &installation).unwrap(), + "https://custom.default.example.com:8443" + ); + for (observed, port, expected) in [ + ( + "http://api.customer.example", + Some("8443"), + "https://api.customer.example:8443", + ), + ( + "https://checkout.example.org", + Some("8443"), + "https://checkout.example.org:8443", + ), + ( + "http://api.customer.example", + None, + "https://api.customer.example", + ), + ] { + installation.https_port = port.map(str::to_owned); + let custom = json!({ + "metadata":{"name":"custom","namespace":"default"}, + "status":{"url":observed} + }); + assert_eq!( + service_status_url(&custom, "custom", "default", &installation).unwrap(), + expected + ); + } } } diff --git a/cli/tests/kubernetes.rs b/cli/tests/kubernetes.rs index 4f298e3..2e496d2 100644 --- a/cli/tests/kubernetes.rs +++ b/cli/tests/kubernetes.rs @@ -214,6 +214,138 @@ fn deploy_uses_knative_service_with_snapshot_revision_metadata() { ); } +#[cfg(unix)] +#[test] +fn repeated_status_validates_service_observations_without_changing_weighted_traffic() { + use std::os::unix::fs::PermissionsExt; + use std::process::Command; + + use serde_json::{Value, json}; + + let directory = tempfile::tempdir().unwrap(); + std::fs::write( + directory.path().join("lampyr.yaml"), + r#"{ + "apiVersion":"lampyr.dev/v1alpha1","kind":"Project", + "name":"demo","runtime":"nodejs", + "application":{"type":"standalone","command":"node /app/app.js"}, + "resources":{"cpu":"100m","memory":"128Mi"} + }"#, + ) + .unwrap(); + Project::load(directory.path()).unwrap(); + let kubectl = directory.path().join("kubectl"); + std::fs::write( + &kubectl, + r#"#!/bin/sh +case "$*" in + *"get service.serving.knative.dev demo "*) /bin/cat "$SERVICE_OBSERVATION";; + *"get configmap lampyr-install "*) printf '{"data":{"domain":"localhost","httpsPort":"8443"}}';; + *"get capacitygrants"*) ;; + *) printf 'unexpected kubectl call: %s\n' "$*" >&2; exit 1;; +esac +"#, + ) + .unwrap(); + std::fs::set_permissions(&kubectl, std::fs::Permissions::from_mode(0o700)).unwrap(); + let observation = directory.path().join("service.json"); + let current = json!({ + "metadata":{"name":"demo","namespace":"default","generation":3}, + "status":{ + "observedGeneration":3, + "latestCreatedRevisionName":"demo-00002", + "latestReadyRevisionName":"demo-00002", + "url":"http://demo.default.example.com", + "conditions":[{"type":"Ready","status":"True"}], + "traffic":[ + {"revisionName":"demo-00001","percent":25}, + {"revisionName":"demo-00002","percent":75} + ] + } + }); + let mut cases = vec![( + "public", + current.clone(), + Some("https://demo.default.example.com:8443"), + )]; + let mut custom = current.clone(); + custom["status"]["url"] = json!("http://api.customer.example"); + cases.push(("custom", custom, Some("https://api.customer.example:8443"))); + let mut internal = current.clone(); + internal["metadata"]["labels"][kube::VISIBILITY_LABEL] = json!("cluster-local"); + internal["status"]["url"] = json!("http://demo.default.svc.cluster.local"); + cases.push(( + "internal", + internal, + Some("http://demo.default.svc.cluster.local"), + )); + let mut stale = current.clone(); + stale["status"]["observedGeneration"] = json!(2); + cases.push(("stale generation", stale, None)); + let mut missing_generation = current.clone(); + missing_generation["status"] + .as_object_mut() + .unwrap() + .remove("observedGeneration"); + cases.push(("missing observed generation", missing_generation, None)); + let mut wrong_service = current.clone(); + wrong_service["metadata"]["name"] = json!("other"); + wrong_service["status"]["url"] = json!("http://other.default.example.com"); + cases.push(("wrong service", wrong_service, None)); + let mut wrong_namespace = current.clone(); + wrong_namespace["metadata"]["namespace"] = json!("other"); + wrong_namespace["status"]["url"] = json!("http://demo.other.example.com"); + cases.push(("wrong namespace", wrong_namespace, None)); + for (name, url) in [ + ("missing URL", Value::Null), + ("malformed URL", json!("not a URL")), + ( + "unsupported scheme", + json!("ftp://demo.default.example.com"), + ), + ] { + let mut invalid = current.clone(); + invalid["status"]["url"] = url; + cases.push((name, invalid, None)); + } + + let mut failures = Vec::new(); + for (name, service, expected_url) in cases { + std::fs::write(&observation, serde_json::to_vec(&service).unwrap()).unwrap(); + for attempt in 1..=2 { + let output = Command::new(env!("CARGO_BIN_EXE_ly")) + .args(["status", "--context", "fixture"]) + .arg(directory.path()) + .env("PATH", directory.path()) + .env("SERVICE_OBSERVATION", &observation) + .output() + .unwrap(); + let stdout = String::from_utf8_lossy(&output.stdout); + let stderr = String::from_utf8_lossy(&output.stderr); + let valid = match expected_url { + Some(url) => { + output.status.success() + && stdout.contains(url) + && stdout.contains("demo-00001") + && stdout.contains("demo-00002") + && stdout.contains("25%") + && stdout.contains("75%") + } + None => !output.status.success(), + }; + if !valid { + failures.push(format!("{name}, attempt {attempt}: {stdout}{stderr}")); + } + } + assert_eq!( + serde_json::from_slice::(&std::fs::read(&observation).unwrap()) + .unwrap(), + service + ); + } + assert!(failures.is_empty(), "{}", failures.join("\n")); +} + #[test] fn a_readiness_probe_cannot_report_success_after_its_deadline() { let host = lampyr_cli::host::Host { diff --git a/docs/cli/deploy.md b/docs/cli/deploy.md index daa02e3..bc28122 100644 --- a/docs/cli/deploy.md +++ b/docs/cli/deploy.md @@ -73,11 +73,19 @@ rebuild and redeploy each service. ## External exposure Deployment reads `ingress.services` from the enclosing `lampyr.workspace.yaml`. -Listed services use Knative's generated external hostname and the installed -cluster domain. Other services get Knative's `cluster-local` visibility and an +Listed services use Knative's external address. Other services get Knative's +`cluster-local` visibility and an internal address such as `http://worker-processor.default.svc.cluster.local`. Deployment outside a workspace also defaults to cluster-local. +`ly deploy` and `ly status` report the observed Service address. They keep custom +public hosts and use HTTPS with the installation's port mapping. Cluster-local +addresses stay internal HTTP addresses, without a public port mapping. + +The reported URL identifies a Knative route. It does not prove that your +workstation can reach it. Public routes need DNS, TLS, and network access. +For an internal route, use a client in the cluster or a port-forward endpoint. + Bindings can call both public and internal services through cluster DNS. Exposure does not change binding URLs or snapshot identity. After editing the exposure list, redeploy each affected service; a config edit alone does not diff --git a/docs/cli/status.md b/docs/cli/status.md index e616112..c6a08eb 100644 --- a/docs/cli/status.md +++ b/docs/cli/status.md @@ -21,6 +21,10 @@ build/ly status services/hello-edge The output also identifies the application model configured in the local manifest: `handler` or `standalone`. +Status requires the Service controller to have observed the current Service +generation. Its URL follows the same [address policy as deploy](deploy.md#external-exposure). +An internal route URL does not imply access from your workstation. + For each Revision that takes traffic, a `capacity` line reports its CapacityGrant as fiberd-k8s-controller does, with the grant's Fiber limit: diff --git a/scripts/test-existing-kind-integration.sh b/scripts/test-existing-kind-integration.sh index f16ef0e..95bcee8 100755 --- a/scripts/test-existing-kind-integration.sh +++ b/scripts/test-existing-kind-integration.sh @@ -164,20 +164,37 @@ fi "$repo_root/build/ly" build "$service" image="localhost:$registry_port/ci/hello-nodejs:${GITHUB_SHA:-local}" -"$repo_root/build/ly" deploy \ +check_reported_url() { + local deployed=$1 directory=$2 expected=$3 observed + printf '%s\n' "$deployed" + grep -Fxq "url $expected" <<<"$deployed" + for _ in 1 2; do + observed=$("$repo_root/build/ly" status --context "$context" "$directory") + printf '%s\n' "$observed" + grep -Fxq "url $expected" <<<"$observed" + done +} + +public_url="https://hello-nodejs.default.localhost" +if [[ "$https_port" != 443 ]]; then + public_url+=":$https_port" +fi +deployed=$("$repo_root/build/ly" deploy \ --context "$context" \ --image "$image" \ - "$service" + "$service") +check_reported_url "$deployed" "$service" "$public_url" revision_uid=$( kubectl --context "$context" get revisions.serving.knative.dev \ --selector serving.knative.dev/service=hello-nodejs -o json | jq -er '.items | select(length == 1) | .[0].metadata.uid' ) -"$repo_root/build/ly" deploy \ +deployed=$("$repo_root/build/ly" deploy \ --context "$context" \ --image "$image" \ - "$service" + "$service") +check_reported_url "$deployed" "$service" "$public_url" if ! kubectl --context "$context" get revisions.serving.knative.dev \ --selector serving.knative.dev/service=hello-nodejs -o json | jq -e --arg uid "$revision_uid" \ @@ -199,10 +216,9 @@ cp -a "$service" "$internal" rm -r -- "$internal/.lampyr" sed -i 's/^name: hello-nodejs$/name: hello-internal/' "$internal/lampyr.yaml" "$repo_root/build/ly" build "$internal" -"$repo_root/build/ly" deploy --context "$context" \ - --image "localhost:$registry_port/ci/hello-internal:${GITHUB_SHA:-local}" "$internal" -internal_status=$("$repo_root/build/ly" status --context "$context" "$internal") -grep -Fq 'http://hello-internal.default.svc.cluster.local' <<<"$internal_status" +deployed=$("$repo_root/build/ly" deploy --context "$context" \ + --image "localhost:$registry_port/ci/hello-internal:${GITHUB_SHA:-local}" "$internal") +check_reported_url "$deployed" "$internal" 'http://hello-internal.default.svc.cluster.local' code=$(curl --silent --show-error --insecure --output "$project/internal-response" \ --write-out '%{http_code}' \ "https://hello-internal.default.localhost:$https_port/test") @@ -217,7 +233,8 @@ kubectl --context "$context" delete pod hostname-exposure-probe --wait=true printf 'apiVersion: lampyr.dev/v1alpha1\nkind: Workspace\ningress:\n services: []\n' \ > "$project/lampyr.workspace.yaml" -"$repo_root/build/ly" deploy --context "$context" --image "$image" "$service" +deployed=$("$repo_root/build/ly" deploy --context "$context" --image "$image" "$service") +check_reported_url "$deployed" "$service" 'http://hello-nodejs.default.svc.cluster.local' [[ $(kubectl --context "$context" get ksvc hello-nodejs \ -o jsonpath='{.status.url}') == http://hello-nodejs.default.svc.cluster.local ]] code=$(curl --silent --show-error --insecure --output /dev/null \ @@ -228,7 +245,8 @@ code=$(curl --silent --show-error --insecure --output /dev/null \ kubectl --context "$context" annotate ksvc hello-nodejs kubectl.kubernetes.io/last-applied-configuration- printf 'apiVersion: lampyr.dev/v1alpha1\nkind: Workspace\ningress:\n services: [hello-nodejs]\n' \ > "$project/lampyr.workspace.yaml" -"$repo_root/build/ly" deploy --context "$context" --image "$image" "$service" +deployed=$("$repo_root/build/ly" deploy --context "$context" --image "$image" "$service") +check_reported_url "$deployed" "$service" "$public_url" curl --fail --silent --show-error --insecure \ "https://hello-nodejs.default.localhost:$https_port/test" | grep -Fq 'hello from Hyperlight edge' [[ $(kubectl --context "$context" get revisions.serving.knative.dev \