/
validate.go
37 lines (31 loc) · 1.17 KB
/
validate.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
package kubectl
import (
"context"
"github.com/gruntwork-io/go-commons/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// ValidateNamespaceExists will return an error if the provided namespace does not exist on the Kubernetes cluster.
func ValidateNamespaceExists(kubectlOptions *KubectlOptions, namespace string) error {
client, err := GetKubernetesClientFromOptions(kubectlOptions)
if err != nil {
return errors.WithStackTrace(err)
}
_, err = client.CoreV1().Namespaces().Get(context.Background(), namespace, metav1.GetOptions{})
if err != nil {
return errors.WithStackTrace(err)
}
return nil
}
// ValidateServiceAccountExists will return an error if the provided service account does not exist on the provided
// namespace in the Kubernetes cluster.
func ValidateServiceAccountExists(kubectlOptions *KubectlOptions, namespace string, serviceAccount string) error {
client, err := GetKubernetesClientFromOptions(kubectlOptions)
if err != nil {
return errors.WithStackTrace(err)
}
_, err = client.CoreV1().ServiceAccounts(namespace).Get(context.Background(), serviceAccount, metav1.GetOptions{})
if err != nil {
return errors.WithStackTrace(err)
}
return nil
}