Skip to content

Commit

Permalink
feat: add vlan option to network
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Yang <[email protected]>
  • Loading branch information
FrankYang0529 authored and guangbochen committed Dec 1, 2021
1 parent 4de332b commit 8f0b55c
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 4 deletions.
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Network struct {
Gateway string `json:"gateway,omitempty"`
DefaultRoute bool `json:"-"`
BondOptions map[string]string `json:"bondOptions,omitempty"`
VlanID int `json:"vlanId,omitempty"`
}

type HTTPBasicAuth struct {
Expand Down
28 changes: 27 additions & 1 deletion pkg/config/cos.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,9 @@ func UpdateNetworkConfig(stage *yipSchema.Stage, networks map[string]Network, ru
} else {
err = updateNIC(stage, name, &network)
}
if network.VlanID != 0 {
err = updateVLAN(stage, name, &network)
}
if err != nil {
return err
}
Expand All @@ -361,7 +364,7 @@ func UpdateNetworkConfig(stage *yipSchema.Stage, networks map[string]Network, ru
}

if run {
stage.Commands = append(stage.Commands, fmt.Sprintf("wicked ifreload all"))
stage.Commands = append(stage.Commands, "wicked ifreload all")

// in case wicked config is not changed and netconfig is not called
stage.Commands = append(stage.Commands, "netconfig update")
Expand Down Expand Up @@ -434,6 +437,29 @@ func updateBond(stage *yipSchema.Stage, name string, network *Network) error {
return nil
}

func updateVLAN(stage *yipSchema.Stage, name string, network *Network) error {
n := struct {
Name string
Network
}{
Name: name,
Network: *network,
}
ifcfg, err := render("wicked-ifcfg-vlan", n)
if err != nil {
return err
}

stage.Files = append(stage.Files, yipSchema.File{
Path: fmt.Sprintf("/etc/sysconfig/network/ifcfg-vlan%d", network.VlanID),
Content: ifcfg,
Permissions: 0600,
Owner: 0,
Group: 0,
})
return nil
}

func UpdateWifiConfig(stage *yipSchema.Stage, wifis []Wifi, run bool) error {
if len(wifis) == 0 {
return nil
Expand Down
8 changes: 7 additions & 1 deletion pkg/config/templates/rancherd-10-harvester.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,13 @@ bootstrapResources:
enabled: true
{{- if .Vip }}
config:
vip_interface: harvester-mgmt
{{- $vipInterface := "harvester-mgmt" }}
{{- $mgmtNetwork := (index .Networks "harvester-mgmt") }}
{{- if ne $mgmtNetwork.VlanID 0 }}
{{- $vlanStr := print "vlan" $mgmtNetwork.VlanID }}
{{- $vipInterface = (print $vlanStr "@harvester-mgmt") }}
{{- end }}
vip_interface: {{ $vipInterface }}
{{- end }}
kube-vip-cloud-provider:
enabled: true
7 changes: 6 additions & 1 deletion pkg/config/templates/wicked-ifcfg-bond-master
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
STARTMODE='onboot'
BONDING_MASTER='yes'
{{ if and (eq .Method "dhcp") (ne .VlanID 0) -}}
BOOTPROTO='none'
{{ else -}}
BOOTPROTO='{{ .Method }}'
{{ if eq .Method "static" -}}
{{- end }}

{{ if and (eq .Method "static") (eq .VlanID 0) -}}
IPADDR={{ .IP }}
NETMASK={{ .SubnetMask }}
{{- end }}
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/templates/wicked-ifcfg-eth
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
STARTMODE='onboot'
BOOTPROTO='{{ .Method }}'
{{ if eq .Method "static" -}}
{{ if and (eq .Method "static") (eq .VlanID 0) -}}
IPADDR={{ .IP }}
NETMASK={{ .SubnetMask }}
{{- end }}
13 changes: 13 additions & 0 deletions pkg/config/templates/wicked-ifcfg-vlan
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
STARTMODE='onboot'
BOOTPROTO='{{ .Method }}'
ETHERDEVICE='{{ .Name }}'
{{ if and (eq .Method "static") (ne .VlanID 0) -}}
IPADDR={{ .IP }}
NETMASK={{ .SubnetMask }}
{{- end }}
VLAN_ID='{{ .VlanID }}'
{{ $defaultRoute := "no" -}}
{{- if .DefaultRoute -}}
{{- $defaultRoute = "yes" -}}
{{- end }}
DHCLIENT_SET_DEFAULT_ROUTE='{{ $defaultRoute }}'
6 changes: 6 additions & 0 deletions pkg/console/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var (
ErrMsgForceMBROnUEFI = "cannot force MBR on UEFI system"

ErrMsgNetworkMethodUnknown = "unknown network method"
ErrMsgNetworkVLANID = "vlan id should be 0 to 4094"
)

type ValidatorInterface interface {
Expand Down Expand Up @@ -201,6 +202,11 @@ func checkNetworks(networks map[string]config.Network) error {
default:
return prettyError(ErrMsgNetworkMethodUnknown, network.Method)
}

// vlan id 0 means no vlan setting, so it is valid.
if network.VlanID < 0 || network.VlanID > 4094 {
return errors.New(ErrMsgNetworkVLANID)
}
}

return nil
Expand Down

0 comments on commit 8f0b55c

Please sign in to comment.