Skip to content

Commit 708958c

Browse files
committed
refactor: move unmarshal functions to api/ module
1 parent 8f12921 commit 708958c

File tree

3 files changed

+64
-34
lines changed

3 files changed

+64
-34
lines changed

api/variables/json.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import (
88
"fmt"
99

1010
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
11+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
12+
13+
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
1114
)
1215

1316
func MustMarshal(val any) *apiextensionsv1.JSON {
@@ -18,3 +21,62 @@ func MustMarshal(val any) *apiextensionsv1.JSON {
1821

1922
return &apiextensionsv1.JSON{Raw: marshaled}
2023
}
24+
25+
func MarshalToClusterVariable[T any](name string, obj T) (*clusterv1.ClusterVariable, error) {
26+
marshaled, err := json.Marshal(obj)
27+
if err != nil {
28+
return nil, fmt.Errorf("failed to marshal variable value %q: %w", name, err)
29+
}
30+
return &clusterv1.ClusterVariable{
31+
Name: name,
32+
Value: apiextensionsv1.JSON{Raw: marshaled},
33+
}, nil
34+
}
35+
36+
func UnmarshalClusterConfigVariable(clusterVariables []clusterv1.ClusterVariable) (*ClusterConfigSpec, error) {
37+
variableName := v1alpha1.ClusterConfigVariableName
38+
clusterConfig := GetClusterVariableByName(variableName, clusterVariables)
39+
if clusterConfig == nil {
40+
return nil, nil
41+
}
42+
spec := &ClusterConfigSpec{}
43+
err := UnmarshalClusterVariable(clusterConfig, spec)
44+
if err != nil {
45+
return nil, fmt.Errorf("failed to unmarshal cluster variable %q: %w", variableName, err)
46+
}
47+
48+
return spec, nil
49+
}
50+
51+
func UnmarshalWorkerConfigVariable(clusterVariables []clusterv1.ClusterVariable) (*WorkerNodeConfigSpec, error) {
52+
variableName := v1alpha1.WorkerConfigVariableName
53+
workerConfig := GetClusterVariableByName(variableName, clusterVariables)
54+
if workerConfig == nil {
55+
return nil, nil
56+
}
57+
spec := &WorkerNodeConfigSpec{}
58+
err := UnmarshalClusterVariable(workerConfig, spec)
59+
if err != nil {
60+
return nil, fmt.Errorf("failed to unmarshal cluster variable %q: %w", variableName, err)
61+
}
62+
63+
return spec, nil
64+
}
65+
66+
func UnmarshalClusterVariable[T any](clusterVariable *clusterv1.ClusterVariable, obj *T) error {
67+
err := json.Unmarshal(clusterVariable.Value.Raw, obj)
68+
if err != nil {
69+
return fmt.Errorf("failed to unmarshal json: %w", err)
70+
}
71+
72+
return nil
73+
}
74+
75+
func GetClusterVariableByName(name string, clusterVariables []clusterv1.ClusterVariable) *clusterv1.ClusterVariable {
76+
for _, clusterVar := range clusterVariables {
77+
if clusterVar.Name == name {
78+
return &clusterVar
79+
}
80+
}
81+
return nil
82+
}

common/pkg/capi/clustertopology/variables/json.go

Lines changed: 0 additions & 32 deletions
This file was deleted.

pkg/handlers/generic/lifecycle/csi/handler_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
"sigs.k8s.io/controller-runtime/pkg/client/fake"
1616

1717
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
18-
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/variables"
18+
apivariables "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/variables"
1919
)
2020

2121
type fakeCSIProvider struct {
@@ -41,7 +41,7 @@ var testProviderHandlers = map[string]CSIProvider{
4141
}
4242

4343
func testReq(csi *v1alpha1.CSI) (*runtimehooksv1.AfterControlPlaneInitializedRequest, error) {
44-
cv, err := variables.MarshalToClusterVariable(
44+
cv, err := apivariables.MarshalToClusterVariable(
4545
"clusterConfig",
4646
&v1alpha1.GenericClusterConfigSpec{
4747
Addons: &v1alpha1.Addons{

0 commit comments

Comments
 (0)