use of io.fabric8.openshift.api.model.OAuthClient in project fabric8 by fabric8io.
the class Controller method applyEntity.
/**
* Applies the given DTOs onto the Kubernetes master
*/
public void applyEntity(Object dto, String sourceName) throws Exception {
if (dto instanceof Pod) {
applyPod((Pod) dto, sourceName);
} else if (dto instanceof ReplicationController) {
applyReplicationController((ReplicationController) dto, sourceName);
} else if (dto instanceof Service) {
applyService((Service) dto, sourceName);
} else if (dto instanceof Namespace) {
applyNamespace((Namespace) dto);
} else if (dto instanceof Route) {
applyRoute((Route) dto, sourceName);
} else if (dto instanceof BuildConfig) {
applyBuildConfig((BuildConfig) dto, sourceName);
} else if (dto instanceof DeploymentConfig) {
DeploymentConfig resource = (DeploymentConfig) dto;
OpenShiftClient openShiftClient = getOpenShiftClientOrNull();
if (openShiftClient != null && openShiftClient.supportsOpenShiftAPIGroup(OpenShiftAPIGroups.APPS)) {
applyResource(resource, sourceName, openShiftClient.deploymentConfigs());
} else {
LOG.warn("Not connected to OpenShift cluster so cannot apply entity " + dto);
}
} else if (dto instanceof PolicyBinding) {
applyPolicyBinding((PolicyBinding) dto, sourceName);
} else if (dto instanceof RoleBinding) {
applyRoleBinding((RoleBinding) dto, sourceName);
} else if (dto instanceof Role) {
Role resource = (Role) dto;
OpenShiftClient openShiftClient = getOpenShiftClientOrNull();
if (openShiftClient != null && openShiftClient.supportsOpenShiftAPIGroup(OpenShiftAPIGroups.AUTHORIZATION)) {
applyResource(resource, sourceName, openShiftClient.roles());
} else {
LOG.warn("Not connected to OpenShift cluster so cannot apply entity " + dto);
}
} else if (dto instanceof ImageStream) {
applyImageStream((ImageStream) dto, sourceName);
} else if (dto instanceof OAuthClient) {
applyOAuthClient((OAuthClient) dto, sourceName);
} else if (dto instanceof Template) {
applyTemplate((Template) dto, sourceName);
} else if (dto instanceof ServiceAccount) {
applyServiceAccount((ServiceAccount) dto, sourceName);
} else if (dto instanceof Secret) {
applySecret((Secret) dto, sourceName);
} else if (dto instanceof ConfigMap) {
applyResource((ConfigMap) dto, sourceName, kubernetesClient.configMaps());
} else if (dto instanceof DaemonSet) {
applyResource((DaemonSet) dto, sourceName, kubernetesClient.extensions().daemonSets());
} else if (dto instanceof Deployment) {
applyResource((Deployment) dto, sourceName, kubernetesClient.extensions().deployments());
} else if (dto instanceof ReplicaSet) {
applyResource((ReplicaSet) dto, sourceName, kubernetesClient.extensions().replicaSets());
} else if (dto instanceof StatefulSet) {
applyResource((StatefulSet) dto, sourceName, kubernetesClient.apps().statefulSets());
} else if (dto instanceof Ingress) {
applyResource((Ingress) dto, sourceName, kubernetesClient.extensions().ingresses());
} else if (dto instanceof PersistentVolumeClaim) {
applyPersistentVolumeClaim((PersistentVolumeClaim) dto, sourceName);
} else if (dto instanceof HasMetadata) {
HasMetadata entity = (HasMetadata) dto;
try {
String namespace = getNamespace();
String resourceNamespace = getNamespace(entity);
if (Strings.isNotBlank(namespace) && Strings.isNullOrBlank(resourceNamespace)) {
getOrCreateMetadata(entity).setNamespace(namespace);
}
LOG.info("Applying " + getKind(entity) + " " + getName(entity) + " from " + sourceName);
kubernetesClient.resource(entity).inNamespace(namespace).createOrReplace();
} catch (Exception e) {
onApplyError("Failed to create " + getKind(entity) + " from " + sourceName + ". " + e, e);
}
} else {
throw new IllegalArgumentException("Unknown entity type " + dto);
}
}
use of io.fabric8.openshift.api.model.OAuthClient in project fabric8 by fabric8io.
the class Controller method applyOAuthClient.
public void applyOAuthClient(OAuthClient entity, String sourceName) {
OpenShiftClient openShiftClient = getOpenShiftClientOrNull();
if (openShiftClient != null && openShiftClient.supportsOpenShiftAPIGroup(OpenShiftAPIGroups.OAUTH)) {
if (supportOAuthClients) {
String id = getName(entity);
Objects.notNull(id, "No name for " + entity + " " + sourceName);
if (isServicesOnlyMode()) {
LOG.debug("Only processing Services right now so ignoring OAuthClient: " + id);
return;
}
OAuthClient old = openShiftClient.oAuthClients().withName(id).get();
if (isRunning(old)) {
if (isIgnoreRunningOAuthClients()) {
LOG.info("Not updating the OAuthClient which are shared across namespaces as its already running");
return;
}
if (UserConfigurationCompare.configEqual(entity, old)) {
LOG.info("OAuthClient has not changed so not doing anything");
} else {
if (isRecreateMode()) {
openShiftClient.oAuthClients().withName(id).delete();
doCreateOAuthClient(entity, sourceName);
} else {
try {
Object answer = openShiftClient.oAuthClients().withName(id).replace(entity);
LOG.info("Updated OAuthClient result: " + answer);
} catch (Exception e) {
onApplyError("Failed to update OAuthClient from " + sourceName + ". " + e + ". " + entity, e);
}
}
}
} else {
if (!isAllowCreate()) {
LOG.warn("Creation disabled so not creating an OAuthClient from " + sourceName + " name " + getName(entity));
} else {
doCreateOAuthClient(entity, sourceName);
}
}
}
}
}
use of io.fabric8.openshift.api.model.OAuthClient in project jointware by isdream.
the class KubernetesKeyValueStyleGeneratorTest method testOpenShiftWithAllKind.
protected static void testOpenShiftWithAllKind() throws Exception {
info(OPENSHIFT_KIND, OpenShiftDocumentKeyValueStyleGenerator.class.getName(), new Policy());
info(OPENSHIFT_KIND, OpenShiftDocumentKeyValueStyleGenerator.class.getName(), new Group());
info(OPENSHIFT_KIND, OpenShiftDocumentKeyValueStyleGenerator.class.getName(), new User());
info(OPENSHIFT_KIND, OpenShiftDocumentKeyValueStyleGenerator.class.getName(), new OAuthClient());
info(OPENSHIFT_KIND, OpenShiftDocumentKeyValueStyleGenerator.class.getName(), new ClusterRoleBinding());
info(OPENSHIFT_KIND, OpenShiftDocumentKeyValueStyleGenerator.class.getName(), new ImageStreamTag());
info(OPENSHIFT_KIND, OpenShiftDocumentKeyValueStyleGenerator.class.getName(), new ImageStream());
info(OPENSHIFT_KIND, OpenShiftDocumentKeyValueStyleGenerator.class.getName(), new Build());
info(OPENSHIFT_KIND, OpenShiftDocumentKeyValueStyleGenerator.class.getName(), new BuildConfig());
info(OPENSHIFT_KIND, OpenShiftDocumentKeyValueStyleGenerator.class.getName(), new RoleBinding());
info(OPENSHIFT_KIND, OpenShiftDocumentKeyValueStyleGenerator.class.getName(), new Route());
info(OPENSHIFT_KIND, OpenShiftDocumentKeyValueStyleGenerator.class.getName(), new PolicyBinding());
info(OPENSHIFT_KIND, OpenShiftDocumentKeyValueStyleGenerator.class.getName(), new OAuthAuthorizeToken());
info(OPENSHIFT_KIND, OpenShiftDocumentKeyValueStyleGenerator.class.getName(), new Role());
info(OPENSHIFT_KIND, OpenShiftDocumentKeyValueStyleGenerator.class.getName(), new Project());
info(OPENSHIFT_KIND, OpenShiftDocumentKeyValueStyleGenerator.class.getName(), new OAuthAccessToken());
info(OPENSHIFT_KIND, OpenShiftDocumentKeyValueStyleGenerator.class.getName(), new DeploymentConfig());
}
use of io.fabric8.openshift.api.model.OAuthClient in project fabric8-maven-plugin by fabric8io.
the class OpenShiftDependencyResources method addMissingResources.
public void addMissingResources(List<HasMetadata> objects) {
Map<KindAndName, HasMetadata> itemMap = new HashMap<>();
for (HasMetadata item : objects) {
itemMap.put(new KindAndName(item), item);
}
// lets add any openshift specific dependencies (OAuthClient etc) which are not already added
for (Map.Entry<KindAndName, HasMetadata> entry : openshiftDependencyResources.entrySet()) {
KindAndName key = entry.getKey();
HasMetadata dependency = entry.getValue();
if (!itemMap.containsKey(key)) {
objects.add(dependency);
}
}
}
use of io.fabric8.openshift.api.model.OAuthClient in project fabric8 by fabric8io.
the class SessionListener method applyConfiguration.
private boolean applyConfiguration(KubernetesClient client, Controller controller, Configuration configuration, Session session, List<KubernetesList> kubeConfigs) throws Exception {
Logger log = session.getLogger();
Map<Integer, Callable<Boolean>> conditions = new TreeMap<>();
Callable<Boolean> sessionPodsReady = new SessionPodsAreReady(client, session);
Callable<Boolean> servicesReady = new SessionServicesAreReady(client, session, configuration);
Set<HasMetadata> entities = new TreeSet<>(new HasMetadataComparator());
for (KubernetesList c : kubeConfigs) {
entities.addAll(enhance(session, configuration, c).getItems());
}
if (containsImageStreamResources(entities)) {
// no need to use a local image registry
// as we are using OpenShift and
} else {
String registry = getLocalDockerRegistry();
if (Strings.isNotBlank(registry)) {
log.status("Adapting resources to pull images from registry: " + registry);
addRegistryToImageNameIfNotPresent(entities, registry);
} else {
log.status("No local fabric8 docker registry found");
}
}
List<Object> items = new ArrayList<>();
items.addAll(entities);
// Ensure services are processed first.
Collections.sort(items, new Comparator<Object>() {
@Override
public int compare(Object left, Object right) {
if (left instanceof Service) {
return -1;
} else if (right instanceof Service) {
return 1;
} else {
return 0;
}
}
});
boolean isOpenshift = client.isAdaptable(OpenShiftClient.class);
String namespace = session.getNamespace();
String routeDomain = null;
if (Strings.isNotBlank(configuration.getKubernetesDomain())) {
routeDomain = configuration.getKubernetesDomain();
}
preprocessEnvironment(client, controller, configuration, session);
Set<HasMetadata> extraEntities = new TreeSet<>(new HasMetadataComparator());
for (Object entity : items) {
if (entity instanceof Pod) {
Pod pod = (Pod) entity;
log.status("Applying pod:" + getName(pod));
Set<Secret> secrets = generateSecrets(client, session, pod.getMetadata());
String serviceAccountName = pod.getSpec().getServiceAccountName();
if (Strings.isNotBlank(serviceAccountName)) {
generateServiceAccount(client, session, secrets, serviceAccountName);
}
controller.applyPod(pod, session.getId());
conditions.put(1, sessionPodsReady);
} else if (entity instanceof Service) {
Service service = (Service) entity;
String serviceName = getName(service);
log.status("Applying service:" + serviceName);
controller.applyService(service, session.getId());
conditions.put(2, servicesReady);
if (isOpenshift) {
Route route = Routes.createRouteForService(routeDomain, namespace, service, log);
if (route != null) {
log.status("Applying route for:" + serviceName);
controller.applyRoute(route, "route for " + serviceName);
extraEntities.add(route);
}
}
} else if (entity instanceof ReplicationController) {
ReplicationController replicationController = (ReplicationController) entity;
log.status("Applying replication controller:" + getName(replicationController));
Set<Secret> secrets = generateSecrets(client, session, replicationController.getSpec().getTemplate().getMetadata());
String serviceAccountName = replicationController.getSpec().getTemplate().getSpec().getServiceAccountName();
if (Strings.isNotBlank(serviceAccountName)) {
generateServiceAccount(client, session, secrets, serviceAccountName);
}
controller.applyReplicationController(replicationController, session.getId());
conditions.put(1, sessionPodsReady);
} else if (entity instanceof ReplicaSet || entity instanceof Deployment || entity instanceof DeploymentConfig) {
log.status("Applying " + entity.getClass().getSimpleName() + ".");
controller.apply(entity, session.getId());
conditions.put(1, sessionPodsReady);
} else if (entity instanceof OAuthClient) {
OAuthClient oc = (OAuthClient) entity;
// these are global so lets create a custom one for the new namespace
ObjectMeta metadata = KubernetesHelper.getOrCreateMetadata(oc);
String name = metadata.getName();
if (isOpenshift) {
OpenShiftClient openShiftClient = client.adapt(OpenShiftClient.class);
OAuthClient current = openShiftClient.oAuthClients().withName(name).get();
boolean create = false;
if (current == null) {
current = oc;
create = true;
}
boolean updated = false;
// lets add a new redirect entry
List<String> redirectURIs = current.getRedirectURIs();
String namespaceSuffix = "-" + namespace;
String redirectUri = "http://" + name + namespaceSuffix;
if (Strings.isNotBlank(routeDomain)) {
redirectUri += "." + Strings.stripPrefix(routeDomain, ".");
}
if (!redirectURIs.contains(redirectUri)) {
redirectURIs.add(redirectUri);
updated = true;
}
current.setRedirectURIs(redirectURIs);
log.status("Applying OAuthClient:" + name);
controller.setSupportOAuthClients(true);
if (create) {
openShiftClient.oAuthClients().create(current);
} else {
if (updated) {
// TODO this should work!
// openShiftClient.oAuthClients().withName(name).replace(current);
openShiftClient.oAuthClients().withName(name).delete();
current.getMetadata().setResourceVersion(null);
openShiftClient.oAuthClients().create(current);
}
}
}
} else if (entity instanceof HasMetadata) {
log.status("Applying " + entity.getClass().getSimpleName() + ":" + KubernetesHelper.getName((HasMetadata) entity));
controller.apply(entity, session.getId());
} else if (entity != null) {
log.status("Applying " + entity.getClass().getSimpleName() + ".");
controller.apply(entity, session.getId());
}
}
entities.addAll(extraEntities);
// Wait until conditions are meet.
if (!conditions.isEmpty()) {
Callable<Boolean> compositeCondition = new CompositeCondition(conditions.values());
WaitStrategy waitStrategy = new WaitStrategy(compositeCondition, configuration.getWaitTimeout(), configuration.getWaitPollInterval());
if (!waitStrategy.await()) {
log.error("Timed out waiting for pods/services!");
return false;
} else {
log.status("All pods/services are currently 'running'!");
}
} else {
log.warn("No pods/services/replication controllers defined in the configuration!");
}
return true;
}
Aggregations