diff --git a/pkg/api/utils.go b/pkg/api/utils.go index 8fd1f51..f26b4aa 100644 --- a/pkg/api/utils.go +++ b/pkg/api/utils.go @@ -233,6 +233,14 @@ func NewAPIClusterFromCRD(cluster synv1alpha1.Cluster) *Cluster { apiCluster.DynamicFacts = &facts } + acm, err := crdCompileMetaToAPICompileMeta(cluster.Status.CompileMeta) + if err != nil { + // TODO(bastjan) fix error handling + fmt.Println("ERROR failed to convert compile meta:", err.Error()) + } else { + apiCluster.ClusterProperties.CompileMeta = acm + } + if cluster.Spec.GitRepoTemplate != nil { if stewardKey, ok := cluster.Spec.GitRepoTemplate.DeployKeys["steward"]; ok { sshKey := fmt.Sprintf("%s %s", stewardKey.Type, stewardKey.Key) @@ -375,6 +383,13 @@ func SyncCRDFromAPICluster(source ClusterProperties, target *synv1alpha1.Cluster target.Status.Facts[key] = string(encodedFact) } } + + clcm, err := apiCompileMetaToCRDCompileMeta(source.CompileMeta) + if err != nil { + return fmt.Errorf("failed to convert compile meta: %w", err) + } + target.Status.CompileMeta = clcm + return nil } @@ -413,3 +428,39 @@ func newGitRepoTemplate(repo *GitRepo, name string) (*synv1alpha1.GitRepoTemplat } return nil, nil } + +// crdCompileMetaToAPICompileMeta converts a CRD compile meta to an API compile meta. +// Uses json marshalling to convert the structs since their codegen representations are very different. +// Errors only if the marshalling fails. +func crdCompileMetaToAPICompileMeta(crdCompileMeta synv1alpha1.CompileMeta) (*ClusterCompileMeta, error) { + j, err := json.Marshal(crdCompileMeta) + if err != nil { + return nil, fmt.Errorf("failed to marshal compile meta for conversion: %w", err) + } + var apiCompileMeta *ClusterCompileMeta + err = json.Unmarshal(j, apiCompileMeta) + if err != nil { + return nil, fmt.Errorf("failed to unmarshal compile meta for conversion: %w", err) + } + return apiCompileMeta, nil +} + +// apiCompileMetaToCRDCompileMeta converts an API compile meta to a CRD compile meta. +// Uses json marshalling to convert the structs since their codegen representations are very different. +// Errors only if the marshalling fails. +func apiCompileMetaToCRDCompileMeta(apiCompileMeta *ClusterCompileMeta) (synv1alpha1.CompileMeta, error) { + if apiCompileMeta == nil { + return synv1alpha1.CompileMeta{}, nil + } + + j, err := json.Marshal(apiCompileMeta) + if err != nil { + return synv1alpha1.CompileMeta{}, fmt.Errorf("failed to marshal compile meta for conversion: %w", err) + } + var crdCompileMeta synv1alpha1.CompileMeta + err = json.Unmarshal(j, &crdCompileMeta) + if err != nil { + return synv1alpha1.CompileMeta{}, fmt.Errorf("failed to unmarshal compile meta for conversion: %w", err) + } + return crdCompileMeta, nil +}