Skip to content

Commit

Permalink
Merge pull request #20 from wunderio/feature/post-release-delete
Browse files Browse the repository at this point in the history
Integrate k8s client; uninstall Helm releases
  • Loading branch information
agnis-mateuss authored Sep 15, 2022
2 parents 6f9b4e6 + 4ac2a9f commit 84e3764
Show file tree
Hide file tree
Showing 8 changed files with 1,400 additions and 90 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.17.6
go-version: 1.19.1
- name: Run tests
run: go test -v ./tests
releases-matrix:
Expand All @@ -50,6 +50,6 @@ jobs:
goarch: ${{ matrix.goarch }}
release_tag: master
overwrite: true
goversion: "https://go.dev/dl/go1.17.6.linux-amd64.tar.gz"
goversion: "https://go.dev/dl/go1.19.1.linux-amd64.tar.gz"
binary_name: "silta"
ldflags: "-X github.com/wunderio/silta-cli/internal/common.Version=${{ steps.vars.outputs.sha_short }}"
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.17.6
go-version: 1.19.1
- name: Run tests
run: go test -v ./tests
releases-matrix:
Expand All @@ -47,7 +47,7 @@ jobs:
github_token: ${{ secrets.GITHUB_TOKEN }}
goos: ${{ matrix.goos }}
goarch: ${{ matrix.goarch }}
goversion: "https://go.dev/dl/go1.17.6.linux-amd64.tar.gz"
goversion: "https://go.dev/dl/go1.19.1.linux-amd64.tar.gz"
binary_name: "silta"
ldflags: "-X github.com/wunderio/silta-cli/internal/common.Version=${{ steps.vars.outputs.tag }}"
circleci-k8s-test-build:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.17.6
go-version: 1.19.1
- name: Run tests
run: go test -v ./tests
releases-matrix:
Expand All @@ -50,6 +50,6 @@ jobs:
goarch: ${{ matrix.goarch }}
release_tag: test
overwrite: true
goversion: "https://go.dev/dl/go1.17.6.linux-amd64.tar.gz"
goversion: "https://go.dev/dl/go1.19.1.linux-amd64.tar.gz"
binary_name: "silta"
ldflags: "-X github.com/wunderio/silta-cli/internal/common.Version=${{ steps.vars.outputs.sha_short }}"
96 changes: 96 additions & 0 deletions cmd/ciReleaseDelete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package cmd

import (
"context"
"log"
"os"

helmclient "github.com/mittwald/go-helm-client"
"github.com/spf13/cobra"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp" // gcp auth provider
"k8s.io/client-go/tools/clientcmd"
)

var ciReleaseDeleteCmd = &cobra.Command{
Use: "delete",
Short: "Delete a release",
Run: func(cmd *cobra.Command, args []string) {
releaseName, _ := cmd.Flags().GetString("release-name")
namespace, _ := cmd.Flags().GetString("namespace")
deletePVCs, _ := cmd.Flags().GetBool("delete-pvcs")

homeDir, err := os.UserHomeDir()
if err != nil {
log.Fatalf("cannot read user home dir")
}
kubeConfigPath := homeDir + "/.kube/config"

kubeConfig, err := os.ReadFile(kubeConfigPath)
if err != nil {
log.Fatalf("cannot read kubeConfig from path")
}

//k8s go client init logic
config, err := clientcmd.BuildConfigFromFlags("", kubeConfigPath)
if err != nil {
log.Fatalf("cannot read kubeConfig from path: %s", err)
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
log.Fatalf("cannot initialize k8s client: %s", err)
}

//Helm client init logic
opt := &helmclient.KubeConfClientOptions{
Options: &helmclient.Options{
Namespace: namespace,
RepositoryCache: "/tmp/.helmcache",
RepositoryConfig: "/tmp/.helmrepo",
Debug: false,
Linting: false, // Change this to false if you don't want linting.
},
KubeContext: "",
KubeConfig: kubeConfig,
}

helmClient, err := helmclient.NewClientFromKubeConf(opt)
if err != nil {
log.Fatalf("cannot create client from kubeConfig")
}

//Uninstall Helm release
uninstallErr := helmClient.UninstallReleaseByName(releaseName)
if uninstallErr != nil {
log.Fatalf("Error removing a release:%s", uninstallErr)
}

//Delete PVCs
if deletePVCs == true {
PVC_client := clientset.CoreV1().PersistentVolumeClaims(namespace)
list, err := PVC_client.List(context.TODO(), v1.ListOptions{
LabelSelector: "release=" + releaseName,
})
if err != nil {
log.Fatalf("Error getting the list of PVCs: %s", err)
}

for _, v := range list.Items {
PVC_client.Delete(context.TODO(), v.Name, v1.DeleteOptions{})
}
}

},
}

