代码拉取完成,页面将自动刷新
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(data)
err := p.validateNonSpecialIP(schema, data)
if err != nil {
return nil, err
}
return p.Store.Create(apiContext, schema, data)
}
func formatData(data map[string]interface{}) {
var ports []interface{}
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
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。