Skip to content

Commit

Permalink
Feature: yurthub supports setting ipv6 dummy if ip
Browse files Browse the repository at this point in the history
  • Loading branch information
tydra-wang committed May 26, 2022
1 parent 9402695 commit 309bfd9
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 34 deletions.
8 changes: 8 additions & 0 deletions cmd/yurthub/app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ type YurtHubConfiguration struct {
WorkingMode util.WorkingMode
KubeletHealthGracePeriod time.Duration
FilterManager *filter.Manager
CertIPs []net.IP
}

// Complete converts *options.YurtHubOptions to *YurtHubConfiguration
Expand Down Expand Up @@ -135,6 +136,12 @@ func Complete(options *options.YurtHubOptions) (*YurtHubConfiguration, error) {
return nil, err
}

// use dummy ip and bind ip as cert IP SANs
certIPs := []net.IP{
net.ParseIP(options.HubAgentDummyIfIP),
net.ParseIP(options.YurtHubHost),
}

cfg := &YurtHubConfiguration{
LBMode: options.LBMode,
RemoteServers: us,
Expand Down Expand Up @@ -167,6 +174,7 @@ func Complete(options *options.YurtHubOptions) (*YurtHubConfiguration, error) {
YurtSharedFactory: yurtSharedFactory,
KubeletHealthGracePeriod: options.KubeletHealthGracePeriod,
FilterManager: filterManager,
CertIPs: certIPs,
}

return cfg, nil
Expand Down
41 changes: 29 additions & 12 deletions cmd/yurthub/app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,19 @@ import (
"time"

"github.com/spf13/pflag"
"k8s.io/klog/v2"
utilnet "k8s.io/utils/net"

"github.com/openyurtio/openyurt/pkg/projectinfo"
"github.com/openyurtio/openyurt/pkg/yurthub/storage/disk"
"github.com/openyurtio/openyurt/pkg/yurthub/util"
)

const (
DummyIfCIDR = "169.254.0.0/16"
ExclusiveCIDR = "169.254.31.0/24"
DefaultDummyIfIP4 = "169.254.2.1"
DefaultDummyIfIP6 = "fd00::2:1"
DummyIfCIDR4 = "169.254.0.0/16"
ExclusiveCIDR = "169.254.31.0/24"
)

// YurtHubOptions is the main settings for the yurthub
Expand Down Expand Up @@ -90,7 +94,6 @@ func NewYurtHubOptions() *YurtHubOptions {
EnableProfiling: true,
EnableDummyIf: true,
EnableIptables: true,
HubAgentDummyIfIP: "169.254.2.1",
HubAgentDummyIfName: fmt.Sprintf("%s-dummy0", projectinfo.GetHubName()),
DiskCachePath: disk.CacheBaseDir,
AccessServerThroughHub: true,
Expand All @@ -103,8 +106,8 @@ func NewYurtHubOptions() *YurtHubOptions {
return o
}

// ValidateOptions validates YurtHubOptions
func ValidateOptions(options *YurtHubOptions) error {
// Validate validates YurtHubOptions
func (options *YurtHubOptions) Validate() error {
if len(options.NodeName) == 0 {
return fmt.Errorf("node name is empty")
}
Expand All @@ -125,7 +128,7 @@ func ValidateOptions(options *YurtHubOptions) error {
return fmt.Errorf("working mode %s is not supported", options.WorkingMode)
}

if err := verifyDummyIP(options.HubAgentDummyIfIP); err != nil {
if err := options.verifyDummyIP(); err != nil {
return fmt.Errorf("dummy ip %s is not invalid, %w", options.HubAgentDummyIfIP, err)
}

Expand Down Expand Up @@ -168,21 +171,35 @@ func (o *YurtHubOptions) AddFlags(fs *pflag.FlagSet) {
fs.BoolVar(&o.EnableNodePool, "enable-node-pool", o.EnableNodePool, "enable list/watch nodepools resource or not for filters(only used for testing)")
}

// verifyDummyIP verify the specified ip is valid or not
func verifyDummyIP(dummyIP string) error {
//169.254.2.1/32
// verifyDummyIP verify the specified ip is valid or not and set the default ip if empty
func (o *YurtHubOptions) verifyDummyIP() error {
if o.HubAgentDummyIfIP == "" {
if utilnet.IsIPv6String(o.YurtHubHost) {
o.HubAgentDummyIfIP = DefaultDummyIfIP6
} else {
o.HubAgentDummyIfIP = DefaultDummyIfIP4
}
klog.Infof("dummy ip not set, will use %s as default", o.HubAgentDummyIfIP)
return nil
}

dummyIP := o.HubAgentDummyIfIP
dip := net.ParseIP(dummyIP)
if dip == nil {
return fmt.Errorf("dummy ip %s is invalid", dummyIP)
}

_, dummyIfIPNet, err := net.ParseCIDR(DummyIfCIDR)
if utilnet.IsIPv6(dip) {
return nil
}

_, dummyIfIPNet, err := net.ParseCIDR(DummyIfCIDR4)
if err != nil {
return fmt.Errorf("cidr(%s) is invalid, %w", DummyIfCIDR, err)
return fmt.Errorf("cidr(%s) is invalid, %w", DummyIfCIDR4, err)
}

if !dummyIfIPNet.Contains(dip) {
return fmt.Errorf("dummy ip %s is not in cidr(%s)", dummyIP, DummyIfCIDR)
return fmt.Errorf("dummy ip %s is not in cidr(%s)", dummyIP, DummyIfCIDR4)
}

_, exclusiveIPNet, err := net.ParseCIDR(ExclusiveCIDR)
Expand Down
5 changes: 3 additions & 2 deletions cmd/yurthub/app/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func NewCmdStartYurtHub(stopCh <-chan struct{}) *cobra.Command {
cmd.Flags().VisitAll(func(flag *pflag.Flag) {
klog.V(1).Infof("FLAG: --%s=%q", flag.Name, flag.Value)
})
if err := options.ValidateOptions(yurtHubOptions); err != nil {
if err := yurtHubOptions.Validate(); err != nil {
klog.Fatalf("validate options: %v", err)
}

Expand Down Expand Up @@ -125,7 +125,8 @@ func Run(cfg *config.YurtHubConfiguration, stopCh <-chan struct{}) error {
trace++

klog.Infof("%d. create tls config for secure servers ", trace)
cfg.TLSConfig, err = server.GenUseCertMgrAndTLSConfig(restConfigMgr, certManager, filepath.Join(cfg.RootDir, "pki"), cfg.NodeName, cfg.YurtHubProxyServerSecureDummyAddr, stopCh)
cfg.TLSConfig, err = server.GenUseCertMgrAndTLSConfig(
restConfigMgr, certManager, filepath.Join(cfg.RootDir, "pki"), cfg.NodeName, cfg.CertIPs, stopCh)
if err != nil {
return fmt.Errorf("could not create tls config, %w", err)
}
Expand Down
11 changes: 3 additions & 8 deletions pkg/util/certmanager/certmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,8 @@ func NewYurttunnelAgentCertManager(
func NewYurtHubServerCertManager(
clientset kubernetes.Interface,
certDir,
nodeName,
proxyServerSecureDummyAddr string) (certificate.Manager, error) {

host, _, err := net.SplitHostPort(proxyServerSecureDummyAddr)
if err != nil {
return nil, err
}
nodeName string,
certIPs []net.IP) (certificate.Manager, error) {

return newCertManager(
clientset,
Expand All @@ -206,7 +201,7 @@ func NewYurtHubServerCertManager(
certificatesv1.UsageDigitalSignature,
certificatesv1.UsageServerAuth,
},
[]net.IP{net.ParseIP("127.0.0.1"), net.ParseIP(host)},
certIPs,
nil)
}

Expand Down
16 changes: 16 additions & 0 deletions pkg/util/ip/ip.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2021 The OpenYurt Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package ip

import (
Expand Down
16 changes: 16 additions & 0 deletions pkg/util/ip/ip_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2021 The OpenYurt Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package ip

import (
Expand Down
24 changes: 14 additions & 10 deletions pkg/yurthub/network/iptables.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/klog/v2"
"k8s.io/utils/exec"
utilnet "k8s.io/utils/net"

"github.com/openyurtio/openyurt/pkg/util/iptables"
)
Expand All @@ -40,6 +41,9 @@ type IptablesManager struct {

func NewIptablesManager(dummyIfIP, dummyIfPort string) *IptablesManager {
protocol := iptables.ProtocolIpv4
if utilnet.IsIPv6String(dummyIfIP) {
protocol = iptables.ProtocolIpv6
}
execer := exec.New()
iptInterface := iptables.New(execer, protocol)

Expand All @@ -63,16 +67,16 @@ func makeupIptablesRules(ifIP, ifPort string) []iptablesRule {
{iptables.Prepend, iptables.Table("raw"), iptables.ChainOutput, []string{"-p", "tcp", "--sport", ifPort, "-s", ifIP, "-j", "NOTRACK"}},
// accept traffic from 169.254.2.1:10261
{iptables.Prepend, iptables.TableFilter, iptables.ChainOutput, []string{"-p", "tcp", "--sport", ifPort, "-s", ifIP, "-j", "ACCEPT"}},
// skip connection track for traffic from container to 127.0.0.1:10261
{iptables.Prepend, iptables.Table("raw"), iptables.ChainPrerouting, []string{"-p", "tcp", "--dport", ifPort, "--destination", "127.0.0.1", "-j", "NOTRACK"}},
// skip connection track for traffic from host network to 127.0.0.1:10261
{iptables.Prepend, iptables.Table("raw"), iptables.ChainOutput, []string{"-p", "tcp", "--dport", ifPort, "--destination", "127.0.0.1", "-j", "NOTRACK"}},
// accept traffic to 127.0.0.1:10261
{iptables.Prepend, iptables.TableFilter, iptables.ChainInput, []string{"-p", "tcp", "--dport", ifPort, "--destination", "127.0.0.1", "-j", "ACCEPT"}},
// skip connection track for traffic from 127.0.0.1:10261
{iptables.Prepend, iptables.Table("raw"), iptables.ChainOutput, []string{"-p", "tcp", "--sport", ifPort, "-s", "127.0.0.1", "-j", "NOTRACK"}},
// accept traffic from 127.0.0.1:10261
{iptables.Prepend, iptables.TableFilter, iptables.ChainOutput, []string{"-p", "tcp", "--sport", ifPort, "-s", "127.0.0.1", "-j", "ACCEPT"}},
// skip connection track for traffic from container to localhost:10261
{iptables.Prepend, iptables.Table("raw"), iptables.ChainPrerouting, []string{"-p", "tcp", "--dport", ifPort, "--destination", "localhost", "-j", "NOTRACK"}},
// skip connection track for traffic from host network to localhost:10261
{iptables.Prepend, iptables.Table("raw"), iptables.ChainOutput, []string{"-p", "tcp", "--dport", ifPort, "--destination", "localhost", "-j", "NOTRACK"}},
// accept traffic to localhost:10261
{iptables.Prepend, iptables.TableFilter, iptables.ChainInput, []string{"-p", "tcp", "--dport", ifPort, "--destination", "localhost", "-j", "ACCEPT"}},
// skip connection track for traffic from localhost:10261
{iptables.Prepend, iptables.Table("raw"), iptables.ChainOutput, []string{"-p", "tcp", "--sport", ifPort, "-s", "localhost", "-j", "NOTRACK"}},
// accept traffic from localhost:10261
{iptables.Prepend, iptables.TableFilter, iptables.ChainOutput, []string{"-p", "tcp", "--sport", ifPort, "-s", "localhost", "-j", "ACCEPT"}},
}
}

Expand Down
9 changes: 7 additions & 2 deletions pkg/yurthub/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,12 @@ func healthz(w http.ResponseWriter, _ *http.Request) {
}

// GenUseCertMgrAndTLSConfig create a certificate manager for the yurthub server and generate a TLS configuration
func GenUseCertMgrAndTLSConfig(restConfigMgr *rest.RestConfigManager, certificateMgr interfaces.YurtCertificateManager, certDir, nodeName, proxyServerSecureDummyAddr string, stopCh <-chan struct{}) (*tls.Config, error) {
func GenUseCertMgrAndTLSConfig(
restConfigMgr *rest.RestConfigManager,
certificateMgr interfaces.YurtCertificateManager,
certDir, nodeName string,
certIPs []net.IP,
stopCh <-chan struct{}) (*tls.Config, error) {
cfg := restConfigMgr.GetRestConfig(false)
if cfg == nil {
return nil, fmt.Errorf("failed to prepare rest config based ong hub agent client certificate")
Expand All @@ -180,7 +185,7 @@ func GenUseCertMgrAndTLSConfig(restConfigMgr *rest.RestConfigManager, certificat
return nil, err
}
// create a certificate manager for the yurthub server and run the csr approver for both yurthub
serverCertMgr, err := certmanager.NewYurtHubServerCertManager(clientSet, certDir, nodeName, proxyServerSecureDummyAddr)
serverCertMgr, err := certmanager.NewYurtHubServerCertManager(clientSet, certDir, nodeName, certIPs)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 309bfd9

Please sign in to comment.