-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
second experiment with Go hooks
- Loading branch information
Showing
22 changed files
with
560 additions
and
892 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,15 @@ | ||
package global_hooks | ||
|
||
import "github.com/flant/addon-operator/sdk" | ||
import ( | ||
"github.com/flant/addon-operator/pkg/module_manager/go_hook" | ||
"github.com/flant/addon-operator/sdk" | ||
) | ||
|
||
var _ = sdk.Register(&GoHook{}) | ||
var _ = sdk.RegisterFunc(&go_hook.HookConfig{ | ||
OnStartup: &go_hook.OrderedConfig{Order: 10}, | ||
}, handler) | ||
|
||
type GoHook struct { | ||
sdk.CommonGoHook | ||
} | ||
|
||
func (h *GoHook) Metadata() sdk.HookMetadata { | ||
return h.CommonMetadataFromRuntime() | ||
} | ||
|
||
func (h *GoHook) Config() *sdk.HookConfig { | ||
return h.CommonGoHook.Config(&sdk.HookConfig{ | ||
YamlConfig: ` | ||
configVersion: v1 | ||
onStartup: 10 | ||
`, | ||
MainHandler: h.Main, | ||
}) | ||
} | ||
|
||
func (h *GoHook) Main(input *sdk.BindingInput) (*sdk.BindingOutput, error) { | ||
func handler(input *go_hook.HookInput) error { | ||
input.LogEntry.Infof("Start Global Go hook") | ||
return nil, nil | ||
return nil | ||
} |
175 changes: 41 additions & 134 deletions
175
examples/700-go-hook/modules/001-module-go-hooks/hooks/go_hooks.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,183 +1,90 @@ | ||
package hooks | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"time" | ||
|
||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
|
||
"github.com/flant/addon-operator/pkg/utils" | ||
"github.com/flant/addon-operator/pkg/utils/values_store" | ||
"github.com/flant/addon-operator/pkg/module_manager/go_hook" | ||
"github.com/flant/addon-operator/sdk" | ||
|
||
"github.com/flant/shell-operator/pkg/app" | ||
"github.com/flant/shell-operator/pkg/kube" | ||
"github.com/flant/shell-operator/pkg/kube_events_manager/types" | ||
"github.com/flant/shell-operator/pkg/metric_storage/operation" | ||
) | ||
|
||
var _ = sdk.Register(&GoHook{}) | ||
|
||
type GoHook struct { | ||
sdk.CommonGoHook | ||
kubeClient kube.KubernetesClient | ||
} | ||
type podSpecFilteredObj v1.PodSpec | ||
|
||
func (h *GoHook) Metadata() sdk.HookMetadata { | ||
return sdk.HookMetadata{ | ||
Name: "go_hook.go", | ||
Path: "001-module-go-hooks/hooks/go_hook.go", | ||
Module: true, | ||
ModuleName: "module-go-hooks", | ||
func (ps *podSpecFilteredObj) ApplyFilter(obj *unstructured.Unstructured) (go_hook.FilterResult, error) { | ||
pod := &v1.Pod{} | ||
err := go_hook.ConvertUnstructured(obj, pod) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
podSpec := pod.Spec | ||
|
||
return &podSpec, nil | ||
} | ||
|
||
func (h *GoHook) Config() *sdk.HookConfig { | ||
return h.CommonGoHook.Config(&sdk.HookConfig{ | ||
OnStartup: &sdk.OrderedConfig{ | ||
type GoHook struct{} | ||
|
||
func (h *GoHook) Config() *go_hook.HookConfig { | ||
return &go_hook.HookConfig{ | ||
OnStartup: &go_hook.OrderedConfig{ | ||
Order: 10, | ||
Handler: func(input *sdk.BindingInput) (*sdk.BindingOutput, error) { | ||
input.LogEntry.Infof("Hello from module 'hooks-only' golang hook 'go_hooks'!\n") | ||
return nil, nil | ||
}, | ||
}, | ||
|
||
OnBeforeHelm: &sdk.OrderedConfig{ | ||
OnBeforeHelm: &go_hook.OrderedConfig{ | ||
Order: 10, | ||
Handler: func(input *sdk.BindingInput) (*sdk.BindingOutput, error) { | ||
input.LogEntry.Infof("Hello from module 'hooks-only' golang hook 'go_hooks' beforeHelm!\n") | ||
vs := values_store.NewValuesStoreFromValues(input.Values) | ||
|
||
input.LogEntry.Infof("go_hooks beforeHelm hook got values: %s", vs.GetAsYaml()) | ||
|
||
return nil, nil | ||
}, | ||
}, | ||
|
||
Kubernetes: []sdk.KubernetesConfig{ | ||
Kubernetes: []go_hook.KubernetesConfig{ | ||
{ | ||
Name: "pods-for-hooks-only", | ||
ApiVersion: "v1", | ||
Kind: "Pods", | ||
IncludeSnapshotsFrom: []string{"pods"}, | ||
//JqFilter: ".spec", | ||
FilterFunc: func(obj *unstructured.Unstructured) (string, error) { | ||
var pod v1.Pod | ||
err := runtime.DefaultUnstructuredConverter. | ||
FromUnstructured(obj.UnstructuredContent(), &pod) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
res, err := json.Marshal(pod.Spec) | ||
if err != nil { | ||
return "", err | ||
} | ||
fmt.Printf("got pod.spec: %s\n", string(res)) | ||
return string(res), nil | ||
}, | ||
ExecuteHookOnEvents: []types.WatchEventType{types.WatchEventAdded, types.WatchEventModified, types.WatchEventDeleted}, | ||
ExecuteHookOnSynchronization: true, | ||
Handler: func(input *sdk.BindingInput) (*sdk.BindingOutput, error) { | ||
input.LogEntry.Infof("Hello from on_kube.pods2! I have %d snapshots for '%s' event\n", | ||
len(input.BindingContext.Snapshots), | ||
input.BindingContext.WatchEvent) | ||
|
||
vs := values_store.NewValuesStoreFromValues(input.Values) | ||
|
||
input.LogEntry.Infof("go_hooks kube hook got values: %s", vs.GetAsYaml()) | ||
|
||
return nil, nil | ||
}, | ||
Name: "pods", | ||
ApiVersion: "v1", | ||
Kind: "Pods", | ||
Filterable: &podSpecFilteredObj{}, | ||
ExecuteHookOnSynchronization: go_hook.Bool(true), | ||
}, | ||
}, | ||
|
||
Schedule: []sdk.ScheduleConfig{ | ||
Schedule: []go_hook.ScheduleConfig{ | ||
{ | ||
Name: "metrics", | ||
Crontab: "*/5 * * * * *", | ||
Handler: h.SendMetrics, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
// Go hook has no kubectl, but it can initialize its own kubernetes client! | ||
func (h *GoHook) initKubeClient() error { | ||
if h.kubeClient != nil { | ||
return nil | ||
} | ||
|
||
h.kubeClient = kube.NewKubernetesClient() | ||
h.kubeClient.WithContextName(app.KubeContext) | ||
h.kubeClient.WithConfigPath(app.KubeConfig) | ||
h.kubeClient.WithRateLimiterSettings(app.KubeClientQps, app.KubeClientBurst) | ||
// Initialize kube client for kube events hooks. | ||
err := h.kubeClient.Init() | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (h *GoHook) SendMetrics(input *sdk.BindingInput) (*sdk.BindingOutput, error) { | ||
err := h.initKubeClient() | ||
if err != nil { | ||
input.LogEntry.Errorf("Fatal: initialize kube client: %s", err) | ||
return nil, err | ||
} | ||
|
||
podList, err := h.kubeClient.CoreV1().Pods("").List(metav1.ListOptions{}) | ||
if err != nil { | ||
input.LogEntry.Errorf("Fatal: cannot list pods: %s", err) | ||
return nil, err | ||
func (h *GoHook) Metadata() *go_hook.HookMetadata { | ||
return &go_hook.HookMetadata{ | ||
Name: "go_hook.go", | ||
Path: "001-module-go-hooks/hooks/go_hook.go", | ||
Module: true, | ||
ModuleName: "module-go-hooks", | ||
} | ||
} | ||
|
||
input.LogEntry.Infof("Get %d pods:", len(podList.Items)) | ||
for i, pod := range podList.Items { | ||
input.LogEntry.Infof("%02d. Pod/%s in ns/%s", i, pod.Name, pod.Namespace) | ||
func (h *GoHook) Run(input *go_hook.HookInput) error { | ||
for _, o := range input.Snapshots["pods"] { | ||
podSpec := o.(*podSpecFilteredObj) | ||
input.LogEntry.Infof("Got podSpec: %+v", podSpec) | ||
} | ||
|
||
input.LogEntry.Infof("Hello from on_kube.pods2! I have %d snapshots for '%s' event\n", | ||
len(input.BindingContext.Snapshots), | ||
input.BindingContext.WatchEvent) | ||
|
||
vs := values_store.NewValuesStoreFromValues(input.Values) | ||
|
||
input.LogEntry.Infof("go_hooks schedule hook got values: %s", vs.GetAsYaml()) | ||
|
||
out := &sdk.BindingOutput{ | ||
Metrics: []operation.MetricOperation{}, | ||
} | ||
input.LogEntry.Infof("Hello from on_kube.pods2! I have %d snapshots\n", | ||
len(input.Snapshots)) | ||
|
||
v := 1.0 | ||
out.Metrics = append(out.Metrics, operation.MetricOperation{ | ||
*input.Metrics = append(*input.Metrics, operation.MetricOperation{ | ||
Name: "addon_go_hooks_total", | ||
Add: &v, | ||
}) | ||
|
||
out.ConfigValuesPatches = &utils.ValuesPatch{ | ||
[]*utils.ValuesPatchOperation{ | ||
{ | ||
Op: "add", | ||
Path: "/moduleGoHooks/time", | ||
Value: fmt.Sprintf("%d", time.Now().Unix()), | ||
}, | ||
}, | ||
} | ||
input.ConfigValues.Set("moduleGoHooks.time", time.Now().Unix()) | ||
input.Values.Set("moduleGoHooks.timeTemp", time.Now().Unix()) | ||
|
||
out.MemoryValuesPatches = &utils.ValuesPatch{ | ||
[]*utils.ValuesPatchOperation{ | ||
{ | ||
Op: "add", | ||
Path: "/moduleGoHooks/time_temp", | ||
Value: fmt.Sprintf("%d", time.Now().Unix()), | ||
}, | ||
}, | ||
} | ||
return out, nil | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.