fix: backend name for Stateful services. (Service Fabric)
This commit is contained in:
parent
350d61b4a6
commit
799136a714
6 changed files with 174 additions and 151 deletions
188
vendor/github.com/containous/traefik-extra-service-fabric/servicefabric.go
generated
vendored
188
vendor/github.com/containous/traefik-extra-service-fabric/servicefabric.go
generated
vendored
|
@ -69,34 +69,7 @@ func (p *Provider) updateConfig(configurationChan chan<- types.ConfigMessage, po
|
|||
log.Info("Checking service fabric config")
|
||||
}
|
||||
|
||||
services, err := getClusterServices(sfClient)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
templateObjects := struct {
|
||||
Services []ServiceItemExtended
|
||||
}{
|
||||
services,
|
||||
}
|
||||
|
||||
var sfFuncMap = template.FuncMap{
|
||||
"isPrimary": isPrimary,
|
||||
"getDefaultEndpoint": p.getDefaultEndpoint,
|
||||
"getNamedEndpoint": p.getNamedEndpoint,
|
||||
"getApplicationParameter": p.getApplicationParameter,
|
||||
"doesAppParamContain": p.doesAppParamContain,
|
||||
"hasServiceLabel": hasServiceLabel,
|
||||
"getServiceLabelValue": getServiceLabelValue,
|
||||
"getServiceLabelValueWithDefault": getServiceLabelValueWithDefault,
|
||||
"getServiceLabelsWithPrefix": getServiceLabelsWithPrefix,
|
||||
"getServicesWithLabelValueMap": getServicesWithLabelValueMap,
|
||||
"getServicesWithLabelValue": getServicesWithLabelValue,
|
||||
"isExposed": getFuncBoolLabel("expose"),
|
||||
}
|
||||
|
||||
configuration, err := p.GetConfiguration(tmpl, sfFuncMap, templateObjects)
|
||||
|
||||
configuration, err := p.buildConfiguration(sfClient)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -120,24 +93,39 @@ func (p *Provider) updateConfig(configurationChan chan<- types.ConfigMessage, po
|
|||
return nil
|
||||
}
|
||||
|
||||
func (p Provider) doesAppParamContain(app sf.ApplicationItem, key, shouldContain string) bool {
|
||||
value := p.getApplicationParameter(app, key)
|
||||
return strings.Contains(value, shouldContain)
|
||||
}
|
||||
|
||||
func (p Provider) getApplicationParameter(app sf.ApplicationItem, key string) string {
|
||||
for _, param := range app.Parameters {
|
||||
if param.Key == key {
|
||||
return param.Value
|
||||
}
|
||||
func (p *Provider) buildConfiguration(sfClient sfClient) (*types.Configuration, error) {
|
||||
var sfFuncMap = template.FuncMap{
|
||||
"getServices": getServices,
|
||||
"hasLabel": hasFuncService,
|
||||
"getLabelValue": getFuncServiceStringLabel,
|
||||
"getLabelsWithPrefix": getServiceLabelsWithPrefix,
|
||||
"isPrimary": isPrimary,
|
||||
"isExposed": getFuncBoolLabel("expose", false),
|
||||
"getBackendName": getBackendName,
|
||||
"getDefaultEndpoint": getDefaultEndpoint,
|
||||
"getNamedEndpoint": getNamedEndpoint, // FIXME unused
|
||||
"getApplicationParameter": getApplicationParameter, // FIXME unused
|
||||
"doesAppParamContain": doesAppParamContain, // FIXME unused
|
||||
"filterServicesByLabelValue": filterServicesByLabelValue, // FIXME unused
|
||||
}
|
||||
log.Errorf("Parameter %s doesn't exist in app %s", key, app.Name)
|
||||
return ""
|
||||
|
||||
services, err := getClusterServices(sfClient)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
templateObjects := struct {
|
||||
Services []ServiceItemExtended
|
||||
}{
|
||||
Services: services,
|
||||
}
|
||||
|
||||
return p.GetConfiguration(tmpl, sfFuncMap, templateObjects)
|
||||
}
|
||||
|
||||
func (p Provider) getDefaultEndpoint(instance replicaInstance) string {
|
||||
func getDefaultEndpoint(instance replicaInstance) string {
|
||||
id, data := instance.GetReplicaData()
|
||||
endpoint, err := getDefaultEndpoint(data.Address)
|
||||
endpoint, err := getReplicaDefaultEndpoint(data)
|
||||
if err != nil {
|
||||
log.Warnf("No default endpoint for replica %s in service %s endpointData: %s", id, data.Address)
|
||||
return ""
|
||||
|
@ -145,16 +133,64 @@ func (p Provider) getDefaultEndpoint(instance replicaInstance) string {
|
|||
return endpoint
|
||||
}
|
||||
|
||||
func (p Provider) getNamedEndpoint(instance replicaInstance, endpointName string) string {
|
||||
id, data := instance.GetReplicaData()
|
||||
endpoint, err := getNamedEndpoint(data.Address, endpointName)
|
||||
func getReplicaDefaultEndpoint(replicaData *sf.ReplicaItemBase) (string, error) {
|
||||
endpoints, err := decodeEndpointData(replicaData.Address)
|
||||
if err != nil {
|
||||
log.Warnf("No names endpoint of %s for replica %s in endpointData: %s", endpointName, id, data.Address)
|
||||
return "", err
|
||||
}
|
||||
|
||||
var defaultHTTPEndpoint string
|
||||
for _, v := range endpoints {
|
||||
if strings.Contains(v, "http") {
|
||||
defaultHTTPEndpoint = v
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if len(defaultHTTPEndpoint) == 0 {
|
||||
return "", errors.New("no default endpoint found")
|
||||
}
|
||||
return defaultHTTPEndpoint, nil
|
||||
}
|
||||
|
||||
func getNamedEndpoint(instance replicaInstance, endpointName string) string {
|
||||
id, data := instance.GetReplicaData()
|
||||
endpoint, err := getReplicaNamedEndpoint(data, endpointName)
|
||||
if err != nil {
|
||||
log.Warnf("No names endpoint of %s for replica %s in endpointData: %s. Error: %v", endpointName, id, data.Address, err)
|
||||
return ""
|
||||
}
|
||||
return endpoint
|
||||
}
|
||||
|
||||
func getReplicaNamedEndpoint(replicaData *sf.ReplicaItemBase, endpointName string) (string, error) {
|
||||
endpoints, err := decodeEndpointData(replicaData.Address)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
endpoint, exists := endpoints[endpointName]
|
||||
if !exists {
|
||||
return "", errors.New("endpoint doesn't exist")
|
||||
}
|
||||
return endpoint, nil
|
||||
}
|
||||
|
||||
func doesAppParamContain(app sf.ApplicationItem, key, shouldContain string) bool {
|
||||
value := getApplicationParameter(app, key)
|
||||
return strings.Contains(value, shouldContain)
|
||||
}
|
||||
|
||||
func getApplicationParameter(app sf.ApplicationItem, key string) string {
|
||||
for _, param := range app.Parameters {
|
||||
if param.Key == key {
|
||||
return param.Value
|
||||
}
|
||||
}
|
||||
log.Errorf("Parameter %s doesn't exist in app %s", key, app.Name)
|
||||
return ""
|
||||
}
|
||||
|
||||
func getClusterServices(sfClient sfClient) ([]ServiceItemExtended, error) {
|
||||
apps, err := sfClient.GetApplications()
|
||||
if err != nil {
|
||||
|
@ -236,7 +272,7 @@ func getValidInstances(sfClient sfClient, app sf.ApplicationItem, service sf.Ser
|
|||
return validInstances
|
||||
}
|
||||
|
||||
func getServicesWithLabelValueMap(services []ServiceItemExtended, key string) map[string][]ServiceItemExtended {
|
||||
func getServices(services []ServiceItemExtended, key string) map[string][]ServiceItemExtended {
|
||||
result := map[string][]ServiceItemExtended{}
|
||||
for _, service := range services {
|
||||
if value, exists := service.Labels[key]; exists {
|
||||
|
@ -250,7 +286,7 @@ func getServicesWithLabelValueMap(services []ServiceItemExtended, key string) ma
|
|||
return result
|
||||
}
|
||||
|
||||
func getServicesWithLabelValue(services []ServiceItemExtended, key, expectedValue string) []ServiceItemExtended {
|
||||
func filterServicesByLabelValue(services []ServiceItemExtended, key, expectedValue string) []ServiceItemExtended {
|
||||
var srvWithLabel []ServiceItemExtended
|
||||
for _, service := range services {
|
||||
value, exists := service.Labels[key]
|
||||
|
@ -261,25 +297,6 @@ func getServicesWithLabelValue(services []ServiceItemExtended, key, expectedValu
|
|||
return srvWithLabel
|
||||
}
|
||||
|
||||
func getServiceLabelValueWithDefault(service ServiceItemExtended, key, defaultValue string) string {
|
||||
value, exists := service.Labels[key]
|
||||
|
||||
if !exists {
|
||||
return defaultValue
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func getServiceLabelsWithPrefix(service ServiceItemExtended, prefix string) map[string]string {
|
||||
results := make(map[string]string)
|
||||
for k, v := range service.Labels {
|
||||
if strings.HasPrefix(k, prefix) {
|
||||
results[k] = v
|
||||
}
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
func isPrimary(instance replicaInstance) bool {
|
||||
_, data := instance.GetReplicaData()
|
||||
return data.ReplicaRole == "Primary"
|
||||
|
@ -290,7 +307,7 @@ func isHealthy(instanceData *sf.ReplicaItemBase) bool {
|
|||
}
|
||||
|
||||
func hasHTTPEndpoint(instanceData *sf.ReplicaItemBase) bool {
|
||||
_, err := getDefaultEndpoint(instanceData.Address)
|
||||
_, err := getReplicaDefaultEndpoint(instanceData)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
|
@ -314,37 +331,6 @@ func decodeEndpointData(endpointData string) (map[string]string, error) {
|
|||
return endpoints, nil
|
||||
}
|
||||
|
||||
func getDefaultEndpoint(endpointData string) (string, error) {
|
||||
endpoints, err := decodeEndpointData(endpointData)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
var defaultHTTPEndpointExists bool
|
||||
var defaultHTTPEndpoint string
|
||||
for _, v := range endpoints {
|
||||
if strings.Contains(v, "http") {
|
||||
defaultHTTPEndpoint = v
|
||||
defaultHTTPEndpointExists = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !defaultHTTPEndpointExists {
|
||||
return "", errors.New("no default endpoint found")
|
||||
}
|
||||
return defaultHTTPEndpoint, nil
|
||||
}
|
||||
|
||||
func getNamedEndpoint(endpointData string, endpointName string) (string, error) {
|
||||
endpoints, err := decodeEndpointData(endpointData)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
endpoint, exists := endpoints[endpointName]
|
||||
if !exists {
|
||||
return "", errors.New("endpoint doesn't exist")
|
||||
}
|
||||
return endpoint, nil
|
||||
func getBackendName(service ServiceItemExtended, partition PartitionItemExtended) string {
|
||||
return provider.Normalize(service.Name + partition.PartitionInformation.ID)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue