Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally generate an init func for an informer #2989

Merged
merged 5 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions codegen/cmd/injection-gen/generators/informer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package generators

import (
"io"
"os"
"strings"

clientgentypes "k8s.io/code-generator/cmd/client-gen/types"
"k8s.io/gengo/generator"
Expand All @@ -26,6 +28,8 @@ import (
"k8s.io/klog/v2"
)

const SkipInitFuncForInformerEnv = "SKIP_INIT_FUNC_FOR_INFORMER"

// injectionTestGenerator produces a file of listers for a given GroupVersion and
// type.
type injectionGenerator struct {
Expand Down Expand Up @@ -100,15 +104,21 @@ func (g *injectionGenerator) GenerateType(c *generator.Context, t *types.Type, w
}),
}

sw.Do(injectionInformer, m)
if os.Getenv(SkipInitFuncForInformerEnv) == "true" {
sw.Do(strings.ReplaceAll(injectionInformer, "withInformer", "WithInformer"), m)
} else {
sw.Do(initFunc+injectionInformer, m)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we pass the flag value to the context map m and just do the if logic in the template?

Copy link
Contributor Author

@skonto skonto Mar 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me try.

Copy link
Contributor Author

@skonto skonto Mar 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I didn't see it was using go templates behind the scenes, now it makes sense.


return sw.Error()
}

var injectionInformer = `
var initFunc = `
func init() {
{{.injectionRegisterInformer|raw}}(withInformer)
}
`
var injectionInformer = `

// Key is used for associating the Informer inside the context.Context.
type Key struct{}
Expand Down
5 changes: 5 additions & 0 deletions codegen/cmd/injection-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main

import (
"flag"
"os"

"k8s.io/klog/v2"

Expand All @@ -36,13 +37,17 @@ func main() {
genericArgs.AddFlags(pflag.CommandLine)
customArgs.AddFlags(pflag.CommandLine)
flag.Set("logtostderr", "true")
skipInitFuncForInformer := flag.Bool("skipInitFuncForInformer", false, "Skip the init function for informer")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, I was not sure if I should touch those.

pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()

if err := generatorargs.Validate(genericArgs); err != nil {
klog.Fatal("Error: ", err)
}

if *skipInitFuncForInformer {
os.Setenv(generators.SkipInitFuncForInformerEnv, "true")
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed if we setup args properly

// Run it.
if err := genericArgs.Execute(
generators.NameSystems(),
Expand Down
Loading