本文整理汇总了Golang中github.com/skynetservices/skydns/msg.Service类的典型用法代码示例。如果您正苦于以下问题:Golang Service类的具体用法?Golang Service怎么用?Golang Service使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Service类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: PTRRecords
func (s *server) PTRRecords(q dns.Question) (records []dns.RR, err error) {
name := strings.ToLower(q.Name)
path, star := msg.PathWithWildcard(name)
if star {
return nil, fmt.Errorf("reverse can not contain wildcards")
}
r, err := get(s.client, path, false)
if err != nil {
// if server has a forward, forward the query
return nil, err
}
if r.Node.Dir {
return nil, fmt.Errorf("reverse should not be a directory")
}
serv := new(msg.Service)
if err := json.Unmarshal([]byte(r.Node.Value), serv); err != nil {
s.config.log.Infof("failed to parse json: %s", err.Error())
return nil, err
}
ttl := uint32(r.Node.TTL)
if ttl == 0 {
ttl = s.config.Ttl
}
serv.Key = r.Node.Key
// If serv.Host is parseble as a IP address we should not return anything.
// TODO(miek).
records = append(records, serv.NewPTR(q.Name, ttl))
return records, nil
}
示例2: Records
func (g *Backend) Records(name string, exact bool) ([]msg.Service, error) {
path, star := msg.PathWithWildcard(name)
bits := strings.Split(path, "/")
fmt.Println(bits, path, star)
srv := msg.Service{}
srv.Host = "192.168.5.1"
l := make([]msg.Service, 0)
l = append(l, srv)
return l, nil // errors.New("FAIL")
}
示例3: addServiceHTTPHandler
// Handle API add service requests
func (s *Server) addServiceHTTPHandler(w http.ResponseWriter, req *http.Request) {
addServiceCount.Inc(1)
vars := mux.Vars(req)
var uuid string
var ok bool
var secret string
//read the authorization header to get the secret.
secret = req.Header.Get("Authorization")
if err := s.authenticate(secret); err != nil {
http.Error(w, err.Error(), http.StatusForbidden)
return
}
if uuid, ok = vars["uuid"]; !ok {
http.Error(w, "UUID required", http.StatusBadRequest)
return
}
var serv msg.Service
if err := json.NewDecoder(req.Body).Decode(&serv); err != nil {
log.Println("Error: ", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if serv.Host == "" || serv.Port == 0 {
http.Error(w, "Host and Port required", http.StatusBadRequest)
return
}
serv.UUID = uuid
if _, err := s.raftServer.Do(NewAddServiceCommand(serv)); err != nil {
switch err {
case registry.ErrExists:
http.Error(w, err.Error(), http.StatusConflict)
case raft.NotLeaderError:
s.redirectToLeader(w, req)
default:
log.Println("Error: ", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return
}
w.WriteHeader(http.StatusCreated)
}
示例4: decode
func (g *Backend) decode(n *etcd.Node) (*msg.Service, error) {
serv := new(msg.Service)
if err := json.Unmarshal([]byte(n.Value), serv); err != nil {
return nil, err
}
serv.Key = n.Key
serv.Ttl = g.calculateTtl(n, serv)
if serv.Priority == 0 {
serv.Priority = int(g.config.Priority)
}
return serv, nil
}
示例5: loopNodes
// loopNodes recursively loops through the nodes and returns all the values. The nodes' keyname
// will be match against any wildcards when star is true.
func (g *Backend) loopNodes(ns []*etcd.Node, nameParts []string, star bool, bx map[bareService]bool) (sx []msg.Service, err error) {
if bx == nil {
bx = make(map[bareService]bool)
}
Nodes:
for _, n := range ns {
if n.Dir {
nodes, err := g.loopNodes(n.Nodes, nameParts, star, bx)
if err != nil {
return nil, err
}
sx = append(sx, nodes...)
continue
}
if star {
keyParts := strings.Split(n.Key, "/")
for i, n := range nameParts {
if i > len(keyParts)-1 {
// name is longer than key
continue Nodes
}
if n == "*" || n == "any" {
continue
}
if keyParts[i] != n {
continue Nodes
}
}
}
serv := new(msg.Service)
if err := json.Unmarshal([]byte(n.Value), serv); err != nil {
return nil, err
}
b := bareService{serv.Host, serv.Port, serv.Priority, serv.Weight, serv.Text}
if _, ok := bx[b]; ok {
continue
}
bx[b] = true
serv.Key = n.Key
serv.Ttl = g.calculateTtl(n, serv)
if serv.Priority == 0 {
serv.Priority = int(g.config.Priority)
}
sx = append(sx, *serv)
}
return sx, nil
}
示例6: loopNodes
func (g *Backendv3) loopNodes(kv []*mvccpb.KeyValue, nameParts []string, star bool, bx map[bareService]bool) (sx []msg.Service, err error) {
if bx == nil {
bx = make(map[bareService]bool)
}
Nodes:
for _, item := range kv {
if star {
s := string(item.Key[:])
keyParts := strings.Split(s, "/")
for i, n := range nameParts {
if i > len(keyParts)-1 {
continue Nodes
}
if n == "*" || n == "any" {
continue
}
if keyParts[i] != n {
continue Nodes
}
}
}
serv := new(msg.Service)
if err := json.Unmarshal(item.Value, serv); err != nil {
return nil, err
}
b := bareService{serv.Host,
serv.Port,
serv.Priority,
serv.Weight,
serv.Text}
bx[b] = true
serv.Key = string(item.Key)
//TODO: another call (LeaseRequest) for TTL when RPC in etcdv3 is ready
serv.Ttl = g.calculateTtl(item, serv)
if serv.Priority == 0 {
serv.Priority = int(g.config.Priority)
}
sx = append(sx, *serv)
}
return sx, nil
}
示例7: writeService
func writeService(c *cli.Context, service *msg.Service) {
if c.GlobalBool("json") {
if err := json.NewEncoder(os.Stdout).Encode(service); err != nil {
writeError(err)
}
} else {
fmt.Printf("UUID: %s\nName: %s\nHost: %s\nPort: %d\nEnvironment: %s\nRegion: %s\nVersion: %s\n\n",
service.UUID,
service.Name,
service.Host,
service.Port,
service.Environment,
service.Region,
service.Version)
fmt.Printf("TTL %d\nRemaining TTL: %d\n",
service.TTL,
service.RemainingTTL())
}
}
示例8: TestGetService
func TestGetService(t *testing.T) {
s := newTestServer("", 9570, 9571, "")
defer s.Stop()
m := msg.Service{
UUID: "123",
Name: "TestService",
Version: "1.0.0",
Region: "Test",
Host: "localhost",
Environment: "Production",
Port: 9000,
TTL: 4,
Expires: getExpirationTime(4),
}
s.registry.Add(m)
req, _ := http.NewRequest("GET", "/skydns/services/"+m.UUID, nil)
resp := httptest.NewRecorder()
s.router.ServeHTTP(resp, req)
if resp.Code != http.StatusOK {
t.Fatal("Failed to retrieve service")
}
m.TTL = 3 // TTL will be lower as time has passed
expected, err := json.Marshal(m)
if err != nil {
t.Fatal(err)
}
// Newline is expected
expected = append(expected, []byte("\n")...)
if !bytes.Equal(resp.Body.Bytes(), expected) {
t.Fatalf("Returned service is invalid. Expected %q but received %q", string(expected), resp.Body.String())
}
}
示例9: CNAMERecords
func (s *server) CNAMERecords(q dns.Question, name string) (records []dns.RR, err error) {
path, _ := msg.PathWithWildcard(name) // no wildcards here
r, err := get(s.client, path, true)
if err != nil {
return nil, err
}
if !r.Node.Dir {
serv := new(msg.Service)
if err := json.Unmarshal([]byte(r.Node.Value), serv); err != nil {
s.config.log.Infof("failed to parse json: %s", err.Error())
return nil, err
}
ip := net.ParseIP(serv.Host)
ttl := s.calculateTtl(r.Node, serv)
serv.Key = r.Node.Key
serv.Ttl = ttl
if ip == nil {
records = append(records, serv.NewCNAME(q.Name, dns.Fqdn(serv.Host)))
}
}
return records, nil
}
示例10: TestUpdateTTL
func TestUpdateTTL(t *testing.T) {
s := newTestServer("", 9540, 9541, "")
defer s.Stop()
m := msg.Service{
UUID: "123",
Name: "TestService",
Version: "1.0.0",
Region: "Test",
Host: "localhost",
Environment: "Production",
Port: 9000,
TTL: 4,
}
s.registry.Add(m)
m.TTL = 25
b, err := json.Marshal(m)
if err != nil {
t.Fatal(err)
}
req, _ := http.NewRequest("PATCH", "/skydns/services/"+m.UUID, bytes.NewBuffer(b))
resp := httptest.NewRecorder()
s.router.ServeHTTP(resp, req)
if resp.Code != http.StatusOK {
t.Fatal("Failed to update TTL")
}
if serv, err := s.registry.GetUUID(m.UUID); err != nil || serv.TTL != 24 {
t.Fatal("Failed to update TTL", err, serv.TTL)
}
}
示例11: setEntry
// setEntry creates the entire path if it doesn't already exist in the cache,
// then sets the given service record under the given key. The path this entry
// would have occupied in an etcd datastore is computed from the given fqdn and
// stored as the "Key" of the skydns service; this is only required because
// skydns expects the service record to contain a key in a specific format
// (presumably for legacy compatibility). Note that the fqnd string typically
// contains both the key and all elements in the path.
func (cache *TreeCache) setEntry(key string, val *skymsg.Service, fqdn string, path ...string) {
// TODO: Consolidate setEntry and setSubCache into a single method with a
// type switch.
// TODO: Insted of passing the fqdn as an argument, we can reconstruct
// it from the path, provided callers always pass the full path to the
// object. This is currently *not* the case, since callers first create
// a new, empty node, populate it, then parent it under the right path.
// So we don't know the full key till the final parenting operation.
node := cache.ensureChildNode(path...)
// This key is used to construct the "target" for SRV record lookups.
// For normal service/endpoint lookups, this will result in a key like:
// /skydns/local/cluster/svc/svcNS/svcName/record-hash
// but for headless services that govern pods requesting a specific
// hostname (as used by petset), this will end up being:
// /skydns/local/cluster/svc/svcNS/svcName/pod-hostname
val.Key = skymsg.Path(fqdn)
node.Entries[key] = val
}
示例12: SRVRecords
// SRVRecords returns SRV records from etcd.
// If the Target is not an name but an IP address, an name is created .
func (s *server) SRVRecords(q dns.Question, name string, bufsize uint16, dnssec bool) (records []dns.RR, extra []dns.RR, err error) {
path, star := msg.PathWithWildcard(name)
r, err := get(s.client, path, true)
if err != nil {
return nil, nil, err
}
if !r.Node.Dir { // single element
serv := new(msg.Service)
if err := json.Unmarshal([]byte(r.Node.Value), serv); err != nil {
s.config.log.Infof("failed to parse json: %s", err.Error())
return nil, nil, err
}
ip := net.ParseIP(serv.Host)
ttl := s.calculateTtl(r.Node, serv)
if serv.Priority == 0 {
serv.Priority = int(s.config.Priority)
}
serv.Key = r.Node.Key
serv.Ttl = ttl
switch {
case ip == nil:
srv := serv.NewSRV(q.Name, uint16(100))
records = append(records, srv)
if !dns.IsSubDomain(s.config.Domain, srv.Target) {
m1, e1 := s.Lookup(srv.Target, dns.TypeA, bufsize, dnssec)
if e1 == nil {
extra = append(extra, m1.Answer...)
}
m1, e1 = s.Lookup(srv.Target, dns.TypeAAAA, bufsize, dnssec)
if e1 == nil {
// If we have seen CNAME's we *assume* that they already added.
for _, a := range m1.Answer {
if _, ok := a.(*dns.CNAME); !ok {
extra = append(extra, a)
}
}
}
}
case ip.To4() != nil:
serv.Host = msg.Domain(serv.Key)
records = append(records, serv.NewSRV(q.Name, uint16(100)))
extra = append(extra, serv.NewA(serv.Host, ip.To4()))
case ip.To4() == nil:
serv.Host = msg.Domain(serv.Key)
records = append(records, serv.NewSRV(q.Name, uint16(100)))
extra = append(extra, serv.NewAAAA(serv.Host, ip.To16()))
}
return records, extra, nil
}
sx, err := s.loopNodes(&r.Node.Nodes, strings.Split(msg.Path(name), "/"), star, nil)
if err != nil || len(sx) == 0 {
return nil, nil, err
}
// Looping twice to get the right weight vs priority
w := make(map[int]int)
for _, serv := range sx {
weight := 100
if serv.Weight != 0 {
weight = serv.Weight
}
if _, ok := w[serv.Priority]; !ok {
w[serv.Priority] = weight
continue
}
w[serv.Priority] += weight
}
lookup := make(map[string]bool)
for _, serv := range sx {
w1 := 100.0 / float64(w[serv.Priority])
if serv.Weight == 0 {
w1 *= 100
} else {
w1 *= float64(serv.Weight)
}
weight := uint16(math.Floor(w1))
ip := net.ParseIP(serv.Host)
switch {
case ip == nil:
srv := serv.NewSRV(q.Name, weight)
records = append(records, srv)
if _, ok := lookup[srv.Target]; !ok {
if !dns.IsSubDomain(s.config.Domain, srv.Target) {
m1, e1 := s.Lookup(srv.Target, dns.TypeA, bufsize, dnssec)
if e1 == nil {
extra = append(extra, m1.Answer...)
}
m1, e1 = s.Lookup(srv.Target, dns.TypeAAAA, bufsize, dnssec)
if e1 == nil {
// If we have seen CNAME's we *assume* that they are already added.
for _, a := range m1.Answer {
if _, ok := a.(*dns.CNAME); !ok {
extra = append(extra, a)
}
}
}
}
}
//.........这里部分代码省略.........
示例13: Records
// Records implements the SkyDNS Backend interface and returns standard records for
// a name.
//
// The standard pattern is <prefix>.<service_name>.<namespace>.(svc|endpoints|pod).<base>
//
// * prefix may be any series of prefix values
// * _endpoints is a special prefix that returns the same as <service_name>.<namespace>.svc.<base>
// * service_name and namespace must locate a real service
// * unless a fallback is defined, in which case the fallback name will be looked up
// * svc indicates standard service rules apply (clusterIP or endpoints as A records)
// * reverse lookup of IP is only possible for clusterIP
// * SRV records are returned for each host+port combination as:
// _<port_name>._<port_protocol>.<dns>
// _<port_name>.<endpoint_id>.<dns>
// * endpoints always returns each individual endpoint as A records
// * SRV records for endpoints are similar to SVC, but are prefixed with a single label
// that is a hash of the endpoint IP
// * pods is of the form <IP_with_dashes>.<namespace>.pod.<base> and resolves to <IP>
//
func (b *ServiceResolver) Records(dnsName string, exact bool) ([]msg.Service, error) {
if !strings.HasSuffix(dnsName, b.base) {
return nil, errNoSuchName
}
prefix := strings.Trim(strings.TrimSuffix(dnsName, b.base), ".")
segments := strings.Split(prefix, ".")
for i, j := 0, len(segments)-1; i < j; i, j = i+1, j-1 {
segments[i], segments[j] = segments[j], segments[i]
}
if len(segments) == 0 {
return nil, errNoSuchName
}
glog.V(4).Infof("Answering query %s:%t", dnsName, exact)
switch base := segments[0]; base {
case "pod":
if len(segments) != 3 {
return nil, errNoSuchName
}
namespace, encodedIP := segments[1], segments[2]
ip := convertDashIPToIP(encodedIP)
if net.ParseIP(ip) == nil {
return nil, errNoSuchName
}
return []msg.Service{
{
Host: ip,
Port: 0,
Priority: 10,
Weight: 10,
Ttl: 30,
Key: msg.Path(buildDNSName(b.base, "pod", namespace, getHash(ip))),
},
}, nil
case "svc", "endpoints":
if len(segments) < 3 {
return nil, errNoSuchName
}
namespace, name := segments[1], segments[2]
svc, err := b.accessor.Services(namespace).Get(name)
if err != nil {
if errors.IsNotFound(err) && b.fallback != nil {
if fallback, ok := b.fallback(prefix, exact); ok {
return b.Records(fallback+b.base, exact)
}
return nil, errNoSuchName
}
return nil, errNoSuchName
}
// no clusterIP and not headless, no DNS
if len(svc.Spec.ClusterIP) == 0 && svc.Spec.Type != kapi.ServiceTypeExternalName {
return nil, errNoSuchName
}
subdomain := buildDNSName(b.base, base, namespace, name)
endpointPrefix := base == "endpoints"
retrieveEndpoints := endpointPrefix || (len(segments) > 3 && segments[3] == "_endpoints")
includePorts := len(segments) > 3 && hasAllPrefixedSegments(segments[3:], "_") && segments[3] != "_endpoints"
// if has a portal IP and looking at svc
if svc.Spec.ClusterIP != kapi.ClusterIPNone && !retrieveEndpoints {
hostValue := svc.Spec.ClusterIP
if svc.Spec.Type == kapi.ServiceTypeExternalName {
hostValue = svc.Spec.ExternalName
}
defaultService := msg.Service{
Host: hostValue,
Port: 0,
Priority: 10,
Weight: 10,
Ttl: 30,
}
defaultHash := getHash(defaultService.Host)
defaultName := buildDNSName(subdomain, defaultHash)
defaultService.Key = msg.Path(defaultName)
//.........这里部分代码省略.........
示例14: NewAddServiceCommand
// Creates a new AddServiceCommand
func NewAddServiceCommand(s msg.Service) *AddServiceCommand {
s.Expires = getExpirationTime(s.TTL)
return &AddServiceCommand{s}
}
示例15: Records
// Records implements the SkyDNS Backend interface and returns standard records for
// a name.
//
// The standard pattern is <prefix>.<service_name>.<namespace>.(svc|endpoints|pod).<base>
//
// * prefix may be any series of prefix values
// * _endpoints is a special prefix that returns the same as <service_name>.<namespace>.svc.<base>
// * service_name and namespace must locate a real service
// * unless a fallback is defined, in which case the fallback name will be looked up
// * svc indicates standard service rules apply (portalIP or endpoints as A records)
// * reverse lookup of IP is only possible for portalIP
// * SRV records are returned for each host+port combination as:
// _<port_name>._<port_protocol>.<dns>
// _<port_name>.<endpoint_id>.<dns>
// * endpoints always returns each individual endpoint as A records
// * SRV records for endpoints are similar to SVC, but are prefixed with a single label
// that is a hash of the endpoint IP
// * pods is of the form <IP_with_dashes>.<namespace>.pod.<base> and resolves to <IP>
//
func (b *ServiceResolver) Records(dnsName string, exact bool) ([]msg.Service, error) {
if !strings.HasSuffix(dnsName, b.base) {
return nil, nil
}
prefix := strings.Trim(strings.TrimSuffix(dnsName, b.base), ".")
segments := strings.Split(prefix, ".")
for i, j := 0, len(segments)-1; i < j; i, j = i+1, j-1 {
segments[i], segments[j] = segments[j], segments[i]
}
if len(segments) == 0 {
return nil, nil
}
glog.V(4).Infof("Answering query %s:%t", dnsName, exact)
switch base := segments[0]; base {
case "pod":
if len(segments) != 3 {
return nil, nil
}
namespace, encodedIP := segments[1], segments[2]
ip := convertDashIPToIP(encodedIP)
if net.ParseIP(ip) == nil {
return nil, nil
}
return []msg.Service{
{
Host: ip,
Port: 0,
Priority: 10,
Weight: 10,
Ttl: 30,
Key: msg.Path(buildDNSName(b.base, "pod", namespace, getHash(ip))),
},
}, nil
case "svc", "endpoints":
if len(segments) < 3 {
return nil, nil
}
namespace, name := segments[1], segments[2]
svc, err := b.accessor.Services(namespace).Get(name)
if err != nil {
if errors.IsNotFound(err) && b.fallback != nil {
if fallback, ok := b.fallback(prefix, exact); ok {
return b.Records(fallback+b.base, exact)
}
}
return nil, err
}
// no portalIP and not headless, no DNS
if len(svc.Spec.ClusterIP) == 0 {
return nil, nil
}
subdomain := buildDNSName(b.base, base, namespace, name)
endpointPrefix := base == "endpoints"
retrieveEndpoints := endpointPrefix || (len(segments) > 3 && segments[3] == "_endpoints")
// if has a portal IP and looking at svc
if svc.Spec.ClusterIP != kapi.ClusterIPNone && !retrieveEndpoints {
defaultService := msg.Service{
Host: svc.Spec.ClusterIP,
Port: 0,
Priority: 10,
Weight: 10,
Ttl: 30,
}
defaultHash := getHash(defaultService.Host)
defaultName := buildDNSName(subdomain, defaultHash)
defaultService.Key = msg.Path(defaultName)
if len(svc.Spec.Ports) == 0 {
return []msg.Service{defaultService}, nil
}
services := []msg.Service{}
if len(segments) == 3 {
for _, p := range svc.Spec.Ports {
//.........这里部分代码省略.........