Skip to content

Commit 76e0c16

Browse files
Change values to be of apiextensionsv1.JSON to be generic enough to store multiple versions of values.yaml and be compatible with multiple chart versions
1 parent f924185 commit 76e0c16

5 files changed

Lines changed: 43 additions & 31 deletions

File tree

api/v1alpha1/virtualcluster_types.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ limitations under the License.
1717
package v1alpha1
1818

1919
import (
20-
"github.com/loft-sh/vcluster/config"
2120
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21+
"sigs.k8s.io/yaml"
22+
23+
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
2224
)
2325

2426
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
@@ -29,7 +31,20 @@ type VirtualClusterSpec struct {
2931
Chart HelmChart `json:"chart,omitempty"`
3032
// +kubebuilder:pruning:PreserveUnknownFields
3133
// +kubebuilder:validation:Schemaless
32-
Values Values `json:"values,omitempty"`
34+
Values *apiextensionsv1.JSON `json:"values,required"`
35+
}
36+
37+
// GetValues unmarshals the raw values to a map[string]interface{} and returns
38+
// the result.
39+
func (in VirtualCluster) GetValues() (map[string]interface{}, error) {
40+
var values map[string]interface{}
41+
if in.Spec.Values != nil {
42+
err := yaml.Unmarshal(in.Spec.Values.Raw, &values)
43+
if err != nil {
44+
return nil, err
45+
}
46+
}
47+
return values, nil
3348
}
3449

3550
type HelmChart struct {
@@ -38,14 +53,6 @@ type HelmChart struct {
3853
Version string `json:"version,omitempty"`
3954
}
4055

41-
type Values struct {
42-
config.Config `json:",inline"`
43-
}
44-
45-
func (in *Values) DeepCopyInto(out *Values) {
46-
*out = *in
47-
}
48-
4956
// VirtualClusterStatus defines the observed state of VirtualCluster.
5057
type VirtualClusterStatus struct {
5158
// Phase is the current phase of the VirtualCluster

api/v1alpha1/zz_generated.deepcopy.go

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

config/crd/bases/core.openvc.dev_virtualclusters.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ spec:
5858
type: object
5959
values:
6060
x-kubernetes-preserve-unknown-fields: true
61+
required:
62+
- values
6163
type: object
6264
status:
6365
description: VirtualClusterStatus defines the observed state of VirtualCluster.

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/prakashmishra1598/openvc
22

3-
go 1.23.2
3+
go 1.24
44

55
require (
66
github.com/loft-sh/vcluster-config v0.0.0-20250408172738-9d5a6c680d53

internal/controller/virtualcluster_controller.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"os"
2626
"os/exec"
2727
"path/filepath"
28+
"sigs.k8s.io/yaml"
2829
"strings"
2930

3031
"github.com/xeipuuv/gojsonschema"
@@ -38,7 +39,6 @@ import (
3839
"sigs.k8s.io/controller-runtime/pkg/client"
3940
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
4041
"sigs.k8s.io/controller-runtime/pkg/log"
41-
"sigs.k8s.io/yaml"
4242

4343
corev1alpha1 "github.com/prakashmishra1598/openvc/api/v1alpha1"
4444
)
@@ -301,8 +301,15 @@ func (r *VirtualClusterReconciler) Reconcile(ctx context.Context, req ctrl.Reque
301301
func (r *VirtualClusterReconciler) createValuesFile(ctx context.Context, vcluster *corev1alpha1.VirtualCluster) (string, error) {
302302
logger := log.FromContext(ctx)
303303

304-
// Convert the values to YAML
305-
valuesYAML, err := yaml.Marshal(vcluster.Spec.Values)
304+
// Use the raw values directly
305+
values, err := vcluster.GetValues()
306+
if err != nil {
307+
logger.Error(err, "Failed to get values from VirtualCluster")
308+
return "", err
309+
}
310+
311+
// convert valuesYaml to []byte
312+
valuesByteArray, err := yaml.Marshal(values)
306313
if err != nil {
307314
logger.Error(err, "Failed to marshal values to YAML")
308315
return "", err
@@ -311,7 +318,7 @@ func (r *VirtualClusterReconciler) createValuesFile(ctx context.Context, vcluste
311318
// Create the values file
312319
valuesFilePath := filepath.Join(os.TempDir(), fmt.Sprintf("values-%s-%s.yaml", vcluster.Name, vcluster.Namespace))
313320
logger.Info("Writing values file", "path", valuesFilePath)
314-
err = os.WriteFile(valuesFilePath, valuesYAML, 0644)
321+
err = os.WriteFile(valuesFilePath, valuesByteArray, 0644)
315322
if err != nil {
316323
logger.Error(err, "Failed to write values file")
317324
return "", err
@@ -534,9 +541,10 @@ func (r *VirtualClusterReconciler) validateValuesAgainstSchema(ctx context.Conte
534541
logger.Info("Validating values against schema")
535542

536543
// Convert the values to JSON for validation
537-
valuesJSON, err := json.Marshal(vcluster.Spec.Values)
538-
if err != nil {
539-
return fmt.Errorf("failed to marshal values to JSON: %w", err)
544+
valuesJSON := vcluster.Spec.Values.Raw
545+
if valuesJSON == nil {
546+
// Empty values are valid
547+
return nil
540548
}
541549

542550
// Create schema and document loaders

0 commit comments

Comments
 (0)