Skip to content

Commit b63af1f

Browse files
author
Christoph Bühler
committed
fix(rbac): generate role binding (cluster wide) for operator
This fixes #15
1 parent 0b716fa commit b63af1f

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

src/KubeOps/Operator/Commands/Generators/RbacGenerator.cs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,36 @@ public RbacGenerator(EntitySerializer serializer)
2525

2626
public async Task<int> OnExecuteAsync(CommandLineApplication app)
2727
{
28-
var output = _serializer.Serialize(GenerateManagerRbac(), Format);
28+
var role = _serializer.Serialize(GenerateManagerRbac(), Format);
29+
var roleBinding = _serializer.Serialize(new V1ClusterRoleBinding
30+
{
31+
ApiVersion = $"{V1ClusterRoleBinding.KubeGroup}/{V1ClusterRoleBinding.KubeApiVersion}",
32+
Kind = V1ClusterRoleBinding.KubeKind,
33+
Metadata = new V1ObjectMeta {Name = "operator-role-binding"},
34+
RoleRef = new V1RoleRef(V1ClusterRole.KubeGroup, V1ClusterRole.KubeKind, "operator-role"),
35+
Subjects = new List<V1Subject>
36+
{
37+
new V1Subject(V1ServiceAccount.KubeKind, "default", namespaceProperty: "system")
38+
}
39+
}, Format);
2940

3041
if (!string.IsNullOrWhiteSpace(OutputPath))
3142
{
3243
Directory.CreateDirectory(OutputPath);
33-
await using var file = File.Open(Path.Join(OutputPath,
34-
$"operator.{Format.ToString().ToLower()}"), FileMode.Create);
35-
await file.WriteAsync(Encoding.UTF8.GetBytes(output));
44+
await using var roleFile = File.Open(Path.Join(OutputPath,
45+
$"operator-role.{Format.ToString().ToLower()}"), FileMode.Create);
46+
await roleFile.WriteAsync(Encoding.UTF8.GetBytes(role));
47+
await using var bindingFile = File.Open(Path.Join(OutputPath,
48+
$"operator-role-binding.{Format.ToString().ToLower()}"), FileMode.Create);
49+
await bindingFile.WriteAsync(Encoding.UTF8.GetBytes(roleBinding));
3650

3751
var kustomize = new KustomizationConfig
3852
{
39-
Resources = new List<string> {$"operator.{Format.ToString().ToLower()}"},
53+
Resources = new List<string>
54+
{
55+
$"operator-role.{Format.ToString().ToLower()}",
56+
$"operator-role-binding.{Format.ToString().ToLower()}",
57+
},
4058
CommonLabels = new Dictionary<string, string>
4159
{
4260
{"operator-element", "rbac"},
@@ -49,7 +67,8 @@ public async Task<int> OnExecuteAsync(CommandLineApplication app)
4967
}
5068
else
5169
{
52-
await app.Out.WriteLineAsync(output);
70+
await app.Out.WriteLineAsync(role);
71+
await app.Out.WriteLineAsync(roleBinding);
5372
}
5473

5574
return ExitCodes.Success;

0 commit comments

Comments
 (0)