io.fabric8.kubernetes.api.model.ObjectReference Java Examples

The following examples show how to use io.fabric8.kubernetes.api.model.ObjectReference. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ImageStreamService.java    From jkube with Eclipse Public License 2.0 6 votes vote down vote up
private void createOrUpdateImageStreamTag(OpenShiftClient client, ImageName image, ImageStream is) {
    String namespace = client.getNamespace();
    String tagSha = findTagSha(client, image.getSimpleName(), client.getNamespace());
    String name = image.getSimpleName() + "@" + tagSha;

    TagReference tag = extractTag(is);
    ObjectReference from = extractFrom(tag);

    if (!Objects.equals(image.getTag(), tag.getName())) {
        tag.setName(image.getTag());
    }
    if (!Objects.equals("ImageStreamImage", from.getKind())) {
        from.setKind("ImageStreamImage");
    }
    if (!Objects.equals(namespace, from.getNamespace())) {
        from.setNamespace(namespace);
    }
    if (!Objects.equals(name, from.getName())) {
        from.setName(name);
    }
}
 
Example #2
Source File: OpenshiftBuildService.java    From jkube with Eclipse Public License 2.0 6 votes vote down vote up
private void logBuildFailedDetails(OpenShiftClient client, String buildName) {
    try {
        BuildConfig build = client.buildConfigs().withName(buildName).get();
        ObjectReference ref = build.getSpec().getStrategy().getSourceStrategy().getFrom();
        String kind = ref.getKind();
        String name = ref.getName();

        if ("DockerImage".equals(kind)) {
            log.error("Please, ensure that the Docker image '%s' exists and is accessible by OpenShift", name);
        } else if ("ImageStreamTag".equals(kind)) {
            String namespace = ref.getNamespace();
            String namespaceInfo = "current";
            String namespaceParams = "";
            if (namespace != null && !namespace.isEmpty()) {
                namespaceInfo = "'" + namespace + "'";
                namespaceParams = " -n " + namespace;
            }

            log.error("Please, ensure that the ImageStream Tag '%s' exists in the %s namespace (with 'oc get is%s')", name, namespaceInfo, namespaceParams);
        }
    } catch (Exception ex) {
        log.error("Unable to get detailed information from the BuildServiceConfig: " + ex.getMessage());
    }
}
 
Example #3
Source File: RoleBindingOperationsImpl.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
private void enrichFromSubjects(RoleBindingBuilder builder, List<ObjectReference> subjects) {
  for (ObjectReference ref : subjects) {
    switch (ref.getKind()) {
      case USER:
        builder.addToUserNames(ref.getName());
        break;
      case SERVICE_ACCOUNT:
        String namespace = ref.getNamespace();
        if (namespace == null || namespace.isEmpty()) {
          namespace = getNamespace();
        }
        builder.addToUserNames("system:serviceaccount:" + namespace + ":" + ref.getName());
        break;
      case GROUP:
        builder.addToGroupNames(ref.getName());
        break;
    }
  }
}
 
Example #4
Source File: KubernetesCatalogWatch.java    From spring-cloud-kubernetes with Apache License 2.0 5 votes vote down vote up
@Scheduled(
		fixedDelayString = "${spring.cloud.kubernetes.discovery.catalogServicesWatchDelay:30000}")
public void catalogServicesWatch() {
	try {
		List<String> previousState = this.catalogEndpointsState.get();

		// not all pods participate in the service discovery. only those that have
		// endpoints.
		List<Endpoints> endpoints = this.properties.isAllNamespaces()
				? this.kubernetesClient.endpoints().inAnyNamespace().list().getItems()
				: this.kubernetesClient.endpoints().list().getItems();
		List<String> endpointsPodNames = endpoints.stream().map(Endpoints::getSubsets)
				.filter(Objects::nonNull).flatMap(Collection::stream)
				.map(EndpointSubset::getAddresses).filter(Objects::nonNull)
				.flatMap(Collection::stream).map(EndpointAddress::getTargetRef)
				.filter(Objects::nonNull).map(ObjectReference::getName) // pod name
																		// unique in
																		// namespace
				.sorted(String::compareTo).collect(Collectors.toList());

		this.catalogEndpointsState.set(endpointsPodNames);

		if (!endpointsPodNames.equals(previousState)) {
			logger.trace("Received endpoints update from kubernetesClient: {}",
					endpointsPodNames);
			this.publisher.publishEvent(new HeartbeatEvent(this, endpointsPodNames));
		}
	}
	catch (Exception e) {
		logger.error("Error watching Kubernetes Services", e);
	}
}
 
