Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
move config/scorecard scaffold to 'init' plugins
  • Loading branch information
estroz committed Jul 23, 2020
commit ff1c2b0d4be9f42f080be2a4d6abdeee25011f21
3 changes: 0 additions & 3 deletions cmd/operator-sdk/generate/kustomize/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ package kustomize

import (
"github.com/spf13/cobra"

"github.com/operator-framework/operator-sdk/cmd/operator-sdk/generate/kustomize/scorecard"
)

// NewCmd returns the 'kustomize' subcommand.
Expand All @@ -29,7 +27,6 @@ func NewCmd() *cobra.Command {

cmd.AddCommand(
newManifestsCmd(),
scorecard.NewCmd(),
)

return cmd
Expand Down
14 changes: 2 additions & 12 deletions cmd/operator-sdk/generate/kustomize/manifests.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package kustomize

import (
"fmt"
"os"
"path/filepath"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -154,6 +153,7 @@ func (c *manifestsCmd) setDefaults(cfg *config.Config) {
const manifestsKustomization = `resources:
- ../default
- ../samples
- ../scorecard
`

// run generates kustomize bundle bases and a kustomization.yaml if one does not exist.
Expand All @@ -175,18 +175,8 @@ func (c manifestsCmd) run(cfg *config.Config) error {
return fmt.Errorf("error generating kustomize bases: %v", err)
}

// NB(estroz): this is a rather hacky way of adding scorecard componentconfigs to the bundle,
// and won't work if the manifests kustomization.yaml already exists. This should be improved
// with scaffolding markers.
kustomization := manifestsKustomization

// Add a scorecard kustomization if one exists.
info, err := os.Stat(filepath.Join(filepath.Dir(c.outputDir), "scorecard"))
if err == nil && info.IsDir() {
kustomization += "- ../scorecard\n"
}
// Write a kustomization.yaml to outputDir if one does not exist.
if err := kustomize.WriteIfNotExist(c.outputDir, kustomization); err != nil {
if err := kustomize.WriteIfNotExist(c.outputDir, manifestsKustomization); err != nil {
return fmt.Errorf("error writing kustomization.yaml: %v", err)
}

Expand Down
133 changes: 0 additions & 133 deletions cmd/operator-sdk/generate/kustomize/scorecard/cmd.go

This file was deleted.

15 changes: 15 additions & 0 deletions internal/plugins/golang/v2/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@ package v2

import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/spf13/pflag"
"sigs.k8s.io/kubebuilder/pkg/model/config"
"sigs.k8s.io/kubebuilder/pkg/plugin"

"github.com/operator-framework/operator-sdk/internal/plugins/scorecard"
utilplugins "github.com/operator-framework/operator-sdk/internal/util/plugins"
)

Expand Down Expand Up @@ -50,6 +54,17 @@ func (p *initPlugin) Run() error {
return err
}

// Assume projectName was validated by go.kubebuilder.io.
wd, err := os.Getwd()
if err != nil {
return fmt.Errorf("error getting the current path: %v", err)
}
projectName := strings.ToLower(filepath.Base(wd))
// Run the scorecard "phase 2" plugin.
if err := scorecard.RunInit(projectName); err != nil {
return err
}

// Update plugin config section with this plugin's configuration.
cfg := Config{}
if err := p.config.EncodePluginConfig(pluginConfigKey, cfg); err != nil {
Expand Down
18 changes: 17 additions & 1 deletion internal/plugins/helm/v1/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/operator-framework/operator-sdk/internal/kubebuilder/cmdutil"
"github.com/operator-framework/operator-sdk/internal/plugins/helm/v1/chartutil"
"github.com/operator-framework/operator-sdk/internal/plugins/helm/v1/scaffolds"
"github.com/operator-framework/operator-sdk/internal/plugins/scorecard"
utilplugins "github.com/operator-framework/operator-sdk/internal/util/plugins"
)

Expand Down Expand Up @@ -128,7 +129,22 @@ func (p *initPlugin) InjectConfig(c *config.Config) {

// Run will call the plugin actions
func (p *initPlugin) Run() error {
return cmdutil.Run(p)
if err := cmdutil.Run(p); err != nil {
return err
}

// Assume projectName was validated by Validate().
wd, err := os.Getwd()
if err != nil {
return fmt.Errorf("error getting the current path: %v", err)
}
projectName := strings.ToLower(filepath.Base(wd))
// Run the scorecard "phase 2" plugin.
if err := scorecard.RunInit(projectName); err != nil {
return err
}

return nil
}

// Validate perform the required validations for this plugin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,28 @@ patchesJson6902:
{{- end }}
`

// scorecardKustomization holds data required to generate a scorecard's kustomization.yaml.
type scorecardKustomization struct {
const (
// defaultTestImageTag points to the latest-released image.
// TODO: change the tag to "latest" once config scaffolding is in a release,
// as the new config spec won't work with the current latest image.
defaultTestImageTag = "quay.io/operator-framework/scorecard-test:master"

// scorecardConfigName is the default scorecard componentconfig's metadata.name,
// which must be set on all kustomize-able bases. This name is only used for
// `kustomize build` pattern match and not for on-cluster creation.
scorecardConfigName = "config"
)

// defaultDir is the default directory in which to generate kustomize bases and the kustomization.yaml.
var defaultDir = filepath.Join("config", "scorecard")

// RunInit scaffolds kustomize files for kustomizing a scorecard componentconfig.
func RunInit(projectName string) error {
return generate(projectName, defaultTestImageTag, defaultDir)
}

// scorecardKustomizationValues holds data required to generate a scorecard's kustomization.yaml.
type scorecardKustomizationValues struct {
ResourcePaths []string
JSONPatches []kustomizationJSON6902Patch
}
Expand All @@ -59,18 +79,11 @@ type kustomizationJSON6902Patch struct {
Target registry.DefinitionKey
}

const (
// scorecardConfigName is the default scorecard componentconfig's metadata.name,
// which must be set on all kustomize-able bases. This name is only used for
// `kustomize build` pattern match and not for "creation".
scorecardConfigName = "config"
)

// generate scaffolds kustomize bundle bases and a kustomization.yaml.
// TODO(estroz): refactor this to be testable (in-mem fs) and easier to read.
func generate(operatorName, testImageTag, outputDir string) error {

kustomization := scorecardKustomization{}
kustomizationValues := scorecardKustomizationValues{}

// Config bases.
basesDir := filepath.Join(outputDir, "bases")
Expand All @@ -88,7 +101,7 @@ func generate(operatorName, testImageTag, outputDir string) error {
if err := ioutil.WriteFile(basePath, b, 0666); err != nil {
return fmt.Errorf("error writing default scorecard config: %v", err)
}
kustomization.ResourcePaths = append(kustomization.ResourcePaths, relBasePath)
kustomizationValues.ResourcePaths = append(kustomizationValues.ResourcePaths, relBasePath)
scorecardConfigTarget := registry.DefinitionKey{
Group: v1alpha3.SchemeGroupVersion.Group,
Version: v1alpha3.SchemeGroupVersion.Version,
Expand All @@ -112,7 +125,7 @@ func generate(operatorName, testImageTag, outputDir string) error {
if err := ioutil.WriteFile(filepath.Join(patchesDir, basicPatchFileName), b, 0666); err != nil {
return fmt.Errorf("error writing basic scorecard config patch: %v", err)
}
kustomization.JSONPatches = append(kustomization.JSONPatches, kustomizationJSON6902Patch{
kustomizationValues.JSONPatches = append(kustomizationValues.JSONPatches, kustomizationJSON6902Patch{
Path: filepath.Join("patches", basicPatchFileName),
Target: scorecardConfigTarget,
})
Expand All @@ -127,7 +140,7 @@ func generate(operatorName, testImageTag, outputDir string) error {
if err := ioutil.WriteFile(filepath.Join(patchesDir, olmPatchFileName), b, 0666); err != nil {
return fmt.Errorf("error writing default scorecard config: %v", err)
}
kustomization.JSONPatches = append(kustomization.JSONPatches, kustomizationJSON6902Patch{
kustomizationValues.JSONPatches = append(kustomizationValues.JSONPatches, kustomizationJSON6902Patch{
Path: filepath.Join("patches", olmPatchFileName),
Target: scorecardConfigTarget,
})
Expand All @@ -138,7 +151,7 @@ func generate(operatorName, testImageTag, outputDir string) error {
return fmt.Errorf("error parsing default kustomize template: %v", err)
}
buf := bytes.Buffer{}
if err = t.Execute(&buf, kustomization); err != nil {
if err = t.Execute(&buf, kustomizationValues); err != nil {
return fmt.Errorf("error executing on default kustomize template: %v", err)
}
if err := kustomize.Write(outputDir, buf.String()); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,4 @@ Contains subcommands that generate operator-framework kustomize data for the ope

* [operator-sdk generate](../operator-sdk_generate) - Invokes a specific generator
* [operator-sdk generate kustomize manifests](../operator-sdk_generate_kustomize_manifests) - Generates kustomize bases and a kustomization.yaml for operator-framework manifests
* [operator-sdk generate kustomize scorecard](../operator-sdk_generate_kustomize_scorecard) - Generates scorecard configuration files

Loading