Skip to content
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

Create RBAC roles during installation #76

Merged
merged 21 commits into from
Jul 7, 2017
Merged
Show file tree
Hide file tree
Changes from 16 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
6 changes: 3 additions & 3 deletions docs/examples/elastic/elastic-with-scheduled-backup.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ spec:
replicas: 1
backupSchedule:
cronExpression: "@every 6h"
bucketName: "bucket-for-snapshot"
storageSecret:
secretName: "secret-for-bucket"
storageSecretName: "secret-for-bucket"
gcs:
bucket: "bucket-for-snapshot"
12 changes: 12 additions & 0 deletions docs/examples/elastic/snapshot-local.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: kubedb.com/v1alpha1
kind: Snapshot
metadata:
name: snapshot-xyz
labels:
kubedb.com/kind: Elastic
spec:
databaseName: elasticsearch-db
local:
path: "/test"
volumeSource:
emptyDir: {}
6 changes: 3 additions & 3 deletions docs/examples/elastic/snapshot.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ metadata:
kubedb.com/kind: Elastic
spec:
databaseName: elasticsearch-db
bucketName: "bucket-for-snapshot"
storageSecret:
secretName: "secret-for-bucket"
storageSecretName: "secret-for-bucket"
gcs:
bucket: "bucket-for-snapshot"
6 changes: 3 additions & 3 deletions docs/examples/postgres/postgres-with-scheduled-backup.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ spec:
version: 9.5
backupSchedule:
cronExpression: "@every 6h"
bucketName: "bucket-for-snapshot"
storageSecret:
secretName: "secret-for-bucket"
storageSecretName: "secret-for-bucket"
gcs:
bucket: "bucket-for-snapshot"
12 changes: 12 additions & 0 deletions docs/examples/postgres/snapshot-local.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: kubedb.com/v1alpha1
kind: Snapshot
metadata:
name: snapshot-xyz
labels:
kubedb.com/kind: Postgres
spec:
databaseName: postgres-db
local:
path: "/test"
volumeSource:
emptyDir: {}
6 changes: 3 additions & 3 deletions docs/examples/postgres/snapshot.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ metadata:
kubedb.com/kind: Postgres
spec:
databaseName: postgres-db
bucketName: "bucket-for-snapshot"
storageSecret:
secretName: "secret-for-bucket"
storageSecretName: "secret-for-bucket"
gcs:
bucket: "bucket-for-snapshot"
14 changes: 7 additions & 7 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 31 additions & 13 deletions pkg/cmds/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/appscode/go/types"
"github.com/k8sdb/apimachinery/pkg/docker"
"github.com/k8sdb/cli/pkg/kube"
"github.com/k8sdb/cli/pkg/roles"
"github.com/k8sdb/cli/pkg/util"
"github.com/spf13/cobra"
kerr "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -50,8 +51,8 @@ func NewCmdInit(out io.Writer, errOut io.Writer) *cobra.Command {
func RunInit(cmd *cobra.Command, out, errOut io.Writer) error {
upgrade := cmdutil.GetFlagBool(cmd, "upgrade")
namespace := cmdutil.GetFlagString(cmd, "operator-namespace")
serviceAccount := cmdutil.GetFlagString(cmd, "operator-service-account")
version := cmdutil.GetFlagString(cmd, "version")
configureRBAC := cmdutil.GetFlagBool(cmd, "rbac")

client, err := kube.NewKubeClient(cmd)
if err != nil {
Expand Down Expand Up @@ -101,6 +102,22 @@ func RunInit(cmd *cobra.Command, out, errOut io.Writer) error {

deployment.Spec.Template.Spec.Containers[0].Image = fmt.Sprintf("%v:%v", docker.ImageOperator, version)

if configureRBAC {
if err := roles.EnsureRBACStuff(client, namespace); err != nil {
return err
}
deployment.Spec.Template.Spec.ServiceAccountName = roles.ServiceAccountName
} else {
deployment.Spec.Template.Spec.ServiceAccountName = ""
}

deployment.Spec.Template.Spec.Containers[0].Args = []string{
"run",
fmt.Sprintf("--address=:%v", docker.OperatorPortNumber),
fmt.Sprintf("--rbac=%v", configureRBAC),
"--v=3",
}

if err := updateOperatorDeployment(client, deployment); err != nil {
return err
}
Expand All @@ -112,7 +129,13 @@ func RunInit(cmd *cobra.Command, out, errOut io.Writer) error {
return nil
}

if err := createOperatorDeployment(client, namespace, serviceAccount, version); err != nil {
if configureRBAC {
if err := roles.EnsureRBACStuff(client, namespace); err != nil {
return err
}
}

if err := createOperatorDeployment(client, namespace, version, configureRBAC); err != nil {
if kerr.IsAlreadyExists(err) {
fmt.Fprintln(errOut, "Operator deployment already exists.")
} else {
Expand Down Expand Up @@ -144,7 +167,7 @@ var operatorLabel = map[string]string{
"app": "kubedb",
}

func createOperatorDeployment(client kubernetes.Interface, namespace, serviceAccount, version string) error {
func createOperatorDeployment(client kubernetes.Interface, namespace, version string, configureRBAC bool) error {
deployment := &extensions.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: docker.OperatorName,
Expand All @@ -165,6 +188,7 @@ func createOperatorDeployment(client kubernetes.Interface, namespace, serviceAcc
Args: []string{
"run",
fmt.Sprintf("--address=:%v", docker.OperatorPortNumber),
fmt.Sprintf("--rbac=%v", configureRBAC),
"--v=3",
},
Env: []apiv1.EnvVar{
Expand All @@ -177,15 +201,6 @@ func createOperatorDeployment(client kubernetes.Interface, namespace, serviceAcc
},
},
},
{
Name: "OPERATOR_SERVICE_ACCOUNT",
ValueFrom: &apiv1.EnvVarSource{
FieldRef: &apiv1.ObjectFieldSelector{
APIVersion: "v1",
FieldPath: "spec.serviceAccountName",
},
},
},
},
Ports: []apiv1.ContainerPort{
{
Expand All @@ -196,12 +211,15 @@ func createOperatorDeployment(client kubernetes.Interface, namespace, serviceAcc
},
},
},
ServiceAccountName: serviceAccount,
},
},
},
}

if configureRBAC {
deployment.Spec.Template.Spec.ServiceAccountName = roles.ServiceAccountName
}

_, err := client.ExtensionsV1beta1().Deployments(namespace).Create(deployment)
return err
}
Expand Down
22 changes: 13 additions & 9 deletions pkg/describer/k8sdb_describer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (

"github.com/golang/glog"
tapi "github.com/k8sdb/apimachinery/api"
amc "github.com/k8sdb/apimachinery/pkg/controller"
"github.com/k8sdb/apimachinery/pkg/storage"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/pkg/api"
Expand All @@ -16,6 +14,8 @@ import (
"k8s.io/kubernetes/pkg/printers"
)

const statusUnknown = "Unknown"

func (d *humanReadableDescriber) describeElastic(item *tapi.Elastic, describerSettings *printers.DescriberSettings) (string, error) {
clientSet, err := d.ClientSet()
if err != nil {
Expand All @@ -26,8 +26,8 @@ func (d *humanReadableDescriber) describeElastic(item *tapi.Elastic, describerSe
metav1.ListOptions{
LabelSelector: labels.SelectorFromSet(
map[string]string{
amc.LabelDatabaseKind: tapi.ResourceKindElastic,
amc.LabelDatabaseName: item.Name,
tapi.LabelDatabaseKind: tapi.ResourceKindElastic,
tapi.LabelDatabaseName: item.Name,
},
).String(),
},
Expand Down Expand Up @@ -96,8 +96,8 @@ func (d *humanReadableDescriber) describePostgres(item *tapi.Postgres, describer
metav1.ListOptions{
LabelSelector: labels.SelectorFromSet(
map[string]string{
amc.LabelDatabaseKind: tapi.ResourceKindPostgres,
amc.LabelDatabaseName: item.Name,
tapi.LabelDatabaseKind: tapi.ResourceKindPostgres,
tapi.LabelDatabaseName: item.Name,
},
).String(),
},
Expand Down Expand Up @@ -215,8 +215,8 @@ func (d *humanReadableDescriber) describeDormantDatabase(item *tapi.DormantDatab
metav1.ListOptions{
LabelSelector: labels.SelectorFromSet(
map[string]string{
amc.LabelDatabaseKind: item.Labels[amc.LabelDatabaseKind],
amc.LabelDatabaseName: item.Name,
tapi.LabelDatabaseKind: item.Labels[tapi.LabelDatabaseKind],
tapi.LabelDatabaseName: item.Name,
},
).String(),
},
Expand Down Expand Up @@ -321,9 +321,13 @@ func listSnapshots(snapshotList *tapi.SnapshotList, out io.Writer) {
fmt.Fprint(w, " Name\tBucket\tStartTime\tCompletionTime\tPhase\n")
fmt.Fprint(w, " ----\t------\t---------\t--------------\t-----\n")
for _, e := range snapshotList.Items {
container, err := e.Spec.SnapshotStorageSpec.Location()
if err != nil {
container = statusUnknown
}
fmt.Fprintf(w, " %s\t%s\t%s\t%s\t%s\n",
e.Name,
storage.GetLocation(e.Spec.SnapshotStorageSpec),
container,
timeToString(e.Status.StartTime),
timeToString(e.Status.CompletionTime),
e.Status.Phase,
Expand Down
10 changes: 6 additions & 4 deletions pkg/printer/resource_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (
"github.com/golang/glog"
tapi "github.com/k8sdb/apimachinery/api"
"github.com/k8sdb/apimachinery/client/clientset"
amc "github.com/k8sdb/apimachinery/pkg/controller"
"github.com/k8sdb/apimachinery/pkg/storage"
"github.com/k8sdb/cli/pkg/decoder"
"github.com/k8sdb/cli/pkg/util"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -261,7 +259,7 @@ func (h *HumanReadablePrinter) printSnapshot(item *tapi.Snapshot, w io.Writer, o
status = statusUnknown
}

short, found := util.ResourceShortFormFor(item.Labels[amc.LabelDatabaseKind])
short, found := util.ResourceShortFormFor(item.Labels[tapi.LabelDatabaseKind])
database := fmt.Sprintf(`%v/%v`, short, item.Spec.DatabaseName)
if !found {
database = fmt.Sprintf(`%v`, item.Spec.DatabaseName)
Expand All @@ -272,7 +270,11 @@ func (h *HumanReadablePrinter) printSnapshot(item *tapi.Snapshot, w io.Writer, o
}

if options.Wide {
if _, err := fmt.Fprintf(w, "%s\t", storage.GetLocation(item.Spec.SnapshotStorageSpec)); err != nil {
container, err := item.Spec.SnapshotStorageSpec.Location()
if err != nil {
container = statusUnknown
}
if _, err := fmt.Fprintf(w, "%s\t", container); err != nil {
return err
}
}
Expand Down
Loading