Skip to content

Commit 0736a4b

Browse files
Conditionally allow view secrets on ClusterRole (#96)
Issue #, if available: aws-controllers-k8s/community#745 Description of changes: If any of the fields defined in the generator are set to `is_secret: True`, then add `get;list;watch` permissions for secrets on the controller `ClusterRole` Output into `cluster-role-controller.yaml`: ```yaml - apiGroups: - "" resources: - secrets verbs: - get - list - watch ``` By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 32c9216 commit 0736a4b

File tree

5 files changed

+37
-2
lines changed

5 files changed

+37
-2
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ build-ack-generate: ## Build ack-generate binary
2727
@go build ${GO_TAGS} ${GO_LDFLAGS} -o bin/ack-generate cmd/ack-generate/main.go
2828
@echo "ok."
2929

30-
build-controller: ## Generate controller code for SERVICE
30+
build-controller: build-ack-generate ## Generate controller code for SERVICE
3131
@./scripts/install-controller-gen.sh
3232
@./scripts/build-controller.sh $(AWS_SERVICE)
3333

pkg/generate/ack/controller.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020

2121
"github.com/aws-controllers-k8s/code-generator/pkg/generate"
2222
"github.com/aws-controllers-k8s/code-generator/pkg/generate/code"
23+
ackgenconfig "github.com/aws-controllers-k8s/code-generator/pkg/generate/config"
2324
"github.com/aws-controllers-k8s/code-generator/pkg/generate/templateset"
2425
ackmodel "github.com/aws-controllers-k8s/code-generator/pkg/model"
2526
)
@@ -165,7 +166,12 @@ func Controller(
165166
}
166167
}
167168
}
168-
if err = ts.Add("pkg/resource/registry.go", "pkg/resource/registry.go.tpl", metaVars); err != nil {
169+
170+
configVars := &templateConfigVars{
171+
metaVars,
172+
g.GetConfig(),
173+
}
174+
if err = ts.Add("pkg/resource/registry.go", "pkg/resource/registry.go.tpl", configVars); err != nil {
169175
return nil, err
170176
}
171177

@@ -203,3 +209,10 @@ type templateCmdVars struct {
203209
templateset.MetaVars
204210
SnakeCasedCRDNames []string
205211
}
212+
213+
// templateConfigVars contains template variables for the templates that require
214+
// access to the generator configuration definition
215+
type templateConfigVars struct {
216+
templateset.MetaVars
217+
GeneratorConfig *ackgenconfig.Config
218+
}

pkg/generate/config/config.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,19 @@ type PrefixConfig struct {
6868
StatusField string `json:"status_field,omitempty"`
6969
}
7070

71+
// ResourceContainsSecret returns true if any of the fields in any resource are
72+
// defined as secrets.
73+
func (c *Config) ResourceContainsSecret() bool {
74+
for _, resource := range c.Resources {
75+
for _, field := range resource.Fields {
76+
if field.IsSecret {
77+
return true
78+
}
79+
}
80+
}
81+
return false
82+
}
83+
7184
// New returns a new Config object given a supplied
7285
// path to a config file
7386
func New(

pkg/generate/generator.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,12 @@ func (g *Generator) ApplyShapeIgnoreRules() {
682682
}
683683
}
684684

685+
// GetConfig returns the configuration option used to define the current
686+
// generator.
687+
func (g *Generator) GetConfig() *ackgenconfig.Config {
688+
return g.cfg
689+
}
690+
685691
// New returns a new Generator struct for a supplied API model.
686692
// Optionally, pass a file path to a generator config file that can be used to
687693
// instruct the code generator how to handle the API properly

templates/pkg/resource/registry.go.tpl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import (
1111
// +kubebuilder:rbac:groups=services.k8s.aws,resources=adoptedresources/status,verbs=get;update;patch
1212
// +kubebuilder:rbac:groups="",resources=namespaces,verbs=get;list;watch
1313
// +kubebuilder:rbac:groups="",resources=configmaps,verbs=get;list;watch
14+
{{ if .GeneratorConfig.ResourceContainsSecret -}}
15+
// +kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch
16+
{{- end }}
1417

1518
var (
1619
reg = ackrt.NewRegistry()

0 commit comments

Comments
 (0)