This repository has been archived by the owner on Jan 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 558
/
Copy pathnamespace.go
62 lines (55 loc) · 1.52 KB
/
namespace.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package namespace
import (
"encoding/json"
"log"
"os/exec"
"time"
"github.com/Azure/acs-engine/test/e2e/kubernetes/util"
)
// Namespace holds namespace metadata
type Namespace struct {
Metadata Metadata `json:"metadata"`
}
// Metadata holds information like name and created timestamp
type Metadata struct {
CreatedAt time.Time `json:"creationTimestamp"`
Name string `json:"name"`
}
// Create a namespace with the given name
func Create(name string) (*Namespace, error) {
cmd := exec.Command("kubectl", "create", "namespace", name)
util.PrintCommand(cmd)
out, err := cmd.CombinedOutput()
if err != nil {
log.Printf("Error trying to create namespace (%s):%s\n", name, string(out))
return nil, err
}
return Get(name)
}
// Get returns a namespace for with a given name
func Get(name string) (*Namespace, error) {
cmd := exec.Command("kubectl", "get", "namespace", name, "-o", "json")
util.PrintCommand(cmd)
out, err := cmd.CombinedOutput()
if err != nil {
log.Printf("Error trying to get namespace (%s):%s\n", name, string(out))
return nil, err
}
n := Namespace{}
err = json.Unmarshal(out, &n)
if err != nil {
log.Printf("Error unmarshalling namespace json:%s\n", err)
}
return &n, nil
}
// Delete a namespace
func (n *Namespace) Delete() error {
cmd := exec.Command("kubectl", "delete", "namespace", n.Metadata.Name)
util.PrintCommand(cmd)
out, err := cmd.CombinedOutput()
if err != nil {
log.Printf("Error while trying to delete namespace (%s):%s\n", n.Metadata.Name, out)
return err
}
return nil
}