Skip to content

Commit

Permalink
ignore-if-only-created-at-changed
Browse files Browse the repository at this point in the history
Signed-off-by: Tiger Kaovilai <[email protected]>
  • Loading branch information
kaovilai committed May 9, 2023
1 parent ef961e0 commit b392bb7
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 16 deletions.
4 changes: 2 additions & 2 deletions internal/cmd/operator-sdk/generate/bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,9 @@ func (c bundleCmd) runManifests() (err error) {
opts = append(opts, gencsv.WithWriter(stdout))
} else {
opts = append(opts, gencsv.WithBundleWriter(c.outputDir))
if c.ignoreIfOnlyCreatedAt && genutil.IsExist(c.outputDir) {
if c.ignoreIfOnlyCreatedAtChanged && genutil.IsExist(c.outputDir) {
opts = append(opts, gencsv.WithBundleReader(c.outputDir))
opts = append(opts, gencsv.WithIgnoreIfOnlyCreatedAt())
opts = append(opts, gencsv.WithIgnoreIfOnlyCreatedAtChanged())
}
}

Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/operator-sdk/generate/bundle/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type bundleCmd struct {
channels string
defaultChannel string
overwrite bool
ignoreIfOnlyCreatedAt bool
ignoreIfOnlyCreatedAtChanged bool

// These are set if a PROJECT config is not present.
layout string
Expand Down Expand Up @@ -139,7 +139,7 @@ func (c *bundleCmd) addFlagsTo(fs *pflag.FlagSet) {
"Names of service accounts, outside of the operator's Deployment account, "+
"that have bindings to {Cluster}Roles that should be added to the CSV")
fs.BoolVar(&c.overwrite, "overwrite", true, "Overwrite the bundle's metadata and Dockerfile if they exist")
fs.BoolVar(&c.ignoreIfOnlyCreatedAt, "ignore-if-only-createdAt", false, "Ignore if only createdAt is changed")
fs.BoolVar(&c.ignoreIfOnlyCreatedAtChanged, "ignore-if-only-created-at-changed", false, "Ignore if only createdAt is changed")
fs.BoolVarP(&c.quiet, "quiet", "q", false, "Run in quiet mode")
fs.BoolVar(&c.stdout, "stdout", false, "Write bundle manifest to stdout")

Expand Down
17 changes: 8 additions & 9 deletions internal/generate/clusterserviceversion/clusterserviceversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

"github.com/blang/semver/v4"
"github.com/operator-framework/api/pkg/operators/v1alpha1"
operatorsv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
"github.com/operator-framework/operator-registry/pkg/lib/bundle"

"github.com/operator-framework/operator-sdk/internal/generate/clusterserviceversion/bases"
Expand Down Expand Up @@ -58,14 +57,14 @@ type Generator struct {
// {Cluster}Roles to include in a CSV via their Bindings.
ExtraServiceAccounts []string
// RelatedImages are additional images used by the operator.
RelatedImages []operatorsv1alpha1.RelatedImage
RelatedImages []v1alpha1.RelatedImage

// Func that returns the writer the generated CSV's bytes are written to.
getWriter func() (io.Writer, error)
// Func that returns the reader the previous CSV's bytes are read from.
getReader func() (io.Reader, error)

ignoreIfOnlyCreatedAt bool
ignoreIfOnlyCreatedAtChanged bool
}

// Option is a function that modifies a Generator.
Expand Down Expand Up @@ -121,9 +120,9 @@ func WithPackageWriter(dir string) Option {
}
}

func WithIgnoreIfOnlyCreatedAt() Option {
func WithIgnoreIfOnlyCreatedAtChanged() Option {
return func(g *Generator) error {
g.ignoreIfOnlyCreatedAt = true
g.ignoreIfOnlyCreatedAtChanged = true
return nil
}
}
Expand All @@ -149,12 +148,12 @@ func (g *Generator) Generate(opts ...Option) (err error) {
g.setAnnotations(csv)
// If a reader is set, and there is a flag to not update createdAt, then
// set the CSV's createdAt to the previous CSV's createdAt if its the only change.
if g.ignoreIfOnlyCreatedAt && g.getReader != nil {
if g.ignoreIfOnlyCreatedAtChanged && g.getReader != nil {
r, err := g.getReader()
if err != nil {
return err
}
var prevCSV operatorsv1alpha1.ClusterServiceVersion
var prevCSV v1alpha1.ClusterServiceVersion
err = genutil.ReadObject(r, &prevCSV)
if err != nil {
return err
Expand All @@ -163,7 +162,7 @@ func (g *Generator) Generate(opts ...Option) (err error) {
csvWithoutCreatedAtChange := csv.DeepCopy()
// Set WebhookDefinitions if nil to avoid diffing on it
if prevCSV.Spec.WebhookDefinitions == nil {
prevCSV.Spec.WebhookDefinitions = []operatorsv1alpha1.WebhookDescription{}
prevCSV.Spec.WebhookDefinitions = []v1alpha1.WebhookDescription{}
}
if csvWithoutCreatedAtChange.ObjectMeta.Annotations == nil {
csvWithoutCreatedAtChange.ObjectMeta.Annotations = map[string]string{}
Expand Down Expand Up @@ -194,7 +193,7 @@ func (g Generator) setAnnotations(csv *v1alpha1.ClusterServiceVersion) {
}

// generate runs a configured Generator.
func (g *Generator) generate() (base *operatorsv1alpha1.ClusterServiceVersion, err error) {
func (g *Generator) generate() (base *v1alpha1.ClusterServiceVersion, err error) {
if g.Collector == nil {
return nil, fmt.Errorf("cannot generate CSV without a manifests collection")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"github.com/onsi/gomega/format"
operatorversion "github.com/operator-framework/api/pkg/lib/version"
"github.com/operator-framework/api/pkg/operators/v1alpha1"
operatorsv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
"github.com/operator-framework/operator-registry/pkg/lib/bundle"
appsv1 "k8s.io/api/apps/v1"
"sigs.k8s.io/yaml"
Expand Down Expand Up @@ -154,7 +153,7 @@ var _ = Describe("Testing CRDs with single version", func() {
outputFile := filepath.Join(tmp, bundle.ManifestsDir, makeCSVFileName(operatorName))
Expect(outputFile).To(BeAnExistingFile())
Expect(readFileHelper(outputFile)).To(MatchYAML(newCSVUIMetaStr))
var initiallyWrittenCSV operatorsv1alpha1.ClusterServiceVersion
var initiallyWrittenCSV v1alpha1.ClusterServiceVersion
r, err := bundleReader(tmp, makeCSVFileName(operatorName))
Expect(err).ToNot(HaveOccurred())
err = genutil.ReadObject(r, &initiallyWrittenCSV)
Expand All @@ -169,7 +168,7 @@ var _ = Describe("Testing CRDs with single version", func() {
opts = []Option{
WithBundleWriter(tmp),
WithBundleReader(tmp),
WithIgnoreIfOnlyCreatedAt(),
WithIgnoreIfOnlyCreatedAtChanged(),
}
time.Sleep(1*time.Second + 1*time.Millisecond) // sleep to ensure createdAt is different if not for ignore option
Expect(g.Generate(opts...)).ToNot(HaveOccurred())
Expand Down

0 comments on commit b392bb7

Please sign in to comment.