Skip to content

Commit 50f566e

Browse files
committed
Use SDK for ingress and fix instance name match ingress type
1 parent 3c23629 commit 50f566e

File tree

4 files changed

+34
-47
lines changed

4 files changed

+34
-47
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,5 @@ require (
7474
google.golang.org/genproto/googleapis/rpc v0.0.0-20250825161204-c5933d9347a5 // indirect
7575
google.golang.org/grpc v1.75.1 // indirect
7676
)
77+
78+
replace github.com/onkernel/hypeman-go => github.com/stainless-sdks/hypeman-go v0.0.0-20251210223055-431af203f52d

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,6 @@ github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
105105
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
106106
github.com/muesli/termenv v0.16.0 h1:S5AlUN9dENB57rsbnkPyfdGuWIlkmzJjbFf0Tf5FWUc=
107107
github.com/muesli/termenv v0.16.0/go.mod h1:ZRfOIKPFDYQoDFF4Olj7/QJbW60Ol/kL1pU3VfY/Cnk=
108-
github.com/onkernel/hypeman-go v0.6.0 h1:vTi3/SateUYIPj1fF52643fyWenogZT8Vt/vz7QMcp8=
109-
github.com/onkernel/hypeman-go v0.6.0/go.mod h1:BPT1yh0gbby1E+As/xLM3GVjw7752+2C5SaEiJV9rRc=
110108
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
111109
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
112110
github.com/opencontainers/image-spec v1.1.1 h1:y0fUlFfIZhPF1W537XOLg0/fcx6zcHCJwooC2xJA040=
@@ -123,6 +121,8 @@ github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf
123121
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
124122
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
125123
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
124+
github.com/stainless-sdks/hypeman-go v0.0.0-20251210223055-431af203f52d h1:kZHZ9PflX6BMr1cP6jFT+PsR66Et4fVfHQJC1GJmDUg=
125+
github.com/stainless-sdks/hypeman-go v0.0.0-20251210223055-431af203f52d/go.mod h1:BPT1yh0gbby1E+As/xLM3GVjw7752+2C5SaEiJV9rRc=
126126
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
127127
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
128128
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=

pkg/cmd/ingresscmd.go

Lines changed: 26 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cmd
22

