spinkube / spin-operator

Spin Operator is a Kubernetes operator that empowers platform engineers to deploy Spin applications as custom resources to their Kubernetes clusters
https://www.spinkube.dev/docs/spin-operator/
Other
108 stars 15 forks source link

unable to hit external API from SPIN Go component running in SpinKube #216

Open camba1 opened 1 week ago

camba1 commented 1 week ago

What were you doing

Trying to run a simple SPIN Go component in Kubernetes that hits an external (third party) API .

What did you expect

Component to hit the cat facts public API (https://catfact.ninja/fact) and return a fact that would then be sent to the end user

What happened

The component seems to thrown a " Runtime error" on the following line. Not additional logs printed.

req, err := spinhttp.Get("https://catfact.ninja/fact")

Additional Info

Steps to reproduce

Create a simple http-go sample application

spin new -t http-go --accept-defaults simple-get-go  

change the contents of the init function in the main.go file to :

func init() {
    spinhttp.Handle(func(w http.ResponseWriter, r *http.Request) {

        req, err := spinhttp.Get("https://catfact.ninja/fact")
        if err != nil {
            fmt.Printf("Problem getting cat fact. %s\n", err.Error())
            http.Error(w, err.Error(), http.StatusInternalServerError)
            return
        }

        decoder := json.NewDecoder(req.Body)
        newFact := struct {
            Fact   string `json:"fact"`
            Length int    `json:"length"`
        }{}

        err = decoder.Decode(&newFact)
        if err != nil {
            log.Println(err)
            return
        }

        w.Header().Set("Content-Type", "text/plain")
        fmt.Fprintln(w, newFact.Fact)
    })
}

Push container to the image repository

spin registry push --build bolbeck/simple-get-go    

Scaffold the K8s manifest

mkdir k8s
spin kube scaffold --from bolbeck/simple-get-go --out k8s/simplegetgo.yaml

Deploy the container to k3d and port forward to it:

 kubectl apply -f k8s/simplegetgo.yaml       
kubectl port-forward svc/simple-get-go 8083:80     

Navigate to localhost:8083 in the browser (or curl it) and the application throws a "Runtime error" message

Environment

k3d version v5.6.0 k3s version v1.27.5-k3s1 (default) spin-operator 0.1.0

What is the best way to debug this in K3d given that the WASM container does not have a distro and thus I assume one cannot log into the container?

radu-matei commented 1 week ago

Hey, @camba1! We're really close to addressing this in https://github.com/spinkube/spin-operator/pull/207.