github.com/jmrodri/operator-sdk@v0.5.0/commands/operator-sdk/cmd/olm-catalog/gen-csv.go (about)

     1  // Copyright 2018 The Operator-SDK Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package catalog
    16  
    17  import (
    18  	"fmt"
    19  	"path/filepath"
    20  
    21  	"github.com/operator-framework/operator-sdk/internal/util/projutil"
    22  	"github.com/operator-framework/operator-sdk/pkg/scaffold"
    23  	"github.com/operator-framework/operator-sdk/pkg/scaffold/input"
    24  	catalog "github.com/operator-framework/operator-sdk/pkg/scaffold/olm-catalog"
    25  
    26  	"github.com/coreos/go-semver/semver"
    27  	log "github.com/sirupsen/logrus"
    28  	"github.com/spf13/cobra"
    29  )
    30  
    31  var (
    32  	csvVersion    string
    33  	csvConfigPath string
    34  )
    35  
    36  func NewGenCSVCmd() *cobra.Command {
    37  	genCSVCmd := &cobra.Command{
    38  		Use:   "gen-csv",
    39  		Short: "Generates a Cluster Service Version yaml file for the operator",
    40  		Long: `The gen-csv command generates a Cluster Service Version (CSV) YAML manifest
    41  for the operator. This file is used to publish the operator to the OLM Catalog.
    42  
    43  A CSV semantic version is supplied via the --csv-version flag.
    44  
    45  Configure CSV generation by writing a config file 'deploy/olm-catalog/csv-config.yaml`,
    46  		RunE: genCSVFunc,
    47  	}
    48  
    49  	genCSVCmd.Flags().StringVar(&csvVersion, "csv-version", "", "Semantic version of the CSV")
    50  	genCSVCmd.MarkFlagRequired("csv-version")
    51  	genCSVCmd.Flags().StringVar(&csvConfigPath, "csv-config", "", "Path to CSV config file. Defaults to deploy/olm-catalog/csv-config.yaml")
    52  
    53  	return genCSVCmd
    54  }
    55  
    56  func genCSVFunc(cmd *cobra.Command, args []string) error {
    57  	if len(args) != 0 {
    58  		return fmt.Errorf("command %s doesn't accept any arguments", cmd.CommandPath())
    59  	}
    60  
    61  	if err := verifyGenCSVFlags(); err != nil {
    62  		return err
    63  	}
    64  
    65  	absProjectPath := projutil.MustGetwd()
    66  	cfg := &input.Config{
    67  		AbsProjectPath: absProjectPath,
    68  		ProjectName:    filepath.Base(absProjectPath),
    69  	}
    70  	if projutil.GetOperatorType() == projutil.OperatorTypeGo {
    71  		cfg.Repo = projutil.CheckAndGetProjectGoPkg()
    72  	}
    73  
    74  	log.Infof("Generating CSV manifest version %s", csvVersion)
    75  
    76  	s := &scaffold.Scaffold{}
    77  	err := s.Execute(cfg,
    78  		&catalog.CSV{CSVVersion: csvVersion, ConfigFilePath: csvConfigPath},
    79  		&catalog.ConcatCRD{ConfigFilePath: csvConfigPath},
    80  	)
    81  	if err != nil {
    82  		return fmt.Errorf("catalog scaffold failed: (%v)", err)
    83  	}
    84  	return nil
    85  }
    86  
    87  func verifyGenCSVFlags() error {
    88  	v, err := semver.NewVersion(csvVersion)
    89  	if err != nil {
    90  		return fmt.Errorf("%s is not a valid semantic version: (%v)", csvVersion, err)
    91  	}
    92  	// Ensures numerical values composing csvVersion don't contain leading 0's,
    93  	// ex. 01.01.01
    94  	if v.String() != csvVersion {
    95  		return fmt.Errorf("provided CSV version %s contains bad values (parses to %s)", csvVersion, v)
    96  	}
    97  	return nil
    98  }