operator-framework / operator-sdk

SDK for building Kubernetes applications. Provides high level APIs, useful abstractions, and project scaffolding.
https://sdk.operatorframework.io
Apache License 2.0
7.25k stars 1.75k forks source link

segmentation fault building the bundle #5455

Closed raffaelespazzoli closed 2 years ago

raffaelespazzoli commented 2 years ago

Bug Report

running the bundle build command I get a segmentation fault.

What did you do?

here is the commands I run:

make manifests
make bundle IMG=quay.io/$repo/patch-operator:latest

on this project: https://github.com/raffaelespazzoli/patch-operator

What did you expect to see?

bundle gets created in the ./bundle directory

What did you see instead? Under which circumstances?

/home/rspazzol/go/src/github.com/redhat-cop/patch-operator/bin/controller-gen "crd:trivialVersions=true,preserveUnknownFields=false" rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases
operator-sdk generate kustomize manifests -q
fatal error: unexpected signal during runtime execution
[signal SIGSEGV: segmentation violation code=0x1 addr=0x63 pc=0x41f755]
runtime stack:
runtime.throw(0x25b38de, 0x2a)
    /opt/hostedtoolcache/go/1.16.10/x64/src/runtime/panic.go:1117 +0x72
runtime.sigpanic()
    /opt/hostedtoolcache/go/1.16.10/x64/src/runtime/signal_unix.go:718 +0x2e5
runtime.markrootSpans(0xc000077698, 0x16e7)
    /opt/hostedtoolcache/go/1.16.10/x64/src/runtime/mgcmark.go:346 +0x135
runtime.markroot(0xc000077698, 0x16ee)
    /opt/hostedtoolcache/go/1.16.10/x64/src/runtime/mgcmark.go:186 +0x369
runtime.gcDrain(0xc000077698, 0x7)
    /opt/hostedtoolcache/go/1.16.10/x64/src/runtime/mgcmark.go:1014 +0x118
runtime.gcBgMarkWorker.func2()
    /opt/hostedtoolcache/go/1.16.10/x64/src/runtime/mgc.go:2003 +0x17e
runtime.systemstack(0x0)
    /opt/hostedtoolcache/go/1.16.10/x64/src/runtime/asm_amd64.s:379 +0x66
runtime.mstart()
    /opt/hostedtoolcache/go/1.16.10/x64/src/runtime/proc.go:1246
goroutine 33 [GC worker (idle)]:
runtime.systemstack_switch()
    /opt/hostedtoolcache/go/1.16.10/x64/src/runtime/asm_amd64.s:339 fp=0xc00011a760 sp=0xc00011a758 pc=0x46b260
runtime.gcBgMarkWorker()
    /opt/hostedtoolcache/go/1.16.10/x64/src/runtime/mgc.go:1967 +0x1c7 fp=0xc00011a7e0 sp=0xc00011a760 pc=0x41dc47
runtime.goexit()
    /opt/hostedtoolcache/go/1.16.10/x64/src/runtime/asm_amd64.s:1371 +0x1 fp=0xc00011a7e8 sp=0xc00011a7e0 pc=0x46cf61
created by runtime.gcBgMarkStartWorkers
    /opt/hostedtoolcache/go/1.16.10/x64/src/runtime/mgc.go:1835 +0x37
goroutine 1 [runnable]:
runtime.Gosched(...)
    /opt/hostedtoolcache/go/1.16.10/x64/src/runtime/proc.go:292
github.com/operator-framework/operator-sdk/internal/generate/clusterserviceversion/bases/definitions.generator.getMarkedChildrenOfField.func1(0x28b81c0, 0xc000402220, 0x9c01)
    internal/generate/clusterserviceversion/bases/definitions/ast.go:157 +0x336
go/ast.inspector.Visit(0xc00a861700, 0x28b81c0, 0xc000402220, 0x28a9320, 0xc00a861700)
    /opt/hostedtoolcache/go/1.16.10/x64/src/go/ast/walk.go:373 +0x3a
