Created
September 14, 2018 02:33
-
-
Save dulltz/c18f8cb03f08c4b81515a768f500d8b7 to your computer and use it in GitHub Desktop.
Create RBAC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"fmt" | |
"path/filepath" | |
"k8s.io/api/rbac/v1" | |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | |
"k8s.io/client-go/kubernetes" | |
"k8s.io/client-go/tools/clientcmd" | |
"k8s.io/client-go/util/homedir" | |
) | |
func main() { | |
var kubeconfig *string | |
if home := homedir.HomeDir(); home != "" { | |
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file") | |
} else { | |
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file") | |
} | |
flag.Parse() | |
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig) | |
if err != nil { | |
panic(err) | |
} | |
clientset, err := kubernetes.NewForConfig(config) | |
if err != nil { | |
panic(err) | |
} | |
cr, err := clientset.RbacV1().ClusterRoles().Create(&v1.ClusterRole{ | |
ObjectMeta: metav1.ObjectMeta{ | |
Name: "hoge", | |
Labels: map[string]string{"kubernetes.io/bootstrapping": "rbac-defaults"}, | |
}, | |
Rules: []v1.PolicyRule{ | |
{ | |
APIGroups: []string{""}, | |
Resources: []string{"nodes"}, | |
Verbs: []string{"get", "list", "watch"}, | |
}, | |
{ | |
APIGroups: []string{""}, | |
Resources: []string{"pods"}, | |
Verbs: []string{"delete", "get", "list", "watch"}, | |
}, | |
}, | |
}) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println(cr) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment