Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JBIDE-21703] Retrieve istags associated with a resource #960

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,17 @@ public <T extends IResource> List<T> getResources(String kind, String namespace)
}
}

@Override
public <T extends IResource> T getResource(String kind, String namespace, String name) {
try {
if(client.getAuthorizationStrategy() == null) {
client.setAuthorizationStrategy(getAuthorizationStrategy());
}
return client.get(kind, name, namespace);
} catch (UnauthorizedException e) {
return retryGet("Unauthorized. Trying to reauthenticate", e, kind, name, namespace);
}
}
/**
* Get or refresh a resource
*
Expand All @@ -382,16 +393,16 @@ public <T extends IResource> T getResource(IResource resource) {
}
return client.get(resource.getKind(), resource.getName(), resource.getNamespace());
} catch (UnauthorizedException e) {
return retryGet("Unauthorized. Trying to reauthenticate", e, resource);
return retryGet("Unauthorized. Trying to reauthenticate", e, resource.getKind(), resource.getName(), resource.getNamespace());
}
}

private <T extends IResource> T retryGet(String message, OpenShiftException e, IResource resource){
private <T extends IResource> T retryGet(String message, OpenShiftException e, String kind, String name, String namespace){
OpenShiftCoreActivator.pluginLog().logInfo(message);
setToken(null);// token must be invalid, make sure not to try with
// cache
if (connect()) {
return client.get(resource.getKind(), resource.getName(), resource.getNamespace());
return client.get(kind, name, namespace);
}
throw e;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ public interface IOpenShiftConnection {
<T extends IResource> List<T> getResources(String kind);

<T extends IResource> List<T> getResources(String kind, String namespace);

/**
* Retrieve a resource by name
* @param kind
* @param namespace
* @param name
* @return
*/
<T extends IResource> T getResource(String kind, String namespace, String name);

String getUsername();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import org.jboss.tools.common.databinding.ObservablePojo;
import org.jboss.tools.openshift.common.core.connection.ConnectionsRegistryAdapter;
Expand All @@ -29,9 +31,11 @@
import org.jboss.tools.openshift.core.connection.IOpenShiftConnection;
import org.jboss.tools.openshift.internal.core.Trace;
import org.jboss.tools.openshift.internal.core.WatchManager;
import org.jboss.tools.openshift.internal.core.util.ResourceUtils;
import org.jboss.tools.openshift.internal.ui.OpenShiftUIActivator;

import com.openshift.restclient.ResourceKind;
import com.openshift.restclient.model.IBuildConfig;
import com.openshift.restclient.model.IProject;
import com.openshift.restclient.model.IResource;
import com.openshift.restclient.model.IService;
Expand Down Expand Up @@ -73,6 +77,23 @@ public synchronized void refresh() {
buildDeployments();
}


@Override
public Collection<IResource> getImageStreamTagsFor(IService service) {
Optional<Deployment> deployment = deployments.stream().filter(d->service.equals(d.getService())).findFirst();
if(deployment.isPresent()) {
Set<String> imageRefs = deployment.get()
.getBuildConfigs()
.stream()
.map(bc->ResourceUtils.imageRef((IBuildConfig) bc.getResource())).collect(Collectors.toSet());
final String projectName = service.getNamespace();
return imageRefs.stream()
.map(ref->conn.<IResource>getResource(ResourceKind.IMAGE_STREAM_TAG, projectName, ref))
.collect(Collectors.toSet());
}
return Collections.emptySet();
}

