Skip to content
Open
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
66 changes: 50 additions & 16 deletions cli/src/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,12 @@ pub fn deploy(options: Options<'_>, host: &Host) -> Result<Deployed> {
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,
})
}
Expand Down Expand Up @@ -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<bool> {
fn visibility_ready(
service: &Value,
route: &Value,
service_name: &str,
exposed: bool,
) -> Result<bool> {
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"));
Expand All @@ -357,10 +367,7 @@ fn visibility_ready(service: &Value, route: &Value, exposed: bool) -> Result<boo
}

fn deployment_ready(service: &Value, revision: &Value, revision_name: &str) -> 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)
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down
7 changes: 6 additions & 1 deletion cli/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ pub fn publish(options: Options<'_>, host: &Host) -> Result<Delivered> {
!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"
Expand Down
6 changes: 6 additions & 0 deletions cli/src/kube.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::ffi::OsString> {
let paths = std::env::split_paths(&paths)
.map(|path| {
Expand Down
98 changes: 70 additions & 28 deletions cli/src/lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,17 @@ pub fn status(directory: &Path, context: Option<&str>, host: &Host) -> Result<St
validate_name(&project.name)?;
let kube = Kube::new(directory, context, host)?;
let service: Value = kube.get("default", "service.serving.knative.dev", &project.name)?;
ensure!(
kube::observation_current(&service),
"Knative Service status has not observed its current generation"
);
let latest_ready = service
.pointer("/status/latestReadyRevisionName")
.and_then(Value::as_str)
.context("Knative Service has no ready Revision")?
.to_owned();
let installed = kube.installation()?;
let url = service_status_url(&service, &project.name, "default", &installed)?;
let traffic = service
.pointer("/status/traffic")
.and_then(Value::as_array)
Expand Down Expand Up @@ -122,7 +127,7 @@ pub fn status(directory: &Path, context: Option<&str>, host: &Host) -> Result<St
service: project.name.clone(),
application_model: project.application_model().to_owned(),
revision,
url: service_status_url(&service, &installed)?,
url,
traffic,
capacity,
})
Expand Down Expand Up @@ -303,15 +308,15 @@ fn active_revisions(service: &Value) -> Result<BTreeSet<String>> {
Ok(active)
}

pub(crate) fn observed_service_url(service: &Value) -> Result<Url> {
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<Url> {
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)
Expand All @@ -330,15 +335,23 @@ pub(crate) fn observed_service_url(service: &Value) -> Result<Url> {
"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<String> {
let mut url = observed_service_url(service)?;
pub(crate) fn service_status_url(
service: &Value,
name: &str,
namespace: &str,
installation: &Installation,
) -> Result<String> {
let mut url = observed_service_url(service, name, namespace)?;
if kube::cluster_local(service) {
ensure!(
url.scheme() == "http"
Expand All @@ -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() {
Expand Down Expand Up @@ -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()),
Expand All @@ -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!({
Expand All @@ -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!({
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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
);
}
}
}
Loading
Loading