37 Star 411 Fork 76

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
service.go 2.29 KB
一键复制 编辑 原始数据 按行查看 历史
package service
import (
"net"
"strconv"
"fmt"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
v3 "github.com/rancher/types/client/project/v3"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/util/intstr"
)
func New(store types.Store) types.Store {
return &Store{
store,
}
}
type Store struct {
types.Store
}
func (p *Store) Create(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}) (map[string]interface{}, error) {
if schema.ID == "dnsRecord" {
if convert.IsAPIObjectEmpty(data["hostname"]) {
data["kind"] = "ClusterIP"
data["clusterIp"] = nil
} else {
data["kind"] = "ExternalName"
data["clusterIp"] = ""
}
}
formatData(schema, data)
err := p.validateNonSpecialIP(schema, data)
if err != nil {
return nil, err
}
return p.Store.Create(apiContext, schema, data)
}
func formatData(schema *types.Schema, data map[string]interface{}) {
var ports []interface{}
if schema.ID == "service" {
ports = convert.ToInterfaceSlice(data["ports"])
}
// append default port as sky dns won't work w/o at least one port being set
if len(ports) == 0 {
servicePort := v3.ServicePort{
Port: 42,
TargetPort: intstr.Parse(strconv.FormatInt(42, 10)),
Protocol: "TCP",
Name: "default",
}
m, err := convert.EncodeToMap(servicePort)
if err != nil {
logrus.Warnf("Failed to transform service port to map: %v", err)
return
}
ports = append(ports, m)
}
data["ports"] = ports
}
func (p *Store) validateNonSpecialIP(schema *types.Schema, data map[string]interface{}) error {
if schema.ID == "dnsRecord" {
ips := data["ipAddresses"]
if ips != nil {
for _, ip := range ips.([]interface{}) {
IP := net.ParseIP(ip.(string))
if IP == nil {
return fmt.Errorf("%s must be a valid IP address", IP)
}
if IP.IsUnspecified() {
return fmt.Errorf("%s may not be unspecified (0.0.0.0)", IP)
}
if IP.IsLoopback() {
return fmt.Errorf("%s may not be in the loopback range (127.0.0.0/8)", IP)
}
if IP.IsLinkLocalUnicast() {
return fmt.Errorf("%s may not be in the link-local range (169.254.0.0/16)", IP)
}
if IP.IsLinkLocalMulticast() {
return fmt.Errorf("%s may not be in the link-local multicast range (224.0.0.0/24)", IP)
}
}
}
}
return nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.2.0-rc11

搜索帮助

0d507c66 1850385 C8b1a773 1850385