@Override
public Collection<Deployment> getDeployments() {
buildDeployments();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,20 @@
import org.jboss.tools.common.databinding.IObservablePojo;
import org.jboss.tools.openshift.common.core.IRefreshable;

import com.openshift.restclient.model.IResource;
import com.openshift.restclient.model.IService;

public interface IDeploymentResourceMapper extends IObservablePojo, IRefreshable {

Collection<Deployment> getDeployments();

/**
* Retrieve the ImageStreamTags associated with the given
* service. This will most likely trigger a call to the
* server since ImageStreamTags are not a watchable resource
*
* @param service
* @return The collection of imagestreamtags or an empty collection
*/
Collection<IResource> getImageStreamTagsFor(IService service);
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,6 @@ public void setDeploymentConfigResources(Collection<IResource> dcs) {
firePropertyChange(PROP_DEPLOYMENT_CONFIGS, resources.get(ResourceKind.DEPLOYMENT_CONFIG), resources.put(ResourceKind.DEPLOYMENT_CONFIG, init(dcs)));
}

public Collection<IResourceUIModel> getImageStreamTags() {
return resources.get(ResourceKind.IMAGE_STREAM_TAG);
}

public void setImageStreamTags(Collection<IResourceUIModel> models) {
firePropertyChange(PROP_IMAGE_STREAM_TAGS,
resources.get(ResourceKind.IMAGE_STREAM_TAG),
resources.put(ResourceKind.IMAGE_STREAM_TAG, new ArrayList<>(models)));
}

public void setImageStreamTagResources(Collection<IResource> istags) {
firePropertyChange(PROP_IMAGE_STREAM_TAGS,
resources.get(ResourceKind.IMAGE_STREAM_TAG),
resources.put(ResourceKind.IMAGE_STREAM_TAG, init(istags)));
}

@Override
public Collection<IResourceUIModel> getBuilds() {
return resources.get(ResourceKind.BUILD);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.eclipse.wst.server.core.IServerWorkingCopy;
import org.jboss.tools.openshift.common.core.connection.ConnectionURL;
Expand Down Expand Up @@ -136,10 +134,9 @@ private String getDeploymentDirectory(IService service, Map<String, IDeploymentR
String deploymentDirectory = null;
if (service != null
&& deploymentResourceMapperByProjectName != null) {
IProject project = service.getProject();
IDeploymentResourceMapper deploymentMapper = deploymentResourceMapperByProjectName.get(project.getName());
IDeploymentResourceMapper deploymentMapper = deploymentResourceMapperByProjectName.get(service.getNamespace());
if (deploymentMapper != null) {
Collection<IResource> istags = getImageStreamTags(service, deploymentMapper);
Collection<IResource> istags = deploymentMapper.getImageStreamTagsFor(service);
Iterator<IResource> istagsIterator = istags.iterator();
if (istagsIterator.hasNext()) {
IResource imageStreamTag = istagsIterator.next();
Expand Down Expand Up @@ -171,16 +168,6 @@ private String matchFirstGroup(String imageStreamTag, Pattern pattern) {

}

private Collection<IResource> getImageStreamTags(IService service, IDeploymentResourceMapper deploymentMapper) {
Optional<Deployment> deployment = deploymentMapper.getDeployments().stream().filter(d->service.equals(d.getService())).findFirst();
if(deployment.isPresent()) {
Collection<IResource> istags = deployment.get().getImageStreamTags().stream().map(m->m.getResource()).collect(Collectors.toList());
// need to refresh to get full resource WITH labels
return getVerboseResources(istags, getConnection());
}
return Collections.emptyList();
}

public void setDeployProject(org.eclipse.core.resources.IProject project) {
update(getConnection(), getConnections(), project, this.projects, this.sourcePath,
this.podPath, this.deploymentMapperByProjectName, getService(), getServiceItems());
Expand Down Expand Up @@ -264,23 +251,6 @@ private Map<String, IDeploymentResourceMapper> loadImageStreamTags() {
return deploymentMapperByProjectName;
}

/**
* Returns the verbose version of the resources by requesting them individually.
* When listing all resources you only get a compact short version.
* To get the full-blown resource WITH labels etc you have to query them individually.
*
* @param resources
* @param connection
* @return
*/
private List<IResource> getVerboseResources(Collection<IResource> resources, Connection connection) {
if (resources == null
|| resources.isEmpty()) {
return Collections.emptyList();
}
return resources.stream().map(r -> (IResource) connection.getResource(r)).collect(Collectors.toList());
}

protected List<org.eclipse.core.resources.IProject> loadProjects() {
return ProjectUtils.getAllAccessibleProjects();
}
Expand Down