Skip to content

Instantly share code, notes, and snippets.

@bryanl
Created July 11, 2019 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bryanl/09e3dc58a8999d53ed9d1543e1eb471d to your computer and use it in GitHub Desktop.
Save bryanl/09e3dc58a8999d53ed9d1543e1eb471d to your computer and use it in GitHub Desktop.
/*
Copyright (c) 2019 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"github.com/vmware/octant/pkg/plugin"
"github.com/vmware/octant/pkg/plugin/client"
"github.com/vmware/octant/pkg/store"
"github.com/vmware/octant/pkg/view/component"
"github.com/vmware/octant/pkg/view/flexlayout"
)
func main() {
podGVK := schema.GroupVersionKind{Version: "v1", Kind: "Pod"}
capabilities := &plugin.Capabilities{
SupportsPrinterConfig: []schema.GroupVersionKind{podGVK},
SupportsTab: []schema.GroupVersionKind{podGVK},
}
handlers := client.Handlers{
Print: handlePrint,
PrintTab: handleTab,
ObjectStatus: handleObjectStatus,
}
p, err := client.NewPlugin("plugin-name", "a description", capabilities, handlers)
if err != nil {
log.Fatal(err)
}
p.Serve()
}
func handleTab(dashboardClient client.Dashboard, object runtime.Object) (*component.Tab, error) {
if object == nil {
return nil, errors.New("object is nil")
}
layout := flexlayout.New()
section := layout.AddSection()
err := section.Add(component.NewText("content from a plugin"), component.WidthHalf)
if err != nil {
return nil, err
}
tab := component.Tab{
Name: "PluginStub",
Contents: *layout.ToComponent("Plugin"),
}
return &tab, nil
}
func handlePrint(dashboardClient client.Dashboard, object runtime.Object) (plugin.PrintResponse, error) {
if object == nil {
return plugin.PrintResponse{}, errors.Errorf("object is nil")
}
ctx := context.Background()
key, err := store.KeyFromObject(object)
if err != nil {
return plugin.PrintResponse{}, err
}
u, err := dashboardClient.Get(ctx, key)
if err != nil {
return plugin.PrintResponse{}, err
}
log.Printf("loaded object from objectstore: %v", u)
msg := fmt.Sprintf("update from plugin at %s", time.Now().Format(time.RFC3339))
return plugin.PrintResponse{
Config: []component.SummarySection{
{Header: "from-plugin", Content: component.NewText(msg)},
},
Status: []component.SummarySection{
{Header: "from-plugin", Content: component.NewText(msg)},
},
Items: []component.FlexLayoutItem{
{
Width: component.WidthHalf,
View: component.NewText("item 1 from plugin"),
},
{
Width: component.WidthFull,
View: component.NewText("item 2 from plugin"),
},
},
}, nil
}
func handleObjectStatus(dashboardClient client.Dashboard, object runtime.Object) (plugin.ObjectStatusResponse, error) {
if object == nil {
return plugin.ObjectStatusResponse{}, errors.New("object is nil")
}
status := component.PodSummary{
Status: component.NodeStatusError,
Details: []component.Component{component.NewText(fmt.Sprintf("Timestamp: %s", time.Now().Format(time.RFC850)))},
}
return plugin.ObjectStatusResponse{
ObjectStatus: status,
}, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment