-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pipeline: add renderRBAC func #494
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
apiVersion: v1 | ||
kind: ServiceAccount | ||
metadata: | ||
name: "{{ .ServiceAccountName }}" | ||
namespace: "{{ .PipelineNamespace }}" | ||
--- | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: RoleBinding | ||
metadata: | ||
name: "{{ .RoleBindingName }}" | ||
namespace: "{{ .PipelineNamespace }}" | ||
subjects: | ||
- kind: ServiceAccount | ||
name: "{{ .ServiceAccountName }}" | ||
namespace: "{{ .PipelineNamespace }}" | ||
roleRef: | ||
apiGroup: rbac.authorization.k8s.io | ||
kind: ClusterRole | ||
name: tekton-triggers-eventlistener-roles # add role for handle EventListener, configmap, secret and so on. `tekton-triggers-eventlistener-roles` is provided by Tekton | ||
--- | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: ClusterRoleBinding | ||
metadata: | ||
name: "{{ .ClusterRoleBindingName }}" | ||
subjects: | ||
- kind: ServiceAccount | ||
name: "{{ .ServiceAccountName }}" | ||
namespace: "{{ .PipelineNamespace }}" | ||
roleRef: | ||
apiGroup: rbac.authorization.k8s.io | ||
kind: ClusterRole | ||
name: tekton-triggers-eventlistener-clusterroles # add role for handle pod. `tekton-triggers-eventlistener-clusterroles` is provided by Tekton |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
Copyright Kurator Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package render | ||
|
||
import ( | ||
"fmt" | ||
"io/fs" | ||
) | ||
|
||
const ( | ||
// RBACTemplateFileName is the name of the RBAC template file. | ||
RBACTemplateFileName = "rbac.tpl" | ||
RBACTemplateName = "pipeline rbac template" | ||
) | ||
|
||
// RBACConfig contains the configuration data required for the RBAC template. | ||
// Both PipelineName and PipelineNamespace are required. | ||
type RBACConfig struct { | ||
PipelineName string // Name of the pipeline. | ||
PipelineNamespace string // Kubernetes namespace where the pipeline is deployed. | ||
} | ||
|
||
// ServiceAccountName generates the service account name using the pipeline name and namespace. | ||
func (rbac RBACConfig) ServiceAccountName() string { | ||
return rbac.PipelineName | ||
} | ||
|
||
// RoleBindingName generates the role binding name using the service account name. | ||
func (rbac RBACConfig) RoleBindingName() string { | ||
return rbac.ServiceAccountName() | ||
} | ||
|
||
// ClusterRoleBindingName generates the cluster role binding name using the service account name. | ||
func (rbac RBACConfig) ClusterRoleBindingName() string { | ||
return rbac.ServiceAccountName() | ||
} | ||
|
||
// renderRBAC renders the RBAC configuration using a specified template. | ||
func renderRBAC(fsys fs.FS, cfg RBACConfig) ([]byte, error) { | ||
if cfg.PipelineName == "" || cfg.PipelineNamespace == "" { | ||
return nil, fmt.Errorf("invalid RBACConfig: PipelineName and PipelineNamespace must not be empty") | ||
} | ||
return renderPipelineTemplate(fsys, RBACTemplateFileName, RBACTemplateName, cfg) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
Copyright Kurator Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package render | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"kurator.dev/kurator/pkg/fleet-manager/manifests" | ||
) | ||
|
||
var manifestFS = manifests.BuiltinOrDir("manifests") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure why we need to read the while manifest, it is costy There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
|
||
const expectedRBACFilePath = "testdata/rbac/" | ||
|
||
func TestRenderRBAC(t *testing.T) { | ||
// Define test cases including both valid and error scenarios. | ||
cases := []struct { | ||
name string | ||
cfg RBACConfig | ||
expectError bool | ||
expectedFile string | ||
}{ | ||
{ | ||
name: "valid configuration", | ||
cfg: RBACConfig{ | ||
PipelineName: "example", | ||
PipelineNamespace: "default", | ||
}, | ||
expectError: false, | ||
expectedFile: "default-example.yaml", | ||
}, | ||
{ | ||
name: "empty PipelineName", | ||
cfg: RBACConfig{ | ||
PipelineName: "", | ||
PipelineNamespace: "default", | ||
}, | ||
expectError: true, | ||
}, | ||
{ | ||
name: "empty PipelineNamespace", | ||
cfg: RBACConfig{ | ||
PipelineName: "example", | ||
PipelineNamespace: "", | ||
}, | ||
expectError: true, | ||
}, | ||
{ | ||
name: "invalid file system path", | ||
cfg: RBACConfig{ | ||
PipelineName: "example", | ||
PipelineNamespace: "default", | ||
}, | ||
expectError: true, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
fs := manifestFS | ||
// Use an invalid file system for the relevant test case. | ||
if tc.name == "invalid file system path" { | ||
fs = manifests.BuiltinOrDir("invalid-path") | ||
} | ||
|
||
result, err := renderRBAC(fs, tc.cfg) | ||
|
||
if tc.expectError { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
|
||
expected, err := os.ReadFile(expectedRBACFilePath + tc.expectedFile) | ||
assert.NoError(t, err) | ||
assert.Equal(t, string(expected), string(result)) | ||
} | ||
}) | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
pkg/fleet-manager/pipeline/render/testdata/rbac/default-example.yaml
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
apiVersion: v1 | ||
kind: ServiceAccount | ||
metadata: | ||
name: "example" | ||
namespace: "default" | ||
--- | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: RoleBinding | ||
metadata: | ||
name: "example" | ||
namespace: "default" | ||
subjects: | ||
- kind: ServiceAccount | ||
name: "example" | ||
namespace: "default" | ||
roleRef: | ||
apiGroup: rbac.authorization.k8s.io | ||
kind: ClusterRole | ||
name: tekton-triggers-eventlistener-roles # add role for handle EventListener, configmap, secret and so on. `tekton-triggers-eventlistener-roles` is provided by Tekton | ||
--- | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: ClusterRoleBinding | ||
metadata: | ||
name: "example" | ||
subjects: | ||
- kind: ServiceAccount | ||
name: "example" | ||
namespace: "default" | ||
roleRef: | ||
apiGroup: rbac.authorization.k8s.io | ||
kind: ClusterRole | ||
name: tekton-triggers-eventlistener-clusterroles # add role for handle pod. `tekton-triggers-eventlistener-clusterroles` is provided by Tekton |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
Copyright Kurator Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package render | ||
|
||
import ( | ||
"bytes" | ||
"html/template" | ||
"io/fs" | ||
|
||
"github.com/Masterminds/sprig/v3" | ||
"sigs.k8s.io/yaml" | ||
) | ||
|
||
// renderPipelineTemplate reads, parses, and renders a template file using the provided configuration data. | ||
func renderPipelineTemplate(fsys fs.FS, tplFileName, tplName string, cfg interface{}) ([]byte, error) { | ||
out, err := fs.ReadFile(fsys, tplFileName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
t := template.New(tplName) | ||
|
||
tpl, err := t.Funcs(funMap()).Parse(string(out)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var b bytes.Buffer | ||
|
||
if err := tpl.Execute(&b, cfg); err != nil { | ||
return nil, err | ||
} | ||
|
||
return b.Bytes(), nil | ||
} | ||
|
||
// funMap returns a map of functions for use in the template. | ||
func funMap() template.FuncMap { | ||
m := sprig.TxtFuncMap() | ||
m["toYaml"] = toYaml | ||
return m | ||
} | ||
|
||
// toYaml converts a given value to its YAML representation. | ||
func toYaml(value interface{}) string { | ||
y, err := yaml.Marshal(value) | ||
if err != nil { | ||
return "" | ||
} | ||
|
||
return string(y) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry just note this is cluster scope resource, if we create multiple clusterrolebinding, they conflict with same name.
Option one is to make it with different name, alternatively add different sa within the same clusterrolebinding.