This repository has been archived by the owner on Oct 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 257
/
azureidentitybinding.go
78 lines (66 loc) · 2.4 KB
/
azureidentitybinding.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
package azureidentitybinding
import (
"encoding/json"
"html/template"
"os"
"os/exec"
"path"
aadpodid "github.com/Azure/aad-pod-identity/pkg/apis/aadpodidentity/v1"
"github.com/Azure/aad-pod-identity/test/common/util"
"github.com/pkg/errors"
)
// Create will create an Azure Identity Binding on a Kubernetes cluster
func Create(name, selector, templateOutputPath string) error {
t, err := template.New("aadpodidentitybinding.yaml").ParseFiles(path.Join("template", "aadpodidentitybinding.yaml"))
if err != nil {
return errors.Wrap(err, "Failed to parse aadpodidentitybinding.yaml")
}
deployFilePath := path.Join(templateOutputPath, name+"-binding.yaml")
deployFile, err := os.Create(deployFilePath)
if err != nil {
return errors.Wrap(err, "Failed to create a deployment file from aadpodidentitybinding.yaml")
}
defer deployFile.Close()
// Go template parameters to be translated in test/e2e/template/aadpodidentitybinding.yaml
deployData := struct {
Name string
Selector string
}{
name,
selector,
}
if err := t.Execute(deployFile, deployData); err != nil {
return errors.Wrap(err, "Failed to create a deployment file from aadpodidentitybinding.yaml")
}
cmd := exec.Command("kubectl", "apply", "-f", deployFilePath)
util.PrintCommand(cmd)
_, err = cmd.CombinedOutput()
if err != nil {
return errors.Wrap(err, "Failed to deploy AzureIdentityBinding to the Kubernetes cluster")
}
return nil
}
// Delete will delete an Azure Identity Binding on a Kubernetes cluster
func Delete(name, templateOutputPath string) error {
cmd := exec.Command("kubectl", "delete", "-f", path.Join(templateOutputPath, name+"-binding.yaml"), "--ignore-not-found")
util.PrintCommand(cmd)
_, err := cmd.CombinedOutput()
if err != nil {
return errors.Wrap(err, "Failed to delete AzureIdentityBinding from the Kubernetes cluster")
}
return nil
}
// GetAll will return a list of AzureIdentityBinding deployed on a Kubernetes cluster
func GetAll() (*aadpodid.AzureIdentityBindingList, error) {
cmd := exec.Command("kubectl", "get", "AzureIdentityBinding", "-ojson")
util.PrintCommand(cmd)
out, err := cmd.CombinedOutput()
if err != nil {
return nil, errors.Wrap(err, "Failed to get AzureIdentityBinding from the Kubernetes cluster")
}
list := aadpodid.AzureIdentityBindingList{}
if err := json.Unmarshal(out, &list); err != nil {
return nil, errors.Wrap(err, "Failed to unmarshall json")
}
return &list, nil
}