Running minikube v1.8.1, and with
kube = "0.30.0"
k8s-openapi = { version = "0.7.1", features = ["v1_15"] }
(same with v1_17)
use k8s_openapi::{
apimachinery::pkg::apis::meta::v1::ObjectMeta,
api::batch::v1beta1::{CronJob, CronJobSpec, CronJobStatus, JobTemplateSpec},
api::batch::v1::JobSpec,
api::core::v1::{PodTemplateSpec, PodSpec, Container},
};
use kube::{Api, api::PostParams, client::APIClient, config::load_kube_config};
let config = load_kube_config().await.context(err::KubeError)?;
let client = APIClient::new(config);
let api = Api::<CronJob>::all(client);
api.create(
&PostParams::default(),
&CronJob {
metadata: Some(ObjectMeta {
labels: Some(BTreeMap::from_iter(
[("name".into(), name.clone())].iter().cloned()
)),
..Default::default()
}),
spec: Some(CronJobSpec {
schedule: schedule.frequency.clone(),
job_template: JobTemplateSpec {
spec: Some(JobSpec {
template: PodTemplateSpec {
spec: Some(PodSpec {
containers: vec![
Container {
name: "hello".into(),
image: Some("busybox".into()),
args: Some(vec!["/bin/sh".into(),
"-c".into(),
"date; echo Hello".into()]),
..Default::default()
}
],
restart_policy: Some("OnFailure".into()),
..Default::default()
}),
metadata: None,
},
..Default::default()
}),
metadata: None,
},
..Default::default()
}),
status: None,
}).await.context(err::KubeError)?;
I get error:
KubeError { source: Api(ErrorResponse { status: "Failure", message: "the server does not allow this method on the requested resource", reason: "MethodNotAllowed", code: 405 }) }
MethodNotAllowed. Wtf. Ok, that is a strange. Thanks for the report. Will try to cross reference with the api later.
Right, so I tried this on minikube running kubernetes 1.15 and with minor modifications, it worked. There's an attached commit (basically you only missed out the metadata.name - but that is required).
You might have gotten a newer version of cronjob than what your cluster supports.
To expand a bit. Make sure you pin the k8s-openapi api feature to your cluster version. That's the only feasible reason I can see you getting method invalid. I never got that. The example in that branch worked fine.
Thanks! From your example I saw that you use a namespace "default" while I had let api = Api::<CronJob>::all(client);. With namespace "default" it works for me too.
Oh right. Yeah, cronjob is not a cluster level resource! That's a thing we might be able to improve elsewhere.
Most helpful comment
MethodNotAllowed. Wtf. Ok, that is a strange. Thanks for the report. Will try to cross reference with the api later.