mirror of
https://github.com/hashicorp/vault.git
synced 2025-09-10 00:11:09 +02:00
* Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License. Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * add missing license headers * Update copyright file headers to BUS-1.1 * Fix test that expected exact offset on hcl file --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com> Co-authored-by: Sarah Thompson <sthompson@hashicorp.com> Co-authored-by: Brian Kassouf <bkassouf@hashicorp.com>
124 lines
3.3 KiB
Go
124 lines
3.3 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package kubernetes
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
"sync"
|
|
|
|
"github.com/hashicorp/go-hclog"
|
|
sr "github.com/hashicorp/vault/serviceregistration"
|
|
"github.com/hashicorp/vault/serviceregistration/kubernetes/client"
|
|
)
|
|
|
|
const (
|
|
// Labels are placed in a pod's metadata.
|
|
labelVaultVersion = "vault-version"
|
|
labelActive = "vault-active"
|
|
labelSealed = "vault-sealed"
|
|
labelPerfStandby = "vault-perf-standby"
|
|
labelInitialized = "vault-initialized"
|
|
|
|
// This is the path to where these labels are applied.
|
|
pathToLabels = "/metadata/labels/"
|
|
)
|
|
|
|
func NewServiceRegistration(config map[string]string, logger hclog.Logger, state sr.State) (sr.ServiceRegistration, error) {
|
|
namespace, err := getRequiredField(logger, config, client.EnvVarKubernetesNamespace, "namespace")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
podName, err := getRequiredField(logger, config, client.EnvVarKubernetesPodName, "pod_name")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
c, err := client.New(logger)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// The Vault version must be sanitized because it can contain special
|
|
// characters like "+" which aren't acceptable by the Kube API.
|
|
state.VaultVersion = client.Sanitize(state.VaultVersion)
|
|
return &serviceRegistration{
|
|
logger: logger,
|
|
namespace: namespace,
|
|
podName: podName,
|
|
retryHandler: &retryHandler{
|
|
logger: logger,
|
|
namespace: namespace,
|
|
podName: podName,
|
|
initialState: state,
|
|
patchesToRetry: make(map[string]*client.Patch),
|
|
client: c,
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
type serviceRegistration struct {
|
|
logger hclog.Logger
|
|
namespace, podName string
|
|
retryHandler *retryHandler
|
|
}
|
|
|
|
func (r *serviceRegistration) Run(shutdownCh <-chan struct{}, wait *sync.WaitGroup, _ string) error {
|
|
r.retryHandler.Run(shutdownCh, wait)
|
|
return nil
|
|
}
|
|
|
|
func (r *serviceRegistration) NotifyActiveStateChange(isActive bool) error {
|
|
r.retryHandler.Notify(&client.Patch{
|
|
Operation: client.Replace,
|
|
Path: pathToLabels + labelActive,
|
|
Value: strconv.FormatBool(isActive),
|
|
})
|
|
return nil
|
|
}
|
|
|
|
func (r *serviceRegistration) NotifySealedStateChange(isSealed bool) error {
|
|
r.retryHandler.Notify(&client.Patch{
|
|
Operation: client.Replace,
|
|
Path: pathToLabels + labelSealed,
|
|
Value: strconv.FormatBool(isSealed),
|
|
})
|
|
return nil
|
|
}
|
|
|
|
func (r *serviceRegistration) NotifyPerformanceStandbyStateChange(isStandby bool) error {
|
|
r.retryHandler.Notify(&client.Patch{
|
|
Operation: client.Replace,
|
|
Path: pathToLabels + labelPerfStandby,
|
|
Value: strconv.FormatBool(isStandby),
|
|
})
|
|
return nil
|
|
}
|
|
|
|
func (r *serviceRegistration) NotifyInitializedStateChange(isInitialized bool) error {
|
|
r.retryHandler.Notify(&client.Patch{
|
|
Operation: client.Replace,
|
|
Path: pathToLabels + labelInitialized,
|
|
Value: strconv.FormatBool(isInitialized),
|
|
})
|
|
return nil
|
|
}
|
|
|
|
func getRequiredField(logger hclog.Logger, config map[string]string, envVar, configParam string) (string, error) {
|
|
value := ""
|
|
switch {
|
|
case os.Getenv(envVar) != "":
|
|
value = os.Getenv(envVar)
|
|
case config[configParam] != "":
|
|
value = config[configParam]
|
|
default:
|
|
return "", fmt.Errorf(`%s must be provided via %q or the %q config parameter`, configParam, envVar, configParam)
|
|
}
|
|
if logger.IsDebug() {
|
|
logger.Debug(fmt.Sprintf("%q: %q", configParam, value))
|
|
}
|
|
return value, nil
|
|
}
|