Exemplo n.º 1
0
func (s *CiliumNetClientSuite) TestSVCAddFail(c *C) {

	fe, err := types.NewL3n4AddrID(types.TCP, randomAddr1, 1984, 2016)
	c.Assert(err, IsNil)
	wantLBSVC := types.LBSVC{
		FE: *fe,
		BES: []types.L3n4Addr{
			revNat1,
			revNat2,
		},
	}
	addRevNAT := true

	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		c.Assert(r.Method, Equals, "POST")
		c.Assert(r.URL.Path, Equals, "/lb/service")
		var receivedLBSVC types.LBSVC
		err := json.NewDecoder(r.Body).Decode(&receivedLBSVC)
		c.Assert(err, Equals, nil)
		c.Assert(receivedLBSVC, DeepEquals, wantLBSVC)
		w.WriteHeader(http.StatusInternalServerError)
		w.Header().Set("Content-Type", "application/json")
		e := json.NewEncoder(w)
		err = e.Encode(types.ServerError{Code: -1, Text: "daemon didn't complete your request"})
		c.Assert(err, Equals, nil)
	}))
	defer server.Close()

	cli := NewTestClient(server.URL, c)

	err = cli.SVCAdd(wantLBSVC.FE, wantLBSVC.BES, addRevNAT)

	c.Assert(strings.Contains(err.Error(), "daemon didn't complete your request"), Equals, true)
}
Exemplo n.º 2
0
func (s *CiliumNetClientSuite) TestSVCGetFail(c *C) {
	fe, err := types.NewL3n4AddrID(types.TCP, randomAddr1, 1984, 2016)
	c.Assert(err, IsNil)
	feSHA256Sum, err := fe.SHA256Sum()
	c.Assert(err, IsNil)

	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		c.Assert(r.Method, Equals, "GET")
		c.Assert(r.URL.Path, Equals, "/lb/service/"+feSHA256Sum)
		w.WriteHeader(http.StatusInternalServerError)
		w.Header().Set("Content-Type", "application/json")
		e := json.NewEncoder(w)
		err := e.Encode(types.ServerError{Code: -1, Text: "daemon didn't complete your request"})
		c.Assert(err, Equals, nil)
	}))
	defer server.Close()

	cli := NewTestClient(server.URL, c)

	lbSvcReceived, err := cli.SVCGet(fe.L3n4Addr)

	c.Assert(strings.Contains(err.Error(), "daemon didn't complete your request"), Equals, true)
	var nilFe *types.LBSVC
	c.Assert(lbSvcReceived, DeepEquals, nilFe)
}
Exemplo n.º 3
0
func (s *CiliumNetClientSuite) TestSVCAddOK(c *C) {

	fe, err := types.NewL3n4AddrID(types.TCP, randomAddr1, 1984, 2016)
	c.Assert(err, IsNil)
	wantLBSVC := types.LBSVC{
		FE: *fe,
		BES: []types.L3n4Addr{
			revNat1,
			revNat2,
		},
	}
	addRevNAT := true

	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		c.Assert(r.Method, Equals, "POST")
		c.Assert(r.URL.Path, Equals, "/lb/service")
		var receivedLBSVC types.LBSVC
		err := json.NewDecoder(r.Body).Decode(&receivedLBSVC)
		c.Assert(err, Equals, nil)
		c.Assert(receivedLBSVC, DeepEquals, wantLBSVC)
		w.WriteHeader(http.StatusCreated)
	}))
	defer server.Close()

	cli := NewTestClient(server.URL, c)

	err = cli.SVCAdd(wantLBSVC.FE, wantLBSVC.BES, addRevNAT)

	c.Assert(err, Equals, nil)
}
Exemplo n.º 4
0
func (s *CiliumNetClientSuite) TestSVCGetOK(c *C) {
	fe, err := types.NewL3n4AddrID(types.TCP, randomAddr1, 1984, 2016)
	c.Assert(err, IsNil)
	wantLBSVC := types.LBSVC{
		FE: *fe,
		BES: []types.L3n4Addr{
			revNat1,
			revNat2,
		},
	}
	feSHA256Sum, err := fe.SHA256Sum()
	c.Assert(err, IsNil)

	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		c.Assert(r.Method, Equals, "GET")
		c.Assert(r.URL.Path, Equals, "/lb/service/"+feSHA256Sum)
		w.WriteHeader(http.StatusOK)
		err := json.NewEncoder(w).Encode(wantLBSVC)
		c.Assert(err, Equals, nil)
	}))
	defer server.Close()

	cli := NewTestClient(server.URL, c)

	lbSvcReceived, err := cli.SVCGet(fe.L3n4Addr)

	c.Assert(err, IsNil)
	c.Assert(*lbSvcReceived, DeepEquals, wantLBSVC)
}
Exemplo n.º 5
0
func (s *DaemonSuite) TestSVCAddIDOK(c *C) {
	feWant, err := types.NewL3n4AddrID(types.TCP, randomAddr1, 1984, 2016)
	c.Assert(err, IsNil)

	s.d.OnSVCAdd = func(fe types.L3n4AddrID, be []types.L3n4Addr, addRevNAT bool) error {
		c.Assert(fe, DeepEquals, *feWant)
		c.Assert(be, DeepEquals, bes)
		c.Assert(addRevNAT, Equals, false)
		return nil
	}

	err = s.c.SVCAdd(*feWant, bes, false)
	c.Assert(err, IsNil)
}
Exemplo n.º 6
0
func (s *DaemonSuite) TestSVCAddIDFail(c *C) {
	feWant, err := types.NewL3n4AddrID(types.TCP, randomAddr1, 1984, 2016)
	c.Assert(err, IsNil)

	s.d.OnSVCAdd = func(fe types.L3n4AddrID, be []types.L3n4Addr, addRevNAT bool) error {
		c.Assert(fe, DeepEquals, *feWant)
		c.Assert(be, DeepEquals, bes)
		c.Assert(addRevNAT, Equals, true)
		return errors.New("Unable to read lbmap")
	}

	err = s.c.SVCAdd(*feWant, bes, true)
	c.Assert(err, ErrorMatches, ".*Unable to read lbmap.*")
}
Exemplo n.º 7
0
func (s *DaemonSuite) TestSVCDeleteBySHA256SumFail(c *C) {
	feWant, err := types.NewL3n4AddrID(types.TCP, randomAddr1, 1984, 2016)
	c.Assert(err, IsNil)
	feL3n4SHA256Want, err := feWant.SHA256Sum()
	c.Assert(err, IsNil)

	s.d.OnSVCDeleteBySHA256Sum = func(feL3n4SHA256Sum string) error {
		c.Assert(feL3n4SHA256Sum, Equals, feL3n4SHA256Want)
		return errors.New("Unable to read lbmap")
	}

	err = s.c.SVCDeleteBySHA256Sum(feL3n4SHA256Want)
	c.Assert(err, ErrorMatches, ".*Unable to read lbmap.*")
}
Exemplo n.º 8
0
func (s *DaemonSuite) TestSVCDeleteBySHA256SumOK(c *C) {
	feWant, err := types.NewL3n4AddrID(types.TCP, randomAddr1, 1984, 2016)
	c.Assert(err, IsNil)
	feL3n4SHA256Want, err := feWant.SHA256Sum()
	c.Assert(err, IsNil)

	s.d.OnSVCDeleteBySHA256Sum = func(feL3n4SHA256Sum string) error {
		c.Assert(feL3n4SHA256Sum, Equals, feL3n4SHA256Want)
		return nil
	}

	err = s.c.SVCDeleteBySHA256Sum(feL3n4SHA256Want)
	c.Assert(err, IsNil)
}
Exemplo n.º 9
0
func (s *DaemonSuite) TestSVCGetFail(c *C) {
	feWant, err := types.NewL3n4AddrID(types.TCP, randomAddr1, 1984, 2016)
	c.Assert(err, IsNil)
	feL3n4SHA256Want, err := feWant.SHA256Sum()
	c.Assert(err, IsNil)

	s.d.OnSVCGetBySHA256Sum = func(feL3n4SHA256Sum string) (*types.LBSVC, error) {
		c.Assert(feL3n4SHA256Sum, Equals, feL3n4SHA256Want)
		return nil, errors.New("Unable to read lbmap")
	}

	lbSvcReceived, err := s.c.SVCGet(feWant.L3n4Addr)
	c.Assert(err, ErrorMatches, ".*Unable to read lbmap.*")
	c.Assert(lbSvcReceived, IsNil)
}
Exemplo n.º 10
0
func (s *DaemonSuite) TestSVCGetBySHA256SumOK(c *C) {
	feWant, err := types.NewL3n4AddrID(types.TCP, randomAddr1, 1984, 2016)
	c.Assert(err, IsNil)
	feL3n4SHA256Want, err := feWant.SHA256Sum()
	c.Assert(err, IsNil)

	lbSVCWant := types.LBSVC{
		FE:  *feWant,
		BES: bes,
	}

	s.d.OnSVCGetBySHA256Sum = func(feL3n4SHA256Sum string) (*types.LBSVC, error) {
		c.Assert(feL3n4SHA256Sum, Equals, feL3n4SHA256Want)
		return &lbSVCWant, nil
	}

	lbSvcReceived, err := s.c.SVCGetBySHA256Sum(feL3n4SHA256Want)
	c.Assert(err, IsNil)
	c.Assert(*lbSvcReceived, DeepEquals, lbSVCWant)
}
Exemplo n.º 11
0
func (s *DaemonSuite) TestSVCDumpOK(c *C) {
	feWant, err := types.NewL3n4AddrID(types.TCP, randomAddr1, 1984, 2016)
	c.Assert(err, IsNil)
	wantLBSVC := []types.LBSVC{
		{
			FE:  *feWant,
			BES: bes,
		},
		{
			FE:  *feWant,
			BES: bes,
		},
	}
	s.d.OnSVCDump = func() ([]types.LBSVC, error) {
		return wantLBSVC, nil
	}

	lbSVCReceived, err := s.c.SVCDump()
	c.Assert(err, IsNil)
	c.Assert(wantLBSVC, DeepEquals, lbSVCReceived)
}
Exemplo n.º 12
0
func (d *Daemon) addK8sSVCs(svc types.K8sServiceNamespace, svcInfo *types.K8sServiceInfo, se *types.K8sServiceEndpoint) {
	isSvcIPv4 := svcInfo.FEIP.To4() != nil
	if !areIPsConsistent(d.conf.IPv4Enabled, isSvcIPv4, svc, se) {
		return
	}

	uniqPorts := getUniqPorts(svcInfo.Ports)

	for fePortName, fePort := range svcInfo.Ports {
		if !uniqPorts[fePort.Port] {
			continue
		}

		k8sBEPort := se.Ports[fePortName]
		uniqPorts[fePort.Port] = false

		if fePort.ID == 0 {
			feAddr, err := types.NewL3n4Addr(fePort.Protocol, svcInfo.FEIP, fePort.Port)
			if err != nil {
				log.Errorf("Error while creating a new L3n4Addr: %s. Ignoring service...", err)
				continue
			}
			feAddrID, err := d.PutL3n4Addr(*feAddr, 0)
			if err != nil {
				log.Errorf("Error while getting a new service ID: %s. Ignoring service %v...", err, feAddr)
				continue
			}
			log.Debugf("Got feAddr ID %d for service %+v", feAddrID.ID, svc)
			fePort.ID = feAddrID.ID
		}

		isServerPresent := false
		besValues := []types.L3n4Addr{}

		if k8sBEPort != nil {
			for epIP := range se.BEIPs {
				bePort := types.L3n4Addr{
					IP:     net.ParseIP(epIP),
					L4Addr: *k8sBEPort,
				}
				besValues = append(besValues, bePort)

				if !isServerPresent {
					d.ipamConf.AllocatorMutex.RLock()
					if isSvcIPv4 && d.conf.IPv4Enabled {
						isServerPresent = d.conf.NodeAddress.IPv4Address.IP().Equal(bePort.IP) ||
							d.ipamConf.IPv4Allocator.Has(bePort.IP)
					} else {
						isServerPresent = d.conf.NodeAddress.IPv6Address.HostIP().Equal(bePort.IP) ||
							d.ipamConf.IPv6Allocator.Has(bePort.IP)
					}
					d.ipamConf.AllocatorMutex.RUnlock()
				}
			}
		}

		fe, err := types.NewL3n4AddrID(fePort.Protocol, svcInfo.FEIP, fePort.Port, fePort.ID)
		if err != nil {
			log.Errorf("Error while creating a New L3n4AddrID: %s. Ignoring service %v...", err, svcInfo)
			continue
		}
		if err := d.svcAdd(*fe, besValues, isServerPresent); err != nil {
			log.Errorf("Error while inserting service in LB map: %s", err)
		}
	}
}