Skip to content

Commit

Permalink
Added new task CreateNamespace and fixed some doc issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
dancc authored and dancc committed Feb 21, 2018
1 parent cc88942 commit fa67d39
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 12 deletions.
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,24 @@ The `kubernetes` extension acts as a mapper to the [Config](https://github.com/f
All [additional options](https://github.com/fabric8io/kubernetes-client#configuring-the-client) that exist to configure the client are also honored here.

## Tasks

- **Name**: name of the gradle task
- **`config{}`**: Object the `config{}` closure maps to
- **`onNext{}`**: Object the next iteration of `onNext{}` will receive
- **`resource()`**: Object the `resource()` method returns AFTER execution

### Namespace operations

| Name | `config{}` maps to | `resource()` maps to | `onNext()` maps to |
| Name | `config{}` | `onNext{}` | `resource()` |
| --- | --- | --- | --- |
| [ListNamespaces](https://github.com/bmuschko/gradle-kubernetes-plugin/blob/master/src/main/groovy/com/bmuschko/gradle/kubernetes/plugin/tasks/namespaces/ListNamespaces.groovy) | [NonNamespaceOperation](http://static.javadoc.io/io.fabric8/kubernetes-client/3.1.8/io/fabric8/kubernetes/client/dsl/NonNamespaceOperation.html) | [NamespaceList](http://static.javadoc.io/io.fabric8/kubernetes-model/2.0.8/io/fabric8/kubernetes/api/model/NamespaceList.html) | [Namespace](http://static.javadoc.io/io.fabric8/kubernetes-model/2.0.8/io/fabric8/kubernetes/api/model/Namespace.html) |
| [ListNamespaces](https://github.com/bmuschko/gradle-kubernetes-plugin/blob/master/src/main/groovy/com/bmuschko/gradle/kubernetes/plugin/tasks/namespaces/ListNamespaces.groovy) | [NonNamespaceOperation](http://static.javadoc.io/io.fabric8/kubernetes-client/3.1.8/io/fabric8/kubernetes/client/dsl/NonNamespaceOperation.html) | [Namespace](http://static.javadoc.io/io.fabric8/kubernetes-model/2.0.8/io/fabric8/kubernetes/api/model/Namespace.html) | [NamespaceList](http://static.javadoc.io/io.fabric8/kubernetes-model/2.0.8/io/fabric8/kubernetes/api/model/NamespaceList.html) |
| [CreateNamespace](https://github.com/bmuschko/gradle-kubernetes-plugin/blob/master/src/main/groovy/com/bmuschko/gradle/kubernetes/plugin/tasks/namespaces/CreateNamespace.groovy) | [MetadataNestedImpl](http://static.javadoc.io/io.fabric8/kubernetes-model/2.0.8/io/fabric8/kubernetes/api/model/NamespaceFluentImpl.MetadataNestedImpl.html) | [Namespace](http://static.javadoc.io/io.fabric8/kubernetes-model/2.0.8/io/fabric8/kubernetes/api/model/Namespace.html) | [Namespace](http://static.javadoc.io/io.fabric8/kubernetes-model/2.0.8/io/fabric8/kubernetes/api/model/Namespace.html) |

### System operations

| Name | Description |
| --- | --- |
| [Configuration](https://github.com/bmuschko/gradle-kubernetes-plugin/blob/master/src/main/groovy/com/bmuschko/gradle/kubernetes/plugin/tasks/system/Configuration.groovy) | Get the system configuration |
| Name | `config{}` | `onNext{}` | `resource()` |
| --- | --- | --- | --- |
| [Configuration](https://github.com/bmuschko/gradle-kubernetes-plugin/blob/master/src/main/groovy/com/bmuschko/gradle/kubernetes/plugin/tasks/system/Configuration.groovy) | N/A | [Configuration](https://github.com/bmuschko/gradle-kubernetes-plugin/blob/master/src/main/groovy/com/bmuschko/gradle/kubernetes/plugin/tasks/system/Configuration.groovy) | [Configuration](https://github.com/bmuschko/gradle-kubernetes-plugin/blob/master/src/main/groovy/com/bmuschko/gradle/kubernetes/plugin/tasks/system/Configuration.groovy) |

## Reactive-Streams

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.bmuschko.gradle.kubernetes.plugin.tasks.namespaces

import com.bmuschko.gradle.kubernetes.plugin.AbstractFunctionalTest
import org.gradle.testkit.runner.BuildResult
import spock.lang.Requires

/**
*
* All functional tests for the `CreateNamespace` task.
*
*/
class CreateNamespaceFunctionalTest extends AbstractFunctionalTest {

def "Create namespace, execute reactive-streams, and fail with no config"() {
buildFile << """
import com.bmuschko.gradle.kubernetes.plugin.tasks.namespaces.CreateNamespace
task namespaceWork(type: CreateNamespace) {
onError { exc ->
logger.quiet "\${exc.message}"
}
onNext { output ->
logger.quiet '$SHOULD_NOT_REACH_HERE'
}
onComplete {
logger.quiet '$SHOULD_NOT_REACH_HERE'
}
doLast {
if (response()) {
logger.quiet '$RESPONSE_SET_MESSAGE'
}
}
}
task workflow(dependsOn: namespaceWork)
"""

when:
BuildResult result = build('workflow')

then:
result.output.contains('Creating namespace...')
result.output.contains('Required value: name or generateName is required')
!result.output.contains(SHOULD_NOT_REACH_HERE)
}

def "Create namespace, execute reactive-streams, and with config"() {

def generatedNamespaceName = randomString()

buildFile << """
import com.bmuschko.gradle.kubernetes.plugin.tasks.namespaces.CreateNamespace
task namespaceWork(type: CreateNamespace) {
config {
withName("${generatedNamespaceName}")
}
onError {
logger.quiet '$ON_ERROR_NOT_REACHED'
}
onNext { output ->
if (output) {
logger.quiet "$ON_NEXT_REACHED with name \${output.getMetadata().getName()}"
}
}
onComplete {
logger.quiet '$ON_COMPLETE_REACHED'
}
doLast {
if (response()) {
logger.quiet '$RESPONSE_SET_MESSAGE'
}
}
}
task workflow(dependsOn: namespaceWork)
"""

when:
BuildResult result = build('workflow')

then:
result.output.contains('Creating namespace...')
!result.output.contains(ON_ERROR_NOT_REACHED)
!result.output.contains(SHOULD_NOT_REACH_HERE)
result.output.contains(ON_NEXT_REACHED + " with name ${generatedNamespaceName}")
result.output.contains(ON_COMPLETE_REACHED)
result.output.contains(RESPONSE_SET_MESSAGE)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ import spock.lang.Requires
*/
class ListNamespacesFunctionalTest extends AbstractFunctionalTest {

def "List namespaces, execute reactive-streams, with default config"() {
def "List namespaces, execute reactive-streams, with no config"() {
buildFile << """
import com.bmuschko.gradle.kubernetes.plugin.tasks.namespaces.ListNamespaces
task kubeNamespaces(type: ListNamespaces) {
task namespaceWork(type: ListNamespaces) {
onError {
logger.quiet '$ON_ERROR_NOT_REACHED'
}
Expand All @@ -50,7 +50,7 @@ class ListNamespacesFunctionalTest extends AbstractFunctionalTest {
}
}
task workflow(dependsOn: kubeNamespaces)
task workflow(dependsOn: namespaceWork)
"""

when:
Expand All @@ -64,11 +64,11 @@ class ListNamespacesFunctionalTest extends AbstractFunctionalTest {
result.output.contains(RESPONSE_SET_MESSAGE)
}

def "List namespaces, execute reactive-streams, with config"() {
def "List namespaces, execute reactive-streams, and with config"() {
buildFile << """
import com.bmuschko.gradle.kubernetes.plugin.tasks.namespaces.ListNamespaces
task kubeNamespaces(type: ListNamespaces) {
task namespaceWork(type: ListNamespaces) {
config {
withLabel("${randomString()}")
}
Expand All @@ -88,7 +88,7 @@ class ListNamespacesFunctionalTest extends AbstractFunctionalTest {
}
}
task workflow(dependsOn: kubeNamespaces)
task workflow(dependsOn: namespaceWork)
"""

when:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.bmuschko.gradle.kubernetes.plugin.tasks.namespaces

import com.bmuschko.gradle.kubernetes.plugin.tasks.AbstractKubernetesTask

/**
* Create a namespace.
*/
class CreateNamespace extends AbstractKubernetesTask {

@Override
def handleClient(kubernetesClient) {

logger.quiet 'Creating namespace...'
def namespaceMetadata = kubernetesClient.namespaces().createNew().withNewMetadata()

// configure on meta-data
namespaceMetadata = configureOn(namespaceMetadata)

// finalize creation of namespace
def localResponse = namespaceMetadata.endMetadata().done()

// register response for downstream use which in this case
// is just a `Namespace` instance.
responseOn(localResponse)
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.bmuschko.gradle.kubernetes.plugin.tasks.namespaces

import com.bmuschko.gradle.kubernetes.plugin.tasks.AbstractKubernetesTask
Expand All @@ -27,12 +28,17 @@ class ListNamespaces extends AbstractKubernetesTask {

logger.quiet 'Listing namespaces...'
def namespaces = kubernetesClient.namespaces()

// configure on the namespaces instance itself
namespaces = configureOn(namespaces)

// get the `NamespaceList` object which in itself is NOT a list.
def localResponse = namespaces.list()

// register response for downstream use and return list of items
// for `onNext` execution. The `getItems()` call will return null
// if no items were found.
responseOn(namespaces.list()).getItems()
responseOn(localResponse).getItems()
}
}

0 comments on commit fa67d39

Please sign in to comment.