Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions pkg/applyconfiguration/applyconfiguration_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ var _ = Describe("ApplyConfiguration generation from API types", func() {
Expect(optionsRegistry.Register(markers.Must(markers.MakeDefinition("applyconfiguration", markers.DescribesPackage, Generator{})))).To(Succeed())

rt, err := genall.FromOptions(optionsRegistry, []string{
"applyconfiguration",
"applyconfiguration:externalApplyConfigurations=sigs.k8s.io/controller-tools/pkg/applyconfiguration/testdata/cronjob/external.ExternalData@sigs.k8s.io/controller-tools/pkg/applyconfiguration/testdata/cronjob/externalac",
"paths=./api/v1",
})
Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -218,7 +218,7 @@ var _ = Describe("ApplyConfiguration generation from API types", func() {

rt, err := genall.FromOptions(optionsRegistry, []string{
"crd:allowDangerousTypes=true,ignoreUnexportedFields=true", // Run another generator first to make sure they don't interfere; see also: the comment on cronjob_types.go:UntypedBlob
"applyconfiguration",
"applyconfiguration:externalApplyConfigurations=sigs.k8s.io/controller-tools/pkg/applyconfiguration/testdata/cronjob/external.ExternalData@sigs.k8s.io/controller-tools/pkg/applyconfiguration/testdata/cronjob/externalac",
"paths=./api/v1",
})
Expect(err).NotTo(HaveOccurred())
Expand Down
42 changes: 36 additions & 6 deletions pkg/applyconfiguration/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ import (
"errors"
"fmt"
"go/ast"
"maps"
"os"
"path/filepath"
"strings"

"k8s.io/gengo/v2/types"

"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/code-generator/cmd/applyconfiguration-gen/args"
Expand Down Expand Up @@ -56,6 +60,16 @@ const defaultOutputPackage = "applyconfiguration"
type Generator struct {
// HeaderFile specifies the header text (e.g. license) to prepend to generated files.
HeaderFile string `marker:",optional"`

// ExternalApplyConfigurations provides mappings between external types and their applyconfiguration packages.
//
// Use this to reference apply configuration types for external types referenced
// by the Go structs provided as input. Each entry should be in the format:
// <package>.<TypeName>@<applyconfiguration-package>
//
// For example, to reference the apply configuration for corev1.LocalObjectReference:
// k8s.io/api/core/v1.LocalObjectReference@k8s.io/client-go/applyconfigurations/core/v1
ExternalApplyConfigurations []string `marker:",optional"`
}

func (Generator) CheckFilter() loader.NodeFilter {
Expand Down Expand Up @@ -143,10 +157,22 @@ func (d Generator) Generate(ctx *genall.GenerationContext) error {
headerFilePath = tmpFile.Name()
}

// Parse external apply configurations
externalACs := make(map[types.Name]string)
for _, ext := range d.ExternalApplyConfigurations {
parts := strings.SplitN(ext, "@", 2)
if len(parts) != 2 {
return fmt.Errorf("invalid external apply configuration format %q, expected <package>.<TypeName>@<applyconfiguration-package>", ext)
}
typeName := types.ParseFullyQualifiedName(parts[0])
externalACs[typeName] = parts[1]
}

objGenCtx := ObjectGenCtx{
Collector: ctx.Collector,
Checker: ctx.Checker,
HeaderFilePath: headerFilePath,
Collector: ctx.Collector,
Checker: ctx.Checker,
HeaderFilePath: headerFilePath,
ExternalApplyConfigurations: externalACs,
}

errs := []error{}
Expand All @@ -167,9 +193,10 @@ func (d Generator) Generate(ctx *genall.GenerationContext) error {
// It mostly exists so that generating for a package can be easily tested without
// requiring a full set of output rules, etc.
type ObjectGenCtx struct {
Collector *markers.Collector
Checker *loader.TypeChecker
HeaderFilePath string
Collector *markers.Collector
Checker *loader.TypeChecker
HeaderFilePath string
ExternalApplyConfigurations map[types.Name]string
}

// generateForPackage generates apply configuration implementations for
Expand All @@ -186,6 +213,9 @@ func (ctx *ObjectGenCtx) generateForPackage(root *loader.Package) error {
arguments := args.New()
arguments.GoHeaderFile = ctx.HeaderFilePath

// Set external apply configurations
maps.Copy(arguments.ExternalApplyConfigurations, ctx.ExternalApplyConfigurations)

outpkg := outputPkg(ctx.Collector, root)

arguments.OutputDir = filepath.Join(root.Dir, outpkg)
Expand Down

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

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

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

15 changes: 15 additions & 0 deletions pkg/applyconfiguration/testdata/cronjob/api/v1/cronjob_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/intstr"

"sigs.k8s.io/controller-tools/pkg/applyconfiguration/testdata/cronjob/external"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
Expand Down Expand Up @@ -390,6 +392,10 @@ type CronJobSpec struct {
// Test that we can add a field that can only be set to a non-default value on updates using XValidation OptionalOldSelf.
// +kubebuilder:validation:XValidation:rule="oldSelf.hasValue() || self == 0",message="must be set to 0 on creation. can be set to any value on an update.",optionalOldSelf=true
OnlyAllowSettingOnUpdate int32 `json:"onlyAllowSettingOnUpdate,omitempty"`

// EmbeddedExternal tests the ExternalApplyConfigurations feature.
// +optional
EmbeddedExternal *EmbeddedExternalSpec `json:"embeddedExternal,omitempty"`
}

type InlineAlias = EmbeddedStruct
Expand Down Expand Up @@ -766,3 +772,12 @@ type CronJobList struct {
metav1.ListMeta `json:"metadata,omitempty"`
Items []CronJob `json:"items"`
}

// EmbeddedExternalSpec embeds an external type with json:",inline".
// Used to test the ExternalApplyConfigurations feature.
type EmbeddedExternalSpec struct {
external.ExternalData `json:",inline"`

// Extra is an additional field.
Extra string `json:"extra,omitempty"`
}
25 changes: 25 additions & 0 deletions pkg/applyconfiguration/testdata/cronjob/external/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
Copyright 2024 The Kubernetes Authors.
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 external

// ExternalData is a type defined outside the API package,
// used to test generation of apply configurations for types
// that reference external types.
type ExternalData struct {
Key string `json:"key,omitempty"`
Value string `json:"value,omitempty"`
}
24 changes: 24 additions & 0 deletions pkg/applyconfiguration/testdata/cronjob/externalac/externaldata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
Copyright 2024 The Kubernetes Authors.
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 externalac

// ExternalDataApplyConfiguration is a placeholder AC type for testing.
// In real usage, this would be a generated apply configuration type.
type ExternalDataApplyConfiguration struct {
Key *string `json:"key,omitempty"`
Value *string `json:"value,omitempty"`
}
4 changes: 4 additions & 0 deletions pkg/applyconfiguration/zz_generated.markerhelp.go

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