krisnova / naml

Convert Kubernetes YAML to Golang
Apache License 2.0
1.26k stars 38 forks source link

Failed to build result of "naml codify" #99

Open larsks opened 2 years ago

larsks commented 2 years ago

I have an example app that consists of a Service:

apiVersion: v1
kind: Service
metadata:
  labels:
    app: example
  name: example
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 8080
  selector:
    app: example
  type: LoadBalancer

And a Deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: example
  name: example
spec:
  replicas: 1
  selector:
    matchLabels:
      app: example
  template:
    metadata:
      labels:
        app: example
    spec:
      containers:
      - containerPorts:
        - containerPort: 8080
          name: http
          protocol: TCP
        image: alpinelinux/darkhttpd
        name: darkhttpd

If I try to follow the instructions in the README, the build fails because of missing dependencies:

$ mkdir out
$ cat deployment.yaml service.yaml | naml codify > out/main.go
$ cd out
$ naml build -o app
2021-11-21T08:15:27-05:00 [Critical  ]  unable to build NAML binary from source:

+-------------------------+---------------------------------
| Codify Compile Failure  |
+-------------------------+
|
|
| /tmp/3696928851.go:34:2: no required module provides package github.com/kris-nova/naml: go.mod file not found in current directory or any parent directory; see 'go help modules'
/tmp/3696928851.go:30:2: no required module provides package k8s.io/api/core/v1: go.mod file not found in current directory or any parent directory; see 'go help modules'
/tmp/3696928851.go:31:2: no required module provides package k8s.io/apimachinery/pkg/apis/meta/v1: go.mod file not found in current directory or any parent directory; see 'go help modules'
/tmp/3696928851.go:35:2: no required module provides package k8s.io/apimachinery/pkg/runtime: go.mod file not found in current directory or any parent directory; see 'go help modules'
/tmp/3696928851.go:32:2: no required module provides package k8s.io/apimachinery/pkg/util/intstr: go.mod file not found in current directory or any parent directory; see 'go help modules'
/tmp/3696928851.go:36:2: no required module provides package k8s.io/client-go/kubernetes: go.mod file not found in current directory or any parent directory; see 'go help modules'

If I try to resolve the dependency problems by running go mod init and go tidy...

$ go mod init example
go: creating new go.mod: module example
go: to add module requirements and sums:
        go mod tidy
$ go mod tidy
go: finding module for package github.com/kris-nova/naml
go: finding module for package k8s.io/api/core/v1
go: finding module for package k8s.io/apimachinery/pkg/apis/meta/v1
go: finding module for package k8s.io/apimachinery/pkg/util/intstr
go: finding module for package k8s.io/client-go/kubernetes
go: finding module for package k8s.io/apimachinery/pkg/runtime
go: found github.com/kris-nova/naml in github.com/kris-nova/naml v1.0.1
go: found k8s.io/api/core/v1 in k8s.io/api v0.22.4
go: found k8s.io/apimachinery/pkg/apis/meta/v1 in k8s.io/apimachinery v0.22.4
go: found k8s.io/apimachinery/pkg/runtime in k8s.io/apimachinery v0.22.4
go: found k8s.io/apimachinery/pkg/util/intstr in k8s.io/apimachinery v0.22.4
go: found k8s.io/client-go/kubernetes in k8s.io/client-go v0.22.4

...and then attempt to build, it fails with unused imports and an undefined reference:

$ naml build -o app
2021-11-21T08:16:51-05:00 [Critical  ]  unable to build NAML binary from source:

+-------------------------+---------------------------------
| Codify Compile Failure  |
+-------------------------+
|
|
| # command-line-arguments
/tmp/2642747543.go:30:2: imported and not used: "k8s.io/api/core/v1" as corev1
/tmp/2642747543.go:32:2: imported and not used: "k8s.io/apimachinery/pkg/util/intstr"
/tmp/2642747543.go:70:21: undefined: Service
+----------------------------------------------------------

Is the README missing some steps? Am I missing some steps?

I am using naml 1.0.1.

krisnova commented 2 years ago

Thanks for providing the YAML and the failure!

I will add this as a regression in the unit tests and cut a release with the fix. Stay tuned!

krisnova commented 2 years ago

The problem was that naml was expecting the naml delimiter to separate the two files Reforming your command to include the delimiter will help

printf "\n\n---\n\n" | cat file1.yaml - file2.yaml - file3.yaml | naml codify > out/main.go
larsks commented 2 years ago

Well now I feel stupid for not even thinking about that because I deal with multi-document YAML files all the time and never even considered that. But even ignoring that particular issue, I still see the build failures:

$ mkdir out
$ naml codify < deployment.yaml > out/main.go
$ cd out
$ naml build -o app
2021-11-22T11:19:27-05:00 [Critical  ]  unable to build NAML binary from source:

+-------------------------+---------------------------------
| Codify Compile Failure  |
+-------------------------+
|
|
| /tmp/1481936367.go:30:2: no required module provides package github.com/hexops/valast: go.mod file not found in current directory or any parent directory; see 'go help modules'
/tmp/1481936367.go:35:2: no required module provides package github.com/kris-nova/naml: go.mod file not found in current directory or any parent directory; see 'go help modules'
/tmp/1481936367.go:31:2: no required module provides package k8s.io/api/apps/v1: go.mod file not found in current directory or any parent directory; see 'go help modules'
/tmp/1481936367.go:32:2: no required module provides package k8s.io/api/core/v1: go.mod file not found in current directory or any parent directory; see 'go help modules'
/tmp/1481936367.go:33:2: no required module provides package k8s.io/apimachinery/pkg/apis/meta/v1: go.mod file not found in current directory or any parent directory; see 'go help modules'
/tmp/1481936367.go:36:2: no required module provides package k8s.io/apimachinery/pkg/runtime: go.mod file not found in current directory or any parent directory; see 'go help modules'
/tmp/1481936367.go:37:2: no required module provides package k8s.io/client-go/kubernetes: go.mod file not found in current directory or any parent directory; see 'go help modules'
+----------------------------------------------------------
krisnova commented 2 years ago

Oh I didn't see that you were using naml build which is basically just a wrapper of the Go compiler.

For what it's worth I always work using the /out directory in the naml repository.

The provided go.mod that ships with naml should include all the dependencies you need to build your binary.

git clone git@github.com:kris-nova/naml.git
cd naml
git checkout tags/v1.0.1 -b v1.0.1
cat deploy.yaml | naml codify > out/main.go
cd out
naml build -o app
./app
krisnova commented 2 years ago

I think your error still deserves a test and a workflow, i will repeat the same process that I did with #100 and follow up

larsks commented 2 years ago

For what it's worth I always work using the /out directory in the naml repository.

Ah, I see. The README wasn't clear that the out directory was created inside the checked out naml repository. I assumed from the instructions that I could just install the naml binary and run with it.

If I create the output directory inside the naml repository, things work as documented.

krisnova commented 2 years ago

I assumed from the instructions that I could just install the naml binary and run with it.

I want to be clear. We do want to support that functionality with naml.

I think we are learning that naml is probably going to have to have a command such as naml init or naml env that will initialize a directory for compiling.

Keeping this issue open as a reminder for myself to address this in a later release.

Happy to know your code is working now.