forked from GoogleCloudPlatform/k8s-config-connector
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
390 additions
and
10 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
// 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 scaffold | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"text/template" | ||
"unicode" | ||
"unicode/utf8" | ||
|
||
"github.com/GoogleCloudPlatform/k8s-config-connector/dev/tools/controllerbuilder/template/apis" | ||
"github.com/fatih/color" | ||
) | ||
|
||
type APIScaffolder struct { | ||
BaseDir string | ||
GoPackage string | ||
Service string | ||
Version string | ||
PackageProtoTag string | ||
} | ||
|
||
func (a *APIScaffolder) TypeFileNotExist(kind string) bool { | ||
typeFilePath := a.GetTypeFile(kind) | ||
_, err := os.Stat(typeFilePath) | ||
if err == nil { | ||
return false | ||
} | ||
return errors.Is(err, os.ErrNotExist) | ||
} | ||
|
||
func (a *APIScaffolder) GetTypeFile(kind string) string { | ||
fileName := strings.ToLower(kind) + "_types.go" | ||
return filepath.Join(a.BaseDir, a.GoPackage, fileName) | ||
} | ||
|
||
func (a *APIScaffolder) AddTypeFile(kind string) error { | ||
gcpResource := kind | ||
if !strings.HasPrefix(strings.ToLower(kind), a.Service) { | ||
s, size := utf8.DecodeRuneInString(a.Service) | ||
uc := unicode.ToUpper(s) | ||
kind = string(uc) + a.Service[size:] + kind | ||
} | ||
typeFilePath := a.GetTypeFile(kind) | ||
cArgs := &apis.APIArgs{ | ||
Service: a.Service, | ||
Version: a.Version, | ||
Kind: kind, | ||
PackageProtoTag: a.PackageProtoTag, | ||
KindProtoTag: a.PackageProtoTag + "." + gcpResource, | ||
} | ||
return scaffoldTypeFile(typeFilePath, cArgs) | ||
} | ||
|
||
func scaffoldTypeFile(path string, cArgs *apis.APIArgs) error { | ||
tmpl, err := template.New(cArgs.Kind).Parse(apis.TypesTemplate) | ||
if err != nil { | ||
return fmt.Errorf("parse %s_types.go template: %w", strings.ToLower(cArgs.Kind), err) | ||
} | ||
// Apply the APIArgs args to the template | ||
out := &bytes.Buffer{} | ||
if err := tmpl.Execute(out, cArgs); err != nil { | ||
return err | ||
} | ||
// Write the generated <kind>_types.go | ||
if err := WriteToFile(path, out.Bytes()); err != nil { | ||
return err | ||
} | ||
// Format and adjust the go imports in the generated files. | ||
if err := FormatImports(path, out.Bytes()); err != nil { | ||
return err | ||
} | ||
color.HiGreen("New API file added %s\nPlease EDIT it!\n", path) | ||
return nil | ||
} | ||
|
||
func (a *APIScaffolder) GroupVersionFileNotExist() bool { | ||
docFilePath := filepath.Join(a.BaseDir, a.GoPackage, "groupversion_info.go") | ||
_, err := os.Stat(docFilePath) | ||
if err == nil { | ||
return false | ||
} | ||
return errors.Is(err, os.ErrNotExist) | ||
} | ||
|
||
func (a *APIScaffolder) AddGroupVersionFile() error { | ||
docFilePath := filepath.Join(a.BaseDir, a.GoPackage, "groupversion_info.go") | ||
cArgs := &apis.APIArgs{ | ||
Service: a.Service, | ||
Version: a.Version, | ||
PackageProtoTag: a.PackageProtoTag, | ||
} | ||
return scaffoldGropuVersionFile(docFilePath, cArgs) | ||
} | ||
|
||
func (a *APIScaffolder) DocFileNotExist() bool { | ||
docFilePath := filepath.Join(a.BaseDir, a.GoPackage, "doc.go") | ||
_, err := os.Stat(docFilePath) | ||
if err == nil { | ||
return false | ||
} | ||
return errors.Is(err, os.ErrNotExist) | ||
} | ||
|
||
func (a *APIScaffolder) AddDocFile() error { | ||
docFilePath := filepath.Join(a.BaseDir, a.GoPackage, "doc.go") | ||
cArgs := &apis.APIArgs{ | ||
Service: a.Service, | ||
Version: a.Version, | ||
PackageProtoTag: a.PackageProtoTag, | ||
} | ||
return scaffoldDocFile(docFilePath, cArgs) | ||
} | ||
|
||
func scaffoldDocFile(path string, cArgs *apis.APIArgs) error { | ||
tmpl, err := template.New(cArgs.Service).Parse(apis.DocTemplate) | ||
if err != nil { | ||
return fmt.Errorf("parse doc.go template: %w", err) | ||
} | ||
out := &bytes.Buffer{} | ||
if err := tmpl.Execute(out, cArgs); err != nil { | ||
return err | ||
} | ||
if err := WriteToFile(path, out.Bytes()); err != nil { | ||
return err | ||
} | ||
color.HiGreen("New file added %s!\n", path) | ||
return nil | ||
} | ||
|
||
func scaffoldGropuVersionFile(path string, cArgs *apis.APIArgs) error { | ||
tmpl, err := template.New(cArgs.Service).Parse(apis.GroupVersionInfoTemplate) | ||
if err != nil { | ||
return fmt.Errorf("parse groupversion_info.go template: %w", err) | ||
} | ||
out := &bytes.Buffer{} | ||
if err := tmpl.Execute(out, cArgs); err != nil { | ||
return err | ||
} | ||
if err := WriteToFile(path, out.Bytes()); err != nil { | ||
return err | ||
} | ||
color.HiGreen("New file added %s!\n", path) | ||
return nil | ||
} |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package apis | ||
|
||
const DocTemplate = ` | ||
// 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. | ||
{{- if .PackageProtoTag }} | ||
// +kcc:proto={{ .PackageProtoTag }} | ||
{{- end }} | ||
package {{ .Version }} | ||
` |
37 changes: 37 additions & 0 deletions
37
dev/tools/controllerbuilder/template/apis/groupversion_info.go
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package apis | ||
|
||
const GroupVersionInfoTemplate = ` | ||
// 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. | ||
// +kubebuilder:object:generate=true | ||
// +groupName={{.Service}}.cnrm.cloud.google.com | ||
package {{ .Version }} | ||
import ( | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"sigs.k8s.io/controller-runtime/pkg/scheme" | ||
) | ||
var ( | ||
// GroupVersion is group version used to register these objects | ||
GroupVersion = schema.GroupVersion{Group: "{{.Service}}.cnrm.cloud.google.com", Version: "{{.Version}}"} | ||
// SchemeBuilder is used to add go types to the GroupVersionKind scheme | ||
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion} | ||
// AddToScheme adds the types in this group-version to the given scheme. | ||
AddToScheme = SchemeBuilder.AddToScheme | ||
) | ||
` |
Oops, something went wrong.