Open4

k8s のカスタムコントローラーを作る

N9tE9N9tE9

以下コマンドでプロジェクトを作成する

> kubebuilder init --domain lkeix.github.io --repo github.com/lkeix/markdown-view-controller

APIの雛形を作成する

> kubebuilder create api --group view --version v1 --kind MarkdownView
N9tE9N9tE9

internal/controller 以下に reconcilation loop のカスタムコントローラのロジックを実装するみたい

func (r *MarkdownViewReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
	_ = log.FromContext(ctx)

	// TODO(user): your logic here

	return ctrl.Result{}, nil
}
N9tE9N9tE9

CRD マニフェストを作る

マニフェストに含まれるの値を扱えるように構造体にフィールドを作成する。

type MarkdownViewSpec struct {
    // INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
    // Important: Run "make" to regenerate code after modifying this file

    // Markdowns contain the markdown files you want to display.
    // The key indicates the file name and must not overlap with the keys.
    // The value is the content in markdown format.
    //+kubebuilder:validation:Required
    //+kubebuilder:validation:MinProperties=1
    Markdowns map[string]string `json:"markdowns,omitempty"`

    // Replicas is the number of viewers.
    // +kubebuilder:default=1
    // +optional
    Replicas int32 `json:"replicas,omitempty"`

    // ViewerImage is the image name of the viewer.
    // +optional
    ViewerImage string `json:"viewerImage,omitempty"`
}

// +kubebuilder:validation:required
はrequired のフィールド
// +optional
はオプションで指定できるフィールドとして定義する

kubebuilder のオプションは結構ありそうなので、ユースケースに応じて調べる形になりそう

構造体からマニフェストを生成するには以下でできる

$ make manifests

で config/crd/bases 以下にマニフェストが作成される。