Skip to content

Commit

Permalink
#115 add helm installation
Browse files Browse the repository at this point in the history
  • Loading branch information
ppxl committed Nov 28, 2023
1 parent 603c02d commit d6e00bc
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0


## [Unreleased]
### Added
- Add Helm installation with `k3d.installHelm()`; #115

## [1.67.0](https://github.com/cloudogu/ces-build-lib/releases/tag/1.67.0) - 2023-09-04
### Changed
- Switch to hadolint Dockerfile linter; #111
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1067,7 +1067,7 @@ if (response.status == '201' && response.content-type == 'application/json') {

# K3d

`K3d` provides functions to set up and administer a lokal k3s cluster in Docker.
`K3d` provides functions to set up and administer a local k3s cluster in Docker.

Example:

Expand All @@ -1077,11 +1077,15 @@ K3d k3d = new K3d(this, env.WORKSPACE, env.PATH)
try {
stage('Set up k3d cluster') {
k3d.startK3d()
k3d.installHelm() // Helm may be installed additionally
}
stage('Do something with your cluster') {
k3d.kubectl("get nodes")
}
stage('Apply your Helm chart') { // requires previously called installHelm()
k3d.helm("apply path/to/your/chart")
}
stage('build and push development artefact') {
String myCurrentArtefactVersion = "yourTag-1.2.3-dev"
Expand Down
32 changes: 32 additions & 0 deletions src/com/cloudogu/ces/cesbuildlib/K3d.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,23 @@ class K3d {
return script.sh(script: "sudo KUBECONFIG=${k3dDir}/.kube/config kubectl ${command}", returnStdout: returnStdout)
}

/**
* Runs any helm command.
* @param command must contain all necessary arguments and flags.
*/
void helm(command) {
helm(command, false)
}

/**
* Runs any helm command and returns the generated output if configured.
* @param command must contain all necessary arguments and flags.
* @param returnStdout if set to true this method returns the standard output stream generated by helm.
*/
String helm(command, returnStdout) {
return script.sh(script: "sudo KUBECONFIG=${k3dDir}/.kube/config helm ${command}", returnStdout: returnStdout)
}

String kubectlHideCommand(command, returnStdout) {
return script.sh(script: "set +x; sudo KUBECONFIG=${k3dDir}/.kube/config kubectl ${command}", returnStdout: returnStdout)
}
Expand Down Expand Up @@ -359,6 +376,21 @@ spec:
script.echo "Installing kubectl..."
script.sh script: "sudo snap install kubectl --classic"
}
/**
* Installs helm
*/
void installHelm() {
def helmStatusCode = script.sh script: "snap list helm", returnStatus: true
if (helmStatusCode == 0 || helmStatusCode.equals("0")) {
script.echo "helm already installed"
return
}

script.echo "Installing helm..."
script.withEnv(["HOME=${k3dDir}", "PATH=${k3dBinaryDir}:${path}"]) {
script.sh script: "sudo snap install helm --classic"
}
}

private String getExecPodName(String dogu, Integer timeout, Integer interval) {
for (int i = 0; i < timeout / interval; i++) {
Expand Down
30 changes: 30 additions & 0 deletions test/com/cloudogu/ces/cesbuildlib/K3dTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,36 @@ class K3dTest extends GroovyTestCase {
assertThat(scriptMock.allActualArgs[0].trim()).isEqualTo("sudo KUBECONFIG=leK3dWorkSpace/.k3d/.kube/config kubectl get nodes".trim())
assertThat(scriptMock.allActualArgs.size()).isEqualTo(1)
}
void testHelm() {
// given
String workspaceDir = "leWorkspace"
def scriptMock = new ScriptMock()
K3d sut = new K3d(scriptMock, workspaceDir, "leK3dWorkSpace", "path")

// when
sut.helm("apply path/to/chart/")

// then
assertThat(scriptMock.allActualArgs[0].trim()).isEqualTo("sudo KUBECONFIG=leK3dWorkSpace/.k3d/.kube/config helm apply path/to/chart/".trim())
assertThat(scriptMock.allActualArgs.size()).isEqualTo(1)
}

// we cannot test lazy-installation because the mock is incapable of mocking the right types, the right values
// and thus repeated calls to the same script with different results.
void testInstallHelm_initially() {
// given
String workspaceDir = "leWorkspace"
def scriptMock = new ScriptMock()
K3d sut = new K3d(scriptMock, workspaceDir, "leK3dWorkSpace", "path")

// when
sut.installHelm()

// then
assertThat(scriptMock.allActualArgs.size()).isEqualTo(2)
assertThat(scriptMock.allActualArgs[0].trim()).isEqualTo("snap list helm".trim())
assertThat(scriptMock.allActualArgs[1].trim()).isEqualTo("sudo snap install helm --classic".trim())
}

void testStartK3d() {
def workspaceDir = "leWorkspace"
Expand Down

0 comments on commit d6e00bc

Please sign in to comment.