Skip to content

Commit

Permalink
feat: add command which is marking topology as tested
Browse files Browse the repository at this point in the history
  • Loading branch information
serhiy-krupka-vitech committed Mar 25, 2021
1 parent 7e50097 commit c24b0bd
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 7 deletions.
6 changes: 6 additions & 0 deletions apis/largetest/v1beta1/largetestexecution_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type LargeTestExecutionSpec struct {
Image string `json:"image,omitempty"`
Result string `json:"result,omitempty"`
Environment string `json:"environment,omitempty"`
Namespace string `json:"namespace,omitempty"`
Report string `json:"report,omitempty"`
Time string `json:"time,omitempty"`
Topology []AppVersion `json:"topology,omitempty"`
Expand All @@ -49,6 +50,11 @@ type AppVersion struct {
// +genclient
// +k8s:openapi-gen=true
// LargeTestExecution is the Schema for the largetestexecutions API
// +kubebuilder:printcolumn:name="Env",type=string,JSONPath=`.spec.environment`
// +kubebuilder:printcolumn:name="Ns",type=string,JSONPath=`.spec.namespace`
// +kubebuilder:printcolumn:name="Report",type=string,JSONPath=`.spec.report`
// +kubebuilder:printcolumn:name="Result",type=string,JSONPath=`.spec.result`
// +kubebuilder:printcolumn:name="Time",type=string,JSONPath=`.spec.time`
type LargeTestExecution struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Expand Down
4 changes: 4 additions & 0 deletions apis/largetest/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestRootCmd(t *testing.T) {

//o.HelmfileDir = "/Users/serhiykrupka/test-clone"
//o.GitUrl = "https://github.com/vitech-team/test-sk-env.git"
//err := o.Run()
//err := o.Print()
err := cmd.Execute()
assert.NoError(t, err)
}
127 changes: 122 additions & 5 deletions cmd/topology/topologyCommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,28 @@ import (
sdlc "github.com/vitech-team/sdlcctl/apis/largetest/v1beta1"
sdlcUtils "github.com/vitech-team/sdlcctl/cmd/utils"
"io/ioutil"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"os"
"sort"
"strings"
"time"
)

type OptionsTopology struct {
*sdlcUtils.Options
}

type OptionsTopologyTested struct {
Status string
Report string
Commit string
Repo string
Image string
*OptionsTopology
}

var commandRunner cmdrunner.CommandRunner
var gitClient gitclient.Interface

Expand All @@ -40,24 +52,129 @@ func init() {

func NewTopologyCmd(opts *sdlcUtils.Options) (*cobra.Command, *OptionsTopology) {
options := &OptionsTopology{opts}
optionTested := &OptionsTopologyTested{OptionsTopology: options}

command := &cobra.Command{
Use: "topology",
Aliases: []string{"matrix"},
Short: "tp",
Example: "bla bla bla",
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
},
}

printCmd := &cobra.Command{
Use: "print",
Example: "print current topology VS previous",
Run: func(cmd *cobra.Command, args []string) {
err := options.Run()
err := options.Print()
if err != nil {
panic(err.Error())
log.Error(err.Error())
os.Exit(1)
}
},
}

testedCmd := &cobra.Command{
Use: "tested",
Example: "create large test execution for current topology ",
Run: func(cmd *cobra.Command, args []string) {
err := optionTested.MarkWithLargeTestExec()
if err != nil {
log.Error(err.Error())
os.Exit(1)
}
},
}

testedCmd.Flags().StringVarP(
&optionTested.Status, "status", "", "", "large test status success/failed",
)
testedCmd.Flags().StringVarP(
&optionTested.Report, "report", "", "", "report url or place where it can be found",
)
testedCmd.Flags().StringVarP(
&optionTested.Commit, "commit", "", "", "commit what been tested",
)
testedCmd.Flags().StringVarP(
&optionTested.Repo, "repo", "", "", "repository where version been changed",
)
testedCmd.Flags().StringVarP(
&optionTested.Image, "image", "", "", "large reports produced",
)

testedCmd.MarkFlagRequired("status")
testedCmd.MarkFlagRequired("report")
testedCmd.MarkFlagRequired("commit")
testedCmd.MarkFlagRequired("repo")
testedCmd.MarkFlagRequired("image")

command.AddCommand(printCmd)
command.AddCommand(testedCmd)

return command, options
}

func (opt *OptionsTopology) Run() error {
func (opt *OptionsTopologyTested) MarkWithLargeTestExec() error {
currentHelmState := opt.GetEnvironmentsFromHelmFile(opt.Helmfile, opt.HelmfileDir)
for _, env := range currentHelmState {
opt.KubeClient, opt.JxClient, opt.LtClient = sdlcUtils.NewLazyClients(opt.KubeClient, opt.JxClient, opt.LtClient)
err := createIfNotExists(opt, env)
if err != nil {
return err
}

lte := &sdlc.LargeTestExecution{
ObjectMeta: metav1.ObjectMeta{
Namespace: env.Spec.Namespace,
GenerateName: fmt.Sprintf("%s-%s", env.Spec.Namespace, opt.Commit),
},
Spec: sdlc.LargeTestExecutionSpec{
Image: opt.Image,
Result: opt.Status,
Environment: env.Name,
Namespace: env.Spec.Namespace,
Report: opt.Report,
Time: time.Now().String(),
Topology: env.Topology,
},
}
created, err := opt.LtClient.LargetestV1beta1().LargeTestExecutions(env.Spec.Namespace).Create(
context.TODO(), lte, metav1.CreateOptions{},
)
if err != nil {
return err
}
log.WithField("name", created.Name).
WithField("ns", env.Spec.Namespace).
Info("new LargeTestExecution created")
}

return nil
}

func createIfNotExists(opt *OptionsTopologyTested, env sdlcUtils.Environment) error {
var _, err = opt.KubeClient.CoreV1().Namespaces().Get(context.TODO(), env.Spec.Namespace, metav1.GetOptions{})
if err != nil {
if err.(*errors.StatusError).ErrStatus.Reason == metav1.StatusReasonNotFound {
_, err = opt.KubeClient.CoreV1().Namespaces().Create(
context.TODO(),
&v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: env.Spec.Namespace,
},
}, metav1.CreateOptions{},
)
if err != nil {
return err
}
} else {
return err
}
}
return err
}

func (opt *OptionsTopology) Print() error {

comparedEnvironments, err := opt.GetComparedTopology()

Expand Down
20 changes: 19 additions & 1 deletion cmd/topology/topologyCommand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,25 @@ func TestNewTopologyCmd(t *testing.T) {

//o.HelmfileDir = "/Users/serhiykrupka/test-clone"
//o.GitUrl = "https://github.com/vitech-team/test-sk-env.git"
//err := o.Run()
//err := o.Print()
err := cmd.Execute()
assert.NoError(t, err)
}

func TestNewTopologyCmdTested(t *testing.T) {
options := utils.Options{}
cmd, _ := topology.NewTopologyCmd(&options)
options.AddBaseFlags(cmd)
cmd.SetArgs([]string{
"tested",
"--gitUrl", "https://github.com/vitech-team/test-sk-env.git",
"--hfd", "/Users/serhiykrupka/test-clone",
"--status", "ok", "--repo", "https://github.com", "--commit", "123", "--image", "gcr.io", "--report", "rep/ttt/",
})

//o.HelmfileDir = "/Users/serhiykrupka/test-clone"
//o.GitUrl = "https://github.com/vitech-team/test-sk-env.git"
//err := o.Print()
err := cmd.Execute()
assert.NoError(t, err)
}
19 changes: 19 additions & 0 deletions config/crd/bases/largetest.vitechteam.com_largetestexecutions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,30 @@ metadata:
creationTimestamp: null
name: largetestexecutions.largetest.vitechteam.com
spec:
additionalPrinterColumns:
- JSONPath: .spec.environment
name: Env
type: string
- JSONPath: .spec.namespace
name: Ns
type: string
- JSONPath: .spec.report
name: Report
type: string
- JSONPath: .spec.result
name: Result
type: string
- JSONPath: .spec.time
name: Time
type: string
group: largetest.vitechteam.com
names:
kind: LargeTestExecution
listKind: LargeTestExecutionList
plural: largetestexecutions
singular: largetestexecution
scope: Namespaced
subresources: {}
validation:
openAPIV3Schema:
description: LargeTestExecution is the Schema for the largetestexecutions API
Expand All @@ -38,6 +55,8 @@ spec:
type: string
image:
type: string
namespace:
type: string
report:
type: string
result:
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/sirupsen/logrus v1.7.0
github.com/spf13/cobra v1.1.1
github.com/stretchr/testify v1.6.1
k8s.io/api v0.20.5 // indirect
k8s.io/apimachinery v0.20.5
k8s.io/client-go v11.0.1-0.20190805182717-6502b5e7b1b5+incompatible
sigs.k8s.io/controller-runtime v0.8.0
Expand Down

0 comments on commit c24b0bd

Please sign in to comment.