Skip to content

Commit

Permalink
Set generator version in annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
liggitt committed Sep 12, 2017
1 parent 13474f8 commit c4a739a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
15 changes: 11 additions & 4 deletions cmd/audit2rbac/audit2rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ func checkErr(w io.Writer, err error) {

func NewAudit2RBACCommand(stdout, stderr io.Writer) *cobra.Command {
options := &Audit2RBACOptions{
GeneratedPath: ".",
GeneratedNamePrefix: "",
GeneratedLabels: map[string]string{},
GeneratedPath: ".",
GeneratedNamePrefix: "",
GeneratedLabels: map[string]string{},
GeneratedAnnotations: map[string]string{},

Stdout: stdout,
Stderr: stderr,
Expand Down Expand Up @@ -100,8 +101,10 @@ type Audit2RBACOptions struct {
GeneratedPath string
// Prefix for generated object names. Defaults to "audit2rbac:<user>"
GeneratedNamePrefix string
// Labels to apply to generated object names. Defaults to audit2rbac.liggitt.net/generated=true
// Labels to apply to generated object names.
GeneratedLabels map[string]string
// Annotations to apply to generated object names.
GeneratedAnnotations map[string]string

Stdout io.Writer
Stderr io.Writer
Expand All @@ -123,6 +126,9 @@ func (a *Audit2RBACOptions) Complete(serviceAccount string, args []string) error
a.GeneratedLabels["audit2rbac.liggitt.net/user"] = sanitizeLabel(a.User)
a.GeneratedLabels["audit2rbac.liggitt.net/generated"] = "true"
}
if len(a.GeneratedAnnotations) == 0 {
a.GeneratedAnnotations["audit2rbac.liggitt.net/version"] = pkg.Version
}

if len(a.GeneratedNamePrefix) == 0 {
user := a.User
Expand Down Expand Up @@ -198,6 +204,7 @@ func (a *Audit2RBACOptions) Run() error {

opts := pkg.DefaultGenerateOptions()
opts.Labels = a.GeneratedLabels
opts.Annotations = a.GeneratedAnnotations
opts.NamePrefix = a.GeneratedNamePrefix

generated := pkg.NewGenerator(getDiscoveryRoles(), attributes, opts).Generate()
Expand Down
18 changes: 10 additions & 8 deletions pkg/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ type GenerateOptions struct {
ExpandMultipleNamesToUnnamed bool
ExpandMultipleNamespacesToClusterScoped bool

NamePrefix string
Labels map[string]string
NamePrefix string
Labels map[string]string
Annotations map[string]string
}

// DefaultGenerateOptions returns default generation options
Expand All @@ -40,8 +41,9 @@ func DefaultGenerateOptions() GenerateOptions {
ExpandMultipleNamesToUnnamed: true,
ExpandMultipleNamespacesToClusterScoped: true,

NamePrefix: "audit2rbac",
Labels: map[string]string{},
NamePrefix: "audit2rbac",
Labels: map[string]string{},
Annotations: map[string]string{},
}
}

Expand Down Expand Up @@ -152,10 +154,10 @@ func (g *Generator) ensureClusterRoleAndBinding(subject rbac.Subject) *rbac.Clus
}

g.clusterRole = &rbac.ClusterRole{
ObjectMeta: metav1.ObjectMeta{Name: g.Options.NamePrefix, Labels: g.Options.Labels},
ObjectMeta: metav1.ObjectMeta{Name: g.Options.NamePrefix, Labels: g.Options.Labels, Annotations: g.Options.Annotations},
}
g.clusterRoleBinding = &rbac.ClusterRoleBinding{
ObjectMeta: metav1.ObjectMeta{Name: g.Options.NamePrefix, Labels: g.Options.Labels},
ObjectMeta: metav1.ObjectMeta{Name: g.Options.NamePrefix, Labels: g.Options.Labels, Annotations: g.Options.Annotations},
RoleRef: rbac.RoleRef{APIGroup: rbac.GroupName, Kind: "ClusterRole", Name: g.clusterRole.Name},
Subjects: []rbac.Subject{subject},
}
Expand All @@ -175,10 +177,10 @@ func (g *Generator) ensureNamespacedRoleAndBinding(subject rbac.Subject, namespa
}

g.namespacedRole[namespace] = &rbac.Role{
ObjectMeta: metav1.ObjectMeta{Name: g.Options.NamePrefix, Namespace: namespace, Labels: g.Options.Labels},
ObjectMeta: metav1.ObjectMeta{Name: g.Options.NamePrefix, Namespace: namespace, Labels: g.Options.Labels, Annotations: g.Options.Annotations},
}
g.namespacedRoleBinding[namespace] = &rbac.RoleBinding{
ObjectMeta: metav1.ObjectMeta{Name: g.Options.NamePrefix, Namespace: namespace, Labels: g.Options.Labels},
ObjectMeta: metav1.ObjectMeta{Name: g.Options.NamePrefix, Namespace: namespace, Labels: g.Options.Labels, Annotations: g.Options.Annotations},
RoleRef: rbac.RoleRef{APIGroup: rbac.GroupName, Kind: "Role", Name: g.namespacedRole[namespace].Name},
Subjects: []rbac.Subject{subject},
}
Expand Down

0 comments on commit c4a739a

Please sign in to comment.