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
96c244ea
Commit
96c244ea
authored
Jun 16, 2015
by
Brendan Burns
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #9848 from cjcullen/fwfix
Fix mislooping in ssh.go. Add retries to AddSSHKeys.
parents
27704bea
48f672af
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
72 additions
and
52 deletions
+72
-52
gce.go
pkg/cloudprovider/gce/gce.go
+40
-30
master.go
pkg/master/master.go
+3
-3
ssh.go
pkg/util/ssh.go
+29
-19
No files found.
pkg/cloudprovider/gce/gce.go
View file @
96c244ea
...
@@ -32,6 +32,7 @@ import (
...
@@ -32,6 +32,7 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/resource"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/resource"
"github.com/GoogleCloudPlatform/kubernetes/pkg/cloudprovider"
"github.com/GoogleCloudPlatform/kubernetes/pkg/cloudprovider"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/wait"
"code.google.com/p/gcfg"
"code.google.com/p/gcfg"
compute
"code.google.com/p/google-api-go-client/compute/v1"
compute
"code.google.com/p/google-api-go-client/compute/v1"
...
@@ -483,37 +484,46 @@ func (gce *GCECloud) getInstanceByName(name string) (*compute.Instance, error) {
...
@@ -483,37 +484,46 @@ func (gce *GCECloud) getInstanceByName(name string) (*compute.Instance, error) {
}
}
func
(
gce
*
GCECloud
)
AddSSHKeyToAllInstances
(
user
string
,
keyData
[]
byte
)
error
{
func
(
gce
*
GCECloud
)
AddSSHKeyToAllInstances
(
user
string
,
keyData
[]
byte
)
error
{
project
,
err
:=
gce
.
service
.
Projects
.
Get
(
gce
.
projectID
)
.
Do
()
return
wait
.
Poll
(
2
*
time
.
Second
,
30
*
time
.
Second
,
func
()
(
bool
,
error
)
{
if
err
!=
nil
{
project
,
err
:=
gce
.
service
.
Projects
.
Get
(
gce
.
projectID
)
.
Do
()
return
err
if
err
!=
nil
{
}
glog
.
Errorf
(
"Could not get project: %v"
,
err
)
hostname
,
err
:=
os
.
Hostname
()
return
false
,
nil
if
err
!=
nil
{
return
err
}
keyString
:=
fmt
.
Sprintf
(
"%s:%s %s@%s"
,
user
,
strings
.
TrimSpace
(
string
(
keyData
)),
user
,
hostname
)
found
:=
false
for
_
,
item
:=
range
project
.
CommonInstanceMetadata
.
Items
{
if
item
.
Key
==
"sshKeys"
{
item
.
Value
=
addKey
(
item
.
Value
,
keyString
)
found
=
true
break
}
}
}
hostname
,
err
:=
os
.
Hostname
()
if
!
found
{
if
err
!=
nil
{
// This is super unlikely, so log.
glog
.
Errorf
(
"Could not get hostname: %v"
,
err
)
glog
.
Infof
(
"Failed to find sshKeys metadata, creating a new item"
)
return
false
,
nil
project
.
CommonInstanceMetadata
.
Items
=
append
(
project
.
CommonInstanceMetadata
.
Items
,
}
&
compute
.
MetadataItems
{
keyString
:=
fmt
.
Sprintf
(
"%s:%s %s@%s"
,
user
,
strings
.
TrimSpace
(
string
(
keyData
)),
user
,
hostname
)
Key
:
"sshKeys"
,
found
:=
false
Value
:
keyString
,
for
_
,
item
:=
range
project
.
CommonInstanceMetadata
.
Items
{
})
if
item
.
Key
==
"sshKeys"
{
}
item
.
Value
=
addKey
(
item
.
Value
,
keyString
)
op
,
err
:=
gce
.
service
.
Projects
.
SetCommonInstanceMetadata
(
gce
.
projectID
,
project
.
CommonInstanceMetadata
)
.
Do
()
found
=
true
if
err
!=
nil
{
break
return
err
}
}
}
return
gce
.
waitForGlobalOp
(
op
)
if
!
found
{
// This is super unlikely, so log.
glog
.
Infof
(
"Failed to find sshKeys metadata, creating a new item"
)
project
.
CommonInstanceMetadata
.
Items
=
append
(
project
.
CommonInstanceMetadata
.
Items
,
&
compute
.
MetadataItems
{
Key
:
"sshKeys"
,
Value
:
keyString
,
})
}
op
,
err
:=
gce
.
service
.
Projects
.
SetCommonInstanceMetadata
(
gce
.
projectID
,
project
.
CommonInstanceMetadata
)
.
Do
()
if
err
!=
nil
{
glog
.
Errorf
(
"Could not Set Metadata: %v"
,
err
)
return
false
,
nil
}
if
err
:=
gce
.
waitForGlobalOp
(
op
);
err
!=
nil
{
glog
.
Errorf
(
"Could not Set Metadata: %v"
,
err
)
return
false
,
nil
}
return
true
,
nil
})
}
}
func
addKey
(
metadataBefore
,
keyString
string
)
string
{
func
addKey
(
metadataBefore
,
keyString
string
)
string
{
...
...
pkg/master/master.go
View file @
96c244ea
...
@@ -210,7 +210,7 @@ type Master struct {
...
@@ -210,7 +210,7 @@ type Master struct {
InsecureHandler
http
.
Handler
InsecureHandler
http
.
Handler
// Used for secure proxy
// Used for secure proxy
tunnels
util
.
SSHTunnelList
tunnels
*
util
.
SSHTunnelList
tunnelsLock
sync
.
Mutex
tunnelsLock
sync
.
Mutex
installSSHKey
InstallSSHKey
installSSHKey
InstallSSHKey
}
}
...
@@ -772,7 +772,7 @@ func (m *Master) Dial(net, addr string) (net.Conn, error) {
...
@@ -772,7 +772,7 @@ func (m *Master) Dial(net, addr string) (net.Conn, error) {
}
}
func
(
m
*
Master
)
needToReplaceTunnels
(
addrs
[]
string
)
bool
{
func
(
m
*
Master
)
needToReplaceTunnels
(
addrs
[]
string
)
bool
{
if
len
(
m
.
tunnels
)
!=
len
(
addrs
)
{
if
m
.
tunnels
==
nil
||
m
.
tunnels
.
Len
(
)
!=
len
(
addrs
)
{
return
true
return
true
}
}
// TODO (cjcullen): This doesn't need to be n^2
// TODO (cjcullen): This doesn't need to be n^2
...
@@ -850,7 +850,7 @@ func (m *Master) setupSecureProxy(user, keyfile string) {
...
@@ -850,7 +850,7 @@ func (m *Master) setupSecureProxy(user, keyfile string) {
if
err
:=
m
.
loadTunnels
(
user
,
keyfile
);
err
!=
nil
{
if
err
:=
m
.
loadTunnels
(
user
,
keyfile
);
err
!=
nil
{
glog
.
Errorf
(
"Failed to load SSH Tunnels: %v"
,
err
)
glog
.
Errorf
(
"Failed to load SSH Tunnels: %v"
,
err
)
}
}
if
len
(
m
.
tunnels
)
!=
0
{
if
m
.
tunnels
!=
nil
&&
m
.
tunnels
.
Len
(
)
!=
0
{
// Sleep for 10 seconds if we have some tunnels.
// Sleep for 10 seconds if we have some tunnels.
// TODO (cjcullen): tunnels can lag behind actually existing nodes.
// TODO (cjcullen): tunnels can lag behind actually existing nodes.
time
.
Sleep
(
9
*
time
.
Second
)
time
.
Sleep
(
9
*
time
.
Second
)
...
...
pkg/util/ssh.go
View file @
96c244ea
...
@@ -207,9 +207,11 @@ type SSHTunnelEntry struct {
...
@@ -207,9 +207,11 @@ type SSHTunnelEntry struct {
Tunnel
*
SSHTunnel
Tunnel
*
SSHTunnel
}
}
type
SSHTunnelList
[]
SSHTunnelEntry
type
SSHTunnelList
struct
{
entries
[]
SSHTunnelEntry
}
func
MakeSSHTunnels
(
user
,
keyfile
string
,
addresses
[]
string
)
(
SSHTunnelList
,
error
)
{
func
MakeSSHTunnels
(
user
,
keyfile
string
,
addresses
[]
string
)
(
*
SSHTunnelList
,
error
)
{
tunnels
:=
[]
SSHTunnelEntry
{}
tunnels
:=
[]
SSHTunnelEntry
{}
for
ix
:=
range
addresses
{
for
ix
:=
range
addresses
{
addr
:=
addresses
[
ix
]
addr
:=
addresses
[
ix
]
...
@@ -219,18 +221,22 @@ func MakeSSHTunnels(user, keyfile string, addresses []string) (SSHTunnelList, er
...
@@ -219,18 +221,22 @@ func MakeSSHTunnels(user, keyfile string, addresses []string) (SSHTunnelList, er
}
}
tunnels
=
append
(
tunnels
,
SSHTunnelEntry
{
addr
,
tunnel
})
tunnels
=
append
(
tunnels
,
SSHTunnelEntry
{
addr
,
tunnel
})
}
}
return
tunnels
,
nil
return
&
SSHTunnelList
{
tunnels
}
,
nil
}
}
func
(
l
SSHTunnelList
)
Open
()
error
{
// Open attempts to open all tunnels in the list, and removes any tunnels that
for
ix
:=
range
l
{
// failed to open.
if
err
:=
l
[
ix
]
.
Tunnel
.
Open
();
err
!=
nil
{
func
(
l
*
SSHTunnelList
)
Open
()
error
{
// Remove a failed Open from the list.
var
openTunnels
[]
SSHTunnelEntry
glog
.
Errorf
(
"Failed to open tunnel %v: %v"
,
l
[
ix
],
err
)
for
ix
:=
range
l
.
entries
{
l
=
append
(
l
[
:
ix
],
l
[
ix
+
1
:
]
...
)
if
err
:=
l
.
entries
[
ix
]
.
Tunnel
.
Open
();
err
!=
nil
{
glog
.
Errorf
(
"Failed to open tunnel %v: %v"
,
l
.
entries
[
ix
],
err
)
}
else
{
openTunnels
=
append
(
openTunnels
,
l
.
entries
[
ix
])
}
}
}
}
if
len
(
l
)
==
0
{
l
.
entries
=
openTunnels
if
len
(
l
.
entries
)
==
0
{
return
errors
.
New
(
"Failed to open any tunnels."
)
return
errors
.
New
(
"Failed to open any tunnels."
)
}
}
return
nil
return
nil
...
@@ -239,9 +245,9 @@ func (l SSHTunnelList) Open() error {
...
@@ -239,9 +245,9 @@ func (l SSHTunnelList) Open() error {
// Close asynchronously closes all tunnels in the list after waiting for 1
// Close asynchronously closes all tunnels in the list after waiting for 1
// minute. Tunnels will still be open upon this function's return, but should
// minute. Tunnels will still be open upon this function's return, but should
// no longer be used.
// no longer be used.
func
(
l
SSHTunnelList
)
Close
()
{
func
(
l
*
SSHTunnelList
)
Close
()
{
for
ix
:=
range
l
{
for
ix
:=
range
l
.
entries
{
entry
:=
l
[
ix
]
entry
:=
l
.
entries
[
ix
]
go
func
()
{
go
func
()
{
defer
HandleCrash
()
defer
HandleCrash
()
time
.
Sleep
(
1
*
time
.
Minute
)
time
.
Sleep
(
1
*
time
.
Minute
)
...
@@ -252,22 +258,26 @@ func (l SSHTunnelList) Close() {
...
@@ -252,22 +258,26 @@ func (l SSHTunnelList) Close() {
}
}
}
}
func
(
l
SSHTunnelList
)
Dial
(
network
,
addr
string
)
(
net
.
Conn
,
error
)
{
func
(
l
*
SSHTunnelList
)
Dial
(
network
,
addr
string
)
(
net
.
Conn
,
error
)
{
if
len
(
l
)
==
0
{
if
len
(
l
.
entries
)
==
0
{
return
nil
,
fmt
.
Errorf
(
"Empty tunnel list."
)
return
nil
,
fmt
.
Errorf
(
"Empty tunnel list."
)
}
}
return
l
[
mathrand
.
Int
()
%
len
(
l
)]
.
Tunnel
.
Dial
(
network
,
addr
)
return
l
.
entries
[
mathrand
.
Int
()
%
len
(
l
.
entries
)]
.
Tunnel
.
Dial
(
network
,
addr
)
}
}
func
(
l
SSHTunnelList
)
Has
(
addr
string
)
bool
{
func
(
l
*
SSHTunnelList
)
Has
(
addr
string
)
bool
{
for
ix
:=
range
l
{
for
ix
:=
range
l
.
entries
{
if
l
[
ix
]
.
Address
==
addr
{
if
l
.
entries
[
ix
]
.
Address
==
addr
{
return
true
return
true
}
}
}
}
return
false
return
false
}
}
func
(
l
*
SSHTunnelList
)
Len
()
int
{
return
len
(
l
.
entries
)
}
func
EncodePrivateKey
(
private
*
rsa
.
PrivateKey
)
[]
byte
{
func
EncodePrivateKey
(
private
*
rsa
.
PrivateKey
)
[]
byte
{
return
pem
.
EncodeToMemory
(
&
pem
.
Block
{
return
pem
.
EncodeToMemory
(
&
pem
.
Block
{
Bytes
:
x509
.
MarshalPKCS1PrivateKey
(
private
),
Bytes
:
x509
.
MarshalPKCS1PrivateKey
(
private
),
...
...
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