Categorygithub.com/deckhouse/module-sdk
module
0.1.0
Repository: https://github.com/deckhouse/module-sdk.git
Documentation: pkg.go.dev

# README

Module SDK

SDK to easy compile your hooks as a binary and integrate with addon operator

Usage

One file example

This file must be in 'hooks/' folder to build binary (see examples for correct layout)

package main

import (
  "context"
  "log/slog"

  "github.com/deckhouse/module-sdk/pkg"
  "github.com/deckhouse/module-sdk/pkg/app"
  objectpatch "github.com/deckhouse/module-sdk/pkg/object-patch"
  "github.com/deckhouse/module-sdk/pkg/registry"
  v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var _ = registry.RegisterFunc(config, handlerHook)

var config = &pkg.HookConfig{
  Kubernetes: []pkg.KubernetesConfig{
    {
      Name:       "apiservers",
      APIVersion: "v1",
      Kind:       "Pod",
      NamespaceSelector: &pkg.NamespaceSelector{
        NameSelector: &pkg.NameSelector{
          MatchNames: []string{"kube-system"},
        },
      },
      LabelSelector: &v1.LabelSelector{
        MatchLabels: map[string]string{"component": "kube-apiserver"},
      },
      JqFilter: ".metadata.name",
    },
  },
}

func handlerHook(_ context.Context, input *pkg.HookInput) error {
  podNames, err := objectpatch.UnmarshalToStruct[string](input.Snapshots, "apiservers")
  if err != nil {
    return err
  }

  input.Logger.Info("found apiserver pods", slog.Any("podNames", podNames))

  input.Values.Set("test.internal.apiServers", podNames)

  return nil
}

func main() {
  app.Run()
}

More examples you can find here

Testing

If you want to test your JQ filter, you can use JQ helper like in example here

For deckhouse developers

Environment variables

ParameterRequiredDefault valueDescription
BINDING_CONTEXT_PATHin/binding_context.jsonPath to binding context file
VALUES_PATHin/values_path.jsonPath to values file
CONFIG_VALUES_PATHin/config_values_path.jsonPath to config values file
METRICS_PATHout/metrics.jsonPath to metrics file
KUBERNETES_PATCH_PATHout/kubernetes.jsonPath to kubernetes patch file
VALUES_JSON_PATCH_PATHout/values.jsonPath to values patch file
CONFIG_VALUES_JSON_PATCH_PATHout/config_values.jsonPath to config values patch file
HOOK_CONFIG_PATHout/hook_config.jsonPath to dump hook configurations in file
CREATE_FILESfalseAllow hook to create files by himself (by default, waiting for addon operator to create)
LOG_LEVELFATALLog level (suppressed by default)

Work sequence

Deckhouse register process

  1. To register your hooks, add them to import section in main package like in examples
  2. Compile your binary and deliver to "hooks" folder in Deckhouse
  3. Addon operator finds it automatically and register all your hooks in binary, corresponding with your HookConfigs
  4. When addon operator has a reason, it calls hook in your binary
  5. After executing hook, addon operator process hook output

Calling hook

  1. Addon operator create temporary files for input and output data (see ENV for examples)
  2. Addon operator execute hook with corresponding ID and ENV variables pointed to files
  3. Hook reads all files and pass incoming data in HookInput
  4. Hook executes and write all resulting data from collectors contained in HookInput
  5. Addon operator reads info from temporary output files

# Packages

No description provided by the author