|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1" |
| 7 | + corev1 "k8s.io/api/core/v1" |
| 8 | + rbacv1 "k8s.io/api/rbac/v1" |
| 9 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 10 | + "k8s.io/client-go/kubernetes" |
| 11 | +) |
| 12 | + |
| 13 | +func createServiceAccount(preflight troubleshootv1beta1.Preflight, namespace string, clientset *kubernetes.Clientset) (string, error) { |
| 14 | + name := fmt.Sprintf("preflight-%s", preflight.Name) |
| 15 | + |
| 16 | + serviceAccount := corev1.ServiceAccount{ |
| 17 | + TypeMeta: metav1.TypeMeta{ |
| 18 | + APIVersion: "v1", |
| 19 | + Kind: "ServiceAccount", |
| 20 | + }, |
| 21 | + ObjectMeta: metav1.ObjectMeta{ |
| 22 | + Name: name, |
| 23 | + Namespace: namespace, |
| 24 | + }, |
| 25 | + Secrets: []corev1.ObjectReference{ |
| 26 | + { |
| 27 | + APIVersion: "v1", |
| 28 | + Kind: "Secret", |
| 29 | + Name: name, |
| 30 | + Namespace: namespace, |
| 31 | + }, |
| 32 | + }, |
| 33 | + } |
| 34 | + _, err := clientset.CoreV1().ServiceAccounts(namespace).Create(&serviceAccount) |
| 35 | + if err != nil { |
| 36 | + return "", err |
| 37 | + } |
| 38 | + |
| 39 | + role := rbacv1.ClusterRole{ |
| 40 | + TypeMeta: metav1.TypeMeta{ |
| 41 | + APIVersion: "v1", |
| 42 | + Kind: "ClusterRole", |
| 43 | + }, |
| 44 | + ObjectMeta: metav1.ObjectMeta{ |
| 45 | + Name: name, |
| 46 | + Namespace: namespace, |
| 47 | + }, |
| 48 | + Rules: []rbacv1.PolicyRule{ |
| 49 | + { |
| 50 | + APIGroups: []string{""}, |
| 51 | + Resources: []string{ |
| 52 | + "namespaces", |
| 53 | + "pods", |
| 54 | + "services", |
| 55 | + "secrets", |
| 56 | + }, |
| 57 | + Verbs: metav1.Verbs{"list"}, |
| 58 | + }, |
| 59 | + { |
| 60 | + APIGroups: []string{"apps"}, |
| 61 | + Resources: []string{"deployments"}, |
| 62 | + Verbs: metav1.Verbs{"list"}, |
| 63 | + }, |
| 64 | + { |
| 65 | + APIGroups: []string{"extensions"}, |
| 66 | + Resources: []string{"ingresses"}, |
| 67 | + Verbs: metav1.Verbs{"list"}, |
| 68 | + }, |
| 69 | + { |
| 70 | + APIGroups: []string{"storage.k8s.io"}, |
| 71 | + Resources: []string{"storageclasses"}, |
| 72 | + Verbs: metav1.Verbs{"list"}, |
| 73 | + }, |
| 74 | + { |
| 75 | + APIGroups: []string{"apiextensions.k8s.io"}, |
| 76 | + Resources: []string{"customresourcedefinitions"}, |
| 77 | + Verbs: metav1.Verbs{"list"}, |
| 78 | + }, |
| 79 | + }, |
| 80 | + } |
| 81 | + _, err = clientset.RbacV1().ClusterRoles().Create(&role) |
| 82 | + if err != nil { |
| 83 | + return "", err |
| 84 | + } |
| 85 | + |
| 86 | + roleBinding := rbacv1.ClusterRoleBinding{ |
| 87 | + TypeMeta: metav1.TypeMeta{ |
| 88 | + APIVersion: "v1", |
| 89 | + Kind: "ClusterRoleBinding", |
| 90 | + }, |
| 91 | + ObjectMeta: metav1.ObjectMeta{ |
| 92 | + Name: name, |
| 93 | + Namespace: namespace, |
| 94 | + }, |
| 95 | + Subjects: []rbacv1.Subject{ |
| 96 | + { |
| 97 | + Kind: "ServiceAccount", |
| 98 | + Name: name, |
| 99 | + Namespace: namespace, |
| 100 | + }, |
| 101 | + }, |
| 102 | + RoleRef: rbacv1.RoleRef{ |
| 103 | + APIGroup: "rbac.authorization.k8s.io", |
| 104 | + Kind: "ClusterRole", |
| 105 | + Name: name, |
| 106 | + }, |
| 107 | + } |
| 108 | + _, err = clientset.RbacV1().ClusterRoleBindings().Create(&roleBinding) |
| 109 | + if err != nil { |
| 110 | + return "", err |
| 111 | + } |
| 112 | + |
| 113 | + return name, nil |
| 114 | +} |
| 115 | + |
| 116 | +func removeServiceAccount(name string, namespace string, clientset *kubernetes.Clientset) error { |
| 117 | + if err := clientset.RbacV1().ClusterRoleBindings().Delete(name, &metav1.DeleteOptions{}); err != nil { |
| 118 | + return err |
| 119 | + } |
| 120 | + |
| 121 | + if err := clientset.RbacV1().ClusterRoles().Delete(name, &metav1.DeleteOptions{}); err != nil { |
| 122 | + return err |
| 123 | + } |
| 124 | + |
| 125 | + if err := clientset.CoreV1().ServiceAccounts(namespace).Delete(name, &metav1.DeleteOptions{}); err != nil { |
| 126 | + return err |
| 127 | + } |
| 128 | + |
| 129 | + |
| 130 | + return nil |
| 131 | +} |
0 commit comments