Skip to content

Commit e0f4ca9

Browse files
authored
Added additionalProperties subfield to K8Slice Property field (#4) (#120)
1 parent 4028593 commit e0f4ca9

File tree

5 files changed

+101
-4
lines changed

5 files changed

+101
-4
lines changed

apis/nodecore/v1alpha1/flavor_types_common.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
package v1alpha1
1616

17+
import "k8s.io/apimachinery/pkg/runtime"
18+
1719
// CarbonFootprint represents the carbon footprint of a Flavor.
1820
type CarbonFootprint struct {
1921
Embodied int `json:"embodied"`
@@ -38,4 +40,6 @@ type Properties struct {
3840
CarbonFootprint *CarbonFootprint `json:"carbon-footprint,omitempty"`
3941
// Network authorization policies of the K8Slice Flavor
4042
NetworkAuthorizations *NetworkAuthorizations `json:"networkAuthorizations,omitempty"`
43+
// AdditionalProperties represents the additional properties of the K8Slice Flavor.
44+
AdditionalProperties map[string]runtime.RawExtension `json:"additionalProperties,omitempty"`
4145
}

apis/nodecore/v1alpha1/zz_generated.deepcopy.go

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/utils/models/flavor-k8slice-models.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package models
1616

1717
import (
18+
"encoding/json"
1819
"strings"
1920

2021
"k8s.io/apimachinery/pkg/api/resource"
@@ -52,10 +53,11 @@ type K8SliceCharacteristics struct {
5253

5354
// K8SliceProperties represents the properties of a Kubernetes slice.
5455
type K8SliceProperties struct {
55-
Latency int `json:"latency,omitempty"`
56-
SecurityStandards []string `json:"securityStandards,omitempty"`
57-
CarbonFootprint *CarbonFootprint `json:"carbonFootprint,omitempty"`
58-
NetworkAuthorizations *NetworkAuthorizations `json:"networkAuthorizations,omitempty"`
56+
Latency int `json:"latency,omitempty"`
57+
SecurityStandards []string `json:"securityStandards,omitempty"`
58+
CarbonFootprint *CarbonFootprint `json:"carbonFootprint,omitempty"`
59+
NetworkAuthorizations *NetworkAuthorizations `json:"networkAuthorizations,omitempty"`
60+
AdditionalProperties map[string]json.RawMessage `json:"additionalProperties,omitempty"`
5961
}
6062

6163
// K8SlicePartitionability represents the partitionability of a Kubernetes slice.

pkg/utils/parseutil/parseutil.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,14 @@ func ParseFlavor(flavor *nodecorev1alpha1.Flavor) *models.Flavor {
626626
},
627627
}
628628

629+
if err := parseK8SlicePropertyAdditionalProperties(
630+
&flavorTypeStruct.Properties,
631+
&modelFlavorTypeData.Properties,
632+
); err != nil {
633+
klog.Errorf("Error parsing K8Slice additional properties: %s", err)
634+
return nil
635+
}
636+
629637
// Encode the K8Slice data into JSON
630638
encodedFlavorTypeData, err := json.Marshal(modelFlavorTypeData)
631639
if err != nil {
@@ -709,6 +717,39 @@ func ParseFlavor(flavor *nodecorev1alpha1.Flavor) *models.Flavor {
709717
return &modelFlavor
710718
}
711719

720+
func parseK8SlicePropertyAdditionalProperties(k8SliceProperty *nodecorev1alpha1.Properties, k8SliceObjProperty *models.K8SliceProperties) error {
721+
if k8SliceProperty == nil {
722+
klog.Info("K8Slice property is nil")
723+
return nil
724+
}
725+
726+
if k8SliceObjProperty == nil {
727+
klog.Info("K8Slice object property is nil")
728+
return nil
729+
}
730+
731+
// Check the additional properties
732+
if k8SliceProperty.AdditionalProperties == nil {
733+
klog.Info("Additional properties is nil")
734+
return nil
735+
}
736+
737+
// Initialize the additional properties map
738+
k8SliceObjProperty.AdditionalProperties = make(map[string]json.RawMessage)
739+
740+
// Parse the additional properties
741+
for key, value := range k8SliceProperty.AdditionalProperties {
742+
// Parse runtime.RawExtension to json.RawMessage
743+
jsonValue, err := json.Marshal(value)
744+
if err != nil {
745+
return err
746+
}
747+
k8SliceObjProperty.AdditionalProperties[key] = jsonValue
748+
}
749+
750+
return nil
751+
}
752+
712753
// ParseContract creates a Contract Object.
713754
func ParseContract(contract *reservationv1alpha1.Contract) *models.Contract {
714755
return &models.Contract{

pkg/utils/resourceforge/forge.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,12 @@ func ForgeFlavorFromObj(flavor *models.Flavor) (*nodecorev1alpha1.Flavor, error)
915915
},
916916
},
917917
}
918+
919+
if err := forgeK8SlicePropertyAdditionalPropertiesFromObj(&flavorTypeDataModel.Properties, &flavorTypeData.Properties); err != nil {
920+
klog.Errorf("Error when forging K8Slice additional properties: %s", err)
921+
return nil, err
922+
}
923+
918924
flavorTypeDataJSON, err := json.Marshal(flavorTypeData)
919925
if err != nil {
920926
klog.Errorf("Error when marshaling K8SliceType: %s", err)
@@ -995,6 +1001,43 @@ func ForgeFlavorFromObj(flavor *models.Flavor) (*nodecorev1alpha1.Flavor, error)
9951001
return f, nil
9961002
}
9971003

1004+
func forgeK8SlicePropertyAdditionalPropertiesFromObj(k8SliceObjProperties *models.K8SliceProperties,
1005+
k8SliceProperties *nodecorev1alpha1.Properties) error {
1006+
if k8SliceObjProperties == nil {
1007+
klog.Info("K8Slice properties not found")
1008+
return nil
1009+
}
1010+
1011+
if k8SliceProperties == nil {
1012+
klog.Info("K8Slice additional properties not found")
1013+
return nil
1014+
}
1015+
1016+
// Check additional properties
1017+
if k8SliceObjProperties.AdditionalProperties == nil {
1018+
klog.Info("K8Slice additional properties not found")
1019+
return nil
1020+
}
1021+
1022+
// Initialize additional properties
1023+
k8SliceProperties.AdditionalProperties = make(map[string]runtime.RawExtension)
1024+
1025+
// Forge additional properties
1026+
for key, value := range k8SliceObjProperties.AdditionalProperties {
1027+
// Check for empty value
1028+
if value == nil {
1029+
klog.Errorf("Empty value for additional property %s", key)
1030+
return fmt.Errorf("empty value for additional property %s", key)
1031+
}
1032+
// Convert json.RawMessage to runtime.RawExtension
1033+
rawExtension := runtime.RawExtension{Raw: value}
1034+
// Add additional property to K8SliceProperties
1035+
k8SliceProperties.AdditionalProperties[key] = rawExtension
1036+
}
1037+
1038+
return nil
1039+
}
1040+
9981041
// ForgeK8SliceConfiguration creates a Configuration from a FlavorSelector.
9991042
func ForgeK8SliceConfiguration(selector nodecorev1alpha1.K8SliceSelector, flavor *nodecorev1alpha1.K8Slice) *nodecorev1alpha1.K8SliceConfiguration {
10001043
var cpu, memory, pods resource.Quantity

0 commit comments

Comments
 (0)