Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Package resolvers now only returns resolved resources #1724

Merged
merged 6 commits into from
Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 5 additions & 11 deletions pkg/controller/instance/resolver_incluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@ import (
// This resolver is only used to make sure that all the dependencies of an operator exist and that referenced operator versions
// are installed and uniquely identifiable by the passed operator name, appVersion and operatorVersion parameters
// (see pkg/apis/kudo/v1beta1/operatorversion_types_helpers.go::OperatorVersionName method).
// Also, note that unlike other resolvers, the resulting 'packages.Package' struct does not contain package 'packages.Files'
// (we don't have the original files) and doesn't have an Instance resource because multiple Instances of the same
// Operator/OperatorVersion can exist.
type InClusterResolver struct {
c client.Client
ns string
}

func (r InClusterResolver) Resolve(name string, appVersion string, operatorVersion string) (*packages.Package, error) {
func (r InClusterResolver) Resolve(name string, appVersion string, operatorVersion string) (*packages.Resources, error) {
ovn := kudoapi.OperatorVersionName(name, appVersion, operatorVersion)

ov, err := kudoapi.GetOperatorVersionByName(ovn, r.ns, r.c)
Expand All @@ -37,12 +34,9 @@ func (r InClusterResolver) Resolve(name string, appVersion string, operatorVersi
return nil, fmt.Errorf("failed to resolve operator %s/%s", r.ns, name)
}

return &packages.Package{
Resources: &packages.Resources{
Operator: o,
OperatorVersion: ov,
Instance: nil,
},
Files: nil,
return &packages.Resources{
Operator: o,
OperatorVersion: ov,
Instance: nil,
}, nil
}
8 changes: 4 additions & 4 deletions pkg/kudoctl/cmd/generate/maintainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
// AddMaintainer adds a maintainer to the operator.yaml
func AddMaintainer(fs afero.Fs, path string, m *kudoapi.Maintainer) error {

p, err := reader.ReadDir(fs, path)
pf, err := reader.PackageFilesFromDir(fs, path)
if err != nil {
return err
}
o := p.Files.Operator
o := pf.Operator

o.Maintainers = append(o.Maintainers, m)

Expand All @@ -23,10 +23,10 @@ func AddMaintainer(fs afero.Fs, path string, m *kudoapi.Maintainer) error {

// MaintainerList provides a list of operator maintainers
func MaintainerList(fs afero.Fs, path string) ([]*kudoapi.Maintainer, error) {
p, err := reader.ReadDir(fs, path)
pf, err := reader.PackageFilesFromDir(fs, path)
if err != nil {
return nil, err
}

return p.Files.Operator.Maintainers, nil
return pf.Operator.Maintainers, nil
}
8 changes: 4 additions & 4 deletions pkg/kudoctl/cmd/generate/parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ import (
// AddParameter writes a parameter to the params.yaml file
func AddParameter(fs afero.Fs, path string, p *packages.Parameter) error {

pf, err := reader.ReadDir(fs, path)
pf, err := reader.PackageFilesFromDir(fs, path)
if err != nil {
return err
}

params := pf.Files.Params
params := pf.Params
params.Parameters = append(params.Parameters, *p)

return writeParameters(fs, path, *params)
}

func ParameterNameList(fs afero.Fs, path string) (paramNames []string, err error) {
pf, err := reader.ReadDir(fs, path)
pf, err := reader.PackageFilesFromDir(fs, path)
if err != nil {
return nil, err
}

for _, parameter := range pf.Files.Params.Parameters {
for _, parameter := range pf.Params.Parameters {
paramNames = append(paramNames, parameter.Name)
}

Expand Down
14 changes: 7 additions & 7 deletions pkg/kudoctl/cmd/generate/plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,41 @@ import (
// AddPlan adds a plan to the operator.yaml file
func AddPlan(fs afero.Fs, path string, planName string, plan *kudoapi.Plan) error {

pf, err := reader.ReadDir(fs, path)
pf, err := reader.PackageFilesFromDir(fs, path)
if err != nil {
return err
}

o := pf.Files.Operator
o := pf.Operator
if o.Plans == nil {
o.Plans = make(map[string]kudoapi.Plan)
}
plans := o.Plans
plans[planName] = *plan
pf.Files.Operator.Plans = plans
pf.Operator.Plans = plans

return writeOperator(fs, path, o)
}

// PlanList provides a list of operator plans
func PlanList(fs afero.Fs, path string) (map[string]kudoapi.Plan, error) {
p, err := reader.ReadDir(fs, path)
p, err := reader.PackageFilesFromDir(fs, path)
if err != nil {
return nil, err
}

return p.Files.Operator.Plans, nil
return p.Operator.Plans, nil
}

// PlanNameList provides a list of operator plan names
func PlanNameList(fs afero.Fs, path string) ([]string, error) {

names := []string{}
p, err := reader.ReadDir(fs, path)
p, err := reader.PackageFilesFromDir(fs, path)
if err != nil {
return nil, err
}
for name := range p.Files.Operator.Plans {
for name := range p.Operator.Plans {
names = append(names, name)
}
sort.Strings(names)
Expand Down
8 changes: 4 additions & 4 deletions pkg/kudoctl/cmd/generate/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import (

// AddTask adds a task to the operator.yaml file
func AddTask(fs afero.Fs, path string, task *kudoapi.Task) error {
p, err := reader.ReadDir(fs, path)
p, err := reader.PackageFilesFromDir(fs, path)
if err != nil {
return err
}
o := p.Files.Operator
o := p.Operator

o.Tasks = append(o.Tasks, *task)

Expand All @@ -26,12 +26,12 @@ func AddTask(fs afero.Fs, path string, task *kudoapi.Task) error {

// TaskList provides a list of operator tasks
func TaskList(fs afero.Fs, path string) ([]kudoapi.Task, error) {
p, err := reader.ReadDir(fs, path)
p, err := reader.PackageFilesFromDir(fs, path)
if err != nil {
return nil, err
}

return p.Files.Operator.Tasks, nil
return p.Operator.Tasks, nil
}

func TaskInList(fs afero.Fs, path, taskName string) (bool, error) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/kudoctl/cmd/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ func installOperator(operatorArgument string, options *Options, fs afero.Fs, set
resolver = pkgresolver.New(repoClient)
}

pkg, err := resolver.Resolve(operatorArgument, options.AppVersion, options.OperatorVersion)
pr, err := resolver.Resolve(operatorArgument, options.AppVersion, options.OperatorVersion)
if err != nil {
return fmt.Errorf("failed to resolve operator package for: %s %w", operatorArgument, err)
}

dependencies, err := deps.Resolve(operatorArgument, pkg.Resources.OperatorVersion, resolver)
dependencies, err := deps.Resolve(operatorArgument, pr.OperatorVersion, resolver)
if err != nil {
return err
}
Expand All @@ -111,7 +111,7 @@ func installOperator(operatorArgument string, options *Options, fs afero.Fs, set
kudoClient,
options.InstanceName,
settings.Namespace,
*pkg.Resources,
*pr,
options.Parameters,
dependencies,
installOpts)
Expand Down
6 changes: 3 additions & 3 deletions pkg/kudoctl/cmd/package_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func newPackageParamsCmd(fs afero.Fs, out io.Writer) *cobra.Command {
}

// packageDiscovery is used by all list cmds to "discover" the packages
func packageDiscovery(fs afero.Fs, settings *env.Settings, repoName, pathOrName, appVersion, operatorVersion string) (*packages.Package, error) {
func packageDiscovery(fs afero.Fs, settings *env.Settings, repoName, pathOrName, appVersion, operatorVersion string) (*packages.Resources, error) {
repository, err := repo.ClientFromSettings(fs, settings.Home, repoName)
if err != nil {
return nil, fmt.Errorf("could not build operator repository: %w", err)
Expand All @@ -70,9 +70,9 @@ func packageDiscovery(fs afero.Fs, settings *env.Settings, repoName, pathOrName,

clog.V(3).Printf("getting package pkg files for %v with version: %v_%v", pathOrName, appVersion, operatorVersion)
resolver := pkgresolver.New(repository)
pf, err := resolver.Resolve(pathOrName, appVersion, operatorVersion)
pr, err := resolver.Resolve(pathOrName, appVersion, operatorVersion)
if err != nil {
return nil, fmt.Errorf("failed to resolve package files for operator: %s: %w", pathOrName, err)
}
return pf, nil
return pr, nil
}
43 changes: 27 additions & 16 deletions pkg/kudoctl/cmd/package_list_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
"github.com/spf13/afero"
"github.com/spf13/cobra"

kudoapi "github.com/kudobuilder/kudo/pkg/apis/kudo/v1beta1"
"github.com/kudobuilder/kudo/pkg/kudoctl/clog"
"github.com/kudobuilder/kudo/pkg/kudoctl/cmd/generate"
"github.com/kudobuilder/kudo/pkg/kudoctl/env"
"github.com/kudobuilder/kudo/pkg/kudoctl/packages"
"github.com/kudobuilder/kudo/pkg/util/convert"
)

Expand All @@ -28,6 +28,22 @@ type packageListParamsCmd struct {
OperatorVersion string
}

type Parameters []kudoapi.Parameter

// Len returns the number of params.
// This is needed to allow sorting of params.
func (p Parameters) Len() int { return len(p) }

// Swap swaps the position of two items in the params slice.
// This is needed to allow sorting of params.
func (p Parameters) Swap(i, j int) { p[i], p[j] = p[j], p[i] }

// Less returns true if the name of a param a is less than the name of param b.
// This is needed to allow sorting of params.
func (p Parameters) Less(x, y int) bool {
return p[x].Name < p[y].Name
}

const (
pacakgeListParamsExample = `# show parameters from local-folder (where local-folder is a folder in the current directory)
kubectl kudo package list parameters local-folder
Expand Down Expand Up @@ -82,26 +98,26 @@ func (c *packageListParamsCmd) run(settings *env.Settings) error {
if !onlyOneSet(c.requiredOnly, c.namesOnly, c.descriptions) {
return fmt.Errorf("only one of the flags 'required', 'names', 'descriptions' can be set")
}
pf, err := packageDiscovery(c.fs, settings, c.RepoName, c.pathOrName, c.AppVersion, c.OperatorVersion)
pr, err := packageDiscovery(c.fs, settings, c.RepoName, c.pathOrName, c.AppVersion, c.OperatorVersion)
if err != nil {
return err
}

if err := displayParamsTable(pf.Files, c.out, c.requiredOnly, c.namesOnly, c.descriptions); err != nil {
if err := displayParamsTable(pr.OperatorVersion.Spec.Parameters, c.out, c.requiredOnly, c.namesOnly, c.descriptions); err != nil {
return err
}

return nil
}

func displayParamsTable(pf *packages.Files, out io.Writer, printRequired, printNames, printDesc bool) error {
sort.Sort(pf.Params.Parameters)
func displayParamsTable(params Parameters, out io.Writer, printRequired, printNames, printDesc bool) error {
sort.Sort(params)
table := uitable.New()
tValue := true
if printRequired {
table.AddRow("Name")
found := false
for _, p := range pf.Params.Parameters {
for _, p := range params {
if p.Default == nil && p.Required == &tValue {
found = true
table.AddRow(p.Name)
Expand All @@ -119,7 +135,7 @@ func displayParamsTable(pf *packages.Files, out io.Writer, printRequired, printN
}
if printNames {
table.AddRow("Name")
for _, p := range pf.Params.Parameters {
for _, p := range params {
table.AddRow(p.Name)
}
if _, err := fmt.Fprintln(out, table); err != nil {
Expand All @@ -134,17 +150,12 @@ func displayParamsTable(pf *packages.Files, out io.Writer, printRequired, printN
} else {
table.AddRow("Name", "Default", "Required", "Immutable")
}
sort.Sort(pf.Params.Parameters)
for _, p := range pf.Params.Parameters {
pDefault, err := convert.WrapParamValue(p.Default, p.Type)
if err != nil {
return err
}

sort.Sort(params)
for _, p := range params {
if printDesc {
table.AddRow(p.Name, convert.StringValue(pDefault), p.IsRequired(), p.IsImmutable(), p.Description)
table.AddRow(p.Name, convert.StringValue(p.Default), p.IsRequired(), p.IsImmutable(), p.Description)
} else {
table.AddRow(p.Name, convert.StringValue(pDefault), p.IsRequired(), p.IsImmutable())
table.AddRow(p.Name, convert.StringValue(p.Default), p.IsRequired(), p.IsImmutable())
}
}
_, _ = fmt.Fprintln(out, table)
Expand Down
2 changes: 1 addition & 1 deletion pkg/kudoctl/cmd/package_list_params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ func TestParamsList(t *testing.T) {
t.Fatalf("failed reading .golden: %s", err)
}

assert.Equal(t, out.String(), string(g), "yaml does not match .golden file %s", gp)
assert.Equal(t, string(g), out.String(), "yaml does not match .golden file %s", gp)
}
24 changes: 12 additions & 12 deletions pkg/kudoctl/cmd/package_list_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"github.com/thoas/go-funk"
"github.com/xlab/treeprint"

kudoapi "github.com/kudobuilder/kudo/pkg/apis/kudo/v1beta1"
"github.com/kudobuilder/kudo/pkg/engine/task"
"github.com/kudobuilder/kudo/pkg/kudoctl/env"
"github.com/kudobuilder/kudo/pkg/kudoctl/packages"
)

type packageListPlansCmd struct {
Expand Down Expand Up @@ -59,21 +59,21 @@ func newPackageListPlansCmd(fs afero.Fs, out io.Writer) *cobra.Command {
}

func (c *packageListPlansCmd) run(settings *env.Settings) error {
pf, err := packageDiscovery(c.fs, settings, c.RepoName, c.pathOrName, c.AppVersion, c.OperatorVersion)
pr, err := packageDiscovery(c.fs, settings, c.RepoName, c.pathOrName, c.AppVersion, c.OperatorVersion)
if err != nil {
return err
}

displayPlanTable(pf.Files, c.WithTasksResources, c.out)
displayPlanTable(pr.OperatorVersion, c.WithTasksResources, c.out)
return nil
}

func displayPlanTable(pf *packages.Files, withTasks bool, out io.Writer) {
func displayPlanTable(ov *kudoapi.OperatorVersion, withTasks bool, out io.Writer) {
tree := treeprint.New()
planNames := sortedPlanNames(pf)
planNames := sortedPlanNames(ov.Spec.Plans)
tree.SetValue("plans")
for _, name := range planNames {
plan := pf.Operator.Plans[name]
plan := ov.Spec.Plans[name]
pNode := tree.AddBranch(fmt.Sprintf("%s (%s)", name, plan.Strategy))

for _, phase := range plan.Phases {
Expand All @@ -82,7 +82,7 @@ func displayPlanTable(pf *packages.Files, withTasks bool, out io.Writer) {
sNode := phNode.AddMetaBranch("step", step.Name)
for _, taskName := range step.Tasks {
if withTasks {
addTaskNodeWithResources(sNode, taskName, pf)
addTaskNodeWithResources(sNode, taskName, ov.Spec.Tasks)
} else {
sNode.AddNode(taskName)
}
Expand All @@ -91,24 +91,24 @@ func displayPlanTable(pf *packages.Files, withTasks bool, out io.Writer) {
}
}

if len(pf.Operator.Plans) == 0 {
if len(ov.Spec.Plans) == 0 {
fmt.Fprintf(out, "no plans found\n")
} else {
fmt.Fprintln(out, tree.String())
}
}

func sortedPlanNames(pf *packages.Files) []string {
planNames, ok := funk.Keys(pf.Operator.Plans).([]string)
func sortedPlanNames(plans map[string]kudoapi.Plan) []string {
planNames, ok := funk.Keys(plans).([]string)
if !ok {
panic("funk.Keys returned unexpected type")
}
sort.Strings(planNames)
return planNames
}

func addTaskNodeWithResources(sNode treeprint.Tree, taskName string, pf *packages.Files) {
for _, t := range pf.Operator.Tasks {
func addTaskNodeWithResources(sNode treeprint.Tree, taskName string, tasks []kudoapi.Task) {
for _, t := range tasks {
if t.Name == taskName {
switch t.Kind {
case task.ApplyTaskKind:
Expand Down