Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.
Rust agentai-agentapplecomputer-usecomputer-use-agentcontainerizationcuadesktop-automationhacktoberfestlumemacosmanusoperatorswiftvirtualizationvirtualization-frameworkwindowswindows-sandbox
fix(cyclops-sdk): state the absent pull secret in the template merge patch (#6848)
r33drichards6711ab2ccca7
diff --git a/libs/fleet/sdk/src/templates.rs b/libs/fleet/sdk/src/templates.rsindex f09a06b342..5b0df5adfc 100644--- a/libs/fleet/sdk/src/templates.rs+++ b/libs/fleet/sdk/src/templates.rs@@ -98,7 +98,7 @@ impl CyclopsClient {&template.metadata.namespace,&template.metadata.name,)?;- let body = to_json(&template)?;+ let body = template_merge_patch_json(&template)?;self.send_json_crud("update template",merge_patch_request(item_url, Some(body)),@@ -122,6 +122,30 @@ impl CyclopsClient {}}+/// Serialize a template as a JSON merge patch that says what the caller meant.+///+/// `reconcile_template` overwrites `template.spec` with the caller's complete+/// desired spec, but a merge patch only touches the keys it mentions, and+/// `imagePullSecret` is `skip_serializing_if = "Option::is_none"`. A desired+/// spec with no pull secret therefore left a pull secret already stored on the+/// template in place -- the patch could not express "no secret".+///+/// Writing the absence out as an explicit null makes the patch mean what the+/// caller asked for. It is also what the gateway's pool admission policy+/// requires: a patch that carries `containerDiskImage` is refused unless it+/// also states the pull secret, so every reconcile of an existing template+/// backed by a registry image was rejected with 403 "k8s request is not+/// allowed" (trycua/cua#3159).+fn template_merge_patch_json(template: &Template) -> Result<Vec<u8>, SdkError> {+ let mut value = serde_json::to_value(template).map_err(|error| SdkError::Body {+ reason: error.to_string(),+ })?;+ if template.spec.vm_template.image_pull_secret.is_none() {+ value["spec"]["vmTemplate"]["imagePullSecret"] = serde_json::Value::Null;+ }+ to_json(&value)+}+fn merge_patch_request(url: Url, body: Option<Vec<u8>>) -> HttpRequest {let mut request = json_request("PATCH", url, body);request.headers[1].value = "application/merge-patch+json".into();diff --git a/libs/fleet/sdk/tests/template_flow.rs b/libs/fleet/sdk/tests/template_flow.rsnew file mode 100644index 0000000000..6d52161c5b--- /dev/null+++ b/libs/fleet/sdk/tests/template_flow.rs@@ -0,0 +1,178 @@+mod support;++use cyclops_sdk::{+ CreateTemplateRequest, CyclopsClient, CyclopsConfiguration, CyclopsCredentials, HttpHeader,+ HttpResponse, ResourceMetadata, Template,+};+use std::sync::Arc;+use support::ScriptedHttpClient;++const BASE_URL: &str = "https://cyclops.example:8443/prefix";+const TOKEN_URL: &str = "https://identity.example/oauth/token";+const NAMESPACE: &str = "example-pool";+const TEMPLATE_COLLECTION: &str = "https://cyclops.example:8443/prefix/api/k8s/apis/osgym.cua.ai/v1alpha1/namespaces/example-pool/osgymsandboxtemplates";+const TEMPLATE_ITEM: &str = "https://cyclops.example:8443/prefix/api/k8s/apis/osgym.cua.ai/v1alpha1/namespaces/example-pool/osgymsandboxtemplates/example-pool";++#[tokio::test]+async fn reconcile_template_creates_when_the_template_is_absent() {+ let http = Arc::new(ScriptedHttpClient::new([+ Ok(token()),+ Ok(response(404, br#"{"kind":"Status","code":404}"#)),+ Ok(json_response(201, &template(None))),+ ]));++ client(Arc::clone(&http))+ .reconcile_template(create_request(None))+ .await+ .unwrap();++ let requests = http.authenticated_requests().await;+ assert_eq!(requests.len(), 2);+ assert_eq!(requests[0].method, "GET");+ assert_eq!(requests[0].url, TEMPLATE_ITEM);+ assert_eq!(requests[1].method, "POST");+ assert_eq!(requests[1].url, TEMPLATE_COLLECTION);+}++/// The gateway refuses a merge patch that carries `containerDiskImage` without+/// also stating the pull secret, so reconciling an existing registry-backed+/// template used to fail with 403 "k8s request is not allowed"+/// (trycua/cua#3159). A desired spec with no pull secret has to say so.+#[tokio::test]+async fn reconcile_template_patches_a_null_pull_secret_when_the_desired_spec_has_none() {+ let http = Arc::new(ScriptedHttpClient::new([+ Ok(token()),+ Ok(json_response(200, &template(Some("ecr-credentials")))),+ Ok(json_response(200, &template(None))),+ ]));++ client(Arc::clone(&http))+ .reconcile_template(create_request(None))+ .await+ .unwrap();++ let requests = http.authenticated_requests().await;+ assert_merge_patch_request(&requests[1], TEMPLATE_ITEM);+ let body: serde_json::Value =+ serde_json::from_slice(requests[1].body.as_deref().unwrap()).unwrap();+ assert_eq!(+ body["spec"]["vmTemplate"]["imagePullSecret"],+ serde_json::Value::Null+ );+ assert!(+ body["spec"]["vmTemplate"]+ .as_object()+ .unwrap()+ .contains_key("imagePullSecret")+ );+}++#[tokio::test]+async fn reconcile_template_keeps_a_pull_secret_the_desired_spec_asks_for() {+ let http = Arc::new(ScriptedHttpClient::new([+ Ok(token()),+ Ok(json_response(200, &template(None))),+ Ok(json_response(200, &template(Some("ecr-credentials")))),+ ]));++ client(Arc::clone(&http))+ .reconcile_template(create_request(Some("ecr-credentials")))+ .await+ .unwrap();++ let requests = http.authenticated_requests().await;+ assert_merge_patch_request(&requests[1], TEMPLATE_ITEM);+ let body: serde_json::Value =+ serde_json::from_slice(requests[1].body.as_deref().unwrap()).unwrap();+ assert_eq!(+ body["spec"]["vmTemplate"]["imagePullSecret"],+ "ecr-credentials"+ );+}++fn client(http: Arc<ScriptedHttpClient>) -> Arc<CyclopsClient> {+ CyclopsClient::connect(+ CyclopsConfiguration {+ base_url: BASE_URL.into(),+ token_url: TOKEN_URL.into(),+ credentials: CyclopsCredentials::new("client".into(), "secret".into()),+ pool_poll_interval_ms: 1,+ pool_poll_limit: 1,+ claim_poll_interval_ms: 1,+ claim_poll_limit: 1,+ },+ http,+ )+ .unwrap()+}++fn spec(pull_secret: Option<&str>) -> cyclops_sdk_schema::OSGymSandboxTemplateSpec {+ serde_json::from_value(serde_json::json!({+ "vmTemplate": {+ "containerDiskImage": "ghcr.io/trycua/minecraft-agent:1.20.1",+ "imagePullSecret": pull_secret,+ "services": [{ "name": "server", "targetPort": 8000, "protocol": "TCP" }],+ },+ }))+ .unwrap()+}++fn create_request(pull_secret: Option<&str>) -> CreateTemplateRequest {+ CreateTemplateRequest {+ namespace: NAMESPACE.into(),+ name: NAMESPACE.into(),+ spec: spec(pull_secret),+ }+}++fn template(pull_secret: Option<&str>) -> Template {+ Template {+ api_version: "osgym.cua.ai/v1alpha1".into(),+ kind: "OSGymSandboxTemplate".into(),+ metadata: ResourceMetadata {+ namespace: NAMESPACE.into(),+ name: NAMESPACE.into(),+ labels: None,+ creation_timestamp: None,+ },+ spec: spec(pull_secret),+ }+}++fn token() -> HttpResponse {+ response(200, br#"{"access_token":"token-a","expires_in":3600}"#)+}++fn json_response(status: u16, value: &impl serde::Serialize) -> HttpResponse {+ response(status, &serde_json::to_vec(value).unwrap())+}++fn response(status: u16, body: &[u8]) -> HttpResponse {+ HttpResponse {+ status,+ headers: Vec::new(),+ body: body.to_vec(),+ }+}++fn assert_merge_patch_request(request: &cyclops_sdk::HttpRequest, url: &str) {+ assert_eq!(request.method, "PATCH");+ assert_eq!(request.url, url);+ assert_eq!(+ request.headers,+ vec![+ HttpHeader {+ name: "accept".into(),+ value: "application/json".into(),+ },+ HttpHeader {+ name: "content-type".into(),+ value: "application/merge-patch+json".into(),+ },+ HttpHeader {+ name: "authorization".into(),+ value: "Bearer token-a".into(),+ },+ ]+ );+}