Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

service: Skip upsert of service for disabled IP family #15026

Merged
merged 1 commit into from
Mar 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pkg/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,16 @@ func (s *Service) UpsertService(params *lb.SVC) (bool, lb.ID, error) {
option.EnableSVCSourceRangeCheck)
}

ipv6Svc := params.Frontend.IsIPv6()
if ipv6Svc && !option.Config.EnableIPv6 {
err := fmt.Errorf("Unable to upsert service %s as IPv6 is disabled", params.Frontend.L3n4Addr.String())
return false, lb.ID(0), err
}
if !ipv6Svc && !option.Config.EnableIPv4 {
err := fmt.Errorf("Unable to upsert service %s as IPv4 is disabled", params.Frontend.L3n4Addr.String())
return false, lb.ID(0), err
}

// In case we do DSR + IPIP, then it's required that the backends use
// the same destination port as the frontend service.
if option.Config.NodePortMode == option.NodePortModeDSR &&
Expand Down
56 changes: 56 additions & 0 deletions pkg/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type ManagerTestSuite struct {
prevOptionLBSourceRanges bool
prevOptionNPAlgo string
prevOptionDPMode string
ipv6 bool
}

var _ = Suite(&ManagerTestSuite{})
Expand All @@ -62,6 +63,8 @@ func (m *ManagerTestSuite) SetUpTest(c *C) {

m.prevOptionNPAlgo = option.Config.NodePortAlg
m.prevOptionDPMode = option.Config.DatapathMode

m.ipv6 = option.Config.EnableIPv6
}

func (m *ManagerTestSuite) TearDownTest(c *C) {
Expand All @@ -71,11 +74,13 @@ func (m *ManagerTestSuite) TearDownTest(c *C) {
option.Config.EnableSVCSourceRangeCheck = m.prevOptionLBSourceRanges
option.Config.NodePortAlg = m.prevOptionNPAlgo
option.Config.DatapathMode = m.prevOptionDPMode
option.Config.EnableIPv6 = m.ipv6
}

var (
frontend1 = *lb.NewL3n4AddrID(lb.TCP, net.ParseIP("1.1.1.1"), 80, lb.ScopeExternal, 0)
frontend2 = *lb.NewL3n4AddrID(lb.TCP, net.ParseIP("1.1.1.2"), 80, lb.ScopeExternal, 0)
frontend3 = *lb.NewL3n4AddrID(lb.TCP, net.ParseIP("f00d::1"), 80, lb.ScopeExternal, 0)
backends1 = []lb.Backend{
*lb.NewBackend(0, lb.TCP, net.ParseIP("10.0.0.1"), 8080),
*lb.NewBackend(0, lb.TCP, net.ParseIP("10.0.0.2"), 8080),
Expand All @@ -84,9 +89,22 @@ var (
*lb.NewBackend(0, lb.TCP, net.ParseIP("10.0.0.2"), 8080),
*lb.NewBackend(0, lb.TCP, net.ParseIP("10.0.0.3"), 8080),
}
backends3 = []lb.Backend{
*lb.NewBackend(0, lb.TCP, net.ParseIP("fd00::2"), 8080),
*lb.NewBackend(0, lb.TCP, net.ParseIP("fd00::3"), 8080),
}
)

func (m *ManagerTestSuite) TestUpsertAndDeleteService(c *C) {
m.testUpsertAndDeleteService(c)
}

func (m *ManagerTestSuite) TestUpsertAndDeleteServiceWithoutIPv6(c *C) {
option.Config.EnableIPv6 = false
m.testUpsertAndDeleteService(c)
}

func (m *ManagerTestSuite) testUpsertAndDeleteService(c *C) {
// Should create a new service with two backends and session affinity
p := &lb.SVC{
Frontend: frontend1,
Expand Down Expand Up @@ -178,6 +196,44 @@ func (m *ManagerTestSuite) TestUpsertAndDeleteService(c *C) {
c.Assert(len(m.lbmap.AffinityMatch[uint16(id2)]), Equals, 2)
c.Assert(len(m.lbmap.SourceRanges[uint16(id2)]), Equals, 2)

// Should add IPv6 service only if IPv6 is enabled
c.Assert(err, IsNil)
cidr1, err = cidr.ParseCIDR("fd00::/8")
c.Assert(err, IsNil)
p3 := &lb.SVC{
Frontend: frontend3,
Backends: backends3,
Type: lb.SVCTypeLoadBalancer,
TrafficPolicy: lb.SVCTrafficPolicyCluster,
SessionAffinity: true,
SessionAffinityTimeoutSec: 300,
Name: "svc3",
Namespace: "ns3",
LoadBalancerSourceRanges: []*cidr.CIDR{cidr1},
}
created, id3, err := m.svc.UpsertService(p3)
if option.Config.EnableIPv6 {
c.Assert(err, IsNil)
c.Assert(created, Equals, true)
c.Assert(id3, Equals, lb.ID(3))
c.Assert(len(m.lbmap.ServiceByID[uint16(id3)].Backends), Equals, 2)
c.Assert(len(m.lbmap.BackendByID), Equals, 4)
c.Assert(m.svc.svcByID[id3].svcName, Equals, "svc3")
c.Assert(m.svc.svcByID[id3].svcNamespace, Equals, "ns3")
c.Assert(len(m.lbmap.AffinityMatch[uint16(id3)]), Equals, 2)
c.Assert(len(m.lbmap.SourceRanges[uint16(id3)]), Equals, 1)

// Should remove the IPv6 service
found, err := m.svc.DeleteServiceByID(lb.ServiceID(id3))
c.Assert(err, IsNil)
c.Assert(found, Equals, true)
} else {
c.Assert(err, ErrorMatches, "Unable to upsert service .+ as IPv6 is disabled")
c.Assert(created, Equals, false)
}
c.Assert(len(m.lbmap.ServiceByID), Equals, 2)
c.Assert(len(m.lbmap.BackendByID), Equals, 2)

// Should remove the service and the backend, but keep another service and
// its backends. Also, should remove the affinity match.
found, err := m.svc.DeleteServiceByID(lb.ServiceID(id1))
Expand Down