8
8
"fmt"
9
9
10
10
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"
11
14
)
12
15
13
16
func MustMarshal (val any ) * apiextensionsv1.JSON {
@@ -18,3 +21,62 @@ func MustMarshal(val any) *apiextensionsv1.JSON {
18
21
19
22
return & apiextensionsv1.JSON {Raw : marshaled }
20
23
}
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
+ }
0 commit comments