forked from openshift-kni/eco-goinfra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
namespace.go
98 lines (90 loc) · 3.15 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"fmt"
"github.com/openshift-kni/eco-goinfra/pkg/clients"
"github.com/openshift-kni/eco-goinfra/pkg/namespace"
)
func createExample() {
// Init client struct. If path to kubeconfig is empty string then KUBECONFIG env var is used.
apiClients := clients.New("")
// If error occurs during apiClient initialization function returns nil pointer.
if apiClients == nil {
panic("Failed to load api client")
}
// Init namespace struct. NewBuilder function require minimum set of parameters.
exampleNamespace := namespace.NewBuilder(apiClients, "example")
// Mutate exampleNamespace struct and add labels to the namespace object
exampleNamespace = exampleNamespace.WithLabel("examplekey", "examplevalue")
// Create namespace on cluster
_, err := exampleNamespace.Create()
if err != nil {
fmt.Print(err.Error())
panic("Failed to create namespace on cluster")
}
}
func deleteExistingExample() {
// Init client struct. If path to kubeconfig is empty string then KUBECONFIG env var is used.
apiClients := clients.New("")
// If error occurs during apiClient initialization function returns nil pointer.
if apiClients == nil {
panic("Failed to load api client")
}
// Pull existing namespace from cluster
exampleNamespace, err := namespace.Pull(apiClients, "example")
if err != nil {
fmt.Print(err.Error())
panic("Failed to Pull namespace from cluster")
}
// Delete namespace
err = exampleNamespace.Delete()
if err != nil {
fmt.Print(err.Error())
panic("Failed to delete namespace from cluster")
}
}
func updateExistingExample() {
// Init client struct. If path to kubeconfig is empty string then KUBECONFIG env var is used.
apiClients := clients.New("")
// If error occurs during apiClient initialization function returns nil pointer.
if apiClients == nil {
panic("Failed to load api client")
}
// Pull existing namespace from cluster
exampleNamespace, err := namespace.Pull(apiClients, "example")
if err != nil {
fmt.Print(err.Error())
panic("Failed to Pull namespace from cluster")
}
// Update object definition using With* function
exampleNamespace = exampleNamespace.WithLabel("key", "value")
// Update object on cluster
_, err = exampleNamespace.Update()
if err != nil {
fmt.Print(err.Error())
panic("Failed to Update namespace from cluster")
}
}
func createOneLinerExample() {
// Init client struct. If path to kubeconfig is empty string then KUBECONFIG env var is used.
apiClients := clients.New("")
// If error occurs during apiClient initialization function returns nil pointer.
if apiClients == nil {
panic("Failed to load api client")
}
// Create namespace with labels using single line
_, err := namespace.NewBuilder(apiClients, "example").WithLabel("key", "value").Create()
if err != nil {
fmt.Print(err.Error())
panic("Failed to create namespace on cluster")
}
}
func main() {
// Example of how to create namespace using ./pkg/namespace.
createExample()
// Example of how to delete existing namespace on cluster using ./pkg/namespace.
deleteExistingExample()
// Update existing object using With mutation function.
updateExistingExample()
// Create namespace using one line.
createOneLinerExample()
}