use of io.kubernetes.client.openapi.models.V1OwnerReference in project java by kubernetes-client.
the class DeploymentHelper method listReplicaSets.
/**
* listReplicaSets returns a list of RSes the given deployment targets. Note that this does NOT
* attempt to reconcile ControllerRef (adopt/orphan), because only the controller itself should do
* that. However, it does filter out anything whose ControllerRef doesn't match.
*/
private static List<V1ReplicaSet> listReplicaSets(V1Deployment deployment, AppsV1Api api) throws ApiException {
String namespace = deployment.getMetadata().getNamespace();
LabelSelector selector = LabelSelector.parse(deployment.getSpec().getSelector());
List<V1ReplicaSet> all = rsListFromClient(namespace, selector.toString(), api);
List<V1ReplicaSet> owned = new ArrayList<>(all.size());
for (V1ReplicaSet rs : all) {
List<V1OwnerReference> refs = rs.getMetadata().getOwnerReferences();
Optional<V1OwnerReference> ref = refs.stream().filter(o -> o.getController() != null && o.getController()).findAny();
// Only include those whose ControllerRef matches the Deployment.
if (ref.isPresent() && ref.get().getUid().equals(deployment.getMetadata().getUid())) {
owned.add(rs);
}
}
return owned;
}
use of io.kubernetes.client.openapi.models.V1OwnerReference in project java by kubernetes-client.
the class Jobs method cronJobToJob.
/**
* Convert V1beta1CronJob object into V1Job object, based on kubectl code
* https://github.com/kubernetes/kubectl/blob/master/pkg/cmd/create/create_job.go
*
* @param cronJob cronJob object (required)
* @param jobName cronJob name
* @return V1Job object
*/
public static V1Job cronJobToJob(V1beta1CronJob cronJob, String jobName) {
Map<String, String> annotations = new HashMap<>();
Map<String, String> labels = new HashMap<>();
V1JobSpec jobSpec = null;
V1beta1CronJobSpec cronJobSpec = cronJob.getSpec();
if (cronJobSpec != null && cronJobSpec.getJobTemplate() != null) {
V1ObjectMeta metadata = cronJobSpec.getJobTemplate().getMetadata();
if (metadata != null) {
if (metadata.getAnnotations() != null) {
annotations.putAll(metadata.getAnnotations());
}
if (metadata.getLabels() != null) {
labels.putAll(metadata.getLabels());
}
}
jobSpec = cronJobSpec.getJobTemplate().getSpec();
}
annotations.put("cronjob.kubernetes.io/instantiate", "manual");
V1OwnerReference v1OwnerReference = new V1OwnerReference();
v1OwnerReference.setKind("CronJob");
v1OwnerReference.setName(cronJob.getMetadata().getName());
v1OwnerReference.setBlockOwnerDeletion(true);
v1OwnerReference.setController(true);
v1OwnerReference.setUid(cronJob.getMetadata().getUid());
v1OwnerReference.setApiVersion("batch/v1beta1");
V1ObjectMeta jobMetadata = new V1ObjectMeta();
jobMetadata.setName(jobName != null ? jobName : cronJob.getMetadata().getName() + "-manual");
jobMetadata.setAnnotations(annotations);
jobMetadata.setLabels(labels);
jobMetadata.setOwnerReferences(Arrays.asList(v1OwnerReference));
V1Job job = new V1Job();
job.setKind("Job");
job.setApiVersion("batch/v1");
job.setMetadata(jobMetadata);
job.setSpec(jobSpec);
return job;
}
Aggregations