Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions commands/operator-sdk/cmd/cmdutil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ import (
)

const configYaml = "./config/config.yaml"
const tmpDockerfile = "./tmp/build/Dockerfile"

// MustInProjectRoot checks if the current dir is the project root.
func MustInProjectRoot() {
// if the current directory has the "./config/config.yaml" file, then it is safe to say
// if the current directory has the "./tmp/build/Dockerfile" file, then it is safe to say
// we are at the project root.
_, err := os.Stat(configYaml)
_, err := os.Stat(tmpDockerfile)
if err != nil && os.IsNotExist(err) {
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("must in project root dir: %v", err))
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,9 @@ func RenderDeployCrdFiles(deployPath, apiVersion, kind string) error {

func renderDeployFiles(deployDir, projectName, apiVersion, kind, operatorType string) error {
rbacTd := tmplData{
ProjectName: projectName,
GroupName: groupName(apiVersion),
ProjectName: projectName,
GroupName: groupName(apiVersion),
IsGoOperator: isGoOperator(operatorType),
}
if err := renderWriteFile(filepath.Join(deployDir, rbacYaml), rbacTmplName, rbacYamlTmpl, rbacTd); err != nil {
return err
Expand Down
63 changes: 61 additions & 2 deletions pkg/generator/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,55 @@ roleRef:
apiGroup: rbac.authorization.k8s.io
`

const rbacYamlAnsibleExp = `kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: app-operator
rules:
- apiGroups:
- app.example.com
resources:
- "*"
verbs:
- "*"
- apiGroups:
- ""
resources:
- pods
- services
- endpoints
- persistentvolumeclaims
- events
- configmaps
- secrets
verbs:
- "*"
- apiGroups:
- apps
resources:
- deployments
- daemonsets
- replicasets
- statefulsets
verbs:
- "*"

---

kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: app-operator
subjects:
- kind: ServiceAccount
name: default
namespace: default
roleRef:
kind: ClusterRole
name: app-operator
apiGroup: rbac.authorization.k8s.io
`

const saYamlExp = `apiVersion: v1
kind: ServiceAccount
metadata:
Expand Down Expand Up @@ -338,7 +387,7 @@ func TestGenDeploy(t *testing.T) {
t.Errorf("\nTest failed. Below is the diff of the expected vs actual results.\nRed text is missing and green text is extra.\n\n" + dmp.DiffPrettyText(diffs))
}

// Test Ansible Operator
// Test Ansible Operator operator.yaml
buf = &bytes.Buffer{}
td = tmplData{
ProjectName: appProjectName,
Expand All @@ -358,7 +407,7 @@ func TestGenDeploy(t *testing.T) {
}

buf = &bytes.Buffer{}
if err := renderFile(buf, rbacTmplName, rbacYamlTmpl, tmplData{ProjectName: appProjectName, GroupName: appGroupName}); err != nil {
if err := renderFile(buf, rbacTmplName, rbacYamlTmpl, tmplData{ProjectName: appProjectName, GroupName: appGroupName, IsGoOperator: true}); err != nil {
t.Error(err)
}
if rbacYamlExp != buf.String() {
Expand All @@ -367,6 +416,16 @@ func TestGenDeploy(t *testing.T) {
t.Errorf("\nTest failed. Below is the diff of the expected vs actual results.\nRed text is missing and green text is extra.\n\n" + dmp.DiffPrettyText(diffs))
}

// Test Ansible Operator rbac.yaml
buf = &bytes.Buffer{}
if err := renderFile(buf, rbacTmplName, rbacYamlTmpl, tmplData{ProjectName: appProjectName, GroupName: appGroupName, IsGoOperator: false}); err != nil {
t.Error(err)
}
if rbacYamlAnsibleExp != buf.String() {
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(rbacYamlAnsibleExp, buf.String(), false)
t.Errorf("\nTest failed. Below is the diff of the expected vs actual results.\nRed text is missing and green text is extra.\n\n" + dmp.DiffPrettyText(diffs))
}
buf = &bytes.Buffer{}
if err := renderFile(buf, saTmplName, saYamlTmpl, tmplData{ProjectName: appProjectName}); err != nil {
t.Error(err)
Expand Down
17 changes: 15 additions & 2 deletions pkg/generator/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,11 @@ spec:
value: "{{.ProjectName}}"
`

const rbacYamlTmpl = `kind: Role
// For Ansible Operator we are assuming namespace: default on ClusterRoleBinding
// Documentation will tell user to update
const rbacYamlTmpl = `{{- if .IsGoOperator }}kind: Role
{{- else -}}
kind: ClusterRole{{ end }}
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: {{.ProjectName}}
Expand Down Expand Up @@ -510,16 +514,25 @@ rules:
- "*"

---

{{ if .IsGoOperator }}
kind: RoleBinding
{{- else }}
kind: ClusterRoleBinding{{ end }}
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: {{.ProjectName}}
subjects:
- kind: ServiceAccount
{{- if .IsGoOperator }}
name: {{.ProjectName}}
{{- else }}
name: default
namespace: default{{ end }}
roleRef:
{{- if .IsGoOperator }}
kind: Role
{{- else }}
kind: ClusterRole{{ end }}
name: {{.ProjectName}}
apiGroup: rbac.authorization.k8s.io
`
Expand Down