Skip to content

Commit

Permalink
Adds test suite
Browse files Browse the repository at this point in the history
Signed-off-by: PuneetPunamiya <ppunamiy@redhat.com>
  • Loading branch information
PuneetPunamiya committed Apr 24, 2024
1 parent 5ff906c commit a8ffc00
Show file tree
Hide file tree
Showing 291 changed files with 28,799 additions and 26 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/tektoncd/plumbing v0.0.0-20221005220331-b2ddcdddc5e7
go.uber.org/zap v1.26.0
gomodules.xyz/jsonpatch/v2 v2.4.0
gotest.tools/v3 v3.1.0
k8s.io/api v0.27.1
k8s.io/apimachinery v0.27.3
k8s.io/client-go v0.27.1
Expand Down Expand Up @@ -108,7 +109,6 @@ require (
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gotest.tools/v3 v3.1.0 // indirect
k8s.io/apiextensions-apiserver v0.26.5 // indirect
k8s.io/gengo v0.0.0-20221011193443-fad74ee6edd9 // indirect
k8s.io/klog/v2 v2.100.1 // indirect
Expand Down
76 changes: 76 additions & 0 deletions pkg/actions/actions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package actions

import (
"context"
"fmt"
"os"
"sync"

"github.com/openshift-pipelines/manual-approval-gate/pkg/cli"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/discovery"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/restmapper"
)

var (
doOnce sync.Once
apiGroupRes []*restmapper.APIGroupResources
)

// List fetches the resource and convert it to respective object
func ListV1(gr schema.GroupVersionResource, c *cli.Clients, opts metav1.ListOptions, ns string, obj interface{}) error {
unstructuredObj, err := list(gr, c.Dynamic, c.ApprovalTask.Discovery(), ns, opts)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to list objects from %s namespace \n", ns)
return err
}

return runtime.DefaultUnstructuredConverter.FromUnstructured(unstructuredObj.UnstructuredContent(), obj)
}

// list takes a partial resource and fetches a list of that resource's objects in the cluster using the dynamic client.
func list(gr schema.GroupVersionResource, dynamic dynamic.Interface, discovery discovery.DiscoveryInterface, ns string, op metav1.ListOptions) (*unstructured.UnstructuredList, error) {
gvr, err := GetGroupVersionResource(gr, discovery)
if err != nil {
return nil, err
}

allRes, err := dynamic.Resource(*gvr).Namespace(ns).List(context.Background(), op)
if err != nil {
return nil, err
}

return allRes, nil
}

func GetGroupVersionResource(gr schema.GroupVersionResource, discovery discovery.DiscoveryInterface) (*schema.GroupVersionResource, error) {
var err error
doOnce.Do(func() {
err = InitializeAPIGroupRes(discovery)
})
if err != nil {
return nil, err
}

rm := restmapper.NewDiscoveryRESTMapper(apiGroupRes)
gvr, err := rm.ResourceFor(gr)
if err != nil {
return nil, err
}

return &gvr, nil
}

// InitializeAPIGroupRes initializes and populates the discovery client.
func InitializeAPIGroupRes(discovery discovery.DiscoveryInterface) error {
var err error
apiGroupRes, err = restmapper.GetAPIGroupResources(discovery)
if err != nil {
return err
}
return nil
}
56 changes: 38 additions & 18 deletions pkg/cli/cmd/list/list.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,43 @@
package list

import (
"context"
"fmt"
"log"
"os"
"text/tabwriter"
"text/template"

"github.com/fatih/color"
"github.com/openshift-pipelines/manual-approval-gate/pkg/actions"
"github.com/openshift-pipelines/manual-approval-gate/pkg/apis/approvaltask/v1alpha1"
cli "github.com/openshift-pipelines/manual-approval-gate/pkg/cli"
"github.com/openshift-pipelines/manual-approval-gate/pkg/cli/flags"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
)

type ListOptions struct {
AllNamespaces bool
}

var (
taskGroupResource = schema.GroupVersionResource{Group: "openshift-pipelines.org", Resource: "approvaltasks"}
)

var ConditionColor = map[string]color.Attribute{
"Rejected": color.FgHiRed,
"Approved": color.FgHiGreen,
"Pending": color.FgHiBlue,
}

const listTemplate = `NAME NumberOfApprovalsRequired PendingApprovals Rejected STATUS{{range .ApprovalTasks.Items}}
{{.Name}} {{.Spec.NumberOfApprovalsRequired}} {{pendingApprovals .}} {{rejected .}} {{state .}}{{end}}
const listTemplate = `{{- $at := len .ApprovalTasks.Items }}{{ if eq $at 0 -}}
No ApprovalTasks found
{{else -}}
NAME NumberOfApprovalsRequired PendingApprovals Rejected STATUS
{{range .ApprovalTasks.Items -}}
{{.Name}} {{.Spec.NumberOfApprovalsRequired}} {{pendingApprovals .}} {{rejected .}} {{state .}}
{{end}}
{{- end -}}
`

func pendingApprovals(at *v1alpha1.ApprovalTask) int {
Expand Down Expand Up @@ -61,33 +72,32 @@ func state(at *v1alpha1.ApprovalTask) string {
return ColorStatus(state)
}

func Root(p cli.Params) *cobra.Command {
func Command(p cli.Params) *cobra.Command {
opts := &ListOptions{}
funcMap := template.FuncMap{
"pendingApprovals": pendingApprovals,
"state": state,
"rejected": rejected,
}
c := &cobra.Command{
Use: "list",
Short: "List all approval tasks",
Long: `This command lists all the approval tasks.`,
Run: func(cmd *cobra.Command, args []string) {
Use: "list",
Short: "List all approval tasks",
Long: `This command lists all the approval tasks.`,
PersistentPreRunE: flags.PersistentPreRunE(p),
RunE: func(cmd *cobra.Command, args []string) error {
cs, err := p.Clients()
if err != nil {
fmt.Println("Error getting clients:", err)
return
return err
}

ns := p.Namespace()
if opts.AllNamespaces {
ns = ""
}

at, err := cs.ApprovalTask.ApprovalTasks(ns).List(context.TODO(), metav1.ListOptions{})
if err != nil {
fmt.Println("Failed to list the approval tasks:", err)
return
var at *v1alpha1.ApprovalTaskList
if err := actions.ListV1(taskGroupResource, cs, metav1.ListOptions{}, ns, &at); err != nil {
return fmt.Errorf("failed to list Tasks from namespace %s: %v", ns, err)
}

var data = struct {
Expand All @@ -96,16 +106,26 @@ func Root(p cli.Params) *cobra.Command {
ApprovalTasks: at,
}

t, err := template.New("List Approvaltask").Funcs(funcMap).Parse(listTemplate)
// w := tabwriter.NewWriter(cmd.OutOrStdout(), 8, 8, 8, ' ', 0)
// t, err := template.New("List Approvaltask").Funcs(funcMap).Parse(test1)

w := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 5, 3, ' ', tabwriter.TabIndent)
t := template.Must(template.New("List ApprovalTasks").Funcs(funcMap).Parse(listTemplate))

if err != nil {
return err
}

w := tabwriter.NewWriter(os.Stdout, 8, 8, 8, ' ', 0)
if err := t.Execute(w, data); err != nil {
log.Fatal(err)
return err
}
w.Flush()

return w.Flush()

},
}
flags.AddOptions(c)

c.Flags().BoolVarP(&opts.AllNamespaces, "all-namespaces", "A", opts.AllNamespaces, "list Tasks from all namespaces")

Expand Down
Loading

0 comments on commit a8ffc00

Please sign in to comment.