Skip to content

Commit

Permalink
Add compileMeta to /clusters/* scope
Browse files Browse the repository at this point in the history
  • Loading branch information
bastjan committed May 28, 2024
1 parent 7894905 commit a33c096
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions pkg/api/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

0 comments on commit a33c096

Please sign in to comment.