Skip to content
This repository has been archived by the owner on Jun 17, 2022. It is now read-only.

Commit

Permalink
add namespace filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
imsky committed Apr 17, 2019
1 parent 1ff0987 commit fa0cb59
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2018 Better Mortgage
Copyright (c) 2018-2019 Better Mortgage

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
22 changes: 20 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,34 @@ func filterObjectsByLabel(objects []runtime.Object, label string) []runtime.Obje
return filteredObjects
}

func filterObjectsByNamespace(objects []runtime.Object, namespace string) []runtime.Object {
if namespace == "" {
return objects
}

filteredObjects := make([]runtime.Object, 0, 1)
for _, o := range objects {
metadata, _ := getObjectMetadata(o)
if metadata.GetNamespace() == namespace {
filteredObjects = append(filteredObjects, o)
}
}

return filteredObjects
}

func main() {
//todo: exit status, write to stderr, embed version and build
flag.Usage = func() {
fmt.Println("Usage: kubechange -l <label> <file> ...")
fmt.Printf("kubechange helps keep local and remote Kubernetes state up-to-date\n\n")
fmt.Println("-l string\tLabel to use as a filter")
fmt.Println("-n string\tNamespace of compared resources")
fmt.Println("-e string\tUpdate cluster objects")
}

label := flag.String("l", "", "Label to use as filter")
namespace := flag.String("n", "", "Namespace of compared resources")
execute := flag.Bool("e", false, "Update cluster objects")

homedir := os.Getenv("HOME")
Expand Down Expand Up @@ -199,7 +217,7 @@ func main() {
panic(err)
}

srcObjects := filterObjectsByLabel(localObjects, *label)
srcObjects := filterObjectsByLabel(filterObjectsByNamespace(localObjects, *namespace), *label)
//todo: consider all namespaces
namespaces := getObjectNamespaces(srcObjects)

Expand All @@ -217,7 +235,7 @@ func main() {
}
}

dstObjects := filterObjectsByLabel(remoteObjects, *label)
dstObjects := filterObjectsByLabel(filterObjectsByNamespace(remoteObjects, *namespace), *label)

pairs := pairObjectsByCriteria(srcObjects, dstObjects, PairCriteria{*label})

Expand Down
19 changes: 19 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,25 @@ func TestParsing(t *testing.T) {
}
}

func TestFiltering(t *testing.T) {
files := readFiles([]string{"example-test-job.yml"})

for _, file := range files {
objects, _ := parseManifests(file)
filteredByNamespace := filterObjectsByNamespace(objects, "foo")

if len(filteredByNamespace) != 0 {
t.Errorf("Expected no objects")
}

filteredByNamespace = filterObjectsByNamespace(objects, "default")

if len(filteredByNamespace) != 1 {
t.Errorf("Expected 1 object")
}
}
}

func TestCompare(t *testing.T) {
fields := compareNodeSelector(map[string]string{
"group": "prod",
Expand Down

0 comments on commit fa0cb59

Please sign in to comment.