use of org.eclipse.jkube.kit.config.image.ImageName in project jkube by eclipse.
the class TriggersAnnotationEnricher method createAnnotation.
protected String createAnnotation(Visitable<?> builder) {
final List<ImageChangeTrigger> triggerList = new ArrayList<>();
builder.accept(new TypedVisitor<ContainerBuilder>() {
@Override
public void visit(ContainerBuilder cb) {
Container container = cb.build();
String containerName = container.getName();
String containerImage = container.getImage();
ImageName image = new ImageName(containerImage);
if (isContainerAllowed(containerName) && image.getRegistry() == null && image.getUser() == null) {
// Imagestreams used as trigger are in the same namespace
String tag = image.getTag() != null ? image.getTag() : "latest";
ImageChangeTrigger trigger = new ImageChangeTriggerBuilder().withNewFrom().withKind("ImageStreamTag").withName(image.getSimpleName() + ":" + tag).endFrom().build();
trigger.setAdditionalProperty("fieldPath", "spec.template.spec.containers[?(@.name==\"" + containerName + "\")].image");
triggerList.add(trigger);
}
}
});
try {
return ResourceUtil.toJson(triggerList);
} catch (JsonProcessingException e) {
getLog().error("Error while creating ImageStreamTag triggers for Kubernetes resources: %s", e);
return "[]";
}
}
use of org.eclipse.jkube.kit.config.image.ImageName in project jkube by eclipse.
the class RegistryService method pushImage.
/**
* Push a set of images to a registry
*
* @param imageConfig image to push (but only if they have a build configuration)
* @param retries how often to retry
* @param registryConfig a global registry configuration
* @param skipTag flag to skip pushing tagged images
* @throws IOException exception
*/
public void pushImage(ImageConfiguration imageConfig, int retries, RegistryConfig registryConfig, boolean skipTag) throws IOException {
BuildConfiguration buildConfig = imageConfig.getBuildConfiguration();
String name = imageConfig.getName();
if (buildConfig != null) {
String configuredRegistry = EnvUtil.firstRegistryOf(new ImageName(imageConfig.getName()).getRegistry(), imageConfig.getRegistry(), registryConfig.getRegistry());
AuthConfig authConfig = createAuthConfig(true, new ImageName(name).getUser(), configuredRegistry, registryConfig);
long start = System.currentTimeMillis();
docker.pushImage(name, authConfig, configuredRegistry, retries);
log.info("Pushed %s in %s", name, EnvUtil.formatDurationTill(start));
if (!skipTag) {
for (String tag : imageConfig.getBuildConfiguration().getTags()) {
if (tag != null) {
docker.pushImage(new ImageName(name, tag).getFullName(), authConfig, configuredRegistry, retries);
}
}
}
}
}
use of org.eclipse.jkube.kit.config.image.ImageName in project jkube by eclipse.
the class DockerAccessWithHcClient method pushImage.
@Override
public void pushImage(String image, AuthConfig authConfig, String registry, int retries) throws DockerAccessException {
ImageName name = new ImageName(image);
String pushUrl = urlBuilder.pushImage(name, registry);
TemporaryImageHandler temporaryImageHandler = tagTemporaryImage(name, registry);
DockerAccessException dae = null;
try {
doPushImage(pushUrl, createAuthHeader(authConfig), createPullOrPushResponseHandler(), HTTP_OK, retries);
} catch (IOException e) {
dae = new DockerAccessException(e, "Unable to push '%s'%s", image, (registry != null) ? " to registry '" + registry + "'" : "");
throw dae;
} finally {
temporaryImageHandler.handle(dae);
}
}
use of org.eclipse.jkube.kit.config.image.ImageName in project jkube by eclipse.
the class DockerAccessWithHcClient method tag.
@Override
public void tag(String sourceImage, String targetImage, boolean force) throws DockerAccessException {
ImageName source = new ImageName(sourceImage);
ImageName target = new ImageName(targetImage);
try {
String url = urlBuilder.tagContainer(source, target, force);
delegate.post(url, HTTP_CREATED);
} catch (IOException e) {
throw new DockerAccessException(e, "Unable to add tag [%s] to image [%s]", targetImage, sourceImage, e);
}
}
use of org.eclipse.jkube.kit.config.image.ImageName in project jkube by eclipse.
the class ImageStreamServiceTest method simple.
@Test
public void simple() throws Exception {
ImageStreamService service = new ImageStreamService(client, "default", log);
final ImageStream lookedUpIs = lookupImageStream("ab12cd");
setupClientMock(lookedUpIs, "default", "test");
ImageName name = new ImageName("test:1.0");
File target = File.createTempFile("ImageStreamServiceTest", ".yml");
service.appendImageStreamResource(name, target);
assertTrue(target.exists());
Map result = readImageStreamDescriptor(target);
System.out.println(result.toString());
assertNotNull(result);
List<Map> items = getItemsList(result);
assertEquals(1, items.size());
Map isRead = (Map<String, Object>) items.get(0);
assertNotNull(isRead);
assertEquals("ImageStream", isRead.get("kind"));
Map spec = (Map<String, Object>) isRead.get("spec");
assertNotNull(spec);
List tags = (List) spec.get("tags");
assertNotNull(tags);
assertEquals(1, tags.size());
Map tag = (Map) tags.get(0);
Map from = (Map) tag.get("from");
assertEquals("ImageStreamImage", from.get("kind"));
assertEquals("test@ab12cd", from.get("name"));
assertEquals("default", from.get("namespace"));
// Add a second image stream
ImageStream secondIs = lookupImageStream("secondIS");
setupClientMock(secondIs, "default", "second-test");
ImageName name2 = new ImageName("second-test:1.0");
service.appendImageStreamResource(name2, target);
result = readImageStreamDescriptor(target);
System.out.println(result.toString());
items = getItemsList(result);
assertEquals(2, items.size());
Set<String> names = new HashSet<>(Arrays.asList("second-test", "test"));
for (Map item : items) {
assertTrue(names.remove(((Map) item.get("metadata")).get("name")));
}
assertTrue(names.isEmpty());
}
Aggregations