func init() {
ciReleaseCmd.AddCommand(ciReleaseDeleteCmd)

ciReleaseDeleteCmd.Flags().String("release-name", "", "Release name")
ciReleaseDeleteCmd.Flags().String("namespace", "", "Project name (namespace, i.e. \"drupal-project\")")
ciReleaseDeleteCmd.Flags().Bool("delete-pvcs", false, "Delete PVCs")

ciReleaseDeleteCmd.MarkFlagRequired("release-name")
ciReleaseDeleteCmd.MarkFlagRequired("namespace")
}
1 change: 1 addition & 0 deletions docs/silta_ci_release.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ silta ci release [flags]

* [silta ci](silta_ci.md) - Silta CI Commands
* [silta ci release clean-failed](silta_ci_release_clean-failed.md) - Clean failed releases
* [silta ci release delete](silta_ci_release_delete.md) - Delete a release
* [silta ci release deploy](silta_ci_release_deploy.md) - Deploy release
* [silta ci release environmentname](silta_ci_release_environmentname.md) - Return environment name
* [silta ci release info](silta_ci_release_info.md) - Print release information
Expand Down
28 changes: 28 additions & 0 deletions docs/silta_ci_release_delete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
## silta ci release delete

Delete a release

```
silta ci release delete [flags]
```

### Options

```
--delete-pvcs Delete PVCs
-h, --help help for delete
--namespace string Project name (namespace, i.e. "drupal-project")
--release-name string Release name
```

### Options inherited from parent commands

```
--debug Print variables, do not execute external commands, rather print them
--use-env Use environment variables for value assignment (default true)
```

### SEE ALSO

* [silta ci release](silta_ci_release.md) - CI release related commands

132 changes: 130 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,142 @@ go 1.17

require (
github.com/Luzifer/go-openssl/v4 v4.1.0
github.com/spf13/cobra v1.3.0
github.com/spf13/cobra v1.4.0
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/mittwald/go-helm-client v0.11.3
k8s.io/apimachinery v0.24.3
k8s.io/client-go v0.24.3
)

require (
cloud.google.com/go v0.99.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/BurntSushi/toml v1.0.0 // indirect
github.com/MakeNowJust/heredoc v0.0.0-20170808103936-bb23615498cd // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.1.1 // indirect
github.com/Masterminds/sprig/v3 v3.2.2 // indirect
github.com/Masterminds/squirrel v1.5.3 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/asaskevich/govalidator v0.0.0-20200428143746-21a406dcc535 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/chai2010/gettext-go v0.0.0-20160711120539-c6fed771bfd5 // indirect
github.com/containerd/containerd v1.6.6 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect
github.com/cyphar/filepath-securejoin v0.2.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/cli v20.10.17+incompatible // indirect
github.com/docker/distribution v2.8.1+incompatible // indirect
github.com/docker/docker v20.10.17+incompatible // indirect
github.com/docker/docker-credential-helpers v0.6.4 // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-metrics v0.0.1 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/emicklei/go-restful v2.9.5+incompatible // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/go-errors/errors v1.0.1 // indirect
github.com/go-gorp/gorp/v3 v3.0.2 // indirect
github.com/go-logr/logr v1.2.2 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.19.5 // indirect
github.com/go-openapi/swag v0.19.14 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/btree v1.0.1 // indirect
github.com/google/gnostic v0.6.9 // indirect
github.com/google/go-cmp v0.5.6 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/uuid v1.2.0 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/gosuri/uitable v0.0.4 // indirect
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect
github.com/huandu/xstrings v1.3.2 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jmoiron/sqlx v1.3.5 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect
github.com/lib/pq v1.10.6 // indirect
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/moby/locker v1.0.1 // indirect
github.com/moby/spdystream v0.2.0 // indirect
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/onsi/gomega v1.17.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.3-0.20211202183452-c5a74bcca799 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.12.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/rubenv/sql-migrate v1.1.1 // indirect
github.com/russross/blackfriday v1.5.2 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/crypto v0.0.0-20220214200702-86341886e292 // indirect
github.com/stretchr/testify v1.7.2 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca // indirect
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // indirect
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f // indirect
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20220107163113-42d7afdf6368 // indirect
google.golang.org/grpc v1.43.0 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
helm.sh/helm/v3 v3.9.1 // indirect
k8s.io/api v0.24.3 // indirect
k8s.io/apiextensions-apiserver v0.24.2 // indirect
k8s.io/apiserver v0.24.2 // indirect
k8s.io/cli-runtime v0.24.3 // indirect
k8s.io/component-base v0.24.2 // indirect
k8s.io/klog/v2 v2.60.1 // indirect
k8s.io/kube-openapi v0.0.0-20220328201542-3ee0da9b0b42 // indirect
k8s.io/kubectl v0.24.2 // indirect
k8s.io/utils v0.0.0-20220210201930-3a6ce19ff2f9 // indirect
oras.land/oras-go v1.2.0 // indirect
sigs.k8s.io/json v0.0.0-20211208200746-9f7c6b3444d2 // indirect
sigs.k8s.io/kustomize/api v0.11.4 // indirect
sigs.k8s.io/kustomize/kyaml v0.13.6 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.1 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)
Loading

0 comments on commit 84e3764

Please sign in to comment.