Files
my-rook-config/pkg/clusterd/network.go
T

98 lines
2.2 KiB
Go
Raw Normal View History

2017-10-17 09:33:37 -07:00
/*
Copyright 2016 The Rook Authors. All rights reserved.
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 clusterd
import (
"fmt"
"net"
)
type NetworkInfo struct {
2018-06-05 15:15:49 +08:00
PublicAddr string
ClusterAddr string
PublicNetwork string // public network and subnet mask in CIDR notation
ClusterNetwork string // cluster network and subnet mask in CIDR notation
// deprecated ipv4 format address
// TODO: remove these legacy fields in the future
PublicAddrIPv4 string
ClusterAddrIPv4 string
2018-06-05 15:15:49 +08:00
}
// Simplify adapts deprecated fields
// TODO: remove this function in the future
2018-06-27 13:29:06 +02:00
func (in NetworkInfo) Simplify() NetworkInfo {
out := NetworkInfo{
2018-06-05 15:15:49 +08:00
PublicNetwork: in.PublicNetwork,
ClusterNetwork: in.ClusterNetwork,
}
if in.PublicAddr != "" {
out.PublicAddr = in.PublicAddr
} else {
out.PublicAddr = in.PublicAddrIPv4
}
if in.ClusterAddr != "" {
out.ClusterAddr = in.ClusterAddr
} else {
out.ClusterAddr = in.ClusterAddrIPv4
}
return out
}
func VerifyNetworkInfo(networkInfo NetworkInfo) error {
2018-06-05 15:15:49 +08:00
if err := verifyIPAddr(networkInfo.PublicAddr); err != nil {
return err
}
2018-06-05 15:15:49 +08:00
if err := verifyIPAddr(networkInfo.ClusterAddr); err != nil {
return err
}
if err := verifyIPNetwork(networkInfo.PublicNetwork); err != nil {
return err
}
if err := verifyIPNetwork(networkInfo.ClusterNetwork); err != nil {
return err
}
return nil
}
func verifyIPAddr(addr string) error {
if addr == "" {
// empty strings are OK
return nil
}
if net.ParseIP(addr) == nil {
return fmt.Errorf("failed to parse IP address %s", addr)
}
return nil
}
func verifyIPNetwork(network string) error {
if network == "" {
// empty strings are OK
return nil
}
_, _, err := net.ParseCIDR(network)
return err
}