-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(kubernetes): properly handle missing namespaces (#120)
* fix(kubernetes): properly handle missing namespaces Most cluster native operations won't succeed if the namespaces of the object is yet to be created, even though it will be just created. To enable a better user experience than kubectl does, let's do the following: - on diff, just report every object in a missing object as entirely new - on apply, create namespaces first to succeed in a single run (needed two before) * doc(kubernetes): util.DiffName godoc
- Loading branch information
Showing
7 changed files
with
140 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package client | ||
|
||
import ( | ||
"bytes" | ||
"os/exec" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/grafana/tanka/pkg/kubernetes/manifest" | ||
"github.com/grafana/tanka/pkg/kubernetes/util" | ||
) | ||
|
||
// DiffServerSide takes the desired state and computes the differences on the | ||
// server, returning them in `diff(1)` format | ||
func (k Kubectl) DiffServerSide(data manifest.List) (*string, error) { | ||
ns, err := k.Namespaces() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ready, missing := separateMissingNamespace(data, ns) | ||
argv := []string{"diff", | ||
"--context", k.context.Get("name").MustStr(), | ||
"-f", "-", | ||
} | ||
cmd := exec.Command("kubectl", argv...) | ||
|
||
raw := bytes.Buffer{} | ||
cmd.Stdout = &raw | ||
cmd.Stderr = FilterWriter{regexp.MustCompile(`exit status \d`)} | ||
|
||
cmd.Stdin = strings.NewReader(ready.String()) | ||
|
||
err = cmd.Run() | ||
|
||
// kubectl uses exit status 1 to tell us that there is a diff | ||
if exitError, ok := err.(*exec.ExitError); ok && exitError.ExitCode() == 1 { | ||
} else if err != nil { | ||
return nil, err | ||
} | ||
|
||
s := raw.String() | ||
for _, r := range missing { | ||
d, err := util.DiffStr(util.DiffName(r), "", r.String()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
s += d | ||
} | ||
|
||
if s != "" { | ||
return &s, nil | ||
} | ||
|
||
// no diff -> nil | ||
return nil, nil | ||
} | ||
|
||
func separateMissingNamespace(in manifest.List, exists map[string]bool) (ready, missingNamespace manifest.List) { | ||
for _, r := range in { | ||
if !exists[r.Metadata().Namespace()] { | ||
missingNamespace = append(missingNamespace, r) | ||
continue | ||
} | ||
ready = append(ready, r) | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters