Skip to content

Conversation

@alexzielenski
Copy link
Member

@alexzielenski alexzielenski commented Aug 17, 2023

#239 seems to have gone stale so I took a stab at fixing this myself.

One distinction this approach taken here has over the original attempt is that it is able to construct arbitrary interleavings of aliases and pointers from any default convertible to the same base type.

Based on #247 since it fixes bug resolving local package names

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Aug 17, 2023
@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Aug 17, 2023
Copy link
Member Author

@alexzielenski alexzielenski left a comment

Choose a reason for hiding this comment

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

Annotated some changes to give context to reviewer

var defaultString string
if len(defaultMap) == 1 {
defaultString = defaultMap[0]
} else if len(defaultMap) > 1 {
Copy link
Member Author

Choose a reason for hiding this comment

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

Moved this line up from below since defaultMap is not ever mutated

klog.Fatalf("Found more than one default tag for %v", t.Kind)
} else if len(defaultMap) == 0 {

if len(defaultString) == 0 {
Copy link
Member Author

@alexzielenski alexzielenski Aug 17, 2023

Choose a reason for hiding this comment

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

Changed len(defaultMap) == 0 -> len(defaultString) == 0` since that seemed to be the original intention to allow nested default to work. Nested default is only used if default map is empty, so the original condition here would block their use.


t, depth := resolveTypeAndDepth(t)
if depth > 0 && defaultString == "" {
baseT, depth := resolveTypeAndDepth(t)
Copy link
Member Author

@alexzielenski alexzielenski Aug 17, 2023

Choose a reason for hiding this comment

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

Renamed t->baseT since getNestedDefault would always be invoked on the primitive base type like string, int, struct, etc and always yield no nested default string unless base type was struct with default defined. But that case is already unusable due to mustEnforceDefault enforcing all structs use {} or nothing as their default value.

It seems the original intention of the code was to call getNestedDefault on the input argument. This would allow a default declared on an alias to be respected. I've refactored t->baseT usages elsewhere where the base primitive element type was desired.

@alexzielenski
Copy link
Member Author

/cc @sttts

@k8s-ci-robot k8s-ci-robot requested a review from sttts August 17, 2023 01:00
@alexzielenski alexzielenski force-pushed the default-alias-fix branch 4 times, most recently from c4967f5 to 7b0b2a6 Compare August 17, 2023 01:24
@k8s-ci-robot k8s-ci-robot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Aug 17, 2023
@alexzielenski alexzielenski force-pushed the default-alias-fix branch 3 times, most recently from 14834e1 to cafd10d Compare August 17, 2023 01:48
@apelisse
Copy link
Member

That's great, I still need to continue reviewing, I just want to mention that these defaults won't be included in the OpenAPI. The code that parses the default flags into their OpenAPI components lives in kube-openapi.

Comment on lines 144 to 148
// Type-level default is not respected unless a pointer
AliasDefault DefaultedValueItem `json:",omitempty"`

// Type-level default is not respected unless a pointer
AliasPointerDefault *DefaultedValueItem `json:",omitempty"`
Copy link
Member

Choose a reason for hiding this comment

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

My memory on this is blurry, but shouldn't we accept the defaults if it's omitempty, even if it's not a pointer?

Copy link
Member Author

Choose a reason for hiding this comment

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

That's what I would've expected but the source seems explicitly written to only read the default from the struct/alias type if the type is pointer (depth > 0):

func populateDefaultValue(node *callNode, t *types.Type, tags string, commentLines []string) *callNode {
defaultMap := extractDefaultTag(commentLines)
var defaultString string
if len(defaultMap) == 1 {
defaultString = defaultMap[0]
}
t, depth := resolveTypeAndDepth(t)
if depth > 0 && defaultString == "" {
defaultString = getNestedDefault(t)
}

Copy link
Member

Choose a reason for hiding this comment

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

That's probably a bug to be honest.

Copy link
Member

@apelisse apelisse left a comment

Choose a reason for hiding this comment

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

Thanks
/lgtm

// +default=ref(SomeDefault)
AliasOverride Item

// Type-level default is not respected unless a pointer
Copy link
Member

Choose a reason for hiding this comment

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

Not sure if that makes sense or not.

@k8s-ci-robot k8s-ci-robot added lgtm "Looks good to me", indicates that a PR is ready to be merged. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. labels Aug 22, 2023
@k8s-ci-robot k8s-ci-robot removed lgtm "Looks good to me", indicates that a PR is ready to be merged. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. labels Aug 23, 2023
@alexzielenski alexzielenski force-pushed the default-alias-fix branch 3 times, most recently from e625bcb to 5d46758 Compare August 24, 2023 00:23
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Aug 24, 2023
Copy link
Member

@apelisse apelisse left a comment

Choose a reason for hiding this comment

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

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 24, 2023
struct was getting very large

also adds omitempty tag for non-pointer fields. Openapi-gen would throw error. probably a bug that it is accepted in defaulter-gen

test: import package of types that are used for casting

cleanup: go mod tidy
also unconditionally cast when using references since we dont know their source type

otherwise if you have a `string` field, and provide a `type Value string`-valued default, you get an error trying to assign an alias to a string

cleanup and commenting
@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 24, 2023
Copy link
Member

@apelisse apelisse left a comment

Choose a reason for hiding this comment

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

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 24, 2023
@apelisse
Copy link
Member

pull-gengo-test seems to have errors but the job is still succeeding, is this expected?

@alexzielenski
Copy link
Member Author

alexzielenski commented Aug 24, 2023

pull-gengo-test seems to have errors but the job is still succeeding, is this expected?

yes those are expected errors from the import boss test

@alexzielenski
Copy link
Member Author

alexzielenski commented Aug 25, 2023

Tested against current k8s master branch with only one minor package naming change due to #247 (configv1alpha1 "pkg/controller/apis/config/v1alpha1" is no longer being added to import tracker since it is the local package):

diff --git a/pkg/controller/apis/config/v1alpha1/zz_generated.defaults.go b/pkg/controller/apis/config/v1alpha1/zz_generated.defaults.go
index bd27200c5a4..b441808f2ec 100644
--- a/pkg/controller/apis/config/v1alpha1/zz_generated.defaults.go
+++ b/pkg/controller/apis/config/v1alpha1/zz_generated.defaults.go
@@ -23,7 +23,7 @@ package v1alpha1
 
 import (
 	runtime "k8s.io/apimachinery/pkg/runtime"
-	cloudproviderconfigv1alpha1 "k8s.io/cloud-provider/config/v1alpha1"
+	configv1alpha1 "k8s.io/cloud-provider/config/v1alpha1"
 	v1alpha1 "k8s.io/kube-controller-manager/config/v1alpha1"
 )
 
@@ -39,5 +39,5 @@ func RegisterDefaults(scheme *runtime.Scheme) error {
 
 func SetObjectDefaults_KubeControllerManagerConfiguration(in *v1alpha1.KubeControllerManagerConfiguration) {
 	SetDefaults_KubeControllerManagerConfiguration(in)
-	cloudproviderconfigv1alpha1.SetDefaults_KubeCloudSharedConfiguration(&in.KubeCloudShared)
+	configv1alpha1.SetDefaults_KubeCloudSharedConfiguration(&in.KubeCloudShared)
 }

@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 25, 2023
Copy link
Member

@apelisse apelisse left a comment

Choose a reason for hiding this comment

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

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 28, 2023
@sttts
Copy link
Contributor

sttts commented Aug 29, 2023

/approve

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: alexzielenski, apelisse, sttts

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Aug 29, 2023
@k8s-ci-robot k8s-ci-robot merged commit 9cce18d into kubernetes:master Aug 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants