Skip to content

Commit

Permalink
Add missing test and docs for docker registry credentials feature from
Browse files Browse the repository at this point in the history
…#19 fixed in 136ca2f
  • Loading branch information
schnatterer committed Mar 10, 2023
1 parent 2530878 commit 256bc2b
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 26 deletions.
40 changes: 19 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,32 +343,30 @@ First of all there are some mandatory properties e.g. the information about your
The GitOps-build-lib uses some docker images internally (To run Helm or Kubectl commands and specific Validators inside a docker container).
All of these have set default images, but you can change them if you wish to.
```groovy
def gitopsConfig = [
buildImages: [
// These are used to run helm and kubectl commands in the core logic
helm: 'ghcr.io/cloudogu/helm:3.5.4-1',
kubectl: 'lachlanevenson/k8s-kubectl:v1.19.3',
// These are used for each specific validator via an imageRef property inside the validators config. See [Validators] for examples.
kubeval: 'ghcr.io/cloudogu/helm:3.5.4-1',
helmKubeval: 'ghcr.io/cloudogu/helm:3.5.4-1',
yamllint: 'cytopia/yamllint:1.25-0.7'
]
]
```

Optional - if image is in a private repository, you can pass a `credentialsId` for pulling images.

```groovy
def gitopsConfig = [
buildImages: [
// These are used to run helm and kubectl commands in the core logic
//
helm: [
image: 'ghcr.io/cloudogu/helm:3.11.1-2'
credentialsId: 'myCredentials' (optional - only needed if image is in a private repository. CredentialsId is getting pulled from Jenkins credentials)
],
kubectl: [
image: 'lachlanevenson/k8s-kubectl:v1.24.8'
credentialsId: 'myCredentials' (optional - only needed if image is in a private repository. CredentialsId is getting pulled from Jenkins credentials)
],
// These are used for each specific validator via an imageRef property inside the validators config. See [Validators] for examples.
kubeval: [
image: 'ghcr.io/cloudogu/helm:3.5.4-1'
credentialsId: 'myCredentials' (optional - only needed if image is in a private repository. CredentialsId is getting pulled from Jenkins credentials)
image: 'ghcr.io/cloudogu/helm:3.11.1-2',
credentialsId: 'myCredentials'
],
helmKubeval: [
image: 'ghcr.io/cloudogu/helm:3.5.4-1'
credentialsId: 'myCredentials' (optional - only needed if image is in a private repository. CredentialsId is getting pulled from Jenkins credentials)
],
yamllint: [
image: 'cytopia/yamllint:1.25-0.9'
credentialsId: 'myCredentials' (optional - only needed if image is in a private repository. CredentialsId is getting pulled from Jenkins credentials)
]
// ...
]
]
```
Expand Down
12 changes: 7 additions & 5 deletions src/com/cloudogu/gitopsbuildlib/docker/DockerWrapper.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ class DockerWrapper {
void withDockerImage(def imageConfig, Closure body) {
// imageConfig can either be a Map or a String, depending on the old or the new format if this field
// The old format was a String containing an image url. The new one is a map with an image url and optional credentials
if(imageConfig instanceof Map && imageConfig.containsKey('credentialsId') && imageConfig.credentialsId) {
def registryUrl = getRegistryUrlFromImage(imageConfig.image)
script.docker.withRegistry("https://${registryUrl}", imageConfig.credentialsId) {
if (imageConfig instanceof Map) {
if (imageConfig.containsKey('credentialsId') && imageConfig.credentialsId) {
def registryUrl = getRegistryUrlFromImage(imageConfig.image)
script.docker.withRegistry("https://${registryUrl}", imageConfig.credentialsId) {
runDockerImage(imageConfig.image, body)
}
} else {
runDockerImage(imageConfig.image, body)
}
} else if (imageConfig instanceof Map && imageConfig.containsKey('image')){
runDockerImage(imageConfig.image, body)
} else {
// When imageConfig is a String
runDockerImage(imageConfig, body)
Expand Down
48 changes: 48 additions & 0 deletions test/com/cloudogu/gitopsbuildlib/docker/DockerWrapperTest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.cloudogu.gitopsbuildlib.docker

import com.cloudogu.gitopsbuildlib.ScriptMock
import org.junit.jupiter.api.Test

import static org.assertj.core.api.Assertions.assertThat

class DockerWrapperTest {

public static final String EXPECTED_IMAGE = 'ghcr.io/cloudogu/helm:3.11.1-2'

def scriptMock = new ScriptMock()

def dockerWrapper = new DockerWrapper(scriptMock.mock)

def imageConfigMap = [
image: EXPECTED_IMAGE,
]
def imageConfigMapWithCredentials = [
image: EXPECTED_IMAGE,
credentialsId: 'myCredentials'
]
def imageConfigString = EXPECTED_IMAGE

@Test
void 'works with imageConfig string'() {

dockerWrapper.withDockerImage(imageConfigString) {
}
assertThat(scriptMock.dockerMock.actualImages[0]).isEqualTo(EXPECTED_IMAGE)
}

@Test
void 'works with imageConfig Map'() {
dockerWrapper.withDockerImage(imageConfigMap) {
}
assertThat(scriptMock.dockerMock.actualImages[0]).isEqualTo(EXPECTED_IMAGE)
}

@Test
void 'works with imageConfig Map with Credentials'() {
dockerWrapper.withDockerImage(imageConfigMapWithCredentials) {
}
assertThat(scriptMock.dockerMock.actualImages[0]).isEqualTo(EXPECTED_IMAGE)
assertThat(scriptMock.dockerMock.actualRegistryArgs[0]).isEqualTo('https://ghcr.io/cloudogu')
assertThat(scriptMock.dockerMock.actualRegistryArgs[1]).isEqualTo('myCredentials')
}
}

0 comments on commit 256bc2b

Please sign in to comment.