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
eb83af0d
Unverified
Commit
eb83af0d
authored
May 05, 2023
by
Manuel Buil
Committed by
GitHub
May 05, 2023
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #7422 from manuelbuil/modify-utils
Migrate netutil methods into /util/net.go
parents
cedefeff
437ad128
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
62 additions
and
69 deletions
+62
-69
agent.go
pkg/cli/agent/agent.go
+2
-2
server.go
pkg/cli/server/server.go
+1
-2
iface.go
pkg/netutil/iface.go
+0
-65
net.go
pkg/util/net.go
+59
-0
No files found.
pkg/cli/agent/agent.go
View file @
eb83af0d
...
@@ -11,8 +11,8 @@ import (
...
@@ -11,8 +11,8 @@ import (
"github.com/k3s-io/k3s/pkg/agent"
"github.com/k3s-io/k3s/pkg/agent"
"github.com/k3s-io/k3s/pkg/cli/cmds"
"github.com/k3s-io/k3s/pkg/cli/cmds"
"github.com/k3s-io/k3s/pkg/datadir"
"github.com/k3s-io/k3s/pkg/datadir"
"github.com/k3s-io/k3s/pkg/netutil"
"github.com/k3s-io/k3s/pkg/token"
"github.com/k3s-io/k3s/pkg/token"
"github.com/k3s-io/k3s/pkg/util"
"github.com/k3s-io/k3s/pkg/version"
"github.com/k3s-io/k3s/pkg/version"
"github.com/rancher/wrangler/pkg/signals"
"github.com/rancher/wrangler/pkg/signals"
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"
...
@@ -60,7 +60,7 @@ func Run(ctx *cli.Context) error {
...
@@ -60,7 +60,7 @@ func Run(ctx *cli.Context) error {
}
}
if
cmds
.
AgentConfig
.
FlannelIface
!=
""
&&
len
(
cmds
.
AgentConfig
.
NodeIP
)
==
0
{
if
cmds
.
AgentConfig
.
FlannelIface
!=
""
&&
len
(
cmds
.
AgentConfig
.
NodeIP
)
==
0
{
cmds
.
AgentConfig
.
NodeIP
.
Set
(
net
util
.
GetIPFromInterface
(
cmds
.
AgentConfig
.
FlannelIface
))
cmds
.
AgentConfig
.
NodeIP
.
Set
(
util
.
GetIPFromInterface
(
cmds
.
AgentConfig
.
FlannelIface
))
}
}
logrus
.
Info
(
"Starting "
+
version
.
Program
+
" agent "
+
ctx
.
App
.
Version
)
logrus
.
Info
(
"Starting "
+
version
.
Program
+
" agent "
+
ctx
.
App
.
Version
)
...
...
pkg/cli/server/server.go
View file @
eb83af0d
...
@@ -18,7 +18,6 @@ import (
...
@@ -18,7 +18,6 @@ import (
"github.com/k3s-io/k3s/pkg/daemons/config"
"github.com/k3s-io/k3s/pkg/daemons/config"
"github.com/k3s-io/k3s/pkg/datadir"
"github.com/k3s-io/k3s/pkg/datadir"
"github.com/k3s-io/k3s/pkg/etcd"
"github.com/k3s-io/k3s/pkg/etcd"
"github.com/k3s-io/k3s/pkg/netutil"
"github.com/k3s-io/k3s/pkg/rootless"
"github.com/k3s-io/k3s/pkg/rootless"
"github.com/k3s-io/k3s/pkg/server"
"github.com/k3s-io/k3s/pkg/server"
"github.com/k3s-io/k3s/pkg/token"
"github.com/k3s-io/k3s/pkg/token"
...
@@ -201,7 +200,7 @@ func run(app *cli.Context, cfg *cmds.Server, leaderControllers server.CustomCont
...
@@ -201,7 +200,7 @@ func run(app *cli.Context, cfg *cmds.Server, leaderControllers server.CustomCont
}
}
if
cmds
.
AgentConfig
.
FlannelIface
!=
""
&&
len
(
cmds
.
AgentConfig
.
NodeIP
)
==
0
{
if
cmds
.
AgentConfig
.
FlannelIface
!=
""
&&
len
(
cmds
.
AgentConfig
.
NodeIP
)
==
0
{
cmds
.
AgentConfig
.
NodeIP
.
Set
(
net
util
.
GetIPFromInterface
(
cmds
.
AgentConfig
.
FlannelIface
))
cmds
.
AgentConfig
.
NodeIP
.
Set
(
util
.
GetIPFromInterface
(
cmds
.
AgentConfig
.
FlannelIface
))
}
}
if
serverConfig
.
ControlConfig
.
PrivateIP
==
""
&&
len
(
cmds
.
AgentConfig
.
NodeIP
)
!=
0
{
if
serverConfig
.
ControlConfig
.
PrivateIP
==
""
&&
len
(
cmds
.
AgentConfig
.
NodeIP
)
!=
0
{
...
...
pkg/netutil/iface.go
deleted
100644 → 0
View file @
cedefeff
package
netutil
import
(
"fmt"
"net"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
func
GetIPFromInterface
(
ifaceName
string
)
string
{
ip
,
err
:=
getIPFromInterface
(
ifaceName
)
if
err
!=
nil
{
logrus
.
Warn
(
errors
.
Wrap
(
err
,
"unable to get global unicast ip from interface name"
))
}
else
{
logrus
.
Infof
(
"Found ip %s from iface %s"
,
ip
,
ifaceName
)
}
return
ip
}
func
getIPFromInterface
(
ifaceName
string
)
(
string
,
error
)
{
iface
,
err
:=
net
.
InterfaceByName
(
ifaceName
)
if
err
!=
nil
{
return
""
,
err
}
addrs
,
err
:=
iface
.
Addrs
()
if
err
!=
nil
{
return
""
,
err
}
if
iface
.
Flags
&
net
.
FlagUp
==
0
{
return
""
,
fmt
.
Errorf
(
"the interface %s is not up"
,
ifaceName
)
}
globalUnicasts
:=
[]
string
{}
globalUnicastsIPv6
:=
[]
string
{}
for
_
,
addr
:=
range
addrs
{
ip
,
_
,
err
:=
net
.
ParseCIDR
(
addr
.
String
())
if
err
!=
nil
{
return
""
,
errors
.
Wrapf
(
err
,
"unable to parse CIDR for interface %s"
,
iface
.
Name
)
}
// if not IPv4 adding it on IPv6 list
if
ip
.
To4
()
==
nil
{
if
ip
.
IsGlobalUnicast
()
{
globalUnicastsIPv6
=
append
(
globalUnicastsIPv6
,
ip
.
String
())
}
continue
}
if
ip
.
IsGlobalUnicast
()
{
globalUnicasts
=
append
(
globalUnicasts
,
ip
.
String
())
}
}
if
len
(
globalUnicasts
)
>
1
{
return
""
,
fmt
.
Errorf
(
"multiple global unicast addresses defined for %s, please set ip from one of %v"
,
ifaceName
,
globalUnicasts
)
}
if
len
(
globalUnicasts
)
==
1
&&
len
(
globalUnicastsIPv6
)
==
0
{
return
globalUnicasts
[
0
],
nil
}
else
if
len
(
globalUnicastsIPv6
)
>
0
&&
len
(
globalUnicasts
)
==
1
{
return
globalUnicasts
[
0
]
+
","
+
globalUnicastsIPv6
[
0
],
nil
}
else
if
len
(
globalUnicastsIPv6
)
>
0
{
return
globalUnicastsIPv6
[
0
],
nil
}
return
""
,
fmt
.
Errorf
(
"can't find ip for interface %s"
,
ifaceName
)
}
pkg/util/net.go
View file @
eb83af0d
...
@@ -7,6 +7,7 @@ import (
...
@@ -7,6 +7,7 @@ import (
"os"
"os"
"strings"
"strings"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"github.com/urfave/cli"
apinet
"k8s.io/apimachinery/pkg/util/net"
apinet
"k8s.io/apimachinery/pkg/util/net"
)
)
...
@@ -299,3 +300,61 @@ func IPStringToIPNet(address string) (*net.IPNet, error) {
...
@@ -299,3 +300,61 @@ func IPStringToIPNet(address string) (*net.IPNet, error) {
_
,
cidr
,
err
:=
net
.
ParseCIDR
(
address
)
_
,
cidr
,
err
:=
net
.
ParseCIDR
(
address
)
return
cidr
,
err
return
cidr
,
err
}
}
// GetIPFromInterface is the public function that returns the IP of an interface
func
GetIPFromInterface
(
ifaceName
string
)
string
{
ip
,
err
:=
getIPFromInterface
(
ifaceName
)
if
err
!=
nil
{
logrus
.
Warn
(
fmt
.
Errorf
(
"unable to get global unicast ip from interface name: %w"
,
err
))
}
else
{
logrus
.
Infof
(
"Found ip %s from iface %s"
,
ip
,
ifaceName
)
}
return
ip
}
// getIPFromInterface is the private function that returns de IP of an interface
func
getIPFromInterface
(
ifaceName
string
)
(
string
,
error
)
{
iface
,
err
:=
net
.
InterfaceByName
(
ifaceName
)
if
err
!=
nil
{
return
""
,
err
}
addrs
,
err
:=
iface
.
Addrs
()
if
err
!=
nil
{
return
""
,
err
}
if
iface
.
Flags
&
net
.
FlagUp
==
0
{
return
""
,
fmt
.
Errorf
(
"the interface %s is not up"
,
ifaceName
)
}
globalUnicasts
:=
[]
string
{}
globalUnicastsIPv6
:=
[]
string
{}
for
_
,
addr
:=
range
addrs
{
ip
,
_
,
err
:=
net
.
ParseCIDR
(
addr
.
String
())
if
err
!=
nil
{
return
""
,
fmt
.
Errorf
(
"unable to parse CIDR for interface %s: %w"
,
iface
.
Name
,
err
)
}
// if not IPv4 adding it on IPv6 list
if
ip
.
To4
()
==
nil
{
if
ip
.
IsGlobalUnicast
()
{
globalUnicastsIPv6
=
append
(
globalUnicastsIPv6
,
ip
.
String
())
}
continue
}
if
ip
.
IsGlobalUnicast
()
{
globalUnicasts
=
append
(
globalUnicasts
,
ip
.
String
())
}
}
if
len
(
globalUnicasts
)
>
1
{
return
""
,
fmt
.
Errorf
(
"multiple global unicast addresses defined for %s, please set ip from one of %v"
,
ifaceName
,
globalUnicasts
)
}
if
len
(
globalUnicasts
)
==
1
&&
len
(
globalUnicastsIPv6
)
==
0
{
return
globalUnicasts
[
0
],
nil
}
else
if
len
(
globalUnicastsIPv6
)
>
0
&&
len
(
globalUnicasts
)
==
1
{
return
globalUnicasts
[
0
]
+
","
+
globalUnicastsIPv6
[
0
],
nil
}
else
if
len
(
globalUnicastsIPv6
)
>
0
{
return
globalUnicastsIPv6
[
0
],
nil
}
return
""
,
fmt
.
Errorf
(
"can't find ip for interface %s"
,
ifaceName
)
}
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