-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
210 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
src/main/kotlin/eu/openanalytics/shinyproxyoperator/components/EventFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/** | ||
* ShinyProxy-Operator | ||
* | ||
* Copyright (C) 2021-2024 Open Analytics | ||
* | ||
* =========================================================================== | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the Apache License as published by | ||
* The Apache Software Foundation, either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* Apache License for more details. | ||
* | ||
* You should have received a copy of the Apache License | ||
* along with this program. If not, see <http://www.apache.org/licenses/> | ||
*/ | ||
package eu.openanalytics.shinyproxyoperator.components | ||
|
||
import eu.openanalytics.shinyproxyoperator.crd.ShinyProxy | ||
import eu.openanalytics.shinyproxyoperator.crd.ShinyProxyInstance | ||
import io.fabric8.kubernetes.api.model.EventBuilder | ||
import io.fabric8.kubernetes.client.KubernetesClient | ||
import mu.KotlinLogging | ||
import java.time.Instant | ||
import java.time.ZonedDateTime | ||
import java.time.format.DateTimeFormatter | ||
|
||
|
||
class EventFactory(private val kubeClient: KubernetesClient) { | ||
|
||
private val logger = KotlinLogging.logger {} | ||
|
||
fun createNewInstanceEvent(shinyProxy: ShinyProxy, shinyProxyInstance: ShinyProxyInstance) { | ||
createEvent(shinyProxy, shinyProxyInstance, "Normal", "StartingNewInstance", "Configuration changed") | ||
} | ||
|
||
fun createInstanceReadyEvent(shinyProxy: ShinyProxy, shinyProxyInstance: ShinyProxyInstance) { | ||
createEvent(shinyProxy, shinyProxyInstance, "Normal", "InstanceReady", "ShinyProxy ready") | ||
} | ||
|
||
private fun createEvent(shinyProxy: ShinyProxy, shinyProxyInstance: ShinyProxyInstance, type: String, action: String, reason: String, message: String? = null) { | ||
val k8sMicroTime = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'.'SSSSSSXXX") | ||
|
||
//@formatter:off | ||
val eventBuilder = EventBuilder() | ||
.withNewMetadata() | ||
.withGenerateName("test-event") | ||
.withNamespace(shinyProxy.metadata.namespace) | ||
.addNewOwnerReference() | ||
.withController(true) | ||
.withKind("ShinyProxy") | ||
.withApiVersion("openanalytics.eu/v1") | ||
.withName(shinyProxy.metadata.name) | ||
.withUid(shinyProxy.metadata.uid) | ||
.endOwnerReference() | ||
.withLabels<String, String>(LabelFactory.labelsForShinyProxyInstance(shinyProxy, shinyProxyInstance)) | ||
.endMetadata() | ||
.withNewInvolvedObject() | ||
.withKind("ShinyProxy") | ||
.withApiVersion("openanalytics.eu/v1") | ||
.withName(shinyProxy.metadata.name) | ||
.withNamespace(shinyProxy.metadata.namespace) | ||
.withUid(shinyProxy.metadata.uid) | ||
.endInvolvedObject() | ||
.withNewEventTime(k8sMicroTime.format(ZonedDateTime.now())) | ||
.withReportingInstance("shinyproxy-operator") // TODO | ||
.withReportingComponent("shinyproxy-operator") | ||
.withAction(action) // which action failed, machine-readable | ||
.withType(type) // Warning or Normal | ||
.withReason(reason); // reason is why the action was taken, human-readable, 128 characters | ||
//@formatter:on | ||
|
||
if (message != null) { | ||
eventBuilder.withMessage(message) | ||
} | ||
|
||
kubeClient.v1().events().resource(eventBuilder.build()).create() | ||
} | ||
|
||
private fun truncateMessage(message: String?): String? { | ||
if (message == null) { | ||
return null | ||
} | ||
if (message.length > 1024) { | ||
return message.substring(0, 1012) + " [truncated]" | ||
} | ||
return message | ||
} | ||
|
||
fun createInstanceFailed(shinyProxy: ShinyProxy, shinyProxyInstance: ShinyProxyInstance, message: String?, creationTimestamp: Instant?) { | ||
val events = kubeClient.v1().events().withLabels(LabelFactory.labelsForShinyProxyInstance(shinyProxy, shinyProxyInstance)).list() | ||
val truncatedMessage = truncateMessage(message) | ||
if (events.items.any { | ||
it.action == "StartingNewInstanceFailed" | ||
&& it.type == "Warning" | ||
&& it.message == truncatedMessage | ||
&& Instant.parse(it.eventTime.time) > creationTimestamp | ||
}) { | ||
return | ||
} | ||
logger.warn { "${shinyProxy.logPrefix(shinyProxyInstance)} Pods are failing: ${message?.replace("\n", "")}" } | ||
createEvent(shinyProxy, shinyProxyInstance, "Warning", "StartingNewInstanceFailed", "ShinyProxy failed to start", truncatedMessage) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/main/kotlin/eu/openanalytics/shinyproxyoperator/controller/ReplicaSetStatusChecker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* ShinyProxy-Operator | ||
* | ||
* Copyright (C) 2021-2024 Open Analytics | ||
* | ||
* =========================================================================== | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the Apache License as published by | ||
* The Apache Software Foundation, either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* Apache License for more details. | ||
* | ||
* You should have received a copy of the Apache License | ||
* along with this program. If not, see <http://www.apache.org/licenses/> | ||
*/ | ||
package eu.openanalytics.shinyproxyoperator.controller | ||
|
||
import eu.openanalytics.shinyproxyoperator.crd.ShinyProxy | ||
import eu.openanalytics.shinyproxyoperator.crd.ShinyProxyInstance | ||
import java.time.Instant | ||
|
||
class ReplicaSetStatusChecker(private val podRetriever: PodRetriever) { | ||
|
||
data class Status(val failed: Boolean, val failureMessage: String?, val creationTimestamp: Instant?) | ||
|
||
fun check(shinyProxy: ShinyProxy, shinyProxyInstance: ShinyProxyInstance): Status { | ||
val pods = podRetriever.getShinyProxyPods(shinyProxy, shinyProxyInstance) | ||
if (pods.isEmpty()) { | ||
// no pods -> not ready yet, but not failed | ||
return Status(false, null, null) | ||
} | ||
for (pod in pods) { | ||
if (pod.status.containerStatuses.isEmpty()) { | ||
// no container status yet | ||
continue | ||
} | ||
val shinyproxyContainer = pod.status.containerStatuses.firstOrNull { it.name == "shinyproxy" } | ||
if (shinyproxyContainer == null) { | ||
// no shinyproxy container status | ||
continue | ||
} | ||
if (!shinyproxyContainer.ready && shinyproxyContainer.restartCount >= 1) { | ||
// container has failed | ||
val msg = shinyproxyContainer.lastState?.terminated?.message ?: "Unknown error" | ||
val creationTimestamp = Instant.parse(pod.metadata.creationTimestamp) | ||
return Status(true, msg, creationTimestamp) | ||
} | ||
} | ||
return Status(false, null, null) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters