1
0
Fork 0
mirror of https://github.com/arangodb/kube-arangodb.git synced 2024-12-14 11:57:37 +00:00

Merge pull request #385 from arangodb/bug-fix/arangodb-exporter-fixes

Create headless service for exporter pods.
This commit is contained in:
Max Neunhöffer 2019-05-13 16:54:00 +02:00 committed by GitHub
commit a8bcd2477f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 75 additions and 2 deletions

View file

@ -38,6 +38,8 @@ type DeploymentStatus struct {
// to access syncmasters (only set when dc2dc synchronization is enabled).
SyncServiceName string `json:"syncServiceName,omitempty"`
ExporterServiceName string `json:"exporterServiceName,omitempty"`
// Images holds a list of ArangoDB images with their ID and ArangoDB version.
Images ImageInfoList `json:"arangodb-images,omitempty"`
// Image that is currently being used when new pods are created
@ -69,6 +71,7 @@ func (ds *DeploymentStatus) Equal(other DeploymentStatus) bool {
ds.Reason == other.Reason &&
ds.ServiceName == other.ServiceName &&
ds.SyncServiceName == other.SyncServiceName &&
ds.ExporterServiceName == other.ExporterServiceName &&
ds.Images.Equal(other.Images) &&
ds.CurrentImage.Equal(other.CurrentImage) &&
ds.Members.Equal(other.Members) &&

View file

@ -25,7 +25,7 @@ package resources
import (
"time"
"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1alpha"
@ -117,6 +117,21 @@ func (r *Resources) EnsureServices() error {
}
}
}
if spec.Metrics.IsEnabled() {
name, _, err := k8sutil.CreateExporterService(svcs, apiObject, apiObject.AsOwner())
if err != nil {
log.Debug().Err(err).Msgf("Failed to create %s exporter service", name)
return maskAny(err)
}
status, lastVersion := r.context.GetStatus()
if status.ExporterServiceName != name {
status.ExporterServiceName = name
if err := r.context.UpdateStatus(status, lastVersion); err != nil {
return maskAny(err)
}
}
}
return nil
}

View file

@ -29,7 +29,7 @@ import (
"strconv"
"strings"
"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
@ -64,6 +64,45 @@ func CreateSyncMasterClientServiceName(deploymentName string) string {
return deploymentName + "-sync"
}
// CreateExporterClientServiceName returns the name of the service used by arangodb-exporter clients for the given
// deployment name.
func CreateExporterClientServiceName(deploymentName string) string {
return deploymentName + "-exporter"
}
// CreateExporterService
func CreateExporterService(svcs ServiceInterface, deployment metav1.Object, owner metav1.OwnerReference) (string, bool, error) {
deploymentName := deployment.GetName()
svcName := CreateExporterClientServiceName(deploymentName)
selectorLabels := LabelsForExporterServiceSelector(deploymentName)
svc := &v1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: svcName,
Labels: LabelsForExporterService(deploymentName),
},
Spec: v1.ServiceSpec{
ClusterIP: v1.ClusterIPNone,
Ports: []v1.ServicePort{
v1.ServicePort{
Name: "exporter",
Protocol: v1.ProtocolTCP,
Port: ArangoExporterPort,
},
},
Selector: selectorLabels,
},
}
addOwnerRefToObject(svc.GetObjectMeta(), &owner)
if _, err := svcs.Create(svc); IsAlreadyExists(err) {
return svcName, false, nil
} else if err != nil {
return svcName, false, maskAny(err)
}
return svcName, true, nil
}
// CreateHeadlessService prepares and creates a headless service in k8s, used to provide a stable
// DNS name for all pods.
// If the service already exists, nil is returned.

View file

@ -50,6 +50,22 @@ func addOwnerRefToObject(obj metav1.Object, ownerRef *metav1.OwnerReference) {
}
}
// LabelsForExporterServiceSelector returns a map of labels, used to select the all arangodb-exporter containers
func LabelsForExporterServiceSelector(deploymentName string) map[string]string {
return map[string]string{
LabelKeyArangoDeployment: deploymentName,
LabelKeyArangoExporter: "yes",
}
}
// LabelsForExporterService returns a map of labels, used to select the all arangodb-exporter containers
func LabelsForExporterService(deploymentName string) map[string]string {
return map[string]string{
LabelKeyArangoDeployment: deploymentName,
LabelKeyApp: AppName,
}
}
// LabelsForDeployment returns a map of labels, given to all resources for given deployment name
func LabelsForDeployment(deploymentName, role string) map[string]string {
l := map[string]string{