go/ast.Walk(0x28a9320, 0xc00a861700, 0x28b81c0, 0xc000402220)
    /opt/hostedtoolcache/go/1.16.10/x64/src/go/ast/walk.go:52 +0x63
go/ast.Walk(0x28a9320, 0xc00a861700, 0x28b83f0, 0xc00168c150)
    /opt/hostedtoolcache/go/1.16.10/x64/src/go/ast/walk.go:111 +0x21fd
go/ast.Walk(0x28a9320, 0xc00a861700, 0x28b8468, 0xc00168c168)
    /opt/hostedtoolcache/go/1.16.10/x64/src/go/ast/walk.go:140 +0x2525
go/ast.Walk(0x28a9320, 0xc00a861700, 0x28b8058, 0xc00169e100)
    /opt/hostedtoolcache/go/1.16.10/x64/src/go/ast/walk.go:74 +0xb1f
go/ast.Inspect(...)
    /opt/hostedtoolcache/go/1.16.10/x64/src/go/ast/walk.go:385
github.com/operator-framework/operator-sdk/internal/generate/clusterserviceversion/bases/definitions.generator.getMarkedChildrenOfField(0xc00089d320, 0xc00089d350, 0xc0002ffac0, 0xc00168e130, 0x4, 0x0, 0x0, 0xc00169a091, 0x15, 0xc000b14150, ...)
    internal/generate/clusterserviceversion/bases/definitions/ast.go:110 +0x125
github.com/operator-framework/operator-sdk/internal/generate/clusterserviceversion/bases/definitions.generator.getTypedDescriptors(0xc00089d320, 0xc00089d350, 0xc0002ffac0, 0xc000ce6cc0, 0x29279d8, 0x23a4ca0, 0x251ee55, 0x4, 0x3e0000c001668340, 0xc001668340, ...)
    internal/generate/clusterserviceversion/bases/definitions/crd.go:128 +0x165
github.com/operator-framework/operator-sdk/internal/generate/clusterserviceversion/bases/definitions.generator.buildCRDDescriptionFromType(0xc00089d320, 0xc00089d350, 0xc000ac802d, 0x13, 0xc0015d7550, 0x8, 0xc00168e0f4, 0x5, 0xc0002ffac0, 0xc00168e0f4, ...)
    internal/generate/clusterserviceversion/bases/definitions/crd.go:101 +0x6e7
github.com/operator-framework/operator-sdk/internal/generate/clusterserviceversion/bases/definitions.ApplyDefinitionsForKeysGo(0xc0009ecd80, 0x251d37a, 0x3, 0xc000c91740, 0x1, 0x1, 0x15, 0x31)
    internal/generate/clusterserviceversion/bases/definitions/definitions.go:80 +0x6f0
github.com/operator-framework/operator-sdk/internal/generate/clusterserviceversion/bases.ClusterServiceVersion.GetBase(0xc000c7a940, 0x40, 0xc000c7f4f0, 0xe, 0x251c306, 0x2, 0x251d37a, 0x3, 0xc000c91740, 0x1, ...)
    internal/generate/clusterserviceversion/bases/clusterserviceversion.go:90 +0x35c
github.com/operator-framework/operator-sdk/internal/cmd/operator-sdk/generate/kustomize.manifestsCmd.run(0xc000c7f4f0, 0xe, 0xc000124c40, 0x10, 0xc000124c40, 0x10, 0x251d37a, 0x3, 0x1, 0x0, ...)
    internal/cmd/operator-sdk/generate/kustomize/manifests.go:205 +0x35a
github.com/operator-framework/operator-sdk/internal/cmd/operator-sdk/generate/kustomize.newManifestsCmd.func1(0xc000c6b180, 0xc000c71d10, 0x0, 0x1, 0x0, 0x0)
    internal/cmd/operator-sdk/generate/kustomize/manifests.go:118 +0x21b
