|
| 1 | +/* |
| 2 | +Copyright 2018 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package v2 |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "path/filepath" |
| 22 | + "strings" |
| 23 | + |
| 24 | + "sigs.k8s.io/kubebuilder/pkg/scaffold/input" |
| 25 | + "sigs.k8s.io/kubebuilder/pkg/scaffold/resource" |
| 26 | +) |
| 27 | + |
| 28 | +var _ input.File = &CRDViewerRole{} |
| 29 | + |
| 30 | +// CRD Viewer role scaffolds the config/rbca/<kind>_viewer_role.yaml |
| 31 | +type CRDViewerRole struct { |
| 32 | + input.Input |
| 33 | + |
| 34 | + // Resource is a resource in the API group |
| 35 | + Resource *resource.Resource |
| 36 | +} |
| 37 | + |
| 38 | +// GetInput implements input.File |
| 39 | +func (g *CRDViewerRole) GetInput() (input.Input, error) { |
| 40 | + if g.Path == "" { |
| 41 | + g.Path = filepath.Join("config", "rbac", fmt.Sprintf("%s_viewer_role.yaml", strings.ToLower(g.Resource.Kind))) |
| 42 | + } |
| 43 | + |
| 44 | + g.TemplateBody = crdRoleViewerTemplate |
| 45 | + return g.Input, nil |
| 46 | +} |
| 47 | + |
| 48 | +// Validate validates the values |
| 49 | +func (g *CRDViewerRole) Validate() error { |
| 50 | + return g.Resource.Validate() |
| 51 | +} |
| 52 | + |
| 53 | +const crdRoleViewerTemplate = `# permissions to do viewer {{ .Resource.Resource }}. |
| 54 | +apiVersion: rbac.authorization.k8s.io/v1 |
| 55 | +kind: ClusterRole |
| 56 | +metadata: |
| 57 | + name: {{ lower .Resource.Kind }}-viewer-role |
| 58 | +rules: |
| 59 | +- apiGroups: |
| 60 | + - {{ .Resource.Group }}.{{ .Domain }} |
| 61 | + resources: |
| 62 | + - {{ .Resource.Resource }} |
| 63 | + verbs: |
| 64 | + - get |
| 65 | + - list |
| 66 | + - watch |
| 67 | +- apiGroups: |
| 68 | + - {{ .Resource.Group }}.{{ .Domain }} |
| 69 | + resources: |
| 70 | + - {{ .Resource.Resource }}/status |
| 71 | + verbs: |
| 72 | + - get |
| 73 | +` |
0 commit comments