service.go 3.28 KB
Newer Older
ale's avatar
ale committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
package main

import (
	"fmt"
	"sort"
)

type MonitoringEndpoint struct {
	JobName string `yaml:"job_name"`
	Port    int    `yaml:"port"`
	Scheme  string `yaml:"scheme,omitempty"`
}

func (m MonitoringEndpoint) TargetStatusURL() string {
	return fmt.Sprintf("https://monitor.%s/graph?expr=ok{job=%s}", config.Domain, m.JobName)
}

func (m MonitoringEndpoint) ServiceDashboardURL() string {
	return fmt.Sprintf("https://grafana.%s/service-dashboard?service=%s", config.Domain, m.JobName)
}

type PublicEndpoint struct {
	Name           string   `yaml:"name"`
	Domains        []string `yaml:"domains,omitempty"`
	Port           int      `yaml:"port"`
	Scheme         string   `yaml:"scheme,omitempty"`
	EnableSSOProxy bool     `yaml:"enable_sso_proxy,omitempty"`
}

func (p PublicEndpoint) URL() string {
	return fmt.Sprintf("https://%s.%s/", p.Name, config.PublicDomains[0])
}

func (p PublicEndpoint) LogsURL() string {
	return fmt.Sprintf("https://logs.%s/kibana/bla/bla/?vhost=%s.%s", config.Domain, p.Name, config.PublicDomains[0])
}

// Boolean fields that default to true are problematic, so we use
// pointers to detect the case where the value is unset.
type ServiceCredentials struct {
	Name         string `yaml:"name"`
	EnableClient *bool  `yaml:"enable_client,omitempty"`
	EnableServer *bool  `yaml:"enable_server,omitempty"`
}

func (c *ServiceCredentials) HasClient() bool {
	if c.EnableClient == nil {
		return true
	}
	return *c.EnableClient
}

func (c *ServiceCredentials) HasServer() bool {
	if c.EnableServer == nil {
		return true
	}
	return *c.EnableServer
}

type Container struct {
	Name    string              `yaml:"name"`
	Image   string              `yaml:"image"`
	Port    int                 `yaml:"port"`
	Volumes []map[string]string `yaml:"volumes,omitempty"`
	Env     map[string]string   `yaml:"env,omitempty"`
}

type Service struct {
	Name string

	NumInstances          int                  `yaml:"num_instances,omitempty"`
	SchedulingGroup       string               `yaml:"scheduling_group"`
	MasterElection        bool                 `yaml:"master_election,omitempty"`
	MasterSchedulingGroup string               `yaml:"master_scheduling_group,omitempty"`
	Ports                 []int                `yaml:"ports,omitempty"`
	ServiceCredentials    []ServiceCredentials `yaml:"service_credentials,omitempty"`
	LDAPCredentials       []struct {
		Name string `yaml:"name"`
	} `yaml:"ldap_credentials,omitempty"`
	MonitoringEndpoints []MonitoringEndpoint `yaml:"monitoring_endpoints,omitempty"`
	PublicEndpoints     []PublicEndpoint     `yaml:"public_endpoints,omitempty"`
	Containers          []Container          `yaml:"containers,omitempty"`
}

type ServiceMap map[string]*Service

func (m ServiceMap) Keys() []string {
	var out []string
	for k := range m {
		out = append(out, k)
	}
	sort.Sort(sortedStringList(out))
	return out
}

func (s *Service) HasClientServiceCredentials() bool {
	for _, c := range s.ServiceCredentials {
		if c.HasClient() {
			return true
		}
	}
	return false
}

func (s *Service) HasServerServiceCredentials() bool {
	for _, c := range s.ServiceCredentials {
		if c.HasServer() {
			return true
		}
	}
	return false
}

func (s *Service) HasPublicURLs() bool {
	return len(s.PublicEndpoints) > 0
}

func (s *Service) HasMonitoringEndpoints() bool {
	return len(s.MonitoringEndpoints) > 0
}