github.com/spf13/cobra.(*Command).execute(0xc000c6b180, 0xc000c71d00, 0x1, 0x1, 0xc000c6b180, 0xc000c71d00)
    /home/runner/go/pkg/mod/github.com/spf13/cobra@v1.2.1/command.go:856 +0x472
github.com/spf13/cobra.(*Command).ExecuteC(0xc000c6f680, 0xc000c6f680, 0x40d52a, 0x7fc076d44d20)
    /home/runner/go/pkg/mod/github.com/spf13/cobra@v1.2.1/command.go:974 +0x375
github.com/spf13/cobra.(*Command).Execute(...)
    /home/runner/go/pkg/mod/github.com/spf13/cobra@v1.2.1/command.go:902
sigs.k8s.io/kubebuilder/v3/pkg/cli.CLI.Run(...)
    /home/runner/go/pkg/mod/sigs.k8s.io/kubebuilder/v3@v3.0.0-alpha.0.0.20211001202619-87eb9d55ecdc/pkg/cli/cli.go:463
github.com/operator-framework/operator-sdk/internal/cmd/operator-sdk/cli.Run(0xc0000600b8, 0x41ad01)
    internal/cmd/operator-sdk/cli/cli.go:68 +0x65
main.main()
    cmd/operator-sdk/main.go:28 +0x25
goroutine 6 [chan receive]:
k8s.io/klog/v2.(*loggingT).flushDaemon(0x3a0ef00)
    /home/runner/go/pkg/mod/k8s.io/klog/v2@v2.9.0/klog.go:1169 +0x8b
created by k8s.io/klog/v2.init.0
    /home/runner/go/pkg/mod/k8s.io/klog/v2@v2.9.0/klog.go:420 +0xdf
make: *** [Makefile:155: bundle] Error 2

Environment

Operator type:

/language go

Kubernetes cluster type:

N/A

$ operator-sdk version

operator-sdk version
operator-sdk version: "v1.15.0", commit: "f6326e832a8a5e5453d0ad25e86714a0de2c0fc8", kubernetes version: "1.21", go version: "go1.16.10", GOOS: "linux", GOARCH: "amd64"

$ go version (if language is Go)

go version
go version go1.16.8 linux/amd64

$ kubectl version

kubectl version
Client Version: version.Info{Major:"1", Minor:"21", GitVersion:"v1.21.0", GitCommit:"cb303e613a121a29364f75cc67d3d580833a7479", GitTreeState:"archive", BuildDate:"2021-07-22T00:00:00Z", GoVersion:"go1.16.6", Compiler:"gc", Platform:"linux/amd64"}
error: You must be logged in to the server (the server has asked for the client to provide credentials)

Possible Solution

this issue might be related: https://github.com/operator-framework/operator-sdk/issues/4990 I tried to understand the workaround and apply it to my project but it didn't work.

Additional context

the issue above suggests that when two types with the same name and different packages appear in the same CRD the operator-sdk bundle enters an infinite loop...

camilamacedo86 commented 2 years ago

Hi @raffaelespazzoli,

Could you please describe and point out what do you make here:

I tried to understand the workaround and apply it to my project but it didn't work.

Also, you say:

the issue above suggests that when two types with the same name and different packages appear in the same CRD the operator-sdk bundle enters an infinite loop...

I am looking at : https://github.com/raffaelespazzoli/patch-operator/blob/main/PROJECT and I can see only 1 API scaffold.

If you are looking to have multi-group support which means allow your project have more than one group for you have scenarios such as:

Then, you must allow its supports on the project. See:

Screenshot 2021-12-15 at 10 40 04

Link: https://sdk.operatorframework.io/docs/building-operators/golang/tutorial/

By clicking on the link informed you will be redirected to the doc: https://book.kubebuilder.io/migration/multi-group.html which has all steps.

Also, be aware of: https://sdk.operatorframework.io/docs/faqs/#can-i-customize-the-projects-initialized-with-operator-sdk and I'd recommend you always try to use the tool to do the scaffolds as not to deviate from the default layout and instead of that just add your customizations on top.

