Skip to main content
Work in Progress

You are viewing development documentation built from the latest commit on main (last tag v0.2.0-rc.2, source commit 25e1a892). APIs and features are subject to change.

VictoriaMetrics Builders

Go Reference Go Reference

The victoriametrics package provides the generated constructors for the VictoriaMetrics operator’s operator.victoriametrics.com/v1beta1 kinds that start pods: VMSingle, VMAgent and VMAlert. It is the entry point for building those objects in kure.

Overview

The Go types are kure’s own copy of the operator’s, at operator v0.76.0, in the subpackage api/v1beta1 (its README, pkg/kubernetes/victoriametrics/api/v1beta1/README.md, says what is copied and what is not). kure does not import the operator’s API module, because that module pulls the whole VictoriaMetrics tree into a consumer’s build. A test in the subpackage holds the copy to the operator’s CRDs, and this package’s golden tests validate the objects they build against those CRDs.

Each block on this page is the body of an Example function in example_test.go, which go test runs: it imports this package as victoriametrics, the types as vmv1beta1 "github.com/go-kure/kure/pkg/kubernetes/victoriametrics/api/v1beta1", corev1 "k8s.io/api/core/v1", k8s.io/apimachinery/pkg/api/resource, and fmt for the line that prints what the example built.

Constructors

Every kind this package registers has a generated Create<Kind> wrapper in zz_generated_create.go, produced from the scheme by pkg/kubernetes/internal/gen (make gen-builders, checked by make check-builders in CI). A wrapper delegates to kubernetes.Create[T] and emits TypeMeta and identity only: no default, no label, no spec value. All three kinds are namespaced and take (name, namespace).

obj := victoriametrics.CreateVMSingle("metrics", "monitoring")
fmt.Println(obj.APIVersion, obj.Kind, obj.Namespace+"/"+obj.Name)

The kinds this package registers, their scope, and what stated that scope are rows in the generated Supported kinds and field maturity tables; their module is github.com/go-kure/kure, with no version, because kure holds the types itself.

See the Kubernetes Builders page for the full builder contract: construction, sugar admission classes, purity and the migration ledgers.

Supported Resources

ResourceWhat it starts
VMSingleA single-node VictoriaMetrics database
VMAgentAn agent that scrapes targets and writes to remote storage
VMAlertA rule evaluator that queries a datasource and sends alerts to a notifier

VMSingle

vm := victoriametrics.CreateVMSingle("metrics", "monitoring")
vm.Spec.RetentionPeriod = "30d"
vm.Spec.Storage = &corev1.PersistentVolumeClaimSpec{
    AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce},
    Resources: corev1.VolumeResourceRequirements{
        Requests: corev1.ResourceList{corev1.ResourceStorage: resource.MustParse("20Gi")},
    },
}
vm.Spec.Image = vmv1beta1.Image{Repository: "victoriametrics/victoria-metrics", Tag: "v1.131.0"}
fmt.Println(vm.Spec.RetentionPeriod, vm.Spec.Image.Reference())

Image.Reference returns the image string the operator starts, repository:tag, or repository@tag for a digest.

VMAgent

vm := victoriametrics.CreateVMAgent("agent", "monitoring")
vm.Spec.RemoteWrite = []vmv1beta1.VMAgentRemoteWriteSpec{
    {URL: "http://vmsingle-metrics.monitoring.svc:8428/api/v1/write"},
}
vm.Spec.SelectAllByDefault = true
fmt.Println(len(vm.Spec.RemoteWrite), vm.Spec.HasConfigReloader())

HasConfigReloader and CreatesClusterRBAC answer, from the spec, whether the operator will start a config-reloader container and whether it will create a ClusterRole for the object.

VMAlert

vm := victoriametrics.CreateVMAlert("alert", "monitoring")
vm.Spec.Datasource = vmv1beta1.VMAlertDatasourceSpec{URL: "http://vmsingle-metrics.monitoring.svc:8428"}
vm.Spec.Notifier = &vmv1beta1.VMAlertNotifierSpec{URL: "http://alertmanager.monitoring.svc:9093"}
vm.Spec.EvaluationInterval = "1m"
fmt.Println(vm.Spec.Datasource.URL)

Encoded output

encoding/json does not drop an empty struct under omitempty, so a built object encodes a few struct-valued spec fields as {} (image, resources, configReloaderResources, and on VMSingle and VMAgent arbitraryFSAccessThroughSMs). The operator’s CRDs accept them, and the operator reads {} as unset.