Skip to content

Commit

Permalink
Simplify Pod struct
Browse files Browse the repository at this point in the history
Signed-off-by: Jussi Maki <[email protected]>
  • Loading branch information
joamaki committed Mar 18, 2024
1 parent 504d61e commit b6b894b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
4 changes: 2 additions & 2 deletions demo/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ func registerPodHTTPHandler(mux *http.ServeMux, db *statedb.DB, pods statedb.Tab
txn := db.ReadTxn()
iter, _ := pods.Get(txn, PodPhaseIndex.Query(v1.PodRunning))
t := tabwriter.NewWriter(w, 10, 4, 2, ' ', 0)
fmt.Fprintf(t, "NAME\tSTARTED\tPOD IP\tSTATUS\n")
fmt.Fprintf(t, "NAME\tSTARTED\tSTATUS\n")
for pod, _, ok := iter.Next(); ok; pod, _, ok = iter.Next() {
fmt.Fprintf(t, "%s/%s\t%s ago\t%s\t%s\n", pod.Namespace, pod.Name, time.Since(pod.Status.StartTime.Time), pod.Status.PodIP, pod.ReconciliationStatus())
fmt.Fprintf(t, "%s/%s\t%s ago\t\t%s\n", pod.Namespace, pod.Name, time.Since(pod.StartTime), pod.ReconciliationStatus())
}
t.Flush()
})
Expand Down
2 changes: 1 addition & 1 deletion demo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func podReflectorConfig(client *kubernetes.Clientset, pods statedb.RWTable[*Pod]
Transform: func(obj any) (*Pod, bool) {
pod, ok := obj.(*v1.Pod)
if ok {
return &Pod{Pod: pod, reconciliationStatus: reconciler.StatusPending()}, true
return fromV1Pod(pod), true
}
return nil, false
},
Expand Down
2 changes: 1 addition & 1 deletion demo/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (o *podOps) Prune(context.Context, statedb.ReadTxn, statedb.Iterator[*Pod])

// Update implements reconciler.Operations.
func (o *podOps) Update(ctx context.Context, txn statedb.ReadTxn, pod *Pod, changed *bool) error {
o.log.Info("Pod updated", "name", pod.Namespace+"/"+pod.Name, "phase", pod.Status.Phase)
o.log.Info("Pod updated", "name", pod.Namespace+"/"+pod.Name, "phase", pod.Phase)
return nil
}

Expand Down
22 changes: 19 additions & 3 deletions demo/tables.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"time"

v1 "k8s.io/api/core/v1"

"github.com/cilium/statedb"
Expand All @@ -9,17 +11,31 @@ import (
)

type Pod struct {
*v1.Pod
Name, Namespace string
Phase v1.PodPhase
StartTime time.Time

reconciliationStatus reconciler.Status
}

func fromV1Pod(p *v1.Pod) *Pod {
return &Pod{
Name: p.Name,
Namespace: p.Namespace,
Phase: p.Status.Phase,
StartTime: p.Status.StartTime.Time,
reconciliationStatus: reconciler.StatusPending(),
}
}

func (p *Pod) ReconciliationStatus() reconciler.Status {
return p.reconciliationStatus
}

func (p *Pod) WithReconciliationStatus(s reconciler.Status) *Pod {
return &Pod{Pod: p.Pod, reconciliationStatus: s}
p2 := *p
p2.reconciliationStatus = s
return &p2
}

const PodTableName = "pods"
Expand All @@ -36,7 +52,7 @@ var (
PodPhaseIndex = statedb.Index[*Pod, v1.PodPhase]{
Name: "phase",
FromObject: func(pod *Pod) index.KeySet {
return index.NewKeySet(index.String(string(pod.Status.Phase)))
return index.NewKeySet(index.String(string(pod.Phase)))
},
FromKey: func(key v1.PodPhase) index.Key {
return index.String(string(key))
Expand Down

0 comments on commit b6b894b

Please sign in to comment.