Skip to content

Commit

Permalink
Support argo-rollouts (#4)
Browse files Browse the repository at this point in the history
* chore: add argo-rollout@1.2.1

* chore: add argo-rollout modules

* feat: client supports argorollout

* feat: restart argo rollout resources

* chore: rollout kind
  • Loading branch information
anarcher authored May 25, 2022
1 parent a649713 commit 4a7fadf
Show file tree
Hide file tree
Showing 7 changed files with 1,007 additions and 21 deletions.
23 changes: 16 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.17

require (
github.com/CrowdSurge/banner v0.0.0-20140923200336-8c0e79dc5ff7
github.com/argoproj/argo-rollouts v1.2.1
github.com/aws/aws-sdk-go v1.38.21
github.com/fatih/color v1.10.0
github.com/peterbourgon/ff/v3 v3.0.0
Expand All @@ -15,25 +16,33 @@ require (
)

require (
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful v2.9.5+incompatible // indirect
github.com/go-logr/logr v1.2.0 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.19.5 // indirect
github.com/go-openapi/swag v0.19.14 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.5 // indirect
github.com/google/go-cmp v0.5.6 // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/googleapis/gnostic v0.5.5 // indirect
github.com/imdario/mergo v0.3.11 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/net v0.0.0-20211209124913-491a49abca63 // indirect
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f // indirect
golang.org/x/net v0.0.0-20220121210141-e204ce36a2ba // indirect
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150 // indirect
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
google.golang.org/appengine v1.6.7 // indirect
Expand Down
880 changes: 872 additions & 8 deletions go.sum

Large diffs are not rendered by default.

22 changes: 20 additions & 2 deletions pkg/cmd/show_nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type NodeInfo struct {
Node v1.Node
Deployments []v1.Pod
Statusfulsets []v1.Pod
ArgoRollouts []v1.Pod
}

type ShowNodesConfig struct {
Expand Down Expand Up @@ -93,8 +94,13 @@ func (c *ShowNodesConfig) Exec(ctx context.Context, args []string) error {
var (
deploys []v1.Pod
sts []v1.Pod
rls []v1.Pod
)
for _, p := range pods.Items {
if isArgoRolloutPod(&p) {
rls = append(rls, p)
continue
}
if isDeploymentPod(&p) {
deploys = append(deploys, p)
continue
Expand All @@ -108,17 +114,18 @@ func (c *ShowNodesConfig) Exec(ctx context.Context, args []string) error {
Node: n,
Deployments: deploys,
Statusfulsets: sts,
ArgoRollouts: rls,
}
nodeInfos = append(nodeInfos, ni)
}

tbl := table.New("Node", "Status", "Deployment", "Statusfulsets")
tbl := table.New("Node", "Status", "Deployment", "Statusfulsets", "ArgoRollouts")
headerFmt := color.New(color.FgGreen, color.Underline).SprintfFunc()
columnFmt := color.New(color.FgYellow).SprintfFunc()
tbl.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(columnFmt)

for _, n := range nodeInfos {
tbl.AddRow(n.Node.Name, nodeStatus(&n.Node), len(n.Deployments), len(n.Statusfulsets))
tbl.AddRow(n.Node.Name, nodeStatus(&n.Node), len(n.Deployments), len(n.Statusfulsets), len(n.ArgoRollouts))
}

tbl.Print()
Expand All @@ -140,6 +147,9 @@ func (c *ShowNodesConfig) Exec(ctx context.Context, args []string) error {
for _, p := range n.Statusfulsets {
tbl.AddRow("StatefulSet", fmt.Sprintf("%s/%s", p.Namespace, p.Name))
}
for _, p := range n.ArgoRollouts {
tbl.AddRow("ArgoRollouts", fmt.Sprintf("%s/%s", p.Namespace, p.Name))
}
tbl.Print()
fmt.Println("")
}
Expand Down Expand Up @@ -170,6 +180,14 @@ func isStatefulSetPod(p *v1.Pod) bool {
return false
}

func isArgoRolloutPod(p *v1.Pod) bool {
// todo(anarcher): use replicaset?
if _, ok := p.Labels["rollouts-pod-template-hash"]; ok {
return true
}
return false
}

func nodeStatus(n *v1.Node) string {
conds := []string{}
for _, c := range n.Status.Conditions {
Expand Down
16 changes: 12 additions & 4 deletions pkg/kubernetes/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ import (

"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"

roclientset "github.com/argoproj/argo-rollouts/pkg/client/clientset/versioned"
)

type Client struct {
clientset *kubernetes.Clientset
kubeConfig string
clientset *kubernetes.Clientset
rolloutsClientset roclientset.Interface
kubeConfig string
}

func NewClient(kubeConfig string) (*Client, error) {
Expand All @@ -27,10 +30,15 @@ func NewClient(kubeConfig string) (*Client, error) {
if err != nil {
return nil, fmt.Errorf("%w kubeconfig: %s", err, kubeConfig)
}
rolloutsClientset, err := roclientset.NewForConfig(config)
if err != nil {
return nil, fmt.Errorf("rolloutsClient: %w kubeconfig: %s", err, kubeConfig)
}

c := &Client{
clientset: clientset,
kubeConfig: kubeConfig,
clientset: clientset,
rolloutsClientset: rolloutsClientset,
kubeConfig: kubeConfig,
}

return c, nil
Expand Down
35 changes: 35 additions & 0 deletions pkg/kubernetes/rollout.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package kubernetes

import (
"context"
"encoding/json"
"fmt"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"

"github.com/argoproj/argo-rollouts/pkg/apis/rollouts/v1alpha1"
)

func (c *Client) Rollouts(ctx context.Context) (*v1alpha1.RolloutList, error) {
rls, err := c.rolloutsClientset.ArgoprojV1alpha1().Rollouts("").List(ctx, metav1.ListOptions{})
if err != nil {
return nil, fmt.Errorf("failed to load argo rollouts: %w", err)
}
return rls, nil
}

func (c *Client) PatchRollout(ctx context.Context, r *v1alpha1.Rollout, patch map[string]interface{}) error {
bs, err := json.Marshal(patch)
if err != nil {
return err
}

pt := types.MergePatchType
_, err = c.rolloutsClientset.ArgoprojV1alpha1().Rollouts(r.Namespace).Patch(ctx, r.Name, pt, bs, metav1.PatchOptions{})
if err != nil {
return err
}

return nil
}
46 changes: 46 additions & 0 deletions pkg/resource/argorollout.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package resource

import (
"context"
"time"

"github.com/anarcher/kroller/pkg/kubernetes"
"github.com/argoproj/argo-rollouts/pkg/apis/rollouts/v1alpha1"
)

type ArgoRollout struct {
client *kubernetes.Client
rollout *v1alpha1.Rollout
}

func addArgoRollouts(rl *RolloutList, list *v1alpha1.RolloutList, client *kubernetes.Client) {
for _, _i := range list.Items {
i := _i
rl.add(&ArgoRollout{
rollout: &i,
client: client,
})
}
}

func (r *ArgoRollout) Kind() string {
return "rollout"
}

func (r *ArgoRollout) Name() string {
return r.rollout.Name
}

func (r *ArgoRollout) Namespace() string {
return r.rollout.Namespace
}

func (r *ArgoRollout) Restart(ctx context.Context) error {
patch := map[string]interface{}{
"spec": map[string]interface{}{
"restartAt": time.Now().Format(time.RFC3339),
},
}

return r.client.PatchRollout(ctx, r.rollout, patch)
}
6 changes: 6 additions & 0 deletions pkg/resource/rollout.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,15 @@ func GetRolloutList(ctx context.Context, client *kubernetes.Client) (RolloutList
return nil, err
}

rls, err := client.Rollouts(ctx)
if err != nil {
return nil, err
}

var rl RolloutList
addDeploymentList(&rl, ds, client)
addStatefulSetList(&rl, sts, client)
addArgoRollouts(&rl, rls, client)

return rl, nil
}

0 comments on commit 4a7fadf

Please sign in to comment.