Skip to content

Commit

Permalink
feat(pkg/meta): meta package for selector keys
Browse files Browse the repository at this point in the history
Signed-off-by: Lorenzo Fontana <[email protected]>
  • Loading branch information
fntlnz committed Nov 22, 2018
1 parent 1020f95 commit c196818
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cmd/kubectl-trace/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func run(cmd *cobra.Command, args []string) {
a.WithLogger(log)
a.WithContext(ctx)

a.AttachJob(job.Name, job.Namespace)
a.AttachJob(tj.ID, job.Namespace)

<-ctx.Done()
}
6 changes: 3 additions & 3 deletions pkg/attacher/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"time"

"github.com/fntlnz/kubectl-trace/pkg/meta"
"go.uber.org/zap"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes/scheme"
Expand Down Expand Up @@ -53,14 +54,13 @@ func (a *Attacher) WithContext(c context.Context) {
a.ctx = c
}

func (a *Attacher) AttachJob(jobName, namespace string) {
a.Attach(fmt.Sprintf("job-name=%s", jobName), namespace)
func (a *Attacher) AttachJob(traceJobID string, namespace string) {
a.Attach(fmt.Sprintf("%s=%s", meta.TraceIDLabelKey, traceJobID), namespace)
}

func (a *Attacher) Attach(selector, namespace string) {
go wait.PollImmediate(time.Second, 10*time.Second, func() (bool, error) {
pl, err := a.CoreV1Client.Pods(namespace).List(metav1.ListOptions{
//LabelSelector: "job-name=test-renzo",
LabelSelector: selector,
})

Expand Down
6 changes: 6 additions & 0 deletions pkg/meta/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package meta

const (
TraceIDLabelKey = "fntlnz.wtf/kubectl-trace-id"
TraceLabelKey = "fntlnz.wtf/kubectl-trace"
)
11 changes: 6 additions & 5 deletions pkg/tracejob/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tracejob
import (
"fmt"

"github.com/fntlnz/kubectl-trace/pkg/meta"
batchv1 "k8s.io/api/batch/v1"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -25,7 +26,7 @@ type TraceJob struct {

func (t *TraceJobClient) DeleteJob(nj TraceJob) error {
selectorOptions := metav1.ListOptions{
LabelSelector: fmt.Sprintf("fntlnz.wtf/kubectl-trace-id=%s", nj.ID),
LabelSelector: fmt.Sprintf("%s=%s", meta.TraceIDLabelKey, nj.ID),
}
jl, err := t.JobClient.List(selectorOptions)

Expand Down Expand Up @@ -71,12 +72,12 @@ func (t *TraceJobClient) CreateJob(nj TraceJob) (*batchv1.Job, error) {
Name: nj.Name,
Namespace: nj.Namespace,
Labels: map[string]string{
"fntlnz.wtf/kubectl-trace": nj.Name,
"fntlnz.wtf/kubectl-trace-id": nj.ID,
meta.TraceLabelKey: nj.Name,
meta.TraceIDLabelKey: nj.ID,
},
Annotations: map[string]string{
"fntlnz.wtf/kubectl-trace": nj.Name,
"fntlnz.wtf/kubectl-trace-id": nj.ID,
meta.TraceLabelKey: nj.Name,
meta.TraceIDLabelKey: nj.ID,
},
}

Expand Down

0 comments on commit c196818

Please sign in to comment.