-
Notifications
You must be signed in to change notification settings - Fork 13
Introduce resource.PartialObject and k8s.DeferredNegotiatedSerializer… #1024
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
Open
IfSentient
wants to merge
2
commits into
main
Choose a base branch
from
partial-object-decoding
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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 hidden or 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 hidden or 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,170 @@ | ||
| package resource | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "time" | ||
|
|
||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| "k8s.io/apimachinery/pkg/runtime/schema" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| ) | ||
|
|
||
| var _ Object = &PartialObject{} | ||
|
|
||
| // PartialObject implements resource.Object but only actually contains metadata information, and the raw payload that was used for unmarshaling. | ||
| // This is useful in accelerating the unmarshal process that is done serially with a NegotiatedSerializer in kubernetes watch requests, | ||
| // but does consume more memory as the entire original payload is embedded to avoid needing to copy or attempt to understand the non-metadata fields. | ||
| // | ||
| // PartialObject is _Experimental_ and may be removed in a future release | ||
| type PartialObject struct { | ||
| metav1.TypeMeta `json:",inline"` | ||
| metav1.ObjectMeta `json:"metadata"` | ||
| Raw []byte `json:"-"` | ||
| } | ||
|
|
||
| type metadataOnlyObject struct { | ||
| *metav1.TypeMeta `json:",inline"` | ||
| *metav1.ObjectMeta `json:"metadata"` | ||
| } | ||
|
|
||
| func (p *PartialObject) UnmarshalJSON(b []byte) error { | ||
| md := metadataOnlyObject{} | ||
| if err := json.Unmarshal(b, &md); err != nil { | ||
| return err | ||
| } | ||
| p.TypeMeta = *md.TypeMeta | ||
| p.ObjectMeta = *md.ObjectMeta | ||
| p.Raw = b | ||
| return nil | ||
| } | ||
|
|
||
| func (p *PartialObject) GetRaw() []byte { | ||
| return p.Raw | ||
| } | ||
|
|
||
| func (p *PartialObject) DeepCopyObject() runtime.Object { | ||
| return p.Copy() | ||
| } | ||
|
|
||
| func (*PartialObject) GetSpec() any { | ||
| return nil | ||
| } | ||
|
|
||
| func (*PartialObject) SetSpec(any) error { | ||
| return fmt.Errorf("spec cannot be set on a PartialObject") | ||
| } | ||
|
|
||
| func (*PartialObject) GetSubresources() map[string]any { | ||
| return map[string]any{} | ||
| } | ||
|
|
||
| func (*PartialObject) GetSubresource(string) (any, bool) { | ||
| return nil, false | ||
| } | ||
|
|
||
| func (*PartialObject) SetSubresource(string, any) error { | ||
| return fmt.Errorf("subresource cannot be set on a PartialObject") | ||
| } | ||
|
|
||
| func (p *PartialObject) GetStaticMetadata() StaticMetadata { | ||
| return StaticMetadata{ | ||
| Name: p.ObjectMeta.Name, | ||
| Namespace: p.ObjectMeta.Namespace, | ||
| Group: p.GroupVersionKind().Group, | ||
| Version: p.GroupVersionKind().Version, | ||
| Kind: p.GroupVersionKind().Kind, | ||
| } | ||
| } | ||
|
|
||
| func (p *PartialObject) SetStaticMetadata(metadata StaticMetadata) { | ||
| p.Name = metadata.Name | ||
| p.Namespace = metadata.Namespace | ||
| p.SetGroupVersionKind(schema.GroupVersionKind{ | ||
| Group: metadata.Group, | ||
| Version: metadata.Version, | ||
| Kind: metadata.Kind, | ||
| }) | ||
| } | ||
|
|
||
| // GetCommonMetadata returns CommonMetadata for the object | ||
| // | ||
| //nolint:dupl | ||
| func (p *PartialObject) GetCommonMetadata() CommonMetadata { | ||
| var err error | ||
| dt := p.DeletionTimestamp | ||
| var deletionTimestamp *time.Time | ||
| if dt != nil { | ||
| deletionTimestamp = &dt.Time | ||
| } | ||
| updt := time.Time{} | ||
| createdBy := "" | ||
| updatedBy := "" | ||
| if p.Annotations != nil { | ||
| strUpdt, ok := p.Annotations[AnnotationUpdateTimestamp] | ||
| if ok { | ||
| updt, err = time.Parse(time.RFC3339, strUpdt) | ||
| if err != nil { //nolint:staticcheck,revive | ||
| // HMMMM | ||
| } | ||
| } | ||
| createdBy = p.Annotations[AnnotationCreatedBy] | ||
| updatedBy = p.Annotations[AnnotationUpdatedBy] | ||
| } | ||
| return CommonMetadata{ | ||
| UID: string(p.UID), | ||
| ResourceVersion: p.ResourceVersion, | ||
| Generation: p.Generation, | ||
| Labels: p.Labels, | ||
| CreationTimestamp: p.CreationTimestamp.Time, | ||
| DeletionTimestamp: deletionTimestamp, | ||
| Finalizers: p.Finalizers, | ||
| UpdateTimestamp: updt, | ||
| CreatedBy: createdBy, | ||
| UpdatedBy: updatedBy, | ||
| // TODO: populate ExtraFields in PartialObject? | ||
| } | ||
| } | ||
|
|
||
| // SetCommonMetadata sets CommonMetadata for the object | ||
| // | ||
| //nolint:dupl | ||
| func (p *PartialObject) SetCommonMetadata(metadata CommonMetadata) { | ||
| p.UID = types.UID(metadata.UID) | ||
| p.ResourceVersion = metadata.ResourceVersion | ||
| p.Generation = metadata.Generation | ||
| p.Labels = metadata.Labels | ||
| p.CreationTimestamp = metav1.NewTime(metadata.CreationTimestamp) | ||
| if metadata.DeletionTimestamp != nil { | ||
| dt := metav1.NewTime(*metadata.DeletionTimestamp) | ||
| p.DeletionTimestamp = &dt | ||
| } else { | ||
| p.DeletionTimestamp = nil | ||
| } | ||
| p.Finalizers = metadata.Finalizers | ||
| if p.Annotations == nil { | ||
| p.Annotations = make(map[string]string) | ||
| } | ||
| if !metadata.UpdateTimestamp.IsZero() { | ||
| p.Annotations[AnnotationUpdateTimestamp] = metadata.UpdateTimestamp.Format(time.RFC3339) | ||
| } | ||
| if metadata.CreatedBy != "" { | ||
| p.Annotations[AnnotationCreatedBy] = metadata.CreatedBy | ||
| } | ||
| if metadata.UpdatedBy != "" { | ||
| p.Annotations[AnnotationUpdatedBy] = metadata.UpdatedBy | ||
| } | ||
| } | ||
|
|
||
| func (p *PartialObject) Copy() Object { | ||
| cpy := PartialObject{} | ||
| cpy.TypeMeta = metav1.TypeMeta{ | ||
| Kind: p.Kind, | ||
| APIVersion: p.APIVersion, | ||
| } | ||
| p.ObjectMeta.DeepCopyInto(&cpy.ObjectMeta) | ||
| cpy.Raw = make([]byte, len(p.Raw)) | ||
| copy(cpy.Raw, p.Raw) | ||
| return &cpy | ||
| } | ||
This file contains hidden or 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 hidden or 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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -111,12 +111,15 @@ type App struct { | |||||
|
|
||||||
| // AppConfig is the configuration used by App | ||||||
| type AppConfig struct { | ||||||
| Name string | ||||||
| KubeConfig rest.Config | ||||||
| InformerConfig AppInformerConfig | ||||||
| ManagedKinds []AppManagedKind | ||||||
| UnmanagedKinds []AppUnmanagedKind | ||||||
| Converters map[schema.GroupKind]Converter | ||||||
| Name string | ||||||
| KubeConfig rest.Config | ||||||
| // ClientGenerator is the ClientGenerator to use when constructing informers. | ||||||
| // It is optional and will default to k8s.NewClientRegistry(KubeConfig, k8s.DefaultClientConfig()) if not present. | ||||||
| ClientGenerator resource.ClientGenerator | ||||||
| InformerConfig AppInformerConfig | ||||||
| ManagedKinds []AppManagedKind | ||||||
| UnmanagedKinds []AppUnmanagedKind | ||||||
| Converters map[schema.GroupKind]Converter | ||||||
| // VersionedCustomRoutes is a map of version string => custom route handlers for | ||||||
| // custom routes attached at the version level rather than attached to a specific kind. | ||||||
| // Custom route paths for each version should not conflict with plural names of kinds for the version. | ||||||
|
|
@@ -267,10 +270,14 @@ type AppVersionRouteHandlers map[AppVersionRoute]AppCustomRouteHandler | |||||
| // AppConfig MUST contain a valid KubeConfig to be valid. | ||||||
| // Watcher/Reconciler error handling, retry, and dequeue logic can be managed with AppConfig.InformerConfig. | ||||||
| func NewApp(config AppConfig) (*App, error) { | ||||||
| clients := config.ClientGenerator | ||||||
| if clients == nil { | ||||||
| k8s.NewClientRegistry(config.KubeConfig, k8s.DefaultClientConfig()) | ||||||
|
Contributor
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.
Suggested change
Contributor
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. Note, that this should go away after syncing with |
||||||
| } | ||||||
| a := &App{ | ||||||
| informerController: operator.NewInformerController(operator.DefaultInformerControllerConfig()), | ||||||
| runner: app.NewMultiRunner(), | ||||||
| clientGenerator: k8s.NewClientRegistry(config.KubeConfig, k8s.DefaultClientConfig()), | ||||||
| clientGenerator: clients, | ||||||
| kinds: make(map[string]AppManagedKind), | ||||||
| gvrToGVK: make(map[string]string), | ||||||
| internalKinds: make(map[string]resource.Kind), | ||||||
|
|
||||||
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.
These could be nil, couldn't they?