mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-04 09:49:50 +00:00
add serviceaccount option to clusterrrolebinding
This commit is contained in:
parent
79f497bca7
commit
b18e433590
@ -19,6 +19,8 @@ package kubectl
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"strings"
|
||||||
|
|
||||||
"k8s.io/kubernetes/pkg/apis/rbac"
|
"k8s.io/kubernetes/pkg/apis/rbac"
|
||||||
"k8s.io/kubernetes/pkg/runtime"
|
"k8s.io/kubernetes/pkg/runtime"
|
||||||
)
|
)
|
||||||
@ -33,6 +35,8 @@ type ClusterRoleBindingGeneratorV1 struct {
|
|||||||
Users []string
|
Users []string
|
||||||
// Groups to derive the clusterRoleBinding from (optional)
|
// Groups to derive the clusterRoleBinding from (optional)
|
||||||
Groups []string
|
Groups []string
|
||||||
|
// ServiceAccounts to derive the clusterRoleBinding from in namespace:name format(optional)
|
||||||
|
ServiceAccounts []string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure it supports the generator pattern that uses parameter injection.
|
// Ensure it supports the generator pattern that uses parameter injection.
|
||||||
@ -66,6 +70,15 @@ func (s ClusterRoleBindingGeneratorV1) Generate(genericParams map[string]interfa
|
|||||||
delegate.Groups = fromLiteralArray
|
delegate.Groups = fromLiteralArray
|
||||||
delete(genericParams, "group")
|
delete(genericParams, "group")
|
||||||
}
|
}
|
||||||
|
fromSAStrings, found := genericParams["serviceaccount"]
|
||||||
|
if found {
|
||||||
|
fromLiteralArray, isArray := fromSAStrings.([]string)
|
||||||
|
if !isArray {
|
||||||
|
return nil, fmt.Errorf("expected []string, found :%v", fromFileStrings)
|
||||||
|
}
|
||||||
|
delegate.ServiceAccounts = fromLiteralArray
|
||||||
|
delete(genericParams, "serviceaccounts")
|
||||||
|
}
|
||||||
params := map[string]string{}
|
params := map[string]string{}
|
||||||
for key, value := range genericParams {
|
for key, value := range genericParams {
|
||||||
strVal, isString := value.(string)
|
strVal, isString := value.(string)
|
||||||
@ -86,6 +99,7 @@ func (s ClusterRoleBindingGeneratorV1) ParamNames() []GeneratorParam {
|
|||||||
{"clusterrole", false},
|
{"clusterrole", false},
|
||||||
{"user", false},
|
{"user", false},
|
||||||
{"group", false},
|
{"group", false},
|
||||||
|
{"serviceaccount", false},
|
||||||
{"force", false},
|
{"force", false},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -109,11 +123,15 @@ func (s ClusterRoleBindingGeneratorV1) StructuredGenerate() (runtime.Object, err
|
|||||||
Name: user,
|
Name: user,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
for _, group := range s.Groups {
|
for _, sa := range s.ServiceAccounts {
|
||||||
|
tokens := strings.Split(sa, ":")
|
||||||
|
if len(tokens) != 2 {
|
||||||
|
return nil, fmt.Errorf("serviceaccount must be <namespace>:<name>")
|
||||||
|
}
|
||||||
clusterRoleBinding.Subjects = append(clusterRoleBinding.Subjects, rbac.Subject{
|
clusterRoleBinding.Subjects = append(clusterRoleBinding.Subjects, rbac.Subject{
|
||||||
Kind: rbac.GroupKind,
|
Kind: rbac.ServiceAccountKind,
|
||||||
APIVersion: "rbac/v1alpha1",
|
Namespace: tokens[0],
|
||||||
Name: group,
|
Name: tokens[1],
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,6 +55,7 @@ func NewCmdCreateClusterRoleBinding(f cmdutil.Factory, cmdOut io.Writer) *cobra.
|
|||||||
cmd.Flags().String("clusterrole", "", "ClusterRole this ClusterRoleBinding should reference")
|
cmd.Flags().String("clusterrole", "", "ClusterRole this ClusterRoleBinding should reference")
|
||||||
cmd.Flags().StringSlice("user", []string{}, "usernames to bind to the role")
|
cmd.Flags().StringSlice("user", []string{}, "usernames to bind to the role")
|
||||||
cmd.Flags().StringSlice("group", []string{}, "groups to bind to the role")
|
cmd.Flags().StringSlice("group", []string{}, "groups to bind to the role")
|
||||||
|
cmd.Flags().StringSlice("serviceaccount", []string{}, "service accounts to bind to the role")
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,6 +73,7 @@ func CreateClusterRoleBinding(f cmdutil.Factory, cmdOut io.Writer, cmd *cobra.Co
|
|||||||
ClusterRole: cmdutil.GetFlagString(cmd, "clusterrole"),
|
ClusterRole: cmdutil.GetFlagString(cmd, "clusterrole"),
|
||||||
Users: cmdutil.GetFlagStringSlice(cmd, "user"),
|
Users: cmdutil.GetFlagStringSlice(cmd, "user"),
|
||||||
Groups: cmdutil.GetFlagStringSlice(cmd, "group"),
|
Groups: cmdutil.GetFlagStringSlice(cmd, "group"),
|
||||||
|
ServiceAccounts: cmdutil.GetFlagStringSlice(cmd, "serviceaccount"),
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return cmdutil.UsageError(cmd, fmt.Sprintf("Generator: %s not supported.", generatorName))
|
return cmdutil.UsageError(cmd, fmt.Sprintf("Generator: %s not supported.", generatorName))
|
||||||
|
Loading…
Reference in New Issue
Block a user