Skip to content

Commit

Permalink
Merge pull request #337 from Praqma/cleanUntracked
Browse files Browse the repository at this point in the history
use the current helm state to identify untracked releases
  • Loading branch information
luisdavim authored Dec 12, 2019
2 parents 18f137b + e2ce500 commit c3ca345
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 24 deletions.
25 changes: 15 additions & 10 deletions helm_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type releaseState struct {
Updated time.Time
Status string
Chart string
Name string
Namespace string
TillerNamespace string
}
Expand Down Expand Up @@ -198,6 +199,7 @@ func buildState() {
Updated: time,
Status: rel.Releases[i].Status,
Chart: rel.Releases[i].Chart,
Name: rel.Releases[i].Name,
Namespace: rel.Releases[i].Namespace,
TillerNamespace: rel.Releases[i].TillerNamespace,
}
Expand Down Expand Up @@ -227,7 +229,6 @@ func helmReleaseExists(r *release, status string) (bool, releaseState) {

// getReleaseRevision returns the revision number for a release
func getReleaseRevision(rs releaseState) string {

return strconv.Itoa(rs.Revision)
}

Expand Down Expand Up @@ -592,18 +593,22 @@ func initHelm() (bool, string) {
func cleanUntrackedReleases() {
toDelete := make(map[string]map[*release]bool)
log.Println("INFO: checking if any Helmsman managed releases are no longer tracked by your desired state ...")
for ns, releases := range getHelmsmanReleases() {
for r := range releases {
tracked := false
for _, app := range s.Apps {
if app.Name == r.Name && getDesiredTillerNamespace(app) == ns {
tracked = true
}
}

// List all releases managed my Helmsman
for n, hr := range getHelmsmanReleases() {
for name, tracked := range hr {
if !tracked {
rs := currentState[name+"-"+n]
ns := rs.TillerNamespace
if _, ok := toDelete[ns]; !ok {
toDelete[ns] = make(map[*release]bool)
}
r := &release{
Chart: rs.Chart,
Name: rs.Name,
Namespace: rs.Namespace,
TillerNamespace: rs.TillerNamespace,
}
toDelete[ns][r] = true
}
}
Expand All @@ -614,7 +619,7 @@ func cleanUntrackedReleases() {
} else {
for ns, releases := range toDelete {
for r := range releases {
if r.isReleaseConsideredToRun() {
if !r.isReleaseConsideredToRun() {
logDecision(generateDecisionMessage(r, "untracked release [ "+r.Name+" ] is ignored by target flag. Skipping.", false), -800, ignored)
} else {
logDecision(generateDecisionMessage(r, "untracked release found: release [ "+r.Name+" ]. It will be deleted", true), -800, delete)
Expand Down
29 changes: 15 additions & 14 deletions kube_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"io/ioutil"
"log"
"regexp"
"strings"

"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -437,9 +438,9 @@ func labelResource(r *release) {
// getHelmsmanReleases returns a map of all releases that are labeled with "MANAGED-BY=HELMSMAN"
// The releases are categorized by the namespaces in which their Tiller is running
// The returned map format is: map[<Tiller namespace>:map[<releases managed by Helmsman and deployed using this Tiller>:true]]
func getHelmsmanReleases() map[string]map[*release]bool {
func getHelmsmanReleases() map[string]map[string]bool {
var lines []string
releases := make(map[string]map[*release]bool)
releases := make(map[string]map[string]bool)
storageBackend := "configmap"

if s.Settings.StorageBackend == "secret" {
Expand All @@ -462,7 +463,7 @@ func getHelmsmanReleases() map[string]map[*release]bool {
for _, ns := range namespaces {
cmd := command{
Cmd: "kubectl",
Args: []string{"get", storageBackend, "-n", ns, "-l", "MANAGED-BY=HELMSMAN"},
Args: []string{"get", storageBackend, "-n", ns, "-l", "MANAGED-BY=HELMSMAN", "-o", "name"},
Description: "getting helm releases which are managed by Helmsman in namespace [[ " + ns + " ]].",
}

Expand All @@ -475,18 +476,18 @@ func getHelmsmanReleases() map[string]map[*release]bool {
lines = strings.Split(output, "\n")
}

for i := 0; i < len(lines); i++ {
if lines[i] == "" || strings.HasSuffix(strings.TrimSpace(lines[i]), "AGE") {
for _, r := range lines {
if r == "" {
continue
} else {
fields := strings.Fields(lines[i])
if _, ok := releases[ns]; !ok {
releases[ns] = make(map[*release]bool)
}
for _, r := range s.Apps {
if r.Name == fields[0][0:strings.LastIndex(fields[0], ".v")] {
releases[ns][r] = true
}
}
r = regexp.MustCompile(`(^\w+\/|\.v\d+$)`).ReplaceAllString(r, "")
if _, ok := releases[ns]; !ok {
releases[ns] = make(map[string]bool)
}
releases[ns][r] = false
for _, app := range s.Apps {
if r == app.Name {
releases[ns][r] = true
}
}
}
Expand Down

0 comments on commit c3ca345

Please sign in to comment.