Skip to content

Commit

Permalink
Merge branch 'neiser-fixes-seacon2018'
Browse files Browse the repository at this point in the history
  • Loading branch information
lreimer committed May 4, 2018
2 parents d8143b9 + 7c24aa9 commit b7b49b6
Show file tree
Hide file tree
Showing 13 changed files with 100 additions and 58 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,11 @@ kubernetes.tar.gz

# OSX
*.DS_Store

# Eclipse
/.classpath
/.project
/.settings/

/bin/
/out/
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
buildscript {
ext {
kotlinVersion = '1.0.2'
kotlinVersion = '1.2.40'
slf4jVersion = '1.7.20'
spekVersion = '1.0.25'
}
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Tue Mar 29 23:41:17 CEST 2016
#Thu May 03 17:43:50 CEST 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.11-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.11-bin.zip
10 changes: 3 additions & 7 deletions gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ case "`uname`" in
;;
esac

# For Cygwin, ensure paths are in UNIX format before anything is touched.
if $cygwin ; then
[ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
fi

# Attempt to set APP_HOME
# Resolve links: $0 may be a link
PRG="$0"
Expand All @@ -61,9 +56,9 @@ while [ -h "$PRG" ] ; do
fi
done
SAVED="`pwd`"
cd "`dirname \"$PRG\"`/" >&-
cd "`dirname \"$PRG\"`/" >/dev/null
APP_HOME="`pwd -P`"
cd "$SAVED" >&-
cd "$SAVED" >/dev/null

CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar

Expand Down Expand Up @@ -114,6 +109,7 @@ fi
if $cygwin ; then
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
JAVACMD=`cygpath --unix "$JAVACMD"`

# We build the pattern for arguments to be converted via cygpath
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
Expand Down
2 changes: 1 addition & 1 deletion gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ echo location of your Java installation.
goto fail

:init
@rem Get command-line arguments, handling Windowz variants
@rem Get command-line arguments, handling Windows variants

if not "%OS%" == "Windows_NT" goto win9xME_args
if "%@eval[2+2]" == "4" goto 4NT_args
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ open class KubernetesCluster @Inject constructor(private val client: KubernetesC
private val events: Event<ClusterAppEvent>,
private val logger: Logger) : Watcher<Deployment>, Cluster {

private val deployments = Array<Deployment?>(8, { i -> null })
private val names = Array<String?>(8, { i -> null })
private val deployments = Array<Deployment?>(8, { _ -> null })
private val names = Array<String?>(8, { _ -> null })
private var disableEvents = true

@PostConstruct
open fun init() {
Expand All @@ -63,6 +64,8 @@ open class KubernetesCluster @Inject constructor(private val client: KubernetesC

// does not work with GCE (No HTTP 101)
operation.watch(this)

disableEvents = false
}

override fun appExists(appIndex: Int) = deployments.indices.contains(appIndex) && deployments[appIndex] != null
Expand All @@ -83,6 +86,11 @@ open class KubernetesCluster @Inject constructor(private val client: KubernetesC
val name = KubernetesHelper.getName(deployment)
var index = deployments.indexOfFirst { it == null }

if (names.contains(name)) {
logger.info("Deployment with name {} already added. Ignored.", name)
return
}

if (index == -1) {
logger.info("Found new deployment {} but could not add because all rows are occupied.", name)
return
Expand Down Expand Up @@ -133,19 +141,30 @@ open class KubernetesCluster @Inject constructor(private val client: KubernetesC
}

override fun reset() {
0.until(8).forEach {
deployments[it] = null
names[it] = null
}
disableEvents = true
try {
0.until(8).forEach {
deployments[it] = null
names[it] = null
}

val operation = client.extensions().deployments().inNamespace(namespace)
val list = operation.list()
list?.items?.forEach {
addDepolyment(it)
val operation = client.extensions().deployments().inNamespace(namespace)
val list = operation.list()
list?.items?.forEach {
addDepolyment(it)
}
} finally {
disableEvents = false
}

}

override fun eventReceived(action: Watcher.Action?, resource: Deployment?) {
if (disableEvents) {
logger.info("Event {} not processed as globally disabled. Resource={}", action, resource)
return
}

when (action) {
Watcher.Action.ADDED -> {
addDepolyment(resource!!)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class LeapMotionListener constructor(private val launchpad: LaunchpadController,
val swipe = SwipeGesture(gesture)
if (swipe.state() == Gesture.State.STATE_STOP) {
// scale cluster by number of fingers
val fingers = frame?.fingers()?.count() ?: -1
val fingers = frame.fingers()?.count() ?: -1
logger.debug("Detected swipe gesture with {} fingers.", fingers)
launchpad.scale(fingers)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import javax.sound.midi.Receiver
*/
@ApplicationScoped
@Alternative
@Exclude(exceptIfProjectStage = arrayOf(ProjectStage.Development::class))
@Exclude(exceptIfProjectStage = [(ProjectStage.Development::class)])
open class LoggingReceiver @Inject constructor(private val logger: Logger) : Receiver {

override fun send(message: MidiMessage?, timeStamp: Long) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import javax.sound.midi.Transmitter
*/
@ApplicationScoped
@Alternative
@Exclude(exceptIfProjectStage = arrayOf(ProjectStage.Development::class))
@Exclude(exceptIfProjectStage = [(ProjectStage.Development::class)])
open class LoggingTransmitter @Inject constructor(private val logger: Logger) : Transmitter {

private var receiver: Receiver? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ open class MarathonCluster @Inject constructor(private val client: MarathonClien
private val events: Event<ClusterAppEvent>,
private val logger: Logger) : Cluster {

private val apps = Array<MarathonClient.App?>(8, { i -> null })
private val deploying = Array<Boolean>(8, { i -> false })
private val apps = Array<MarathonClient.App?>(8, { _ -> null })
private val deploying = Array<Boolean>(8, { _ -> false })

@PostConstruct
open fun init() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import com.google.gson.GsonBuilder
import com.moandjiezana.toml.Toml
import okhttp3.OkHttpClient
import org.apache.deltaspike.core.api.config.ConfigProperty
import org.apache.deltaspike.core.api.exclude.Exclude
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.io.File
Expand All @@ -38,6 +39,7 @@ import javax.inject.Inject
/**
* The CDI producer for the Marathon Java API.
*/
@Exclude(onExpression = "cluster.service!=marathon")
@ApplicationScoped
open class MarathonProducer @Inject constructor(@ConfigProperty(name = "dcos.configPath")
private val configPath: String?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,113 +49,130 @@ open class OpenShiftCluster @Inject constructor(private val client: OpenShiftCli
private val events: Event<ClusterAppEvent>,
private val logger: Logger) : Watcher<DeploymentConfig>, Cluster {

private val deployments = Array<DeploymentConfig?>(8, { i -> null })
private val names = Array<String?>(8, { i -> null })
private val deploymentConfigs = Array<DeploymentConfig?>(8, { _ -> null })
private val names = Array<String?>(8, { _ -> null })
private var disableEvents = true

@PostConstruct
open fun init() {
logger.info("Connect to OpenShift master {}.", client.masterUrl)
val operation = client.deploymentConfigs().inNamespace(namespace)
val list = operation.list()
list?.items?.forEach {
addDepolyment(it)
addDeploymentConfig(it)
}

// does not work with GCE (No HTTP 101)
operation.watch(this)
disableEvents = false
}

override fun appExists(appIndex: Int) = deployments.indices.contains(appIndex) && deployments[appIndex] != null
override fun appExists(appIndex: Int) = deploymentConfigs.indices.contains(appIndex) && deploymentConfigs[appIndex] != null

override fun labels(appIndex: Int): Map<String, String> {
return labels(deployments[appIndex])
return labels(deploymentConfigs[appIndex])
}

private fun labels(deployment: DeploymentConfig?): Map<String, String> {
return KubernetesHelper.getLabels(deployment)
}

override fun replicas(appIndex: Int): Int {
return deployments[appIndex]?.spec?.replicas ?: -1
return deploymentConfigs[appIndex]?.spec?.replicas ?: -1
}

private fun addDepolyment(deployment: DeploymentConfig) {
val name = KubernetesHelper.getName(deployment)
var index = deployments.indexOfFirst { it == null }
private fun addDeploymentConfig(deploymentConfig: DeploymentConfig) {
val name = KubernetesHelper.getName(deploymentConfig)
var index = deploymentConfigs.indexOfFirst { it == null }

if (names.contains(name)) {
logger.info("DeploymentConfig with name {} already added. Ignored.", name)
return
}

if (index == -1) {
logger.info("Found new DeploymentConfig {} but could not add because all rows are occupied.", name)
return
}

val labels = labels(deployment)
val labels = labels(deploymentConfig)

if (!"true".equals(labels["LAUNCHPAD_ENABLE"], true)) {
return
}

if (labels.containsKey("LAUNCHPAD_ROW")) {
val row = labels["LAUNCHPAD_ROW"]!!.toInt()
if (deployments.indices.contains(row) && deployments[row] == null) {
if (deploymentConfigs.indices.contains(row) && deploymentConfigs[row] == null) {
index = row
}
}

deployments[index] = deployment
deploymentConfigs[index] = deploymentConfig
names[index] = name
logger.info("Added DeploymentConfig {} at index {}.", name, index)
events.fire(ClusterAppEvent(index, replicas(index), labels(deployment), ClusterAppEvent.Type.ADDED))
events.fire(ClusterAppEvent(index, replicas(index), labels(deploymentConfig), ClusterAppEvent.Type.ADDED))
}

/**
* Scale the OpenShift deployment to a number of given replicas.
* Scale the OpenShift deployment config to a number of given replicas.
*
* @param appIndex the deployment index on the Launchpad
* @param replicas the number of replicas
*/
override fun scale(appIndex: Int, replicas: Int) {
if (appIndex > deployments.size) return
if (appIndex > deploymentConfigs.size) return

val deployment = deployments[appIndex]
val name = KubernetesHelper.getName(deployment)
val deploymentConfig = deploymentConfigs[appIndex]
val name = KubernetesHelper.getName(deploymentConfig)

logger.info("Scaling DeploymentConfig {} to {} replicas.",
KubernetesHelper.getName(deployment), replicas)
KubernetesHelper.getName(deploymentConfig), replicas)

synchronized(client) {
deployments[appIndex] = client.deploymentConfigs().inNamespace(namespace).withName(name)
deploymentConfigs[appIndex] = client.deploymentConfigs().inNamespace(namespace).withName(name)
.edit().editSpec()
.withReplicas(replicas)
.endSpec().done()
}

events.fire(ClusterAppEvent(appIndex, replicas, labels(deployment), ClusterAppEvent.Type.DEPLOYED))
events.fire(ClusterAppEvent(appIndex, replicas, labels(deploymentConfig), ClusterAppEvent.Type.DEPLOYED))
}

override fun reset() {
0.until(8).forEach {
deployments[it] = null
names[it] = null
}
disableEvents = true
try {
0.until(8).forEach {
deploymentConfigs[it] = null
names[it] = null
}

val operation = client.deploymentConfigs().inNamespace(namespace)
val list = operation.list()
list?.items?.forEach {
addDepolyment(it)
val operation = client.deploymentConfigs().inNamespace(namespace)
val list = operation.list()
list?.items?.forEach {
addDeploymentConfig(it)
}
} finally {
disableEvents = false
}
}

override fun eventReceived(action: Watcher.Action?, resource: DeploymentConfig?) {
if (disableEvents) {
logger.info("Event {} not processed as globally disabled. Resource={}", action, resource)
return
}

when (action) {
Watcher.Action.ADDED -> {
addDepolyment(resource!!)
addDeploymentConfig(resource!!)
}

Watcher.Action.MODIFIED -> {
val name = KubernetesHelper.getName(resource)
val index = names.indexOf(name)
val previous = deployments[index]
deployments[index] = resource
val previous = deploymentConfigs[index]
deploymentConfigs[index] = resource

// now check if the number of replicas has changed
val oldReplicas = previous?.spec?.replicas ?: 0
Expand All @@ -175,7 +192,7 @@ open class OpenShiftCluster @Inject constructor(private val client: OpenShiftCli
logger.info("Deleted DeploymentConfig {}.", name)

val index = names.indexOf(name)
deployments[index] = null
deploymentConfigs[index] = null
names[index] = null

events.fire(ClusterAppEvent(index, 0, labels(resource), ClusterAppEvent.Type.DELETED))
Expand Down

0 comments on commit b7b49b6

Please sign in to comment.