You can check the doc: https://sdk.operatorframework.io/docs/overview/project-layout/ to understand better the layout scaffold by default.

camilamacedo86 commented 2 years ago

Hi @raffaelespazzoli,

I could reproduce your scenario. Following the steps:

1) Create a new project:

mkdir patch-operator-bkp
cd patch-operator-bkp
operator-sdk init

2) Create the API

operator-sdk create api --group redhatcop --kind Patch --version v1

3) Create the webhook

operator-sdk create webhook --group redhatcop --kind Patch --version v1 --programmatic-validation --defaulting 

4) Run make bundle (ALL works fine)

The problem only occurs when we add a new attr for the type(API) using the lib: "github.com/redhat-cop/operator-utils/api/v1alpha1" and version v1.3.0

package v1

import (
    utilsv1alpha1 "github.com/redhat-cop/operator-utils/api/v1alpha1"
    corev1 "k8s.io/api/core/v1"
    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// EDIT THIS FILE!  THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required.  Any new fields you add must have json tags for the fields to be serialized.

// PatchSpec defines the desired state of Patch
type PatchSpec struct {
    // INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
    // Important: Run "make" to regenerate code after modifying this file

    // Patches is a list of patches that should be enforced at runtime.
    // +kubebuilder:validation:Required
    Patch *utilsv1alpha1.Patch `json:"patch,omitempty"`

    // ServiceAccountRef is the service account to be used to run the controllers associated with this configuration
    // +kubebuilder:validation:Required
    // +kubebuilder:default={"name": "default"}
    ServiceAccountRef corev1.LocalObjectReference `json:"serviceAccountRef,omitempty"`
}

What is the problem? Shows that the issue here is because you are adding the lib "github.com/redhat-cop/operator-utils/api/v1alpha1" and spec Patch *utilsv1alpha1.Patch json:"patch,omitempty" which requires a multigroup support layout. See; https://book.kubebuilder.io/migration/multi-group.html

Following the steps to check that it would be solved by using the multi-group support which shows required in your case because the lib added

1) Create a new project:

mkdir patch-operator-bkp
cd patch-operator-bkp
operator-sdk init

Add Multigroup support with operator-sdk edit --multigroup

2) Create the API

operator-sdk create api --group redhatcop --kind Patch --version v1

3) Create the webhook

operator-sdk create webhook --group redhatcop --kind Patch --version v1 --programmatic-validation --defaulting 

4) Run make bundle (ALL works fine)

The problem only occurs when we add a new attr for the type(API) using the lib: "github.com/redhat-cop/operator-utils/api/v1alpha1" and version v1.3.0

package v1

import (
    utilsv1alpha1 "github.com/redhat-cop/operator-utils/api/v1alpha1"
    corev1 "k8s.io/api/core/v1"
    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// EDIT THIS FILE!  THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required.  Any new fields you add must have json tags for the fields to be serialized.

// PatchSpec defines the desired state of Patch
type PatchSpec struct {
    // INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
    // Important: Run "make" to regenerate code after modifying this file

    // Patches is a list of patches that should be enforced at runtime.
    // +kubebuilder:validation:Required
    Patch *utilsv1alpha1.Patch `json:"patch,omitempty"`

    // ServiceAccountRef is the service account to be used to run the controllers associated with this configuration
    // +kubebuilder:validation:Required
    // +kubebuilder:default={"name": "default"}
    ServiceAccountRef corev1.LocalObjectReference `json:"serviceAccountRef,omitempty"`
}

5) Checking the result:

Screenshot 2021-12-15 at 11 27 28

You can check the POC test at: https://github.com/camilamacedo86/patch-operator-bkp

So, can we close this one since all shows are sorted out? Also, might be nice maybe we be able to raise a warning or error in this scenario when we try to run the command operator-sdk generate kustomize manifests -q. Would you like to collab with that?

raffaelespazzoli commented 2 years ago

wonderful thanks. we can close this.