mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 19:13:34 +00:00
parse --publish ports into spec
This commit is contained in:
@@ -74,20 +74,34 @@ func run(ctx context.Context, uncli *cli.CLI, opts runOptions) error {
|
|||||||
return fmt.Errorf("invalid replication mode: %q", opts.mode)
|
return fmt.Errorf("invalid replication mode: %q", opts.mode)
|
||||||
}
|
}
|
||||||
|
|
||||||
client, err := uncli.ConnectCluster(ctx, opts.cluster)
|
ports := make([]api.PortSpec, len(opts.publish))
|
||||||
if err != nil {
|
for i, publishPort := range opts.publish {
|
||||||
return fmt.Errorf("connect to cluster: %w", err)
|
port, err := api.ParsePortSpec(publishPort)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("invalid service port '%s': %w", publishPort, err)
|
||||||
|
}
|
||||||
|
ports[i] = port
|
||||||
}
|
}
|
||||||
defer client.Close()
|
|
||||||
|
|
||||||
spec := api.ServiceSpec{
|
spec := api.ServiceSpec{
|
||||||
Container: api.ContainerSpec{
|
Container: api.ContainerSpec{
|
||||||
Command: opts.command,
|
Command: opts.command,
|
||||||
Image: opts.image,
|
Image: opts.image,
|
||||||
},
|
},
|
||||||
Mode: opts.mode,
|
Mode: opts.mode,
|
||||||
Name: opts.name,
|
Name: opts.name,
|
||||||
|
Ports: ports,
|
||||||
}
|
}
|
||||||
|
if err := spec.Validate(); err != nil {
|
||||||
|
return fmt.Errorf("invalid service configuration: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := uncli.ConnectCluster(ctx, opts.cluster)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("connect to cluster: %w", err)
|
||||||
|
}
|
||||||
|
defer client.Close()
|
||||||
|
|
||||||
if _, err = client.RunService(ctx, spec); err != nil {
|
if _, err = client.RunService(ctx, spec); err != nil {
|
||||||
return fmt.Errorf("run service: %w", err)
|
return fmt.Errorf("run service: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,7 +41,8 @@ func (p *PortSpec) Validate() error {
|
|||||||
switch p.Protocol {
|
switch p.Protocol {
|
||||||
case ProtocolHTTP, ProtocolHTTPS, ProtocolTCP, ProtocolUDP:
|
case ProtocolHTTP, ProtocolHTTPS, ProtocolTCP, ProtocolUDP:
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("invalid protocol: '%s'", p.Protocol)
|
return fmt.Errorf("invalid protocol '%s', supported protocols: '%s', '%s', '%s', '%s'",
|
||||||
|
p.Protocol, ProtocolHTTP, ProtocolHTTPS, ProtocolTCP, ProtocolUDP)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch p.Mode {
|
switch p.Mode {
|
||||||
|
|||||||
@@ -32,6 +32,8 @@ func (s *ServiceSpec) Validate() error {
|
|||||||
return fmt.Errorf("invalid mode: %q", s.Mode)
|
return fmt.Errorf("invalid mode: %q", s.Mode)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: validate there is no conflict between ports.
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -430,7 +430,7 @@ func TestPortSpec_Validate(t *testing.T) {
|
|||||||
ContainerPort: 8080,
|
ContainerPort: 8080,
|
||||||
Protocol: "invalid",
|
Protocol: "invalid",
|
||||||
},
|
},
|
||||||
wantErr: "invalid protocol: 'invalid'",
|
wantErr: "invalid protocol 'invalid'",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "invalid mode",
|
name: "invalid mode",
|
||||||
|
|||||||
Reference in New Issue
Block a user