-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add capability to lookup values from kubernetes objects
- Loading branch information
1 parent
d1e0c16
commit 52d142e
Showing
7 changed files
with
130 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"strings" | ||
|
||
"github.com/urfave/cli" | ||
) | ||
|
||
// K8ApiKubectl is a kubectl implimentation of K8Api interface | ||
type K8ApiKubectl struct { | ||
K8Api | ||
Cx *cli.Context | ||
} | ||
|
||
// NewK8ApiKubectl creates a concrete class bound to use kubectl | ||
func NewK8ApiKubectl(c *cli.Context) K8Api { | ||
api := &K8ApiKubectl{ | ||
Cx: c, | ||
} | ||
return api | ||
} | ||
|
||
// Lookup will get data from a specified kubernetes object | ||
func (a K8ApiKubectl) Lookup(kind, name, path string) (string, error) { | ||
args := []string{"get", kind + "/" + name, "-o", "custom-columns=:" + path, "--no-headers"} | ||
|
||
cmd, err := newKubeCmd(a.Cx, args, false) | ||
if err != nil { | ||
return "", err | ||
} | ||
stderr, _ := cmd.StderrPipe() | ||
stdout, _ := cmd.StdoutPipe() | ||
if err := cmd.Start(); err != nil { | ||
logDebug.Printf("error starting kubectl: %s", err) | ||
return "", err | ||
} | ||
data, _ := ioutil.ReadAll(stdout) | ||
if err := cmd.Wait(); err != nil { | ||
logDebug.Printf("error with kubectl: %s", err) | ||
errData, _ := ioutil.ReadAll(stderr) | ||
if strings.Contains("NotFound", string(errData[:])) { | ||
return "", fmt.Errorf("Error object %s/%s not found", kind, name) | ||
} | ||
return "", err | ||
} | ||
return strings.TrimSpace(string(data[:])), nil | ||
} |
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,16 @@ | ||
package main | ||
|
||
// K8ApiNoop is a noop API runner used when not connected to a server | ||
type K8ApiNoop struct { | ||
K8Api | ||
} | ||
|
||
// NewK8ApiNoop creats a new K8Api implimentaion based on K8ApiNoop | ||
func NewK8ApiNoop() K8Api { | ||
return &K8ApiNoop{} | ||
} | ||
|
||
// Lookup will pretentd to get data from a specified kubernetes object | ||
func (a K8ApiNoop) Lookup(kind, name, path string) (string, error) { | ||
return "noop", nil | ||
} |
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,7 @@ | ||
package main | ||
|
||
// K8Api is an abstraction to allow the migration to the real API not kubectl | ||
type K8Api interface { | ||
// Lookup abstract interface for finding kuberneets api data by kind, name and path | ||
Lookup(kind, name, path string) (string, error) | ||
} |
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