Skip to content

Commit

Permalink
Merge pull request GoogleCloudPlatform#2130 from justinsb/cli_generat…
Browse files Browse the repository at this point in the history
…e_types

CLI: generate CRD types
  • Loading branch information
google-oss-prow[bot] authored Jul 10, 2024
2 parents a1690b5 + ce54065 commit 902d75b
Show file tree
Hide file tree
Showing 9 changed files with 650 additions and 17 deletions.
36 changes: 21 additions & 15 deletions dev/tools/controllerbuilder/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,46 +19,52 @@ import (
"os"
"strings"

"github.com/GoogleCloudPlatform/k8s-config-connector/dev/tools/controllerbuilder/pkg/commands/generatetypes"
"github.com/GoogleCloudPlatform/k8s-config-connector/dev/tools/controllerbuilder/pkg/options"
"github.com/GoogleCloudPlatform/k8s-config-connector/dev/tools/controllerbuilder/scaffold"
"github.com/GoogleCloudPlatform/k8s-config-connector/dev/tools/controllerbuilder/template"
"github.com/spf13/cobra"
)

var (
serviceName string
func buildAddCommand(baseOptions *options.GenerateOptions) *cobra.Command {
// TODO: Resource and kind name should be the same. Validation the uppercase/lowercase.
kind string
apiVersion string
kind := ""

addCmd = &cobra.Command{
addCmd := &cobra.Command{
Use: "add",
Short: "add direct controller",
RunE: func(cmd *cobra.Command, args []string) error {
// TODO(check kcc root)
cArgs := &template.ControllerArgs{
Service: serviceName,
Version: apiVersion,
Service: baseOptions.ServiceName,
Version: baseOptions.APIVersion,
Kind: kind,
KindToLower: strings.ToLower(kind),
}
path, err := scaffold.BuildControllerPath(serviceName, kind)
path, err := scaffold.BuildControllerPath(baseOptions.ServiceName, kind)
if err != nil {
return err
}
return scaffold.Scaffold(path, cArgs)
},
}
)

func init() {
addCmd.PersistentFlags().StringVarP(&apiVersion, "version", "v", "v1alpha1", "the KRM API version. used to import the KRM API")
addCmd.PersistentFlags().StringVarP(&serviceName, "service", "s", "", "the GCP service name")
addCmd.PersistentFlags().StringVarP(&kind, "resourceInKind", "r", "", "the GCP resource name under the GCP service. should be in camel case ")

return addCmd
}

func Execute() {
if err := addCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
var generateOptions options.GenerateOptions
generateOptions.InitDefaults()

rootCmd := &cobra.Command{}
generateOptions.BindPersistentFlags(rootCmd)

rootCmd.AddCommand(buildAddCommand(&generateOptions))
rootCmd.AddCommand(generatetypes.BuildCommand(&generateOptions))

if err := rootCmd.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
}
7 changes: 6 additions & 1 deletion dev/tools/controllerbuilder/go.mod
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
module github.com/GoogleCloudPlatform/k8s-config-connector/dev/tools/controllerbuilder

go 1.22
go 1.22.0

toolchain go1.22.5

require (
github.com/fatih/color v1.17.0
github.com/spf13/cobra v1.8.0
golang.org/x/tools v0.21.0
google.golang.org/protobuf v1.34.2
k8s.io/apimachinery v0.30.2
k8s.io/klog/v2 v2.130.1
)

require (
github.com/go-logr/logr v1.4.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
Expand Down
41 changes: 41 additions & 0 deletions dev/tools/controllerbuilder/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 83 additions & 0 deletions dev/tools/controllerbuilder/pkg/codegen/generatorbase.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package codegen

import (
"bytes"
"fmt"
"os"
"path/filepath"
"strings"
)

type generatorBase struct {
generatedFiles map[generatedFileKey]*generatedFile
}

func (g *generatorBase) init() {
g.generatedFiles = make(map[generatedFileKey]*generatedFile)
}

func (g *generatorBase) getOutputFile(k generatedFileKey) *generatedFile {
out := g.generatedFiles[k]
if out == nil {
out = &generatedFile{key: k}
g.generatedFiles[k] = out
}
return out
}

type generatedFile struct {
key generatedFileKey
contents bytes.Buffer
}

type generatedFileKey struct {
GoPackagePath string

File string
}

func (f *generatedFile) Write(baseDir string) error {
if f.contents.Len() == 0 {
return nil
}

fullName := f.key.GoPackagePath
tokens := strings.Split(fullName, ".")
dirTokens := []string{baseDir}
dirTokens = append(dirTokens, tokens...)
dir := filepath.Join(dirTokens...)

if err := os.MkdirAll(dir, 0755); err != nil {
return fmt.Errorf("creating directory %q: %w", dir, err)
}

p := filepath.Join(dir, f.key.File)
if err := os.WriteFile(p, f.contents.Bytes(), 0644); err != nil {
return fmt.Errorf("writing %q: %w", p, err)
}

return nil
}

func (v *generatorBase) WriteFiles(baseDir string) error {
for _, f := range v.generatedFiles {
if err := f.Write(baseDir); err != nil {
return err
}
}
return nil
}
Loading

0 comments on commit 902d75b

Please sign in to comment.