Пример #1
0
func init() {
	registerResource(
		api.NewIPPool(),
		api.NewIPPoolList(),
		[]string{"CIDR"},
		[]string{"CIDR", "NAT", "IPIP"},
		map[string]string{
			"CIDR": "{{.Metadata.CIDR}}",
			"NAT":  "{{.Spec.NATOutgoing}}",
			"IPIP": "{{if .Spec.IPIP}}{{.Spec.IPIP.Enabled}}{{else}}false{{end}}",
		},
		func(client *client.Client, resource unversioned.Resource) (unversioned.Resource, error) {
			r := resource.(api.IPPool)
			return client.IPPools().Apply(&r)
		},
		func(client *client.Client, resource unversioned.Resource) (unversioned.Resource, error) {
			r := resource.(api.IPPool)
			return client.IPPools().Create(&r)
		},
		func(client *client.Client, resource unversioned.Resource) (unversioned.Resource, error) {
			r := resource.(api.IPPool)
			return client.IPPools().Update(&r)
		},
		func(client *client.Client, resource unversioned.Resource) (unversioned.Resource, error) {
			r := resource.(api.IPPool)
			return nil, client.IPPools().Delete(r.Metadata)
		},
		func(client *client.Client, resource unversioned.Resource) (unversioned.Resource, error) {
			r := resource.(api.IPPool)
			return client.IPPools().List(r.Metadata)
		},
	)
}
Пример #2
0
// getResourceFromArguments returns a resource instance from the command line arguments.
func getResourceFromArguments(args map[string]interface{}) (unversioned.Resource, error) {
	kind := args["<KIND>"].(string)
	name := argutils.ArgStringOrBlank(args, "<NAME>")
	node := argutils.ArgStringOrBlank(args, "--node")
	workload := argutils.ArgStringOrBlank(args, "--workload")
	orchestrator := argutils.ArgStringOrBlank(args, "--orchestrator")
	resScope := argutils.ArgStringOrBlank(args, "--scope")
	switch strings.ToLower(kind) {
	case "node", "nodes":
		p := api.NewNode()
		p.Metadata.Name = name
		return *p, nil
	case "hostendpoint", "hostendpoints":
		h := api.NewHostEndpoint()
		h.Metadata.Name = name
		h.Metadata.Node = node
		return *h, nil
	case "workloadendpoint", "workloadendpoints":
		h := api.NewWorkloadEndpoint()
		h.Metadata.Name = name
		h.Metadata.Orchestrator = orchestrator
		h.Metadata.Workload = workload
		h.Metadata.Node = node
		return *h, nil
	case "profile", "profiles":
		p := api.NewProfile()
		p.Metadata.Name = name
		return *p, nil
	case "policy", "policies":
		p := api.NewPolicy()
		p.Metadata.Name = name
		return *p, nil
	case "ippool", "ippools":
		p := api.NewIPPool()
		if name != "" {
			_, cidr, err := net.ParseCIDR(name)
			if err != nil {
				return nil, err
			}
			p.Metadata.CIDR = *cidr
		}
		return *p, nil
	case "bgppeer", "bgppeers":
		p := api.NewBGPPeer()
		if name != "" {
			err := p.Metadata.PeerIP.UnmarshalText([]byte(name))
			if err != nil {
				return nil, err
			}
		}
		p.Metadata.Node = node
		switch resScope {
		case "node":
			p.Metadata.Scope = scope.Node
		case "global":
			p.Metadata.Scope = scope.Global
		case "":
			p.Metadata.Scope = scope.Undefined
		default:
			return nil, fmt.Errorf("Unrecognized scope '%s', must be one of: global, node", resScope)
		}
		return *p, nil

	default:
		return nil, fmt.Errorf("Resource type '%s' is not supported", kind)
	}
}