Skip to content

Commit

Permalink
cleanup: centralize code to integrate the project with OLM
Browse files Browse the repository at this point in the history
  • Loading branch information
camilamacedo86 committed Nov 13, 2020
1 parent ddc3cdc commit 7524fe3
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 122 deletions.
4 changes: 3 additions & 1 deletion hack/generate/samples/internal/ansible/memcached.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ func (ma *MemcachedAnsible) Run() {
ma.addingAnsibleTask()
ma.addingMoleculeMockData()

ma.ctx.CreateBundle()
log.Infof("creating the bundle")
err = ma.ctx.CreateBundle()
pkg.CheckError("creating the bundle", err)
}

// addingMoleculeMockData will customize the molecule data
Expand Down
4 changes: 3 additions & 1 deletion hack/generate/samples/internal/go/memcached_with_webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ func (mh *MemcachedGoWithWebhooks) Run() {
mh.implementingWebhooks()
mh.uncommentKustomizationFile()

mh.ctx.CreateBundle()
log.Infof("creating the bundle")
err = mh.ctx.CreateBundle()
pkg.CheckError("creating the bundle", err)

// Clean up built binaries, if any.
pkg.CheckError("cleaning up", os.RemoveAll(filepath.Join(mh.ctx.Dir, "bin")))
Expand Down
4 changes: 3 additions & 1 deletion hack/generate/samples/internal/helm/memcached.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ func (mh *MemcachedHelm) Run() {
"# +kubebuilder:scaffold:rules", policyRolesFragment)
pkg.CheckError("adding customized roles", err)

mh.ctx.CreateBundle()
log.Infof("creating the bundle")
err = mh.ctx.CreateBundle()
pkg.CheckError("creating the bundle", err)
}

// GenerateMemcachedHelmSample will call all actions to create the directory and generate the sample
Expand Down
78 changes: 0 additions & 78 deletions hack/generate/samples/internal/pkg/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,9 @@
package pkg

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"

log "github.com/sirupsen/logrus"
"sigs.k8s.io/kubebuilder/pkg/model/config"

"github.com/operator-framework/operator-sdk/internal/annotations/metrics"
)

// CheckError will exit with exit code 1 when err is not nil.
Expand All @@ -34,74 +27,3 @@ func CheckError(msg string, err error) {
os.Exit(1)
}
}

// CreateBundle runs all commands to create an operator bundle.
func (ctx *SampleContext) CreateBundle() {
log.Infof("integrating project with OLM")
err := ctx.DisableManifestsInteractiveMode()
CheckError("disabling `generate kustomize manifests` interactive mode", err)

err = ctx.Make("bundle", "IMG="+ctx.ImageName)
CheckError("running make bundle", err)

err = ctx.StripBundleAnnotations()
CheckError("stripping bundle annotations", err)

err = ctx.Make("bundle-build", "BUNDLE_IMG="+ctx.BundleImageName)
CheckError("running make bundle-build", err)
}

// StripBundleAnnotations removes all annotations applied to bundle manifests and metadata
// by operator-sdk/internal/annotations/metrics annotators. Doing so decouples samples
// from which operator-sdk version they were build with, as this information is already
// available in git history.
func (ctx SampleContext) StripBundleAnnotations() (err error) {
// Remove metadata labels.
metadataAnnotations := metrics.MakeBundleMetadataLabels(&config.Config{})
metadataFiles := []string{
filepath.Join(ctx.Dir, "bundle", "metadata", "annotations.yaml"),
filepath.Join(ctx.Dir, "bundle.Dockerfile"),
}
if err = removeAllAnnotationLines(metadataAnnotations, metadataFiles); err != nil {
return err
}

// Remove manifests annotations.
manifestsAnnotations := metrics.MakeBundleObjectAnnotations(&config.Config{})
manifestsFiles := []string{
filepath.Join(ctx.Dir, "bundle", "manifests", ctx.ProjectName+".clusterserviceversion.yaml"),
filepath.Join(ctx.Dir, "config", "manifests", "bases", ctx.ProjectName+".clusterserviceversion.yaml"),
}
if err = removeAllAnnotationLines(manifestsAnnotations, manifestsFiles); err != nil {
return err
}

return nil
}

// removeAllAnnotationLines removes each line containing a key in annotations from all files at filePaths.
func removeAllAnnotationLines(annotations map[string]string, filePaths []string) error {
var annotationREs []*regexp.Regexp
for annotation := range annotations {
re, err := regexp.Compile(".+" + regexp.QuoteMeta(annotation) + ".+\n")
if err != nil {
return fmt.Errorf("compiling annotation regexp: %v", err)
}
annotationREs = append(annotationREs, re)
}

for _, file := range filePaths {
b, err := ioutil.ReadFile(file)
if err != nil {
return err
}
for _, re := range annotationREs {
b = re.ReplaceAll(b, []byte{})
}
err = ioutil.WriteFile(file, b, 0644)
if err != nil {
return err
}
}
return nil
}
79 changes: 79 additions & 0 deletions internal/testutils/olm.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ import (
"fmt"
"io/ioutil"
"path/filepath"
"regexp"
"strings"

"github.com/operator-framework/operator-sdk/internal/annotations/metrics"

"sigs.k8s.io/kubebuilder/pkg/model/config"
)

Expand Down Expand Up @@ -89,3 +92,79 @@ func (tc TestContext) DisableManifestsInteractiveMode() error {
replace := content + " --interactive=false"
return ReplaceInFile(filepath.Join(tc.Dir, "Makefile"), content, replace)
}

// CreateBundle runs all commands to create an operator bundle.
func (tc TestContext) CreateBundle() error {
if err := tc.DisableManifestsInteractiveMode(); err != nil {
return err
}

if err := tc.Make("bundle", "IMG="+tc.ImageName); err != nil {
return err
}

if err := tc.stripBundleAnnotations(); err != nil {
return err
}

if err := tc.Make("bundle-build", "BUNDLE_IMG="+tc.BundleImageName); err != nil {
return err
}

return nil
}

// StripBundleAnnotations removes all annotations applied to bundle manifests and metadata
// by operator-sdk/internal/annotations/metrics annotators. Doing so decouples samples
// from which operator-sdk version they were build with, as this information is already
// available in git history.
func (tc TestContext) stripBundleAnnotations() (err error) {
// Remove metadata labels.
metadataAnnotations := metrics.MakeBundleMetadataLabels(&config.Config{})
metadataFiles := []string{
filepath.Join(tc.Dir, "bundle", "metadata", "annotations.yaml"),
filepath.Join(tc.Dir, "bundle.Dockerfile"),
}
if err = removeAllAnnotationLines(metadataAnnotations, metadataFiles); err != nil {
return err
}

// Remove manifests annotations.
manifestsAnnotations := metrics.MakeBundleObjectAnnotations(&config.Config{})
manifestsFiles := []string{
filepath.Join(tc.Dir, "bundle", "manifests", tc.ProjectName+".clusterserviceversion.yaml"),
filepath.Join(tc.Dir, "config", "manifests", "bases", tc.ProjectName+".clusterserviceversion.yaml"),
}
if err = removeAllAnnotationLines(manifestsAnnotations, manifestsFiles); err != nil {
return err
}

return nil
}

