|
| 1 | +// Copyright 2022-2024 FLUIDOS Project |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package v1alpha1 |
| 16 | + |
| 17 | +import ( |
| 18 | + "bytes" |
| 19 | + "fmt" |
| 20 | + |
| 21 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 22 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 23 | + "k8s.io/apimachinery/pkg/runtime" |
| 24 | + "k8s.io/apimachinery/pkg/runtime/serializer/yaml" |
| 25 | +) |
| 26 | + |
| 27 | +// ServiceBlueprintSpec defines the desired state of ServiceBlueprint. |
| 28 | +type ServiceBlueprintSpec struct { |
| 29 | + // Name of the Service Blueprint. |
| 30 | + Name string `json:"name"` |
| 31 | + |
| 32 | + // Description of the Service Blueprint. |
| 33 | + Description string `json:"description"` |
| 34 | + |
| 35 | + // Category of the Service Blueprint. |
| 36 | + Category string `json:"category"` |
| 37 | + |
| 38 | + // Tags of the Service Blueprint. |
| 39 | + Tags []string `json:"tags"` |
| 40 | + |
| 41 | + // HostingPolicies of the Service Blueprint. |
| 42 | + // If empty, the default behavior is to host on the provider cluster. |
| 43 | + // If multiple policies are specified, the first one is the default. |
| 44 | + HostingPolicies []HostingPolicy `json:"hostingPolicies"` |
| 45 | + |
| 46 | + // Templates of the Service Blueprint. |
| 47 | + Templates []ServiceTemplate `json:"templates"` |
| 48 | +} |
| 49 | + |
| 50 | +// ServiceTemplate defines the template of a Service. |
| 51 | +type ServiceTemplate struct { |
| 52 | + // Name of the Service Template. |
| 53 | + Name string `json:"name"` |
| 54 | + // Description of the Service Template. |
| 55 | + Description string `json:"description,omitempty"` |
| 56 | + // YAML template of the Service. |
| 57 | + ServiceTemplateData runtime.RawExtension `json:"serviceData"` |
| 58 | +} |
| 59 | + |
| 60 | +// ServiceBlueprintStatus defines the observed state of ServiceBlueprint. |
| 61 | +type ServiceBlueprintStatus struct { |
| 62 | + // ServiceFlavor linked to the Service Blueprint. |
| 63 | + ServiceFlavors []ServiceFlavor `json:"serviceFlavors,omitempty"` |
| 64 | +} |
| 65 | + |
| 66 | +//+kubebuilder:object:root=true |
| 67 | +//+kubebuilder:subresource:status |
| 68 | + |
| 69 | +// ServiceBlueprint is the Schema for the serviceblueprints API |
| 70 | +// +kubebuilder:printcolumn:name="Name",type=string,JSONPath=`.spec.name` |
| 71 | +// +kubebuilder:printcolumn:name="Description",type=string,JSONPath=`.spec.description` |
| 72 | +// +kubebuilder:printcolumn:name="Category",type=string,JSONPath=`.spec.category` |
| 73 | +// +kubebuilder:printcolumn:name="Tags",type=string,JSONPath=`.spec.tags` |
| 74 | +// +kubebuilder:printcolumn:name="ServiceFlavors",type=string,JSONPath=`.status.serviceFlavors` |
| 75 | +// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp" |
| 76 | +// +kubebuilder:resource:shortName=sbp |
| 77 | +type ServiceBlueprint struct { |
| 78 | + metav1.TypeMeta `json:",inline"` |
| 79 | + metav1.ObjectMeta `json:"metadata,omitempty"` |
| 80 | + |
| 81 | + Spec ServiceBlueprintSpec `json:"spec,omitempty"` |
| 82 | + Status ServiceBlueprintStatus `json:"status,omitempty"` |
| 83 | +} |
| 84 | + |
| 85 | +//+kubebuilder:object:root=true |
| 86 | + |
| 87 | +// ServiceBlueprintList contains a list of ServiceBlueprint. |
| 88 | +type ServiceBlueprintList struct { |
| 89 | + metav1.TypeMeta `json:",inline"` |
| 90 | + metav1.ListMeta `json:"metadata,omitempty"` |
| 91 | + Items []ServiceBlueprint `json:"items"` |
| 92 | +} |
| 93 | + |
| 94 | +func init() { |
| 95 | + SchemeBuilder.Register(&ServiceBlueprint{}, &ServiceBlueprintList{}) |
| 96 | +} |
| 97 | + |
| 98 | +// ValidateAndExtractManifests extracts manifests from ServiceTemplateData and validates them. |
| 99 | +func ValidateAndExtractManifests(templates []ServiceTemplate) ([]*unstructured.Unstructured, error) { |
| 100 | + var manifests []*unstructured.Unstructured |
| 101 | + |
| 102 | + for _, template := range templates { |
| 103 | + // Step 1: Parse the raw data from ServiceTemplateData (YAML or JSON) |
| 104 | + parsedManifests, err := parseRawServiceTemplateData(template.ServiceTemplateData) |
| 105 | + if err != nil { |
| 106 | + return nil, fmt.Errorf("failed to parse service template data for template %s: %w", template.Name, err) |
| 107 | + } |
| 108 | + |
| 109 | + // Step 2: Validate the manifests |
| 110 | + for _, manifest := range parsedManifests { |
| 111 | + if err := validateManifest(manifest); err != nil { |
| 112 | + return nil, fmt.Errorf("validation failed for template %s: %w", template.Name, err) |
| 113 | + } |
| 114 | + manifests = append(manifests, manifest) |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return manifests, nil |
| 119 | +} |
| 120 | + |
| 121 | +// parseRawServiceTemplateData parses the raw YAML/JSON data from ServiceTemplateData. |
| 122 | +func parseRawServiceTemplateData(data runtime.RawExtension) ([]*unstructured.Unstructured, error) { |
| 123 | + var manifests []*unstructured.Unstructured |
| 124 | + |
| 125 | + // Decoding YAML to JSON |
| 126 | + decoder := yaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme) |
| 127 | + |
| 128 | + // Split multiple documents if necessary (YAML can have multiple docs separated by `---`) |
| 129 | + docs := bytes.Split(data.Raw, []byte("\n---\n")) |
| 130 | + |
| 131 | + for _, doc := range docs { |
| 132 | + if len(bytes.TrimSpace(doc)) == 0 { |
| 133 | + continue // Skip empty docs |
| 134 | + } |
| 135 | + |
| 136 | + // Parse into Unstructured to handle dynamic manifest types |
| 137 | + obj := &unstructured.Unstructured{} |
| 138 | + _, _, err := decoder.Decode(doc, nil, obj) |
| 139 | + if err != nil { |
| 140 | + return nil, fmt.Errorf("failed to decode manifest: %w", err) |
| 141 | + } |
| 142 | + |
| 143 | + manifests = append(manifests, obj) |
| 144 | + } |
| 145 | + |
| 146 | + return manifests, nil |
| 147 | +} |
| 148 | + |
| 149 | +// validateManifest performs basic validation on the Kubernetes manifest. |
| 150 | +func validateManifest(manifest *unstructured.Unstructured) error { |
| 151 | + // Basic validation for required fields in Kubernetes objects |
| 152 | + if manifest.GetKind() == "" || manifest.GetAPIVersion() == "" { |
| 153 | + return fmt.Errorf("manifest missing kind or apiVersion") |
| 154 | + } |
| 155 | + |
| 156 | + // Add more specific validation logic if necessary |
| 157 | + |
| 158 | + return nil |
| 159 | +} |
0 commit comments