-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds labels to zeropods to reflect the container scaling status. ``` labels: status.zeropod.ctrox.dev/container1: RUNNING status.zeropod.ctrox.dev/container2: SCALED_DOWN ``` This is enabled by default but it can be disabled by not deploying the status-labels component.
- Loading branch information
Showing
13 changed files
with
202 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
apiVersion: kustomize.config.k8s.io/v1alpha1 | ||
kind: Component | ||
resources: | ||
- rbac.yaml |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
apiVersion: kustomize.config.k8s.io/v1alpha1 | ||
kind: Component | ||
patches: | ||
- patch: |- | ||
- op: add | ||
path: /spec/template/spec/containers/0/args/- | ||
value: -status-labels=true | ||
target: | ||
kind: DaemonSet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package manager | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
"path" | ||
|
||
v1 "github.com/ctrox/zeropod/api/shim/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
) | ||
|
||
const ( | ||
StatusLabelKeyPrefix = "status.zeropod.ctrox.dev" | ||
) | ||
|
||
type PodLabeller struct { | ||
log *slog.Logger | ||
} | ||
|
||
func NewPodLabeller() *PodLabeller { | ||
log := slog.With("component", "podupdater") | ||
log.Info("init") | ||
return &PodLabeller{log: log} | ||
} | ||
|
||
func (pl *PodLabeller) Handle(ctx context.Context, status *v1.ContainerStatus, pod *corev1.Pod) error { | ||
clog := pl.log.With("container", status.Name, "pod", status.PodName, | ||
"namespace", status.PodNamespace, "phase", status.Phase) | ||
clog.Info("status event") | ||
|
||
pl.setLabel(pod, status) | ||
return nil | ||
} | ||
|
||
func (pu *PodLabeller) setLabel(pod *corev1.Pod, status *v1.ContainerStatus) { | ||
if pod.Labels == nil { | ||
pod.Labels = map[string]string{} | ||
} | ||
pod.Labels[path.Join(StatusLabelKeyPrefix, status.Name)] = status.Phase.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package manager | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
"testing" | ||
|
||
v1 "github.com/ctrox/zeropod/api/shim/v1" | ||
"github.com/stretchr/testify/assert" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
) | ||
|
||
func TestPodLabeller(t *testing.T) { | ||
slog.SetLogLoggerLevel(slog.LevelDebug) | ||
|
||
scheme := runtime.NewScheme() | ||
if err := corev1.AddToScheme(scheme); err != nil { | ||
t.Fatal(err) | ||
} | ||
cases := map[string]struct { | ||
statusEventPhase v1.ContainerPhase | ||
beforeEvent map[string]string | ||
expected map[string]string | ||
}{ | ||
"no labels set": { | ||
statusEventPhase: v1.ContainerPhase_RUNNING, | ||
beforeEvent: nil, | ||
expected: map[string]string{ | ||
"status.zeropod.ctrox.dev/first-container": v1.ContainerPhase_RUNNING.String(), | ||
}, | ||
}, | ||
"existing labels are kept": { | ||
statusEventPhase: v1.ContainerPhase_RUNNING, | ||
beforeEvent: map[string]string{"existing": "label"}, | ||
expected: map[string]string{ | ||
"existing": "label", | ||
"status.zeropod.ctrox.dev/first-container": v1.ContainerPhase_RUNNING.String(), | ||
}, | ||
}, | ||
"status label is updated": { | ||
statusEventPhase: v1.ContainerPhase_SCALED_DOWN, | ||
beforeEvent: map[string]string{ | ||
"status.zeropod.ctrox.dev/first-container": v1.ContainerPhase_RUNNING.String(), | ||
}, | ||
expected: map[string]string{ | ||
"status.zeropod.ctrox.dev/first-container": v1.ContainerPhase_SCALED_DOWN.String(), | ||
}, | ||
}, | ||
} | ||
|
||
for name, tc := range cases { | ||
tc := tc | ||
t.Run(name, func(t *testing.T) { | ||
pod := newPod(nil) | ||
pod.SetLabels(tc.beforeEvent) | ||
|
||
if err := NewPodLabeller().Handle( | ||
context.Background(), | ||
&v1.ContainerStatus{ | ||
Name: pod.Spec.Containers[0].Name, | ||
PodName: pod.Name, | ||
PodNamespace: pod.Namespace, | ||
Phase: tc.statusEventPhase, | ||
}, | ||
pod, | ||
); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
assert.Equal(t, pod.GetLabels(), tc.expected) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters