-
Notifications
You must be signed in to change notification settings - Fork 238
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
feat: Simplify the controller registration step via tooling #3459
Merged
google-oss-prow
merged 1 commit into
GoogleCloudPlatform:master
from
yuwenma:register-controller
Jan 8, 2025
+70
−30
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,14 +18,18 @@ import ( | |
"bytes" | ||
"errors" | ||
"fmt" | ||
"io/fs" | ||
"go/format" | ||
"go/parser" | ||
"go/token" | ||
"os" | ||
"path/filepath" | ||
"runtime/debug" | ||
"strings" | ||
"text/template" | ||
|
||
ccTemplate "github.com/GoogleCloudPlatform/k8s-config-connector/dev/tools/controllerbuilder/template/controller" | ||
"github.com/fatih/color" | ||
"golang.org/x/tools/go/ast/astutil" | ||
"golang.org/x/tools/imports" | ||
) | ||
|
||
|
@@ -38,14 +42,47 @@ var funcMap = template.FuncMap{ | |
"ToLower": strings.ToLower, | ||
} | ||
|
||
func Scaffold(service, kind string, cArgs *ccTemplate.ControllerArgs) error { | ||
if err := generateController(service, kind, cArgs); err != nil { | ||
func RegisterController(service, kind string) error { | ||
// Read register file | ||
directControllerPkgPath, err := buildDirectControllerPath() | ||
if err != nil { | ||
return nil | ||
} | ||
registerFilePath := filepath.Join(directControllerPkgPath, "register", "register.go") | ||
fset := token.NewFileSet() | ||
f, err := parser.ParseFile(fset, registerFilePath, nil, parser.ParseComments) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Get main model name | ||
bi, ok := debug.ReadBuildInfo() | ||
if !ok { | ||
return fmt.Errorf("could not read build info") | ||
} | ||
modelPath := strings.TrimSuffix(bi.Main.Path, currRelPath) | ||
|
||
importPath := filepath.Join(modelPath, directControllerRelPath, service) | ||
added := astutil.AddNamedImport(fset, f, "_", importPath) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very cool! |
||
if !added { | ||
fmt.Printf("skip registering controller %s\n", service) | ||
return nil | ||
} | ||
|
||
out := &bytes.Buffer{} | ||
err = format.Node(out, fset, f) | ||
if err != nil { | ||
return fmt.Errorf("error formatting code: %w", err) | ||
} | ||
|
||
if err := FormatImports(registerFilePath, out.Bytes()); err != nil { | ||
return err | ||
} | ||
color.HiGreen("New controller %s has been registered.\n", kind) | ||
return nil | ||
} | ||
|
||
func generateController(service, kind string, cArgs *ccTemplate.ControllerArgs) error { | ||
func GenerateController(service, kind string, cArgs *ccTemplate.ControllerArgs) error { | ||
tmpl, err := template.New(cArgs.Kind).Funcs(funcMap).Parse(ccTemplate.ControllerTemplate) | ||
if err != nil { | ||
return fmt.Errorf("parse controller template: %w", err) | ||
|
@@ -60,6 +97,12 @@ func generateController(service, kind string, cArgs *ccTemplate.ControllerArgs) | |
if err != nil { | ||
return err | ||
} | ||
if _, err := os.Stat(controllerFilePath); err == nil { | ||
fmt.Printf("file %s already exists, skipping\n", controllerFilePath) | ||
return nil | ||
} else if !errors.Is(err, os.ErrNotExist) { | ||
return fmt.Errorf("unexpected controller file: %w", err) | ||
} | ||
|
||
// Write the generated controller.go to pkg/controller/direct/<service>/<resource>_controller.go | ||
if err := WriteToFile(controllerFilePath, controllerOutput.Bytes()); err != nil { | ||
|
@@ -69,11 +112,11 @@ func generateController(service, kind string, cArgs *ccTemplate.ControllerArgs) | |
if err := FormatImports(controllerFilePath, controllerOutput.Bytes()); err != nil { | ||
return err | ||
} | ||
color.HiGreen("New controller %s has been generated. \nEnjoy it!\n", kind) | ||
color.HiGreen("New controller %s has been generated.", kind) | ||
return nil | ||
} | ||
|
||
func buildResourcePath(service, filename string) (string, error) { | ||
func buildDirectControllerPath() (string, error) { | ||
pwd, err := os.Getwd() | ||
if err != nil { | ||
return "", fmt.Errorf("get current working directory: %w", err) | ||
|
@@ -83,24 +126,21 @@ func buildResourcePath(service, filename string) (string, error) { | |
return "", fmt.Errorf("get absolute path %s: %w", pwd, err) | ||
} | ||
seg := strings.Split(abs, currRelPath) | ||
controllerDir := filepath.Join(seg[0], directControllerRelPath, service) | ||
return filepath.Join(seg[0], directControllerRelPath), nil | ||
} | ||
|
||
func buildControllerPath(service, protoResource string) (string, error) { | ||
filename := strings.ToLower(protoResource) + "_controller.go" | ||
directControllerPkgPath, err := buildDirectControllerPath() | ||
if err != nil { | ||
return "", nil | ||
} | ||
controllerDir := filepath.Join(directControllerPkgPath, service) | ||
err = os.MkdirAll(controllerDir, os.ModePerm) | ||
if err != nil { | ||
return "", fmt.Errorf("create controller directory %s: %w", controllerDir, err) | ||
} | ||
resourceFilePath := filepath.Join(controllerDir, filename) | ||
if _, err = os.Stat(resourceFilePath); err != nil { | ||
if !errors.Is(err, fs.ErrNotExist) { | ||
return "", fmt.Errorf("could not stat path %s: %w", resourceFilePath, err) | ||
} | ||
// otherwise create the file | ||
return resourceFilePath, nil | ||
} | ||
return "", fmt.Errorf("file %s already exist", resourceFilePath) | ||
} | ||
|
||
func buildControllerPath(service, protoResource string) (string, error) { | ||
return buildResourcePath(service, strings.ToLower(protoResource)+"_controller.go") | ||
return filepath.Join(controllerDir, filename), nil | ||
} | ||
|
||
func FormatImports(path string, out []byte) error { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: I think you can just always
return errors.Join(err1, err2)