Go client for HAProxy configuration and runtime API
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Thomas Lynch a2738e5bc1 Merge remote-tracking branch 'upstream/master' 2 months ago
.github/workflows BUG/MINOR: ci: removing deprecated make target 3 months ago
assets/images DOC: Add initial README 5 years ago
bin BUILD/MAJOR: swagger: use local binary instead of depending on global one 1 year ago
cmd MEDIUM: enhance struct_equal_generator for v3 model 3 months ago
configuration MINOR: add set-var-fmt for tcp request connection rule 2 months ago
docs BUILD/MAJOR: swagger: use local binary instead of depending on global one 1 year ago
e2e MAJOR: upgrade client-native to v6 3 months ago
errors CLEANUP/MEDIUM: lint: preparing code for new linting rules 3 years ago
misc BUG/MINOR: resolve implicit memory aliasing in for loop 8 months ago
models MINOR: Add support for set-fc-mark, set-fc-tos 2 months ago
options MAJOR: upgrade client-native to v6 3 months ago
runtime Merge remote-tracking branch 'upstream/master' 2 months ago
specification MINOR: Add support for set-fc-mark, set-fc-tos 2 months ago
spoe MAJOR: upgrade client-native to v6 3 months ago
storage Merge remote-tracking branch 'upstream/master' 2 months ago
test MINOR: add set-var-fmt for tcp request connection rule 2 months ago
.allowed_license_types BUILD/MINOR: ci: add check to see if go packages have correct licenses 4 months ago
.gitignore MAJOR: models: add tool to generate Equal and Diff methods 8 months ago
.gitlab-ci.yml CLEANUP/MINOR: ci: remove deprecated gitlab token usage 3 months ago
.golangci.yml BUILD: fix yamllint complaints 3 months ago
.yamllint.yml BUILD/MEDIUM: CI: add specification linters for gitlab 3 years ago
LICENSE LICENSE: Fix typo line 5 years ago
Makefile BUILD/MINOR: kubebuilder: add comments for marker generator for kubebuilder 5 months ago
README.md MAJOR: upgrade client-native to v6 3 months ago
client_native.go MAJOR: upgrade client-native to v6 3 months ago
go.mod MINOR: add set-var-fmt for tcp request connection rule 2 months ago
go.sum MINOR: add set-var-fmt for tcp request connection rule 2 months ago
interface.go MAJOR: upgrade client-native to v6 3 months ago

README.md

HAProxy

HAProxy Native Golang Client

Contributors License

HAProxy Native Client is a client that exposes methods for reading and changing HAProxy configuration files, and executing commands and parsing the output of the HAProxy Runtime API (via unix socket, AKA stats socket in HAProxy).

HAProxy Models

This project contains structs and validation methods that are autogenerated using go-swagger from the swagger specification found here.These structs are also used in the DataPlaneAPI

Requirements

go-swagger v0.30.2. Note that at the moment you need to use version 0.30.2

models can be generated with make models. It automatically combines specification parts into a single file, and then use that to generate models.

Usage Example

//import cfg_opt "github.com/haproxytech/client-native/v6/configuration/options"
confClient, err := configuration.New(context.Background(),
    cfg_opt.ConfigurationFile(haproxyOptions.ConfigFile),
    cfg_opt.HAProxyBin(haproxyOptions.HAProxy),
    cfg_opt.Backups(haproxyOptions.BackupsNumber),
    cfg_opt.UsePersistentTransactions,
    cfg_opt.TransactionsDir(haproxyOptions.TransactionDir),
    cfg_opt.MasterWorker,
    cfg_opt.UseMd5Hash,
)
if err != nil {
    return nil, fmt.Errorf("error setting up configuration client: %s", err.Error())
}

// runtime
// import runtime_options "github.com/haproxytech/client-native/v6/runtime/options"
nbproc := 8
ms := runtime_options.MasterSocket(masterSocket, nbproc)
runtimeClient, err = runtime_api.New(ctx, ms)
if err != nil {
    return nil, fmt.Errorf("error setting up runtime client: %s", err.Error())
}
// or if not using master-worker
socketList := map[int]string{
    1: "/var/run/haproxy.sock"
}
sockets := runtime_options.Sockets(socketList)
runtimeClient, err = runtime_api.New(ctx, mapsDir, sockets)
if err != nil {
    return nil, fmt.Errorf("error setting up runtime client: %s", err.Error())
}
// end runtime

// import "github.com/haproxytech/client-native/v6/options"
opt := []options.Option{
    options.Configuration(confClient),
    options.Runtime(runtimeClient),
}

// additional options - not mandatory

//maps storage
mapStorage, err := storage.New(MapsDir, storage.MapsType)
if err != nil {
    log.Fatalf("error initializing map storage: %v", err)
}
opt = append(opt, options.MapStorage(mapStorage))

//ssl cert storage
sslCertStorage, err := storage.New(SSLCertsDir, storage.SSLType)
if err != nil {
    log.Fatalf("error initializing SSL certs storage: %v", err)
}
opt = append(opt, options.SSLCertStorage(sslCertStorage))

// general storage
generalStorage, err := storage.New(GeneralStorage, storage.GeneralType)
if err != nil {
    log.Fatalf("error initializing General storage: %v", err)
}
opt = append(opt, options.GeneralStorage(generalStorage))

//spoe
prms := spoe.Params{
    SpoeDir:        SpoeDir,
    TransactionDir: SpoeTransactionDir,
}
spoe, err := spoe.NewSpoe(prms)
if err != nil {
    log.Fatalf("error setting up spoe: %v", err)
}
opt = append(opt, options.Spoe(spoe))

// combine all together and create a client
client, err := client_native.New(cyx, opt...)
if err != nil {
    log.Fatalf("Error initializing configuration client: %v", err)
}

// fetch configuration handler
configuration, err := client.Configuration()
if err != nil {
    fmt.Println(err.Error())
}

//fetch all backends
bcks, err := configuration.GetBackends(t)
if err != nil {
    fmt.Println(err.Error())
}
//...

backendsJSON, err := bcks.MarshallBinary()

if err != nil {
    fmt.Println(err.Error())
}

fmt.Println(string(backendsJSON))
//...

Contributing

For commit messages and general style please follow the haproxy project's CONTRIBUTING guide and use that where applicable.