// removeAllAnnotationLines removes each line containing a key in annotations from all files at filePaths.
func removeAllAnnotationLines(annotations map[string]string, filePaths []string) error {
var annotationREs []*regexp.Regexp
for annotation := range annotations {
re, err := regexp.Compile(".+" + regexp.QuoteMeta(annotation) + ".+\n")
if err != nil {
return fmt.Errorf("compiling annotation regexp: %v", err)
}
annotationREs = append(annotationREs, re)
}

for _, file := range filePaths {
b, err := ioutil.ReadFile(file)
if err != nil {
return err
}
for _, re := range annotationREs {
b = re.ReplaceAll(b, []byte{})
}
err = ioutil.WriteFile(file, b, 0644)
if err != nil {
return err
}
}
return nil
}
12 changes: 2 additions & 10 deletions test/e2e-ansible/e2e_ansible_olm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,14 @@ var _ = Describe("Integrating ansible Projects with OLM", func() {
const operatorVersion = "0.0.1"

It("should generate and run a valid OLM bundle and packagemanifests", func() {
By("building the bundle")
err := tc.Make("bundle", "IMG="+tc.ImageName)
Expect(err).NotTo(HaveOccurred())

By("building the operator bundle image")
err = tc.Make("bundle-build", "BUNDLE_IMG="+tc.BundleImageName)
Expect(err).NotTo(HaveOccurred())

if tc.IsRunningOnKind() {
By("loading the bundle image into Kind cluster")
err = tc.LoadImageToKindClusterWithName(tc.BundleImageName)
err := tc.LoadImageToKindClusterWithName(tc.BundleImageName)
Expect(err).NotTo(HaveOccurred())
}

By("adding the 'packagemanifests' rule to the Makefile")
err = tc.AddPackagemanifestsTarget()
err := tc.AddPackagemanifestsTarget()
Expect(err).NotTo(HaveOccurred())

By("generating the operator package manifests")
Expand Down
7 changes: 4 additions & 3 deletions test/e2e-ansible/e2e_ansible_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ func TestE2EAnsible(t *testing.T) {
}

var (
tc testutils.TestContext
tc testutils.
TestContext
)

// BeforeSuite run before any specs are run to perform the required actions for all e2e ansible tests.
Expand Down Expand Up @@ -121,8 +122,8 @@ var _ = BeforeSuite(func() {
Expect(tc.LoadImageToKindClusterWithName("quay.io/operator-framework/scorecard-test:dev")).To(Succeed())
}

By("building the bundle")
err = tc.Make("bundle", "IMG="+tc.ImageName)
By("creating bundle image")
err = tc.CreateBundle()
Expect(err).NotTo(HaveOccurred())
})

Expand Down
16 changes: 2 additions & 14 deletions test/e2e-go/e2e_go_olm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,14 @@ var _ = Describe("Integrating Go Projects with OLM", func() {
const operatorVersion = "0.0.1"

It("should generate and run a valid OLM bundle and packagemanifests", func() {
By("turning off interactive prompts for all generation tasks.")
err := tc.DisableManifestsInteractiveMode()
Expect(err).NotTo(HaveOccurred())

By("building the bundle")
err = tc.Make("bundle", "IMG="+tc.ImageName)
Expect(err).NotTo(HaveOccurred())

By("building the operator bundle image")
err = tc.Make("bundle-build", "BUNDLE_IMG="+tc.BundleImageName)
Expect(err).NotTo(HaveOccurred())

if tc.IsRunningOnKind() {
By("loading the bundle image into Kind cluster")
err = tc.LoadImageToKindClusterWithName(tc.BundleImageName)
err := tc.LoadImageToKindClusterWithName(tc.BundleImageName)
Expect(err).NotTo(HaveOccurred())
}

By("adding the 'packagemanifests' rule to the Makefile")
err = tc.AddPackagemanifestsTarget()
err := tc.AddPackagemanifestsTarget()
Expect(err).NotTo(HaveOccurred())

By("generating the operator package manifests")
Expand Down
8 changes: 2 additions & 6 deletions test/e2e-go/e2e_go_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,6 @@ var _ = BeforeSuite(func() {
filepath.Join(tc.Dir, "config", "default", "kustomization.yaml"),
"#- ../prometheus", "#")).To(Succeed())

By("turning off interactive prompts for all generation tasks.")
err = tc.DisableManifestsInteractiveMode()
Expect(err).NotTo(HaveOccurred())

By("checking the kustomize setup")
err = tc.Make("kustomize")
Expect(err).NotTo(HaveOccurred())
Expand All @@ -119,8 +115,8 @@ var _ = BeforeSuite(func() {
Expect(tc.LoadImageToKindClusterWithName("quay.io/operator-framework/custom-scorecard-tests:dev")).To(Succeed())
}

By("generating the operator bundle")
err = tc.Make("bundle", "IMG="+tc.ImageName)
By("creating bundle image")
err = tc.CreateBundle()
Expect(err).NotTo(HaveOccurred())
})

Expand Down
8 changes: 2 additions & 6 deletions test/e2e-helm/e2e_helm_olm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,14 @@ var _ = Describe("Integrating Helm Projects with OLM", func() {
const operatorVersion = "0.0.1"

It("should generate and run a valid OLM bundle and packagemanifests", func() {
By("building the operator bundle image")
err := tc.Make("bundle-build", "BUNDLE_IMG="+tc.BundleImageName)
Expect(err).NotTo(HaveOccurred())

if tc.IsRunningOnKind() {
By("loading the bundle image into Kind cluster")
err = tc.LoadImageToKindClusterWithName(tc.BundleImageName)
err := tc.LoadImageToKindClusterWithName(tc.BundleImageName)
Expect(err).NotTo(HaveOccurred())
}

By("adding the 'packagemanifests' rule to the Makefile")
err = tc.AddPackagemanifestsTarget()
err := tc.AddPackagemanifestsTarget()
Expect(err).NotTo(HaveOccurred())

By("generating the operator package manifests")
Expand Down
4 changes: 2 additions & 2 deletions test/e2e-helm/e2e_helm_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ var _ = BeforeSuite(func() {
Expect(tc.LoadImageToKindClusterWithName("quay.io/operator-framework/scorecard-test:dev")).To(Succeed())
}

By("generating the operator bundle")
err = tc.Make("bundle", "IMG="+tc.ImageName)
By("creating bundle image")
err = tc.CreateBundle()
Expect(err).NotTo(HaveOccurred())
})

Expand Down

0 comments on commit 7524fe3

Please sign in to comment.