-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plugins): Introduce KeelServiceSdk for external plugins (#1416)
* fix(resources): Fix discovery of supported resource kinds * feat(plugins): Introduce KeelServiceSdk for plugins * fix(pr): Add KeelReadOnlyRepository for plugin usage
- Loading branch information
Showing
6 changed files
with
144 additions
and
50 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
...-api/src/main/kotlin/com/netflix/spinnaker/keel/api/persistence/KeelReadOnlyRepository.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,62 @@ | ||
package com.netflix.spinnaker.keel.api.persistence | ||
|
||
import com.netflix.spinnaker.keel.api.DeliveryConfig | ||
import com.netflix.spinnaker.keel.api.Environment | ||
import com.netflix.spinnaker.keel.api.Resource | ||
import com.netflix.spinnaker.keel.api.ResourceSpec | ||
import com.netflix.spinnaker.keel.api.artifacts.ArtifactType | ||
import com.netflix.spinnaker.keel.api.artifacts.DeliveryArtifact | ||
import com.netflix.spinnaker.keel.api.constraints.ConstraintState | ||
|
||
/** | ||
* A read-only repository for interacting with delivery configs, artifacts, and resources. | ||
*/ | ||
interface KeelReadOnlyRepository { | ||
fun getDeliveryConfig(name: String): DeliveryConfig | ||
|
||
fun environmentFor(resourceId: String): Environment | ||
|
||
fun deliveryConfigFor(resourceId: String): DeliveryConfig | ||
|
||
fun getDeliveryConfigForApplication(application: String): DeliveryConfig | ||
|
||
fun getConstraintState(deliveryConfigName: String, environmentName: String, artifactVersion: String, type: String, artifactReference: String?): ConstraintState? | ||
|
||
fun constraintStateFor(application: String): List<ConstraintState> | ||
|
||
fun constraintStateFor(deliveryConfigName: String, environmentName: String, limit: Int): List<ConstraintState> | ||
|
||
fun constraintStateFor(deliveryConfigName: String, environmentName: String, artifactVersion: String): List<ConstraintState> | ||
|
||
fun pendingConstraintVersionsFor(deliveryConfigName: String, environmentName: String): List<String> | ||
|
||
fun getQueuedConstraintApprovals(deliveryConfigName: String, environmentName: String, artifactReference: String?): Set<String> | ||
|
||
fun getResource(id: String): Resource<ResourceSpec> | ||
|
||
fun hasManagedResources(application: String): Boolean | ||
|
||
fun getResourceIdsByApplication(application: String): List<String> | ||
|
||
fun getResourcesByApplication(application: String): List<Resource<*>> | ||
|
||
fun getArtifact(name: String, type: ArtifactType, deliveryConfigName: String): List<DeliveryArtifact> | ||
|
||
fun getArtifact(name: String, type: ArtifactType, reference: String, deliveryConfigName: String): DeliveryArtifact | ||
|
||
fun getArtifact(deliveryConfigName: String, reference: String): DeliveryArtifact | ||
|
||
fun isRegistered(name: String, type: ArtifactType): Boolean | ||
|
||
fun artifactVersions(artifact: DeliveryArtifact): List<String> | ||
|
||
fun artifactVersions(name: String, type: ArtifactType): List<String> | ||
|
||
fun latestVersionApprovedIn(deliveryConfig: DeliveryConfig, artifact: DeliveryArtifact, targetEnvironment: String): String? | ||
|
||
fun isApprovedFor(deliveryConfig: DeliveryConfig, artifact: DeliveryArtifact, version: String, targetEnvironment: String): Boolean | ||
|
||
fun wasSuccessfullyDeployedTo(deliveryConfig: DeliveryConfig, artifact: DeliveryArtifact, version: String, targetEnvironment: String): Boolean | ||
|
||
fun isCurrentlyDeployedTo(deliveryConfig: DeliveryConfig, artifact: DeliveryArtifact, version: String, targetEnvironment: String): Boolean | ||
} |
12 changes: 12 additions & 0 deletions
12
keel-api/src/main/kotlin/com/netflix/spinnaker/keel/api/plugins/KeelServiceSdk.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,12 @@ | ||
package com.netflix.spinnaker.keel.api.plugins | ||
|
||
import com.netflix.spinnaker.keel.api.actuation.TaskLauncher | ||
import com.netflix.spinnaker.keel.api.persistence.KeelReadOnlyRepository | ||
|
||
/** | ||
* A simple SDK that can be consumed by external plugins to access core Keel functionality. | ||
*/ | ||
interface KeelServiceSdk { | ||
val repository: KeelReadOnlyRepository | ||
val taskLauncher: TaskLauncher | ||
} |
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
15 changes: 15 additions & 0 deletions
15
keel-web/src/main/java/com/netflix/spinnaker/config/KeelServiceSdkConfiguration.java
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,15 @@ | ||
package com.netflix.spinnaker.config; | ||
|
||
import com.netflix.spinnaker.keel.plugins.KeelServiceSdkFactory; | ||
import com.netflix.spinnaker.kork.plugins.sdk.SdkFactory; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
class KeelServiceSdkConfiguration { | ||
@Bean | ||
public static SdkFactory serviceSdkFactory(ApplicationContext applicationContext) { | ||
return new KeelServiceSdkFactory(applicationContext); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
keel-web/src/main/kotlin/com/netflix/spinnaker/keel/plugins/KeelServiceSdkFactory.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,43 @@ | ||
package com.netflix.spinnaker.keel.plugins | ||
|
||
import com.netflix.spinnaker.keel.api.actuation.TaskLauncher | ||
import com.netflix.spinnaker.keel.persistence.KeelRepository | ||
import com.netflix.spinnaker.kork.exceptions.SystemException | ||
import com.netflix.spinnaker.kork.plugins.sdk.SdkFactory | ||
import org.pf4j.PluginWrapper | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.context.ApplicationContext | ||
|
||
/** | ||
* Creates [KeelServiceSdk] objects that can be consumed by external plugins. | ||
*/ | ||
class KeelServiceSdkFactory( | ||
private val applicationContext: ApplicationContext | ||
) : SdkFactory { | ||
|
||
private val log by lazy { LoggerFactory.getLogger(javaClass) } | ||
|
||
private val keelServiceSdk by lazy { | ||
val repository = getFirstBeanOfType(KeelRepository::class.java) | ||
val taskLauncher = getFirstBeanOfType(TaskLauncher::class.java) | ||
KeelServiceSdkImpl(repository, taskLauncher) | ||
} | ||
|
||
override fun create(extensionClass: Class<*>, pluginWrapper: PluginWrapper?): Any = | ||
keelServiceSdk | ||
|
||
private inline fun <reified T> getFirstBeanOfType(clazz: Class<T>): T = | ||
applicationContext.getBeansOfType(clazz) | ||
.let { | ||
if (it.isEmpty()) { | ||
throw SystemException("Failed to locate bean of type ${T::class.java.name} in application context") | ||
} else { | ||
val first = it.entries.first() | ||
if (it.size > 1) { | ||
val options = it.keys.joinToString() | ||
log.warn("Found more than one bean of type ${T::class.java.name} ($options), selecting '${first.key}'") | ||
} | ||
first.value | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
keel-web/src/main/kotlin/com/netflix/spinnaker/keel/plugins/KeelServiceSdkImpl.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,10 @@ | ||
package com.netflix.spinnaker.keel.plugins | ||
|
||
import com.netflix.spinnaker.keel.api.actuation.TaskLauncher | ||
import com.netflix.spinnaker.keel.api.persistence.KeelReadOnlyRepository | ||
import com.netflix.spinnaker.keel.api.plugins.KeelServiceSdk | ||
|
||
class KeelServiceSdkImpl( | ||
override val repository: KeelReadOnlyRepository, | ||
override val taskLauncher: TaskLauncher | ||
) : KeelServiceSdk |