Example #5
Source File: KubernetesCatalogWatchTest.java    From spring-cloud-kubernetes with Apache License 2.0 5 votes vote down vote up
private List<EndpointAddress> createEndpointAddressByPodNames(String[] names) {
	return stream(names).map(name -> {
		ObjectReference podRef = new ObjectReference();
		podRef.setName(name);
		EndpointAddress endpointAddress = new EndpointAddress();
		endpointAddress.setTargetRef(podRef);
		return endpointAddress;
	}).collect(Collectors.toList());
}
 
Example #6
Source File: KafkaConnectS2ICluster.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
/**
 * Generate new source ImageStream
 *
 * @return      Source ImageStream resource definition
 */
public ImageStream generateSourceImageStream() {
    ObjectReference image = new ObjectReference();
    image.setKind("DockerImage");
    image.setName(sourceImageBaseName + ":" + sourceImageTag);

    TagReference sourceTag = new TagReference();
    sourceTag.setName(sourceImageTag);
    sourceTag.setFrom(image);
    sourceTag.setReferencePolicy(new TagReferencePolicyBuilder().withType("Local").build());

    if (insecureSourceRepository)   {
        sourceTag.setImportPolicy(new TagImportPolicyBuilder().withInsecure(true).build());
    }

    ImageStream imageStream = new ImageStreamBuilder()
            .withNewMetadata()
                .withName(KafkaConnectS2IResources.sourceImageStreamName(cluster))
                .withNamespace(namespace)
                .withLabels(getLabelsWithStrimziName(KafkaConnectS2IResources.sourceImageStreamName(cluster), null).toMap())
                .withOwnerReferences(createOwnerReference())
            .endMetadata()
            .withNewSpec()
                .withLookupPolicy(new ImageLookupPolicyBuilder().withLocal(false).build())
                .withTags(sourceTag)
            .endSpec()
            .build();

    return imageStream;
}
 
Example #7
Source File: S2iUtils.java    From dekorate with Apache License 2.0 4 votes vote down vote up
/**
 * Wait for the references ImageStreamTags to become available.
 * @param items       A list of items, possibly referencing image stream tags.
 * @param amount      The max amount of time to wait.
 * @param timeUnit    The time unit of the time to wait.
 * @return            True if the items became available false otherwise.
 */
public static boolean waitForImageStreamTags(Collection<HasMetadata> items, long amount, TimeUnit timeUnit) {
  if (items == null || items.isEmpty()) {
    return true;
  }
  final List<String> tags = new ArrayList<>();
  new KubernetesListBuilder()
    .withItems(new ArrayList<>(items))
    .accept(new Decorator<SourceBuildStrategyFluent>() {
        @Override
        public void visit(SourceBuildStrategyFluent strategy) {
          ObjectReference from = strategy.buildFrom();
          if (from.getKind().equals("ImageStreamTag")) {
            tags.add(from.getName());
          }
        }
      }).build();

  boolean tagsMissing = true;
  long started = System.currentTimeMillis();
  long elapsed = 0;

  while (tagsMissing && elapsed < timeUnit.toMillis(amount) && !Thread.interrupted()) {
    tagsMissing = false;
    for (String tag : tags) {
      ImageStreamTag t = client.imageStreamTags().withName(tag).get();
      if (t == null) {
        tagsMissing = true;
      }
    }

    if (tagsMissing) {
      try {
        Thread.sleep(1000);
        elapsed = System.currentTimeMillis() - started;
      } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
      }
    }
  }
  return !tagsMissing;
}