33
import (
44
"context"
5-
"encoding/json"
65
"fmt"
76
"os"
87
"strings"
@@ -84,29 +83,6 @@ var ingressDeleteCmd = cli.Command{
8483
HideHelpCommand: true,
8584
}
8685

87-
// ingressRule is a custom type to include TLS fields not yet in the SDK
88-
type ingressRule struct {
89-
Match ingressMatch `json:"match"`
90-
Target ingressTarget `json:"target"`
91-
TLS bool `json:"tls,omitempty"`
92-
RedirectHTTP bool `json:"redirect_http,omitempty"`
93-
}
94-
95-
type ingressMatch struct {
96-
Hostname string `json:"hostname"`
97-
Port int64 `json:"port,omitempty"`
98-
}
99-
100-
type ingressTarget struct {
101-
Instance string `json:"instance"`
102-
Port int64 `json:"port"`
103-
}
104-
105-
type ingressCreateRequest struct {
106-
Name string `json:"name"`
107-
Rules []ingressRule `json:"rules"`
108-
}
109-
11086
func handleIngressCreate(ctx context.Context, cmd *cli.Command) error {
11187
args := cmd.Args().Slice()
11288
if len(args) < 1 {
@@ -133,35 +109,27 @@ func handleIngressCreate(ctx context.Context, cmd *cli.Command) error {
133109
opts = append(opts, debugMiddlewareOption)
134110
}
135111

136-
// Build custom request body to include TLS fields
137-
reqBody := ingressCreateRequest{
112+
params := hypeman.IngressNewParams{
138113
Name: name,
139-
Rules: []ingressRule{
114+
Rules: []hypeman.IngressRuleParam{
140115
{
141-
Match: ingressMatch{
116+
Match: hypeman.IngressMatchParam{
142117
Hostname: hostname,
143-
Port: int64(hostPort),
118+
Port: hypeman.Int(int64(hostPort)),
144119
},
145-
Target: ingressTarget{
120+
Target: hypeman.IngressTargetParam{
146121
Instance: instance,
147122
Port: int64(port),
148123
},
149-
TLS: tls,
150-
RedirectHTTP: redirectHTTP,
124+
Tls: hypeman.Bool(tls),
125+
RedirectHTTP: hypeman.Bool(redirectHTTP),
151126
},
152127
},
153128
}
154129

155-
bodyBytes, err := json.Marshal(reqBody)
156-
if err != nil {
157-
return fmt.Errorf("failed to marshal request: %w", err)
158-
}
159-
160-
opts = append(opts, option.WithRequestBody("application/json", bodyBytes))
161-
162130
fmt.Fprintf(os.Stderr, "Creating ingress %s...\n", name)
163131

164-
result, err := client.Ingresses.New(ctx, hypeman.IngressNewParams{}, opts...)
132+
result, err := client.Ingresses.New(ctx, params, opts...)
165133
if err != nil {
166134
return err
167135
}
@@ -207,6 +175,11 @@ func handleIngressList(ctx context.Context, cmd *cli.Command) error {
207175
rule := ing.Rules[0]
208176
hostname = rule.Match.Hostname
209177
target = fmt.Sprintf("%s:%d", rule.Target.Instance, rule.Target.Port)
178+
if rule.Tls {
179+
tlsEnabled = "yes"
180+
} else {
181+
tlsEnabled = "no"
182+
}
210183
}
211184

212185
table.AddRow(
@@ -249,10 +222,22 @@ func handleIngressDelete(ctx context.Context, cmd *cli.Command) error {
249222

250223
// generateIngressName generates an ingress name from hostname
251224
func generateIngressName(hostname string) string {
252-
// Replace dots with dashes, remove invalid chars
225+
// Replace dots with dashes
253226
name := strings.ReplaceAll(hostname, ".", "-")
254227
name = strings.ToLower(name)
255228

229+
// Remove invalid characters (only allow a-z, 0-9, and -)
230+
var cleaned strings.Builder
231+
for _, r := range name {
232+
if (r >= 'a' && r <= 'z') || (r >= '0' && r <= '9') || r == '-' {
233+
cleaned.WriteRune(r)
234+
}
235+
}
236+
name = cleaned.String()
237+
238+
// Trim leading/trailing dashes
239+
name = strings.Trim(name, "-")
240+
256241
// Add random suffix
257242
suffix := randomSuffix(4)
258243
return fmt.Sprintf("%s-%s", name, suffix)

pkg/cmd/run.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func handleRun(ctx context.Context, cmd *cli.Command) error {
8585
}
8686

8787
// Wait for image to be ready (build is asynchronous)
88-
if err := waitForImageReady(ctx, &client, image, imgInfo); err != nil {
88+
if err := waitForImageReady(ctx, &client, imgInfo); err != nil {
8989
return err
9090
}
9191

@@ -150,7 +150,7 @@ func isNotFoundError(err error, target **hypeman.Error) bool {
150150
}
151151

152152
// waitForImageReady polls image status until it becomes ready or failed
153-
func waitForImageReady(ctx context.Context, client *hypeman.Client, imageName string, img *hypeman.Image) error {
153+
func waitForImageReady(ctx context.Context, client *hypeman.Client, img *hypeman.Image) error {
154154
if img.Status == hypeman.ImageStatusReady {
155155
return nil
156156
}
@@ -161,7 +161,7 @@ func waitForImageReady(ctx context.Context, client *hypeman.Client, imageName st
161161
return fmt.Errorf("image build failed")
162162
}
163163

164-
// Poll until ready
164+
// Poll until ready using the normalized image name from the API response
165165
ticker := time.NewTicker(1 * time.Second)
166166
defer ticker.Stop()
167167

@@ -173,7 +173,7 @@ func waitForImageReady(ctx context.Context, client *hypeman.Client, imageName st
173173
case <-ctx.Done():
174174
return ctx.Err()
175175
case <-ticker.C:
176-
updated, err := client.Images.Get(ctx, imageName)
176+
updated, err := client.Images.Get(ctx, img.Name)
177177
if err != nil {
178178
return fmt.Errorf("failed to check image status: %w", err)
179179
}

0 commit comments

Comments
 (0)