Skip to content

Commit

Permalink
Failover to regular catalogSource generation if template generation f…
Browse files Browse the repository at this point in the history
…ails
  • Loading branch information
sherine-k committed Jan 23, 2024
1 parent a3e0b53 commit 409f768
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 43 deletions.
74 changes: 53 additions & 21 deletions v2/pkg/clusterresources/clusterresources.go
Expand Up @@ -145,30 +145,15 @@ func (o *ClusterResourcesGenerator) generateCatalogSource(catalogRef string, cat
}

var obj ofv1alpha1.CatalogSource
generateWithoutTemplate := false
if catalogSourceTemplateFile != "" {
// Initializing catalogSource `obj` from template
_, err := os.Stat(catalogSourceTemplateFile)
if os.IsNotExist(err) {
return fmt.Errorf("targetCatalogSourceTemplate does not exist: %s", catalogSourceTemplateFile)
}
if err != nil {
return fmt.Errorf("error reading targetCatalogSourceTemplate file %s", catalogSourceTemplateFile)
}
bytesRead, err := os.ReadFile(catalogSourceTemplateFile)
obj, err = catalogSourceContentFromTemplate(catalogSourceTemplateFile, catalogSourceName, catalogSpec.Reference)
if err != nil {
return fmt.Errorf("error reading targetCatalogSourceTemplate file %s", catalogSourceTemplateFile)
generateWithoutTemplate = true
o.Log.Error("error generating catalog source from template. Fall back to generating catalog source without template: %v", err)
}
err = yaml.Unmarshal(bytesRead, &obj)
if err != nil {
return fmt.Errorf("%s is not a valid catalog source template", catalogSourceTemplateFile)
}
// fill obj with the values for this catalog
obj.Name = catalogSourceName
obj.Namespace = "openshift-marketplace"
obj.Spec.SourceType = "grpc"
obj.Spec.ConfigMap = "" //Force ConfigMap to empty as SourceType is grpc
obj.Spec.Image = catalogSpec.Reference
} else {
}
if generateWithoutTemplate || catalogSourceTemplateFile == "" {
obj = ofv1alpha1.CatalogSource{
TypeMeta: metav1.TypeMeta{
APIVersion: ofv1alpha1.GroupName + "/" + ofv1alpha1.GroupVersion,
Expand Down Expand Up @@ -212,6 +197,53 @@ func (o *ClusterResourcesGenerator) generateCatalogSource(catalogRef string, cat
return err
}

func catalogSourceContentFromTemplate(templateFile, catalogSourceName, image string) (ofv1alpha1.CatalogSource, error) {
// Initializing catalogSource `obj` from template
var obj ofv1alpha1.CatalogSource
_, err := os.Stat(templateFile)
if os.IsNotExist(err) {
return obj, fmt.Errorf("unable to marshal CatalogSource yaml: targetCatalogSourceTemplate does not exist: %s", templateFile)
}
if err != nil {
return obj, fmt.Errorf("unable to marshal CatalogSource yaml: error reading targetCatalogSourceTemplate file %s", templateFile)
}
bytesRead, err := os.ReadFile(templateFile)
if err != nil {
return obj, fmt.Errorf("unable to marshal CatalogSource yaml: error reading targetCatalogSourceTemplate file %s", templateFile)
}
err = yaml.Unmarshal(bytesRead, &obj)
if err != nil {
return obj, fmt.Errorf("unable to marshal CatalogSource yaml: %s is not a valid catalog source template", templateFile)
}

// validate that the catalogSource is grpc, otherwise fail
if obj.APIVersion != "operators.coreos.com/v1alpha1" {
return ofv1alpha1.CatalogSource{}, fmt.Errorf("catalog template does not correspond to the apiVersion operators.coreos.com/v1alpha1 : %s", obj.APIVersion)
}
if obj.Kind != "CatalogSource" {
return ofv1alpha1.CatalogSource{}, fmt.Errorf("catalog template does not correspond to Kind CatalogSource : %s", obj.Kind)
}
if obj.Spec.SourceType != "" && obj.Spec.SourceType != "grpc" {
return ofv1alpha1.CatalogSource{}, fmt.Errorf("catalog template is not of sourceType grpc")
}
if obj.Spec.ConfigMap != "" {
return ofv1alpha1.CatalogSource{}, fmt.Errorf("catalog template should not have a configMap specified")
}
// fill obj with the values for this catalog
obj.Name = catalogSourceName
obj.Namespace = "openshift-marketplace"
obj.Spec.SourceType = "grpc"
obj.Spec.Image = image

//verify that the resulting obj is a valid CatalogSource object
_, err = yaml.Marshal(obj)
if err != nil {
return ofv1alpha1.CatalogSource{}, fmt.Errorf("unable to marshal CatalogSource yaml: %v", err)
}

return obj, nil
}

func generateImageMirrors(allRelatedImages []v1alpha3.CopyImageSchema) (map[string][]confv1.ImageMirror, error) {
mirrors := make(map[string][]confv1.ImageMirror, 0)

Expand Down
2 changes: 1 addition & 1 deletion v2/tests/catalog-source_template.yaml
Expand Up @@ -7,5 +7,5 @@ spec:
image: totalRubbish
sourceType: grpc
updateStrategy:
registryPoll:
registryPoll:
interval: 30m0s
11 changes: 11 additions & 0 deletions v2/tests/catalog-source_template_KO.yaml
@@ -0,0 +1,11 @@
apiVersion: config.openshift.io/v1
kind: ImageDigestMirrorSet
metadata:
name: totalRubbish
namespace: openshift-marketplace
spec:
image: totalRubbish
sourceType: grpc
updateStrategy:
registryPoll:
interval: 30m0s

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 409f768

Please sign in to comment.