Skip to content

Instantly share code, notes, and snippets.

@rawlingsj
Created March 15, 2018 18:31
Show Gist options
  • Save rawlingsj/4f41ce6c08493116931bf8e86db0688d to your computer and use it in GitHub Desktop.
Save rawlingsj/4f41ce6c08493116931bf8e86db0688d to your computer and use it in GitHub Desktop.
package util
import (
"os/exec"
"strings"
"testing"
"encoding/json"
"fmt"
"regexp"
"github.com/jenkins-x/jx/pkg/jx/cmd/log"
"github.com/stretchr/testify/assert"
)
type Rules struct {
Rules []Rule
}
type Rule struct {
Name string `json:"name"`
TargetTags []string `json:"targetTags"`
}
// DISCLAIMER:: This is just a trial and shouldn't be relied upon to return correct values.
// Deleting firewalls can disable clusters if they are in use so we are not ready for people to rely on this until we make a CLI command
func TestDeleteGKEFirewallRules(t *testing.T) {
data, err := getCommandOutput("", "gcloud", "compute", "firewall-rules", "list", "--format", "json")
assert.NoError(t, err)
var rules []Rule
err = json.Unmarshal([]byte(data), &rules)
assert.NoError(t, err)
out, err := getCommandOutput("", "gcloud", "container", "clusters", "list")
assert.NoError(t, err)
lines := strings.Split(string(out), "\n")
var existingClusters []string
for _, l := range lines {
if strings.Contains(l, "NAME") {
continue
}
log.Infof("Line: %s \n", l)
if strings.TrimSpace(l) == "" {
break
}
fields := strings.Fields(l)
existingClusters = append(existingClusters, fields[0])
}
var nameToDelete []string
for _, rule := range rules {
name := strings.TrimPrefix(rule.Name, "gke-")
if !contains(existingClusters, name) {
for _, tagFull := range rule.TargetTags {
tag := strings.TrimPrefix(tagFull, "gke-")
if !contains(existingClusters, tag) {
log.Infof("Delete: %s %s\n", rule.Name, tagFull)
nameToDelete = append(nameToDelete, rule.Name)
}
}
}
}
log.Info("\n\n\n")
log.Info("*************************************************************")
log.Info("DISCLAIMER:: This is just a trial and shouldn't be relied upon to return correct values.")
log.Info("Deleting firewalls can disable clusters if they are in use so we are not ready for people to rely on this until we make a CLI command")
log.Info("*************************************************************")
args := "gcloud compute firewall-rules delete "
for _, name := range nameToDelete {
args = args + " " + name
}
log.Info(args)
}
func contains(s []string, e string) bool {
for _, a := range s {
var match = regexp.MustCompile(fmt.Sprintf("^%s-[a-z0-9]{8}-node", a))
if match.MatchString(e) {
log.Info("matched prefix\n")
return true
}
}
log.Infof("NOT matched %s\n", e)
return false
}
// getCommandOutput evaluates the given command and returns the trimmed output
func getCommandOutput(dir string, name string, args ...string) (string, error) {
e := exec.Command(name, args...)
if dir != "" {
e.Dir = dir
}
data, err := e.CombinedOutput()
text := string(data)
text = strings.TrimSpace(text)
return text, err
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment