Skip to content

Commit

Permalink
Adds --operator-name flag and preserves ProjectName
Browse files Browse the repository at this point in the history
This patch adds a `--operator-name` flag to `operator-sdk olm-catalog gen-csv` command

As per review comments `ProjectName variable is preserved in cmd/olmcatalog/gen-csv.go
In addition, the `--operator-name` is used to initialize newly added `OperatorName` field in
`internal/pkg/scaffold/input/input.go

The change is also propagated in
`internal/pkg/scaffold/scaffold.go` - `Scaffold` struct
`internal/pkg/scaffold/olm-catalog/csv.go` and in
`internal/pkg/scaffold/olm-catalog/csv_test.go`

Signed-off-by: Nikhil Thomas <nikhilthomas1@gmail.com>
  • Loading branch information
nikhil-thomas committed Jun 19, 2019
1 parent c8ead55 commit f652c8a
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 26 deletions.
16 changes: 10 additions & 6 deletions cmd/operator-sdk/olmcatalog/gen-csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@ import (
)

var (
csvVersion string
fromVersion string
csvConfigPath string
updateCRDs bool
csvVersion string
fromVersion string
csvConfigPath string
updateCRDs bool
absProjectPath = projutil.MustGetwd()
projectName = filepath.Base(absProjectPath)
operatorName = projectName //default to project directory name
)

func newGenCSVCmd() *cobra.Command {
Expand All @@ -58,6 +61,7 @@ Configure CSV generation by writing a config file 'deploy/olm-catalog/csv-config
genCSVCmd.Flags().StringVar(&fromVersion, "from-version", "", "Semantic version of an existing CSV to use as a base")
genCSVCmd.Flags().StringVar(&csvConfigPath, "csv-config", "", "Path to CSV config file. Defaults to deploy/olm-catalog/csv-config.yaml")
genCSVCmd.Flags().BoolVar(&updateCRDs, "update-crds", false, "Update CRD manifests in deploy/{operator-name}/{csv-version} the using latest API's")
genCSVCmd.Flags().StringVar(&operatorName, "operator-name", operatorName, "Operator name to use while generating CSV")

return genCSVCmd
}
Expand All @@ -71,10 +75,10 @@ func genCSVFunc(cmd *cobra.Command, args []string) error {
return err
}

absProjectPath := projutil.MustGetwd()
cfg := &input.Config{
AbsProjectPath: absProjectPath,
ProjectName: filepath.Base(absProjectPath),
ProjectName: projectName,
OperatorName: operatorName,
}
if projutil.IsOperatorGo() {
cfg.Repo = projutil.GetGoPkg()
Expand Down
23 changes: 21 additions & 2 deletions internal/pkg/scaffold/input/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,12 @@ type Input struct {
// AbsProjectPath is the absolute path to the project root, including the project directory.
AbsProjectPath string

// ProjectName is the operator's name, ex. app-operator
// ProjectName is the operator's project directory name
ProjectName string

// OperatorName is the operator's name, ex. app-operator
OperatorName string

// Delims is a slice of two strings representing the left and right delimiter
// defaults to {{ }}
Delims [2]string
Expand Down Expand Up @@ -105,6 +108,19 @@ func (i *Input) SetProjectName(n string) {
}
}

// OperatorName allows the operator name to be set on an object
type OperatorName interface {
// SetOperatorName sets the operator name
SetOperatorName(string)
}

// SetOperatorName sets the operator name
func (i *Input) SetOperatorName(n string) {
if i.OperatorName == "" {
i.OperatorName = n
}
}

// File is a scaffoldable file
type File interface {
// GetInput returns the Input for creating a scaffold file
Expand All @@ -126,6 +142,9 @@ type Config struct {
// AbsProjectPath is the absolute path to the project root, including the project directory.
AbsProjectPath string

// ProjectName is the operator's name, ex. app-operator
// ProjectName is the operator's project directory name
ProjectName string

// OperatorName is the operator's name, ex. app-operator
OperatorName string
}
14 changes: 7 additions & 7 deletions internal/pkg/scaffold/olm-catalog/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ func (s *CSV) GetInput() (input.Input, error) {
return input.Input{}, ErrNoCSVVersion
}
if s.Path == "" {
lowerProjName := strings.ToLower(s.ProjectName)
operatorName := strings.ToLower(s.OperatorName)
// Path is what the operator-registry expects:
// {manifests -> olm-catalog}/{operator_name}/{semver}/{operator_name}.v{semver}.clusterserviceversion.yaml
s.Path = filepath.Join(s.pathPrefix,
scaffold.OLMCatalogDir,
lowerProjName,
operatorName,
s.CSVVersion,
getCSVFileName(lowerProjName, s.CSVVersion),
getCSVFileName(operatorName, s.CSVVersion),
)
}
if s.ConfigFilePath == "" {
Expand Down Expand Up @@ -182,7 +182,7 @@ func getCSVFileName(name, version string) string {
}

func (s *CSV) getCSVPath(ver string) string {
lowerProjName := strings.ToLower(s.ProjectName)
lowerProjName := strings.ToLower(s.OperatorName)
name := getCSVFileName(lowerProjName, ver)
return filepath.Join(s.pathPrefix, scaffold.OLMCatalogDir, lowerProjName, ver, name)
}
Expand Down Expand Up @@ -228,13 +228,13 @@ func (s *CSV) initCSVFields(csv *olmapiv1alpha1.ClusterServiceVersion) {
// Metadata
csv.TypeMeta.APIVersion = olmapiv1alpha1.ClusterServiceVersionAPIVersion
csv.TypeMeta.Kind = olmapiv1alpha1.ClusterServiceVersionKind
csv.SetName(getCSVName(strings.ToLower(s.ProjectName), s.CSVVersion))
csv.SetName(getCSVName(strings.ToLower(s.OperatorName), s.CSVVersion))
csv.SetNamespace("placeholder")
csv.SetAnnotations(map[string]string{"capabilities": "Basic Install"})

// Spec fields
csv.Spec.Version = *semver.New(s.CSVVersion)
csv.Spec.DisplayName = getDisplayName(s.ProjectName)
csv.Spec.DisplayName = getDisplayName(s.OperatorName)
csv.Spec.Description = "Placeholder description"
csv.Spec.Maturity = "alpha"
csv.Spec.Provider = olmapiv1alpha1.AppLink{}
Expand Down Expand Up @@ -329,7 +329,7 @@ func (s *CSV) updateCSVVersions(csv *olmapiv1alpha1.ClusterServiceVersion) error
}

// Now replace all references to the old operator name.
lowerProjName := strings.ToLower(s.ProjectName)
lowerProjName := strings.ToLower(s.OperatorName)
oldCSVName := getCSVName(lowerProjName, oldVer)
newCSVName := getCSVName(lowerProjName, newVer)
err := replaceAllBytes(csv, []byte(oldCSVName), []byte(newCSVName))
Expand Down
24 changes: 14 additions & 10 deletions internal/pkg/scaffold/olm-catalog/csv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ func TestCSVNew(t *testing.T) {
},
}
csvVer := "0.1.0"
projectName := "app-operator"
projectName := "app-operator-dir"
operatorName := "app-operator"

sc := &CSV{CSVVersion: csvVer, pathPrefix: testDataDir}
err := s.Execute(&input.Config{ProjectName: projectName}, sc)
err := s.Execute(&input.Config{ProjectName: projectName, OperatorName: operatorName}, sc)
if err != nil {
t.Fatalf("Failed to execute the scaffold: (%v)", err)
}
Expand All @@ -70,7 +71,8 @@ func TestCSVNew(t *testing.T) {

func TestCSVFromOld(t *testing.T) {
s := &scaffold.Scaffold{Fs: afero.NewMemMapFs()}
projectName := "app-operator"
projectName := "app-operator-dir"
operatorName := "app-operator"
oldCSVVer, newCSVVer := "0.1.0", "0.2.0"

// Write all files in testdata/deploy to fs so manifests are present when
Expand All @@ -84,7 +86,7 @@ func TestCSVFromOld(t *testing.T) {
FromVersion: oldCSVVer,
pathPrefix: testDataDir,
}
err := s.Execute(&input.Config{ProjectName: projectName}, sc)
err := s.Execute(&input.Config{ProjectName: projectName, OperatorName: operatorName}, sc)
if err != nil {
t.Fatalf("Failed to execute the scaffold: (%v)", err)
}
Expand All @@ -99,21 +101,23 @@ func TestCSVFromOld(t *testing.T) {
t.Fatalf("New CSV does not exist at %s", newCSVPath)
}

expName := getCSVName(projectName, newCSVVer)
expName := getCSVName(operatorName, newCSVVer)
if newCSV.ObjectMeta.Name != expName {
t.Errorf("Expected CSV metadata.name %s, got %s", expName, newCSV.ObjectMeta.Name)
}
expReplaces := getCSVName(projectName, oldCSVVer)
expReplaces := getCSVName(operatorName, oldCSVVer)
if newCSV.Spec.Replaces != expReplaces {
t.Errorf("Expected CSV spec.replaces %s, got %s", expReplaces, newCSV.Spec.Replaces)
}
}

func TestUpdateVersion(t *testing.T) {
projectName := "app-operator"
projectName := "app-operator-dir"
operatorName := "app-operator"

oldCSVVer, newCSVVer := "0.1.0", "0.2.0"
sc := &CSV{
Input: input.Input{ProjectName: projectName},
Input: input.Input{ProjectName: projectName, OperatorName: operatorName},
CSVVersion: newCSVVer,
pathPrefix: testDataDir,
}
Expand All @@ -134,7 +138,7 @@ func TestUpdateVersion(t *testing.T) {
if !csv.Spec.Version.Equal(*wantedSemver) {
t.Errorf("Wanted csv version %v, got %v", *wantedSemver, csv.Spec.Version)
}
wantedName := getCSVName(projectName, newCSVVer)
wantedName := getCSVName(operatorName, newCSVVer)
if csv.ObjectMeta.Name != wantedName {
t.Errorf("Wanted csv name %s, got %s", wantedName, csv.ObjectMeta.Name)
}
Expand All @@ -155,7 +159,7 @@ func TestUpdateVersion(t *testing.T) {
t.Errorf("Podspec image changed from %s to %s", wantedImage, csvPodImage)
}

wantedReplaces := getCSVName(projectName, "0.1.0")
wantedReplaces := getCSVName(operatorName, "0.1.0")
if csv.Spec.Replaces != wantedReplaces {
t.Errorf("Wanted csv replaces %s, got %s", wantedReplaces, csv.Spec.Replaces)
}
Expand Down
8 changes: 7 additions & 1 deletion internal/pkg/scaffold/scaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ type Scaffold struct {
Repo string
// AbsProjectPath is the absolute path to the project root, including the project directory.
AbsProjectPath string
// ProjectName is the operator's name, ex. app-operator
// ProjectName is the operator's project directory name
ProjectName string
// OperatorName is the operator's name, ex. app-operator
OperatorName string
// Fs is the filesystem GetWriter uses to write scaffold files.
Fs afero.Fs
// GetWriter returns a writer for writing scaffold files.
Expand All @@ -65,6 +67,9 @@ func (s *Scaffold) setFieldsAndValidate(t input.File) error {
if b, ok := t.(input.ProjectName); ok {
b.SetProjectName(s.ProjectName)
}
if b, ok := t.(input.OperatorName); ok {
b.SetOperatorName(s.OperatorName)
}

// Validate the template is ok
if v, ok := t.(input.Validate); ok {
Expand All @@ -79,6 +84,7 @@ func (s *Scaffold) configure(cfg *input.Config) {
s.Repo = cfg.Repo
s.AbsProjectPath = cfg.AbsProjectPath
s.ProjectName = cfg.ProjectName
s.OperatorName = cfg.OperatorName
}

func validateBoilerplateBytes(b []byte) error {
Expand Down

0 comments on commit f652c8a

Please sign in to comment.