Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
e07a944c
Commit
e07a944c
authored
Mar 12, 2018
by
Ashley Gau
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add Get/Set methods, mutex on instanceGroupAttrs.
parent
7fff54cf
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
81 additions
and
60 deletions
+81
-60
mock.go
pkg/cloudprovider/providers/gce/cloud/mock/mock.go
+76
-60
gce_loadbalancer_utils_test.go
...loudprovider/providers/gce/gce_loadbalancer_utils_test.go
+5
-0
No files found.
pkg/cloudprovider/providers/gce/cloud/mock/mock.go
View file @
e07a944c
...
@@ -28,6 +28,7 @@ import (
...
@@ -28,6 +28,7 @@ import (
"encoding/json"
"encoding/json"
"fmt"
"fmt"
"net/http"
"net/http"
"sync"
alpha
"google.golang.org/api/compute/v0.alpha"
alpha
"google.golang.org/api/compute/v0.alpha"
beta
"google.golang.org/api/compute/v0.beta"
beta
"google.golang.org/api/compute/v0.beta"
...
@@ -228,44 +229,90 @@ func InsertAlphaAddressHook(ctx context.Context, key *meta.Key, obj *alpha.Addre
...
@@ -228,44 +229,90 @@ func InsertAlphaAddressHook(ctx context.Context, key *meta.Key, obj *alpha.Addre
return
convertAndInsertAlphaAddress
(
key
,
obj
,
m
.
Objects
,
meta
.
VersionAlpha
,
projectID
)
return
convertAndInsertAlphaAddress
(
key
,
obj
,
m
.
Objects
,
meta
.
VersionAlpha
,
projectID
)
}
}
// Map from InstanceGroup key to list of Instances
// InstanceGroupAttributes maps from InstanceGroup key to a map of Instances
type
instanceGroupAttributes
struct
{
type
InstanceGroupAttributes
struct
{
instanceMap
map
[
meta
.
Key
][]
*
ga
.
InstanceWithNamedPorts
InstanceMap
map
[
meta
.
Key
]
map
[
string
]
*
ga
.
InstanceWithNamedPorts
Lock
*
sync
.
Mutex
}
}
// AddInstances
Hook mocks adding instances from an InstanceGroup
// AddInstances
adds a list of Instances passed by InstanceReference
func
AddInstancesHook
(
ctx
context
.
Context
,
key
*
meta
.
Key
,
req
*
ga
.
InstanceGroupsAddInstancesRequest
,
m
*
cloud
.
MockInstanceGroups
)
error
{
func
(
igAttrs
*
InstanceGroupAttributes
)
AddInstances
(
key
*
meta
.
Key
,
instanceRefs
[]
*
ga
.
InstanceReference
)
error
{
_
,
err
:=
m
.
Get
(
ctx
,
key
)
igAttrs
.
Lock
.
Lock
(
)
if
err
!=
nil
{
defer
igAttrs
.
Lock
.
Unlock
()
return
&
googleapi
.
Error
{
Code
:
http
.
StatusNotFound
,
instancesWithNamedPorts
,
ok
:=
igAttrs
.
InstanceMap
[
*
key
]
Message
:
fmt
.
Sprintf
(
"Key: %s was not found in InstanceGroups"
,
key
.
String
()),
if
!
ok
{
}
instancesWithNamedPorts
=
make
(
map
[
string
]
*
ga
.
InstanceWithNamedPorts
)
}
}
if
m
.
X
==
nil
{
for
_
,
instance
:=
range
instanceRefs
{
m
.
X
=
instanceGroupAttribute
s
{
iWithPort
:=
&
ga
.
InstanceWithNamedPort
s
{
instanceMap
:
make
(
map
[
meta
.
Key
][]
*
ga
.
InstanceWithNamedPorts
)
,
Instance
:
instance
.
Instance
,
}
}
instancesWithNamedPorts
[
instance
.
Instance
]
=
iWithPort
}
}
var
attrs
instanceGroupAttributes
igAttrs
.
InstanceMap
[
*
key
]
=
instancesWithNamedPorts
attrs
=
m
.
X
.
(
instanceGroupAttributes
)
return
nil
}
instances
,
ok
:=
attrs
.
instanceMap
[
*
key
]
// RemoveInstances removes a list of Instances passed by InstanceReference
func
(
igAttrs
*
InstanceGroupAttributes
)
RemoveInstances
(
key
*
meta
.
Key
,
instanceRefs
[]
*
ga
.
InstanceReference
)
error
{
igAttrs
.
Lock
.
Lock
()
defer
igAttrs
.
Lock
.
Unlock
()
instancesWithNamedPorts
,
ok
:=
igAttrs
.
InstanceMap
[
*
key
]
if
!
ok
{
if
!
ok
{
instances
=
[]
*
ga
.
InstanceWithNamedPorts
{}
instances
WithNamedPorts
=
make
(
map
[
string
]
*
ga
.
InstanceWithNamedPorts
)
}
}
for
_
,
instance
:=
range
req
.
Instances
{
for
_
,
instanceToRemove
:=
range
instanceRefs
{
iWithPort
:=
&
ga
.
InstanceWithNamedPorts
{
if
_
,
ok
:=
instancesWithNamedPorts
[
instanceToRemove
.
Instance
];
ok
{
Instance
:
instance
.
Instance
,
delete
(
instancesWithNamedPorts
,
instanceToRemove
.
Instance
)
}
else
{
return
&
googleapi
.
Error
{
Code
:
http
.
StatusBadRequest
,
Message
:
fmt
.
Sprintf
(
"%s is not a member of %s"
,
instanceToRemove
.
Instance
,
key
.
String
()),
}
}
}
}
igAttrs
.
InstanceMap
[
*
key
]
=
instancesWithNamedPorts
return
nil
}
// List gets a list of InstanceWithNamedPorts
func
(
igAttrs
*
InstanceGroupAttributes
)
List
(
key
*
meta
.
Key
)
[]
*
ga
.
InstanceWithNamedPorts
{
igAttrs
.
Lock
.
Lock
()
defer
igAttrs
.
Lock
.
Unlock
()
instancesWithNamedPorts
,
ok
:=
igAttrs
.
InstanceMap
[
*
key
]
if
!
ok
{
instancesWithNamedPorts
=
make
(
map
[
string
]
*
ga
.
InstanceWithNamedPorts
)
}
var
instanceList
[]
*
ga
.
InstanceWithNamedPorts
for
_
,
val
:=
range
instancesWithNamedPorts
{
instanceList
=
append
(
instanceList
,
val
)
}
return
instanceList
}
instances
=
append
(
instances
,
iWithPort
)
// AddInstancesHook mocks adding instances from an InstanceGroup
func
AddInstancesHook
(
ctx
context
.
Context
,
key
*
meta
.
Key
,
req
*
ga
.
InstanceGroupsAddInstancesRequest
,
m
*
cloud
.
MockInstanceGroups
)
error
{
_
,
err
:=
m
.
Get
(
ctx
,
key
)
if
err
!=
nil
{
return
&
googleapi
.
Error
{
Code
:
http
.
StatusNotFound
,
Message
:
fmt
.
Sprintf
(
"Key: %s was not found in InstanceGroups"
,
key
.
String
()),
}
}
}
attrs
.
instanceMap
[
*
key
]
=
instances
var
attrs
InstanceGroupAttributes
attrs
=
m
.
X
.
(
InstanceGroupAttributes
)
attrs
.
AddInstances
(
key
,
req
.
Instances
)
m
.
X
=
attrs
m
.
X
=
attrs
return
nil
return
nil
}
}
...
@@ -280,17 +327,9 @@ func ListInstancesHook(ctx context.Context, key *meta.Key, req *ga.InstanceGroup
...
@@ -280,17 +327,9 @@ func ListInstancesHook(ctx context.Context, key *meta.Key, req *ga.InstanceGroup
}
}
}
}
if
m
.
X
==
nil
{
var
attrs
InstanceGroupAttributes
m
.
X
=
instanceGroupAttributes
{
instanceMap
:
make
(
map
[
meta
.
Key
][]
*
ga
.
InstanceWithNamedPorts
)}
attrs
=
m
.
X
.
(
InstanceGroupAttributes
)
}
instances
:=
attrs
.
List
(
key
)
var
attrs
instanceGroupAttributes
attrs
=
m
.
X
.
(
instanceGroupAttributes
)
instances
,
ok
:=
attrs
.
instanceMap
[
*
key
]
if
!
ok
{
instances
=
[]
*
ga
.
InstanceWithNamedPorts
{}
}
return
instances
,
nil
return
instances
,
nil
}
}
...
@@ -305,32 +344,9 @@ func RemoveInstancesHook(ctx context.Context, key *meta.Key, req *ga.InstanceGro
...
@@ -305,32 +344,9 @@ func RemoveInstancesHook(ctx context.Context, key *meta.Key, req *ga.InstanceGro
}
}
}
}
if
m
.
X
==
nil
{
var
attrs
InstanceGroupAttributes
m
.
X
=
instanceGroupAttributes
{
attrs
=
m
.
X
.
(
InstanceGroupAttributes
)
instanceMap
:
make
(
map
[
meta
.
Key
][]
*
ga
.
InstanceWithNamedPorts
),
attrs
.
RemoveInstances
(
key
,
req
.
Instances
)
}
}
var
attrs
instanceGroupAttributes
attrs
=
m
.
X
.
(
instanceGroupAttributes
)
instances
,
ok
:=
attrs
.
instanceMap
[
*
key
]
if
!
ok
{
instances
=
[]
*
ga
.
InstanceWithNamedPorts
{}
}
for
_
,
instanceToRemove
:=
range
req
.
Instances
{
for
i
,
instance
:=
range
instances
{
if
instanceToRemove
.
Instance
==
instance
.
Instance
{
// Delete instance from instances without preserving order
instances
[
i
]
=
instances
[
len
(
instances
)
-
1
]
instances
=
instances
[
:
len
(
instances
)
-
1
]
break
}
}
}
attrs
.
instanceMap
[
*
key
]
=
instances
m
.
X
=
attrs
m
.
X
=
attrs
return
nil
return
nil
}
}
...
...
pkg/cloudprovider/providers/gce/gce_loadbalancer_utils_test.go
View file @
e07a944c
...
@@ -23,6 +23,7 @@ package gce
...
@@ -23,6 +23,7 @@ package gce
import
(
import
(
"fmt"
"fmt"
"net/http"
"net/http"
"sync"
compute
"google.golang.org/api/compute/v1"
compute
"google.golang.org/api/compute/v1"
...
@@ -102,6 +103,10 @@ func fakeGCECloud(vals TestClusterValues) (*GCECloud, error) {
...
@@ -102,6 +103,10 @@ func fakeGCECloud(vals TestClusterValues) (*GCECloud, error) {
c
.
MockAddresses
.
InsertHook
=
mock
.
InsertAddressHook
c
.
MockAddresses
.
InsertHook
=
mock
.
InsertAddressHook
c
.
MockAlphaAddresses
.
InsertHook
=
mock
.
InsertAlphaAddressHook
c
.
MockAlphaAddresses
.
InsertHook
=
mock
.
InsertAlphaAddressHook
c
.
MockInstanceGroups
.
X
=
mock
.
InstanceGroupAttributes
{
InstanceMap
:
make
(
map
[
meta
.
Key
]
map
[
string
]
*
compute
.
InstanceWithNamedPorts
),
Lock
:
&
sync
.
Mutex
{},
}
c
.
MockInstanceGroups
.
AddInstancesHook
=
mock
.
AddInstancesHook
c
.
MockInstanceGroups
.
AddInstancesHook
=
mock
.
AddInstancesHook
c
.
MockInstanceGroups
.
RemoveInstancesHook
=
mock
.
RemoveInstancesHook
c
.
MockInstanceGroups
.
RemoveInstancesHook
=
mock
.
RemoveInstancesHook
c
.
MockInstanceGroups
.
ListInstancesHook
=
mock
.
ListInstancesHook
c
.
MockInstanceGroups
.
ListInstancesHook
=
mock
.
ListInstancesHook
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment