-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Operator Skeleton Generators (#1222)
Co-Authored-By: Andreas Neumann <aneumann@mesosphere.com> Signed-off-by: Ken Sipe <kensipe@gmail.com>
- Loading branch information
1 parent
7d84119
commit 60b0f19
Showing
27 changed files
with
1,711 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package generate | ||
|
||
import ( | ||
"github.com/spf13/afero" | ||
|
||
"github.com/kudobuilder/kudo/pkg/apis/kudo/v1beta1" | ||
"github.com/kudobuilder/kudo/pkg/kudoctl/packages/reader" | ||
) | ||
|
||
// AddMaintainer adds a maintainer to the operator.yaml | ||
func AddMaintainer(fs afero.Fs, path string, m *v1beta1.Maintainer) error { | ||
|
||
p, err := reader.ReadDir(fs, path) | ||
if err != nil { | ||
return err | ||
} | ||
o := p.Files.Operator | ||
|
||
o.Maintainers = append(o.Maintainers, m) | ||
|
||
return writeOperator(fs, path, o) | ||
} | ||
|
||
// MaintainerList provides a list of operator maintainers | ||
func MaintainerList(fs afero.Fs, path string) ([]*v1beta1.Maintainer, error) { | ||
p, err := reader.ReadDir(fs, path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return p.Files.Operator.Maintainers, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package generate | ||
|
||
import ( | ||
"flag" | ||
"io/ioutil" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/spf13/afero" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/kudobuilder/kudo/pkg/apis/kudo/v1beta1" | ||
"github.com/kudobuilder/kudo/pkg/kudoctl/files" | ||
) | ||
|
||
var updateGolden = flag.Bool("update", false, "update .golden files and manifests in /config/crd") | ||
|
||
func TestAddMaintainer(t *testing.T) { | ||
goldenFile := "maintainer" | ||
fs := afero.NewMemMapFs() | ||
files.CopyOperatorToFs(fs, "../../packages/testdata/zk", "/opt") | ||
m := v1beta1.Maintainer{ | ||
Name: "Cat in the hat", | ||
Email: "c@hat.com", | ||
} | ||
|
||
err := AddMaintainer(fs, "/opt/zk", &m) | ||
assert.NoError(t, err) | ||
|
||
operator, err := afero.ReadFile(fs, "/opt/zk/operator.yaml") | ||
assert.NoError(t, err) | ||
|
||
gp := filepath.Join("testdata", goldenFile+".golden") | ||
|
||
if *updateGolden { | ||
t.Logf("updating golden file %s", goldenFile) | ||
if err := ioutil.WriteFile(gp, operator, 0644); err != nil { | ||
t.Fatalf("failed to update golden file: %s", err) | ||
} | ||
} | ||
g, err := ioutil.ReadFile(gp) | ||
if err != nil { | ||
t.Fatalf("failed reading .golden: %s", err) | ||
} | ||
|
||
assert.Equal(t, g, operator, "for golden file: %s", gp) | ||
} | ||
|
||
func TestListMaintainers(t *testing.T) { | ||
fs := afero.OsFs{} | ||
m, err := MaintainerList(fs, "../../packages/testdata/zk") | ||
assert.NoError(t, err) | ||
|
||
assert.Equal(t, 3, len(m)) | ||
assert.Equal(t, "Alena Varkockova", m[0].Name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package generate | ||
|
||
import ( | ||
"github.com/spf13/afero" | ||
|
||
"github.com/kudobuilder/kudo/pkg/apis/kudo/v1beta1" | ||
"github.com/kudobuilder/kudo/pkg/kudoctl/packages/reader" | ||
) | ||
|
||
// AddParameter writes a parameter to the params.yaml file | ||
func AddParameter(fs afero.Fs, path string, p *v1beta1.Parameter) error { | ||
|
||
pf, err := reader.ReadDir(fs, path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
params := pf.Files.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) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, parameter := range pf.Files.Params.Parameters { | ||
paramNames = append(paramNames, parameter.Name) | ||
} | ||
|
||
return paramNames, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package generate | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/spf13/afero" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/kudobuilder/kudo/pkg/apis/kudo/v1beta1" | ||
"github.com/kudobuilder/kudo/pkg/kudoctl/files" | ||
) | ||
|
||
func TestAddParameter(t *testing.T) { | ||
goldenFile := "parameter" | ||
path := "/opt/zk" | ||
fs := afero.NewMemMapFs() | ||
files.CopyOperatorToFs(fs, "../../packages/testdata/zk", "/opt") | ||
|
||
bar := "Bar" | ||
p := v1beta1.Parameter{ | ||
Name: "Foo", | ||
Default: &bar, | ||
} | ||
|
||
err := AddParameter(fs, path, &p) | ||
assert.NoError(t, err) | ||
|
||
params, err := afero.ReadFile(fs, "/opt/zk/params.yaml") | ||
assert.NoError(t, err) | ||
|
||
gp := filepath.Join("testdata", goldenFile+".golden") | ||
|
||
if *updateGolden { | ||
t.Logf("updating golden file %s", goldenFile) | ||
if err := ioutil.WriteFile(gp, params, 0644); err != nil { | ||
t.Fatalf("failed to update golden file: %s", err) | ||
} | ||
} | ||
golden, err := ioutil.ReadFile(gp) | ||
if err != nil { | ||
t.Fatalf("failed reading .golden: %s", err) | ||
} | ||
|
||
assert.Equal(t, golden, params, "for golden file: %s", gp) | ||
} | ||
|
||
func TestAddParameter_bad_path(t *testing.T) { | ||
path, _ := os.Getwd() | ||
fs := afero.OsFs{} | ||
|
||
bar := "Bar" | ||
p := v1beta1.Parameter{ | ||
Name: "Foo", | ||
Default: &bar, | ||
} | ||
|
||
err := AddParameter(fs, path, &p) | ||
assert.Error(t, err) | ||
} | ||
|
||
func TestListParams(t *testing.T) { | ||
fs := afero.OsFs{} | ||
ps, err := ParameterNameList(fs, "../../packages/testdata/zk") | ||
assert.NoError(t, err) | ||
|
||
assert.Equal(t, 2, len(ps)) | ||
assert.Equal(t, "memory", ps[0]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package generate | ||
|
||
import ( | ||
"sort" | ||
|
||
"github.com/spf13/afero" | ||
|
||
"github.com/kudobuilder/kudo/pkg/apis/kudo/v1beta1" | ||
"github.com/kudobuilder/kudo/pkg/kudoctl/packages/reader" | ||
) | ||
|
||
// AddPlan adds a plan to the operator.yaml file | ||
func AddPlan(fs afero.Fs, path string, planName string, plan *v1beta1.Plan) error { | ||
|
||
pf, err := reader.ReadDir(fs, path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
o := pf.Files.Operator | ||
plans := o.Plans | ||
plans[planName] = *plan | ||
pf.Files.Operator.Plans = plans | ||
|
||
return writeOperator(fs, path, o) | ||
} | ||
|
||
// PlanList provides a list of operator plans | ||
func PlanList(fs afero.Fs, path string) (map[string]v1beta1.Plan, error) { | ||
p, err := reader.ReadDir(fs, path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return p.Files.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) | ||
if err != nil { | ||
return nil, err | ||
} | ||
for name := range p.Files.Operator.Plans { | ||
names = append(names, name) | ||
} | ||
sort.Strings(names) | ||
return names, nil | ||
} |
Oops, something went wrong.