-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement compression of config fields on resources
Add compression support. Signed-off-by: Utku Ozdemir <utku.ozdemir@siderolabs.com>
- Loading branch information
1 parent
4ed9049
commit e3d46f9
Showing
86 changed files
with
30,627 additions
and
960 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
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
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
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
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,102 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package specs | ||
|
||
import ( | ||
"errors" | ||
|
||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
// MarshalJSON implements json.Marshaler interface. | ||
// | ||
// It represents compressed fields as uncompressed in the output. | ||
func (x *ClusterMachineConfigSpec) MarshalJSON() ([]byte, error) { | ||
obj := x.CloneVT() | ||
|
||
buffer, err := obj.GetUncompressedData() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer buffer.Free() | ||
|
||
obj.Data = buffer.Data() | ||
obj.CompressedData = nil | ||
|
||
return jsonMarshaler.Marshal(obj) | ||
} | ||
|
||
// UnmarshalJSON implements json.Unmarshaler interface. | ||
func (x *ClusterMachineConfigSpec) UnmarshalJSON(data []byte) error { | ||
return unmarshalJSON(x, data) | ||
} | ||
|
||
// MarshalYAML implements yaml.Marshaler interface. | ||
// | ||
// It represents compressed fields as uncompressed in the output. | ||
func (x *ClusterMachineConfigSpec) MarshalYAML() (any, error) { | ||
obj := x.CloneVT() | ||
|
||
buffer, err := obj.GetUncompressedData() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer buffer.Free() | ||
|
||
obj.Data = buffer.Data() | ||
obj.CompressedData = nil | ||
|
||
type alias *ClusterMachineConfigSpec // prevent recursion | ||
|
||
return alias(obj), nil | ||
} | ||
|
||
// UnmarshalYAML implements yaml.Unmarshaler interface. | ||
func (x *ClusterMachineConfigSpec) UnmarshalYAML(node *yaml.Node) error { | ||
type alias ClusterMachineConfigSpec // prevent recursion | ||
|
||
aux := (*alias)(x) | ||
|
||
return unmarshalYAML(x, aux, node) | ||
} | ||
|
||
// GetUncompressedData returns the config data from the ClusterMachineConfigSpec, decompressing it if necessary. | ||
func (x *ClusterMachineConfigSpec) GetUncompressedData(opts ...CompressionOption) (Buffer, error) { | ||
if x == nil { | ||
return newNoOpBuffer(nil), nil | ||
} | ||
|
||
if x.CompressedData == nil { | ||
return newNoOpBuffer(x.Data), nil | ||
} | ||
|
||
return doDecompress(x.CompressedData, getCompressionConfig(opts)) | ||
} | ||
|
||
// SetUncompressedData sets the config data in the ClusterMachineConfigSpec, compressing it if requested. | ||
func (x *ClusterMachineConfigSpec) SetUncompressedData(data []byte, opts ...CompressionOption) error { | ||
if x == nil { | ||
return errors.New("ClusterMachineConfigSpec is nil") | ||
} | ||
|
||
config := getCompressionConfig(opts) | ||
compress := config.Enabled | ||
|
||
if !compress { | ||
x.Data = data | ||
x.CompressedData = nil | ||
|
||
return nil | ||
} | ||
|
||
compressed := doCompress(data, config) | ||
|
||
x.CompressedData = compressed | ||
x.Data = nil | ||
|
||
return nil | ||
} |
Oops, something went wrong.