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

Add kubedb/memcached to CLI #132

Merged
merged 3 commits into from
Dec 4, 2017
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
14 changes: 11 additions & 3 deletions glide.lock

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

4 changes: 4 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import:
version: master
- package: github.com/k8sdb/mongodb
version: master
- package: github.com/k8sdb/redis
version: master
- package: github.com/k8sdb/memcached
version: master
- package: github.com/spf13/cobra
- package: github.com/yudai/gojsondiff
version: 1.0.0
Expand Down
2 changes: 2 additions & 0 deletions pkg/cmds/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ const (
* postgres
* mysql
* mongodb
* redis
* memcached
* snapshot
* dormantdatabase
`
Expand Down
2 changes: 2 additions & 0 deletions pkg/cmds/summarize.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ const (
* postgreses
* mysqls
* mongodbs
* redises
* memcacheds
`
)

Expand Down
12 changes: 12 additions & 0 deletions pkg/decoder/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ func Decode(kind string, data []byte) (runtime.Object, error) {
return nil, err
}
return mongodb, nil
case tapi.ResourceKindRedis:
var redis *tapi.Redis
if err := yaml.Unmarshal(data, &redis); err != nil {
return nil, err
}
return redis, nil
case tapi.ResourceKindMemcached:
var memcached *tapi.Memcached
if err := yaml.Unmarshal(data, &memcached); err != nil {
return nil, err
}
return memcached, nil
case tapi.ResourceKindSnapshot:
var snapshot *tapi.Snapshot
if err := yaml.Unmarshal(data, &snapshot); err != nil {
Expand Down
2 changes: 2 additions & 0 deletions pkg/describer/describer.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ func (h *humanReadableDescriber) addDefaultHandlers() {
h.Handler(h.describePostgres)
h.Handler(h.describeMySQL)
h.Handler(h.describeMongoDB)
h.Handler(h.describeRedis)
h.Handler(h.describeMemcached)
h.Handler(h.describeSnapshot)
h.Handler(h.describeDormantDatabase)
}
Expand Down
104 changes: 100 additions & 4 deletions pkg/describer/k8sdb_describer.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (d *humanReadableDescriber) describeElastic(item *api.Elasticsearch, descri

describeStorage(item.Spec.Storage, out)

statefulSetName := fmt.Sprintf("%v-%v", item.Name, item.ResourceCode())
statefulSetName := fmt.Sprintf("%v", item.Name)

d.describeStatefulSet(item.Namespace, statefulSetName, out)
d.describeService(item.Namespace, item.Name, out)
Expand Down Expand Up @@ -128,7 +128,7 @@ func (d *humanReadableDescriber) describePostgres(item *api.Postgres, describerS

describeStorage(item.Spec.Storage, out)

statefulSetName := fmt.Sprintf("%v-%v", item.Name, item.ResourceCode())
statefulSetName := fmt.Sprintf("%v", item.Name)

d.describeStatefulSet(item.Namespace, statefulSetName, out)
d.describeService(item.Namespace, item.Name, out)
Expand Down Expand Up @@ -196,7 +196,7 @@ func (d *humanReadableDescriber) describeMySQL(item *api.MySQL, describerSetting

describeStorage(item.Spec.Storage, out)

statefulSetName := fmt.Sprintf("%v-%v", item.Name, item.ResourceCode())
statefulSetName := fmt.Sprintf("%v", item.Name)

d.describeStatefulSet(item.Namespace, statefulSetName, out)
d.describeService(item.Namespace, item.Name, out)
Expand Down Expand Up @@ -264,7 +264,7 @@ func (d *humanReadableDescriber) describeMongoDB(item *api.MongoDB, describerSet

describeStorage(item.Spec.Storage, out)

statefulSetName := fmt.Sprintf("%v-%v", item.Name, item.ResourceCode())
statefulSetName := fmt.Sprintf("%v", item.Name)

d.describeStatefulSet(item.Namespace, statefulSetName, out)
d.describeService(item.Namespace, item.Name, out)
Expand All @@ -286,6 +286,102 @@ func (d *humanReadableDescriber) describeMongoDB(item *api.MongoDB, describerSet
})
}

func (d *humanReadableDescriber) describeRedis(item *api.Redis, describerSettings *printers.DescriberSettings) (string, error) {
clientSet, err := d.ClientSet()
if err != nil {
return "", err
}

var events *kapi.EventList
if describerSettings.ShowEvents {
item.Kind = api.ResourceKindRedis
events, err = clientSet.Core().Events(item.Namespace).Search(scheme.Scheme, item)
if err != nil {
return "", err
}
}

return tabbedString(func(out io.Writer) error {
fmt.Fprintf(out, "Name:\t%s\n", item.Name)
fmt.Fprintf(out, "Namespace:\t%s\n", item.Namespace)
fmt.Fprintf(out, "StartTimestamp:\t%s\n", timeToString(&item.CreationTimestamp))
if item.Labels != nil {
printLabelsMultiline(out, "Labels", item.Labels)
}
fmt.Fprintf(out, "Status:\t%s\n", string(item.Status.Phase))
if len(item.Status.Reason) > 0 {
fmt.Fprintf(out, "Reason:\t%s\n", item.Status.Reason)
}
if item.Annotations != nil {
printLabelsMultiline(out, "Annotations", item.Annotations)
}

describeStorage(item.Spec.Storage, out)

statefulSetName := fmt.Sprintf("%v", item.Name)

d.describeStatefulSet(item.Namespace, statefulSetName, out)
d.describeService(item.Namespace, item.Name, out)

if item.Spec.Monitor != nil {
describeMonitor(item.Spec.Monitor, out)
}

if events != nil {
describeEvents(events, out)
}

return nil
})
}

func (d *humanReadableDescriber) describeMemcached(item *api.Memcached, describerSettings *printers.DescriberSettings) (string, error) {
clientSet, err := d.ClientSet()
if err != nil {
return "", err
}

var events *kapi.EventList
if describerSettings.ShowEvents {
item.Kind = api.ResourceKindMemcached
events, err = clientSet.Core().Events(item.Namespace).Search(scheme.Scheme, item)
if err != nil {
return "", err
}
}

return tabbedString(func(out io.Writer) error {
fmt.Fprintf(out, "Name:\t%s\n", item.Name)
fmt.Fprintf(out, "Namespace:\t%s\n", item.Namespace)
fmt.Fprintf(out, "StartTimestamp:\t%s\n", timeToString(&item.CreationTimestamp))
if item.Labels != nil {
printLabelsMultiline(out, "Labels", item.Labels)
}
fmt.Fprintf(out, "Status:\t%s\n", string(item.Status.Phase))
if len(item.Status.Reason) > 0 {
fmt.Fprintf(out, "Reason:\t%s\n", item.Status.Reason)
}
if item.Annotations != nil {
printLabelsMultiline(out, "Annotations", item.Annotations)
}

deploymentName := fmt.Sprintf("%v", item.Name)

d.describeDeployments(item.Namespace, deploymentName, out)
d.describeService(item.Namespace, item.Name, out)

if item.Spec.Monitor != nil {
describeMonitor(item.Spec.Monitor, out)
}

if events != nil {
describeEvents(events, out)
}

return nil
})
}

func (d *humanReadableDescriber) describeSnapshot(item *api.Snapshot, describerSettings *printers.DescriberSettings) (string, error) {
clientSet, err := d.ClientSet()
if err != nil {
Expand Down
30 changes: 30 additions & 0 deletions pkg/describer/kube_describer.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,36 @@ func (d *humanReadableDescriber) describeStatefulSet(namespace, name string, out
fmt.Fprintf(out, " Pods Status:\t%d Running / %d Waiting / %d Succeeded / %d Failed\n", running, waiting, succeeded, failed)
}

func (d *humanReadableDescriber) describeDeployments(namespace, name string, out io.Writer) {
clientSet, err := d.ClientSet()
if err != nil {
return
}

ps, err := clientSet.Extensions().Deployments(namespace).Get(name, metav1.GetOptions{})
if err != nil {
return
}
pc := clientSet.Core().Pods(namespace)

selector, err := metav1.LabelSelectorAsSelector(ps.Spec.Selector)
if err != nil {
return
}

running, waiting, succeeded, failed, err := getPodStatusForController(pc, selector)
if err != nil {
return
}

fmt.Fprint(out, "\n")
fmt.Fprint(out, "Deployment:\t\n")
fmt.Fprintf(out, " Name:\t%s\n", ps.Name)
fmt.Fprintf(out, " Replicas:\t%d current / %d desired\n", ps.Status.Replicas, ps.Spec.Replicas)
fmt.Fprintf(out, " CreationTimestamp:\t%s\n", timeToString(&ps.CreationTimestamp))
fmt.Fprintf(out, " Pods Status:\t%d Running / %d Waiting / %d Succeeded / %d Failed\n", running, waiting, succeeded, failed)
}

func getPodStatusForController(c coreclient.PodInterface, selector labels.Selector) (running, waiting, succeeded, failed int, err error) {
options := metav1.ListOptions{LabelSelector: selector.String()}
rcPods, err := c.List(options)
Expand Down
94 changes: 94 additions & 0 deletions pkg/printer/resource_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ func (h *HumanReadablePrinter) addDefaultHandlers() {
h.Handler(h.printMySQL)
h.Handler(h.printMongoDBList)
h.Handler(h.printMongoDB)
h.Handler(h.printRedisList)
h.Handler(h.printRedis)
h.Handler(h.printMemcachedList)
h.Handler(h.printMemcached)
h.Handler(h.printSnapshotList)
h.Handler(h.printSnapshot)
h.Handler(h.printDormantDatabaseList)
Expand Down Expand Up @@ -152,6 +156,10 @@ func getColumns(options PrintOptions, t reflect.Type) []string {
if options.Wide {
columns = append(columns, "VERSION")
}
case "*v1alpha1.Redis", "*v1alpha1.RedisList":
if options.Wide {
columns = append(columns, "VERSION")
}
case "*v1alpha1.Snapshot", "*v1alpha1.SnapshotList":
columns = append(columns, "DATABASE")
if options.Wide {
Expand Down Expand Up @@ -339,6 +347,92 @@ func (h *HumanReadablePrinter) printMongoDBList(itemList *tapi.MongoDBList, w io
return nil
}

func (h *HumanReadablePrinter) printRedis(item *tapi.Redis, w io.Writer, options PrintOptions) error {
name := formatResourceName(options.Kind, item.Name, options.WithKind)

namespace := item.Namespace

if options.WithNamespace {
if _, err := fmt.Fprintf(w, "%s\t", namespace); err != nil {
return err
}
}

status := item.Status.Phase
if status == "" {
status = statusUnknown
}
if _, err := fmt.Fprintf(w, "%s\t", name); err != nil {
return err
}

if options.Wide {
if _, err := fmt.Fprintf(w, "%s\t", item.Spec.Version); err != nil {
return err
}
}

if _, err := fmt.Fprintf(w, "%s\t%s", status, TranslateTimestamp(item.CreationTimestamp)); err != nil {
return err
}

_, err := fmt.Fprint(w, appendAllLabels(options.ShowLabels, item.Labels))

return err
}

func (h *HumanReadablePrinter) printRedisList(itemList *tapi.RedisList, w io.Writer, options PrintOptions) error {
for _, item := range itemList.Items {
if err := h.printRedis(&item, w, options); err != nil {
return err
}
}
return nil
}

func (h *HumanReadablePrinter) printMemcached(item *tapi.Memcached, w io.Writer, options PrintOptions) error {
name := formatResourceName(options.Kind, item.Name, options.WithKind)

namespace := item.Namespace

if options.WithNamespace {
if _, err := fmt.Fprintf(w, "%s\t", namespace); err != nil {
return err
}
}

status := item.Status.Phase
if status == "" {
status = statusUnknown
}
if _, err := fmt.Fprintf(w, "%s\t", name); err != nil {
return err
}

if options.Wide {
if _, err := fmt.Fprintf(w, "%s\t", item.Spec.Version); err != nil {
return err
}
}

if _, err := fmt.Fprintf(w, "%s\t%s", status, TranslateTimestamp(item.CreationTimestamp)); err != nil {
return err
}

_, err := fmt.Fprint(w, appendAllLabels(options.ShowLabels, item.Labels))

return err
}

func (h *HumanReadablePrinter) printMemcachedList(itemList *tapi.MemcachedList, w io.Writer, options PrintOptions) error {
for _, item := range itemList.Items {
if err := h.printMemcached(&item, w, options); err != nil {
return err
}
}
return nil
}

func (h *HumanReadablePrinter) printSnapshot(item *tapi.Snapshot, w io.Writer, options PrintOptions) error {
name := formatResourceName(options.Kind, item.Name, options.WithKind)

Expand Down
Loading