Skip to content

Commit

Permalink
daemon: validate IPv4NativeRoutingCIDR value in DaemonConfig
Browse files Browse the repository at this point in the history
Signed-off-by: Rene Zbinden <rene.zbinden@postfinance.ch>
  • Loading branch information
zbindenren authored and aanm committed Apr 27, 2020
1 parent 606736c commit e7d4f5c
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
Expand Up @@ -295,9 +295,9 @@ data:
enable-xt-socket-fallback: {{ .Values.global.enableXTSocketFallback | quote }}
install-iptables-rules: {{ .Values.global.installIptablesRules | quote }}
auto-direct-node-routes: {{ .Values.global.autoDirectNodeRoutes | quote }}
{{- if .Values.global.nativeRoutingCIDR }}
{{- if .Values.global.nativeRoutingCIDR }}
native-routing-cidr: {{ .Values.global.nativeRoutingCIDR }}
{{- end }}
{{- end }}

{{- if .Values.global.kubeProxyReplacement }}
kube-proxy-replacement: {{ .Values.global.kubeProxyReplacement | quote }}
Expand Down
13 changes: 13 additions & 0 deletions pkg/option/config.go
Expand Up @@ -2169,6 +2169,10 @@ func (c *DaemonConfig) Validate() error {
return err
}

if err := c.checkIPv4NativeRoutingCIDR(); err != nil {
return nil
}

// Validate that the KVStore Lease TTL value lies between a particular range.
if c.KVstoreLeaseTTL > defaults.KVstoreLeaseMaxTTL || c.KVstoreLeaseTTL < defaults.LockLeaseTTL {
return fmt.Errorf("KVstoreLeaseTTL does not lie in required range(%ds, %ds)",
Expand Down Expand Up @@ -2758,6 +2762,15 @@ func (c *DaemonConfig) checkMapSizeLimits() error {
return nil
}

func (c *DaemonConfig) checkIPv4NativeRoutingCIDR() error {
if c.IPv4NativeRoutingCIDR() == nil && c.Masquerade && c.Tunnel == TunnelDisabled && c.IPAMMode() != IPAMENI {
return fmt.Errorf("native routing cidr must be configured with option --%s in combination with --%s --%s=%s --%s=%s",
IPv4NativeRoutingCIDR, Masquerade, TunnelName, c.Tunnel, IPAM, c.IPAMMode())
}

return nil
}

func (c *DaemonConfig) calculateBPFMapSizes() error {
// BPF map size options
// Any map size explicitly set via option will override the dynamic
Expand Down
65 changes: 65 additions & 0 deletions pkg/option/config_test.go
Expand Up @@ -25,6 +25,7 @@ import (
"reflect"
"testing"

"github.com/cilium/cilium/pkg/cidr"
"github.com/cilium/cilium/pkg/defaults"
"github.com/google/go-cmp/cmp"
flag "github.com/spf13/pflag"
Expand Down Expand Up @@ -467,6 +468,70 @@ func TestCheckMapSizeLimits(t *testing.T) {
}
}

func TestCheckIPv4NativeRoutingCIDR(t *testing.T) {
tests := []struct {
name string
d *DaemonConfig
wantErr bool
}{
{
name: "with native routing cidr",
d: &DaemonConfig{
Masquerade: true,
Tunnel: TunnelDisabled,
IPAM: IPAMAzure,
ipv4NativeRoutingCIDR: cidr.MustParseCIDR("10.127.64.0/18"),
},
wantErr: false,
},
{
name: "without native routing cidr and no masquerade",
d: &DaemonConfig{
Masquerade: false,
Tunnel: TunnelDisabled,
IPAM: IPAMAzure,
},
wantErr: false,
},
{
name: "without native routing cidr and tunnel enabled",
d: &DaemonConfig{
Masquerade: true,
Tunnel: TunnelVXLAN,
IPAM: IPAMAzure,
},
wantErr: false,
},
{
name: "without native routing cidr and tunnel enabled",
d: &DaemonConfig{
Masquerade: true,
Tunnel: TunnelDisabled,
IPAM: IPAMENI,
},
wantErr: false,
},
{
name: "without native routing cidr and with masquerade and tunnel disabled and ipam not eni",
d: &DaemonConfig{
Masquerade: true,
Tunnel: TunnelDisabled,
IPAM: IPAMAzure,
},
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.d.checkIPv4NativeRoutingCIDR()
if tt.wantErr && err == nil {
t.Error("expected error, but got nil")
}
})
}
}

func Test_populateNodePortRange(t *testing.T) {
type want struct {
wantMin int
Expand Down

0 comments on commit e7d4f5c

Please sign in to comment.