Skip to content

Commit ee35c26

Browse files
committed
feat(cli): update gateway and route commands for new API
- Update gateway and route commands to work with new API requiring app field - Simplify command structure and remove redundant parameters - Update parser to handle new command format - Update tests to match new API behavior - Update i18n translations for command descriptions - Update go.mod dependencies
1 parent c926d87 commit ee35c26

17 files changed

Lines changed: 888 additions & 472 deletions

File tree

g.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name: demo01
2+
ports:
3+
- port: 80
4+
protocol: HTTP
5+
- port: 8000
6+
protocol: TCP
7+
- port: 8001
8+
protocol: HTTP

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.26
55
require (
66
github.com/chai2010/gettext-go v1.0.3
77
github.com/containerd/console v1.0.4
8-
github.com/drycc/controller-sdk-go v0.0.0-20260527062908-7d751e6440b0
8+
github.com/drycc/controller-sdk-go v0.0.0-20260528080055-65af8fd9605a
99
github.com/drycc/pkg v0.0.0-20250917064731-345368da3dbf
1010
github.com/minio/selfupdate v0.6.0
1111
github.com/olekukonko/tablewriter v0.0.5

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6N
99
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
1010
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
1111
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
12-
github.com/drycc/controller-sdk-go v0.0.0-20260527062908-7d751e6440b0 h1:LBGTmWRopu4y0i8DIgRaTnr9oycPlRVo7905yOJSPWw=
13-
github.com/drycc/controller-sdk-go v0.0.0-20260527062908-7d751e6440b0/go.mod h1:jV1AUDHtY8aPMF95evHQGXZOX6tUXaf7wgqzUEnD5SM=
12+
github.com/drycc/controller-sdk-go v0.0.0-20260528080055-65af8fd9605a h1:9e61dv4u1PHPWPp6BgtUtIw2tPxaZfLQhHDpn3DDo2w=
13+
github.com/drycc/controller-sdk-go v0.0.0-20260528080055-65af8fd9605a/go.mod h1:jV1AUDHtY8aPMF95evHQGXZOX6tUXaf7wgqzUEnD5SM=
1414
github.com/drycc/pkg v0.0.0-20250917064731-345368da3dbf h1:CYy3NoPhfFhkGAbEppTOQfY/HC2s0FJDcBgbtRKeweg=
1515
github.com/drycc/pkg v0.0.0-20250917064731-345368da3dbf/go.mod h1:BrrNrNskHKm+nJYhXfGuI114w8nupi0AMo8QZHID7CM=
1616
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=

internal/commands/commands.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,13 @@ type Commander interface {
5858
ServicesList(string) error
5959
ServicesAdd(string, string, string, string) error
6060
ServicesRemove(string, string, string, int) error
61-
GatewaysAdd(string, string, int, string) error
6261
GatewaysList(string, int) error
63-
GatewaysRemove(string, string, int, string) error
64-
RoutesCreate(string, string, string, ...api.BackendRefRequest) error
62+
GatewaysInfo(string, string) error
63+
GatewaysApply(string, string) error
64+
GatewaysRemove(string, string) error
6565
RoutesList(string, int) error
66-
RoutesGet(string, string) error
67-
RoutesSet(string, string, string) error
68-
RoutesAttach(string, string, int, string) error
69-
RoutesDetach(string, string, int, string) error
66+
RoutesInfo(string, string) error
67+
RoutesApply(string, string) error
7068
RoutesRemove(string, string) error
7169
GitRemote(string, string, bool) error
7270
GitRemove(string) error

internal/commands/gateways.go

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ package commands
22

33
import (
44
"fmt"
5+
"os"
56
"strings"
67

78
"github.com/drycc/controller-sdk-go/api"
89
"github.com/drycc/controller-sdk-go/gateways"
910
"github.com/drycc/workflow-cli/internal/loader"
11+
"github.com/drycc/workflow-cli/pkg/coder"
12+
"sigs.k8s.io/yaml"
1013
)
1114

1215
// GatewaysList lists gateways for the app
@@ -26,28 +29,69 @@ func (d *DryccCmd) GatewaysList(appID string, results int) error {
2629
if count == 0 {
2730
d.Println(fmt.Sprintf("No gateways found in %s app.", appID))
2831
} else {
29-
table := d.getDefaultFormatTable([]string{"NAME", "LISENTER", "PORT", "PROTOCOL", "ADDRESSES"})
32+
table := d.getDefaultFormatTable([]string{"NAME", "PORT", "PROTOCOL", "ADDRESSES"})
3033
for _, gateway := range gateways {
3134
addresesStr := parseAddress(gateway.Addresses)
32-
for _, listener := range gateway.Listeners {
33-
table.Append([]string{gateway.Name, listener.Name, fmt.Sprint(listener.Port), listener.Protocol, addresesStr})
35+
for _, port := range gateway.Ports {
36+
table.Append([]string{gateway.Name, fmt.Sprint(port.Port), port.Protocol, addresesStr})
3437
}
3538
}
3639
table.Render()
3740
}
3841
return nil
3942
}
4043

41-
// GatewaysAdd adds a gateway to an app.
42-
func (d *DryccCmd) GatewaysAdd(appID, name string, port int, protocol string) error {
44+
// GatewaysInfo shows detailed information about a gateway.
45+
func (d *DryccCmd) GatewaysInfo(appID, name string) error {
4346
appID, s, err := loader.LoadAppSettings(d.ConfigFile, appID)
4447
if err != nil {
4548
return err
4649
}
47-
d.Printf("Adding gateway %s to %s... ", name, appID)
50+
51+
info, err := gateways.Info(s.Client, appID, name)
52+
if d.checkAPICompatibility(s.Client, err) != nil {
53+
return err
54+
}
55+
56+
c := &coder.GatewayCoder{Info: info}
57+
yamlBytes, err := c.Encode()
58+
if err != nil {
59+
return err
60+
}
61+
d.Println(string(yamlBytes))
62+
return nil
63+
}
64+
65+
// GatewaysApply applies gateway configuration from a YAML file.
66+
func (d *DryccCmd) GatewaysApply(appID, filePath string) error {
67+
appID, s, err := loader.LoadAppSettings(d.ConfigFile, appID)
68+
if err != nil {
69+
return err
70+
}
71+
72+
yamlData, err := os.ReadFile(filePath)
73+
if err != nil {
74+
return err
75+
}
76+
77+
jsonData, err := yaml.YAMLToJSON(yamlData)
78+
if err != nil {
79+
return err
80+
}
81+
82+
c := &coder.GatewayCoder{}
83+
if err := c.Decode(jsonData); err != nil {
84+
return fmt.Errorf("invalid gateway configuration: %w", err)
85+
}
86+
if c.Request.Name == "" {
87+
return fmt.Errorf("invalid gateway configuration: missing metadata.name")
88+
}
89+
req := c.Request
90+
91+
d.Printf("Applying gateway %s to %s... ", req.Name, appID)
4892

4993
quit := progress(d.WOut)
50-
err = gateways.New(s.Client, appID, name, port, protocol)
94+
_, err = gateways.Apply(s.Client, appID, req)
5195
quit <- true
5296
<-quit
5397
if d.checkAPICompatibility(s.Client, err) != nil {
@@ -58,16 +102,16 @@ func (d *DryccCmd) GatewaysAdd(appID, name string, port int, protocol string) er
58102
return nil
59103
}
60104

61-
// GatewaysRemove removes a gateway registered with an app.
62-
func (d *DryccCmd) GatewaysRemove(appID, name string, port int, protocol string) error {
105+
// GatewaysRemove removes a gateway from an app.
106+
func (d *DryccCmd) GatewaysRemove(appID, name string) error {
63107
appID, s, err := loader.LoadAppSettings(d.ConfigFile, appID)
64108
if err != nil {
65109
return err
66110
}
67-
d.Printf("Removing gateway %s to %s... ", name, appID)
111+
d.Printf("Removing gateway %s from %s... ", name, appID)
68112

69113
quit := progress(d.WOut)
70-
err = gateways.Delete(s.Client, appID, name, port, protocol)
114+
err = gateways.Delete(s.Client, appID, name)
71115
quit <- true
72116
<-quit
73117
if d.checkAPICompatibility(s.Client, err) != nil {

internal/commands/gateways_test.go

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"fmt"
66
"net/http"
7+
"os"
78
"testing"
89

910
"github.com/drycc/controller-sdk-go/api"
@@ -32,20 +33,15 @@ func TestGatewaysList(t *testing.T) {
3233
"app": "foo",
3334
"name": "foo",
3435
"created": "2023-04-19T00:00:00UTC",
35-
"owner": "test",
3636
"updated": "2023-04-19T00:00:00UTC",
37-
"listeners": [
37+
"ports": [
3838
{
39-
"name": "foo-80-http",
4039
"port": 80,
41-
"protocol": "HTTP",
42-
"allowedRoutes": {"namespaces": {"from": "All"}}
40+
"protocol": "HTTP"
4341
},
4442
{
45-
"name": "foo-443-https",
4643
"port": 443,
47-
"protocol": "HTTPS",
48-
"allowedRoutes": {"namespaces": {"from": "All"}}
44+
"protocol": "HTTPS"
4945
}
5046
],
5147
"addresses": [
@@ -62,13 +58,13 @@ func TestGatewaysList(t *testing.T) {
6258
err = cmdr.GatewaysList("foo", -1)
6359
assert.NoError(t, err)
6460

65-
assert.Equal(t, b.String(), `NAME LISENTER PORT PROTOCOL ADDRESSES
66-
foo foo-80-http 80 HTTP 192.168.11.1
67-
foo foo-443-https 443 HTTPS 192.168.11.1
61+
assert.Equal(t, b.String(), `NAME PORT PROTOCOL ADDRESSES
62+
foo 80 HTTP 192.168.11.1
63+
foo 443 HTTPS 192.168.11.1
6864
`, "output")
6965
}
7066

71-
func TestGatewaysAdd(t *testing.T) {
67+
func TestGatewaysApply(t *testing.T) {
7268
t.Parallel()
7369
cf, server, err := testutil.NewTestServerAndClient()
7470
if err != nil {
@@ -78,21 +74,27 @@ func TestGatewaysAdd(t *testing.T) {
7874
var b bytes.Buffer
7975
cmdr := DryccCmd{WOut: &b, ConfigFile: cf}
8076

81-
server.Mux.HandleFunc("/v2/apps/foo/gateways/", func(w http.ResponseWriter, r *http.Request) {
82-
testutil.AssertBody(t, api.GatewayCreateRequest{Name: "example-go", Port: 443, Protocol: "HTTPS"}, r)
77+
server.Mux.HandleFunc("/v2/apps/foo/gateways/example-go/", func(w http.ResponseWriter, r *http.Request) {
78+
testutil.AssertBody(t, api.GatewayUpdateRequest{App: "foo", Name: "example-go", Ports: []api.GatewayPort{{Port: 443, Protocol: "HTTPS"}}}, r)
8379
testutil.SetHeaders(w)
84-
w.WriteHeader(http.StatusCreated)
85-
// Body isn't used by CLI, so it isn't set.
80+
w.WriteHeader(http.StatusOK)
8681
w.Write([]byte("{}"))
8782
})
8883

89-
err = cmdr.GatewaysAdd("foo", "example-go", 443, "HTTPS")
84+
gatewayFile, err := os.CreateTemp("", "gateway.yaml")
85+
assert.NoError(t, err)
86+
defer os.Remove(gatewayFile.Name())
87+
_, err = gatewayFile.Write([]byte("apiVersion: gateway.networking.k8s.io/v1\nkind: Gateway\nmetadata:\n name: example-go\nspec:\n ports:\n - port: 443\n protocol: HTTPS\n"))
88+
assert.NoError(t, err)
89+
gatewayFile.Close()
90+
91+
err = cmdr.GatewaysApply("foo", gatewayFile.Name())
9092
assert.NoError(t, err)
9193

92-
assert.Equal(t, testutil.StripProgress(b.String()), "Adding gateway example-go to foo... done\n", "output")
94+
assert.Equal(t, testutil.StripProgress(b.String()), "Applying gateway example-go to foo... done\n", "output")
9395
}
9496

95-
func TestGatewaysDelete(t *testing.T) {
97+
func TestGatewaysRemove(t *testing.T) {
9698
t.Parallel()
9799
cf, server, err := testutil.NewTestServerAndClient()
98100
if err != nil {
@@ -102,13 +104,13 @@ func TestGatewaysDelete(t *testing.T) {
102104
var b bytes.Buffer
103105
cmdr := DryccCmd{WOut: &b, ConfigFile: cf}
104106

105-
server.Mux.HandleFunc("/v2/apps/foo/gateways/", func(w http.ResponseWriter, _ *http.Request) {
107+
server.Mux.HandleFunc("/v2/apps/foo/gateways/example-go/", func(w http.ResponseWriter, _ *http.Request) {
106108
testutil.SetHeaders(w)
107109
w.WriteHeader(http.StatusNoContent)
108110
})
109111

110-
err = cmdr.GatewaysRemove("foo", "example-go", 443, "HTTPS")
112+
err = cmdr.GatewaysRemove("foo", "example-go")
111113
assert.NoError(t, err)
112114

113-
assert.Equal(t, testutil.StripProgress(b.String()), "Removing gateway example-go to foo... done\n", "output")
115+
assert.Equal(t, testutil.StripProgress(b.String()), "Removing gateway example-go from foo... done\n", "output")
114116
}

0 commit comments

Comments
 (0)