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

move neg type into syncerKey #922

Merged
merged 1 commit into from
Oct 30, 2019
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
14 changes: 7 additions & 7 deletions pkg/neg/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"fmt"
"sync"

v1 "k8s.io/api/core/v1"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/sets"
Expand Down Expand Up @@ -114,15 +114,9 @@ func (manager *syncerManager) EnsureSyncers(namespace, name string, newPorts neg
syncerKey := getSyncerKey(namespace, name, svcPort, portInfo)
syncer, ok := manager.syncerMap[syncerKey]
if !ok {
networkEndpointType := negtypes.VMNetworkEndpointType
if flags.F.EnableNonGCPMode {
networkEndpointType = negtypes.NonGCPPrivateEndpointType
}

syncer = negsyncer.NewTransactionSyncer(
syncerKey,
portInfo.NegName,
networkEndpointType,
manager.recorder,
manager.cloud,
manager.zoneGetter,
Expand Down Expand Up @@ -311,13 +305,19 @@ func (manager *syncerManager) ensureDeleteNetworkEndpointGroup(name, zone string

// getSyncerKey encodes a service namespace, name, service port and targetPort into a string key
func getSyncerKey(namespace, name string, servicePortKey negtypes.PortInfoMapKey, portInfo negtypes.PortInfo) negtypes.NegSyncerKey {
networkEndpointType := negtypes.VmIpPortEndpointType
if flags.F.EnableNonGCPMode {
networkEndpointType = negtypes.NonGCPPrivateEndpointType
}

return negtypes.NegSyncerKey{
Namespace: namespace,
Name: name,
Port: servicePortKey.ServicePort,
TargetPort: portInfo.TargetPort,
Subset: servicePortKey.Subset,
SubsetLabels: portInfo.SubsetLabels,
NegType: networkEndpointType,
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/neg/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ func TestGarbageCollectionNEG(t *testing.T) {
t.Fatalf("Failed to ensure syncer: %v", err)
}

for _, networkEndpointType := range []negtypes.NetworkEndpointType{negtypes.VMNetworkEndpointType, negtypes.NonGCPPrivateEndpointType} {
for _, networkEndpointType := range []negtypes.NetworkEndpointType{negtypes.VmIpPortEndpointType, negtypes.NonGCPPrivateEndpointType} {
negName := manager.namer.NEG("test", "test", 80)
manager.cloud.CreateNetworkEndpointGroup(&compute.NetworkEndpointGroup{
Name: negName,
Expand Down
31 changes: 14 additions & 17 deletions pkg/neg/syncers/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ type transactionSyncer struct {
// metadata
negtypes.NegSyncerKey
negName string
// The type of the network endpoints in this NEG.
networkEndpointType negtypes.NetworkEndpointType

// syncer provides syncer life cycle interfaces
syncer negtypes.NegSyncer
Expand Down Expand Up @@ -67,21 +65,20 @@ type transactionSyncer struct {
reflector readiness.Reflector
}

func NewTransactionSyncer(negSyncerKey negtypes.NegSyncerKey, networkEndpointGroupName string, networkEndpointType negtypes.NetworkEndpointType, recorder record.EventRecorder, cloud negtypes.NetworkEndpointGroupCloud, zoneGetter negtypes.ZoneGetter, podLister cache.Indexer, serviceLister cache.Indexer, endpointLister cache.Indexer, reflector readiness.Reflector) negtypes.NegSyncer {
func NewTransactionSyncer(negSyncerKey negtypes.NegSyncerKey, networkEndpointGroupName string, recorder record.EventRecorder, cloud negtypes.NetworkEndpointGroupCloud, zoneGetter negtypes.ZoneGetter, podLister cache.Indexer, serviceLister cache.Indexer, endpointLister cache.Indexer, reflector readiness.Reflector) negtypes.NegSyncer {
// TransactionSyncer implements the syncer core
ts := &transactionSyncer{
NegSyncerKey: negSyncerKey,
negName: networkEndpointGroupName,
networkEndpointType: networkEndpointType,
needInit: true,
transactions: NewTransactionTable(),
podLister: podLister,
serviceLister: serviceLister,
endpointLister: endpointLister,
recorder: recorder,
cloud: cloud,
zoneGetter: zoneGetter,
reflector: reflector,
NegSyncerKey: negSyncerKey,
negName: networkEndpointGroupName,
needInit: true,
transactions: NewTransactionTable(),
podLister: podLister,
serviceLister: serviceLister,
endpointLister: endpointLister,
recorder: recorder,
cloud: cloud,
zoneGetter: zoneGetter,
reflector: reflector,
}
// Syncer implements life cycle logic
syncer := newSyncer(negSyncerKey, networkEndpointGroupName, serviceLister, recorder, ts)
Expand Down Expand Up @@ -133,7 +130,7 @@ func (s *transactionSyncer) syncInternal() error {
return nil
}

targetMap, endpointPodMap, err := toZoneNetworkEndpointMap(ep.(*apiv1.Endpoints), s.zoneGetter, s.TargetPort, s.podLister, s.NegSyncerKey.SubsetLabels, s.networkEndpointType)
targetMap, endpointPodMap, err := toZoneNetworkEndpointMap(ep.(*apiv1.Endpoints), s.zoneGetter, s.TargetPort, s.podLister, s.NegSyncerKey.SubsetLabels, s.NegSyncerKey.NegType)
if err != nil {
return err
}
Expand Down Expand Up @@ -181,7 +178,7 @@ func (s *transactionSyncer) ensureNetworkEndpointGroups() error {

var errList []error
for _, zone := range zones {
if err := ensureNetworkEndpointGroup(s.Namespace, s.Name, s.negName, zone, s.NegSyncerKey.String(), s.networkEndpointType, s.cloud, s.serviceLister, s.recorder); err != nil {
if err := ensureNetworkEndpointGroup(s.Namespace, s.Name, s.negName, zone, s.NegSyncerKey.String(), s.NegSyncerKey.NegType, s.cloud, s.serviceLister, s.recorder); err != nil {
errList = append(errList, err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/neg/syncers/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,7 @@ func newTestTransactionSyncer(fakeGCE negtypes.NetworkEndpointGroupCloud) (negty
svcPort := negtypes.NegSyncerKey{
Namespace: testNamespace,
Name: testService,
NegType: negtypes.VmIpPortEndpointType,
Port: 80,
TargetPort: "8080",
}
Expand All @@ -1091,7 +1092,6 @@ func newTestTransactionSyncer(fakeGCE negtypes.NetworkEndpointGroupCloud) (negty

negsyncer := NewTransactionSyncer(svcPort,
testNegName,
negtypes.VMNetworkEndpointType,
record.NewFakeRecorder(100),
fakeGCE,
negtypes.NewFakeZoneGetter(),
Expand Down
8 changes: 4 additions & 4 deletions pkg/neg/syncers/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ func TestEnsureNetworkEndpointGroup(t *testing.T) {
description: "Create NEG of type GCE_VM_IP_PORT",
negName: "gcp-neg",
enableNonGCPMode: false,
networkEndpointType: negtypes.VMNetworkEndpointType,
networkEndpointType: negtypes.VmIpPortEndpointType,
expectedSubnetwork: testSubnetwork,
},
{
Expand Down Expand Up @@ -418,7 +418,7 @@ func TestToZoneNetworkEndpointMapUtil(t *testing.T) {
targetPort: "8888",
endpointSets: map[string]negtypes.NetworkEndpointSet{},
expectMap: negtypes.EndpointPodMap{},
networkEndpointType: negtypes.VMNetworkEndpointType,
networkEndpointType: negtypes.VmIpPortEndpointType,
},
{
desc: "target port number",
Expand All @@ -439,7 +439,7 @@ func TestToZoneNetworkEndpointMapUtil(t *testing.T) {
networkEndpointFromEncodedEndpoint("10.100.3.1||instance3||80"): types.NamespacedName{Namespace: testServiceNamespace, Name: "pod4"},
networkEndpointFromEncodedEndpoint("10.100.1.3||instance1||80"): types.NamespacedName{Namespace: testServiceNamespace, Name: "pod5"},
},
networkEndpointType: negtypes.VMNetworkEndpointType,
networkEndpointType: negtypes.VmIpPortEndpointType,
},
{
desc: "named target port",
Expand All @@ -460,7 +460,7 @@ func TestToZoneNetworkEndpointMapUtil(t *testing.T) {
networkEndpointFromEncodedEndpoint("10.100.3.2||instance3||8081"): types.NamespacedName{Namespace: testServiceNamespace, Name: "pod10"},
networkEndpointFromEncodedEndpoint("10.100.4.2||instance4||8081"): types.NamespacedName{Namespace: testServiceNamespace, Name: "pod11"},
},
networkEndpointType: negtypes.VMNetworkEndpointType,
networkEndpointType: negtypes.VmIpPortEndpointType,
},
{
desc: "Non-GCP network endpoints",
Expand Down
5 changes: 4 additions & 1 deletion pkg/neg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
type NetworkEndpointType string

const (
VMNetworkEndpointType = NetworkEndpointType("GCE_VM_IP_PORT")
VmIpPortEndpointType = NetworkEndpointType("GCE_VM_IP_PORT")
NonGCPPrivateEndpointType = NetworkEndpointType("NON_GCP_PRIVATE_IP_PORT")
)

Expand Down Expand Up @@ -204,6 +204,9 @@ type NegSyncerKey struct {

// Subset label, should set together with Subset.
SubsetLabels string

// NegType is the type of the network endpoints in this NEG.
NegType NetworkEndpointType
}

func (key NegSyncerKey) String() string {
Expand Down