diff --git a/apis/largetest/v1beta1/largetestexecution_types.go b/apis/largetest/v1beta1/largetestexecution_types.go index 482e6c5..5897c8a 100644 --- a/apis/largetest/v1beta1/largetestexecution_types.go +++ b/apis/largetest/v1beta1/largetestexecution_types.go @@ -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"` @@ -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"` diff --git a/apis/largetest/v1beta1/zz_generated.deepcopy.go b/apis/largetest/v1beta1/zz_generated.deepcopy.go index 071cb57..62cad1b 100644 --- a/apis/largetest/v1beta1/zz_generated.deepcopy.go +++ b/apis/largetest/v1beta1/zz_generated.deepcopy.go @@ -42,6 +42,8 @@ func (in *AppVersion) DeepCopy() *AppVersion { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *LargeTestExecution) DeepCopyInto(out *LargeTestExecution) { *out = *in + out.TypeMeta = in.TypeMeta + in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) in.Spec.DeepCopyInto(&out.Spec) } @@ -66,6 +68,8 @@ func (in *LargeTestExecution) DeepCopyObject() runtime.Object { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *LargeTestExecutionList) DeepCopyInto(out *LargeTestExecutionList) { *out = *in + out.TypeMeta = in.TypeMeta + in.ListMeta.DeepCopyInto(&out.ListMeta) if in.Items != nil { in, out := &in.Items, &out.Items *out = make([]LargeTestExecution, len(*in)) diff --git a/cmd/root_test.go b/cmd/root_test.go index 8f37d0d..29286e8 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -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) } diff --git a/cmd/topology/topologyCommand.go b/cmd/topology/topologyCommand.go index 6ccbe73..f6e903d 100644 --- a/cmd/topology/topologyCommand.go +++ b/cmd/topology/topologyCommand.go @@ -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 @@ -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() diff --git a/cmd/topology/topologyCommand_test.go b/cmd/topology/topologyCommand_test.go index 5935a9b..71eb5a0 100644 --- a/cmd/topology/topologyCommand_test.go +++ b/cmd/topology/topologyCommand_test.go @@ -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) } diff --git a/config/crd/bases/largetest.vitechteam.com_largetestexecutions.yaml b/config/crd/bases/largetest.vitechteam.com_largetestexecutions.yaml index c22ff3d..b50a206 100644 --- a/config/crd/bases/largetest.vitechteam.com_largetestexecutions.yaml +++ b/config/crd/bases/largetest.vitechteam.com_largetestexecutions.yaml @@ -8,6 +8,22 @@ 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 @@ -15,6 +31,7 @@ spec: plural: largetestexecutions singular: largetestexecution scope: Namespaced + subresources: {} validation: openAPIV3Schema: description: LargeTestExecution is the Schema for the largetestexecutions API @@ -38,6 +55,8 @@ spec: type: string image: type: string + namespace: + type: string report: type: string result: diff --git a/go.mod b/go.mod index 5b6e31a..2b4d5a9 100644 --- a/go.mod +++ b/go.mod @@ -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