Skip to content

Commit

Permalink
update process stats action
Browse files Browse the repository at this point in the history
  • Loading branch information
pbusko committed Sep 18, 2024
1 parent 67a76aa commit dde7c8b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 56 deletions.
31 changes: 4 additions & 27 deletions api/actions/process_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const (
ApplicationContainerName = "application"
EnvCFInstanceIndex = "CF_INSTANCE_INDEX"
LabelGUID = "korifi.cloudfoundry.org/guid"
LabelPodIndex = "apps.kubernetes.io/pod-index"
stateStarting = "STARTING"
stateRunning = "RUNNING"
stateDown = "DOWN"
Expand Down Expand Up @@ -155,14 +156,9 @@ func (a *ProcessStats) FetchStats(ctx context.Context, authInfo authorization.In
}

func extractIndex(pod corev1.Pod) (int, error) {
container, err := extractProcessContainer(pod.Spec.Containers)
if err != nil {
return 0, err
}

indexString, err := extractEnvVarFromContainer(*container, EnvCFInstanceIndex)
if err != nil {
return 0, err
indexString, exists := pod.ObjectMeta.Labels[LabelPodIndex]
if !exists {
return 0, fmt.Errorf("%s label not found", LabelPodIndex)
}

index, err := strconv.Atoi(indexString)
Expand All @@ -177,25 +173,6 @@ func extractIndex(pod corev1.Pod) (int, error) {
return index, nil
}

func extractProcessContainer(containers []corev1.Container) (*corev1.Container, error) {
for i, c := range containers {
if c.Name == ApplicationContainerName {
return &containers[i], nil
}
}
return nil, fmt.Errorf("container %q not found", ApplicationContainerName)
}

func extractEnvVarFromContainer(container corev1.Container, envVar string) (string, error) {
envs := container.Env
for _, e := range envs {
if e.Name == envVar {
return e.Value, nil
}
}
return "", fmt.Errorf("%s not set", envVar)
}

// Logic from Kubernetes in Action 2nd Edition - Ch 6.
// DOWN => !pod || !pod.conditions.PodScheduled
// CRASHED => any(pod.ContainerStatuses.State isA Terminated)
Expand Down
35 changes: 7 additions & 28 deletions api/actions/process_stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,32 +206,19 @@ var _ = Describe("ProcessStats", func() {
})
})

When("there are no stats for the application container", func() {
When("the pod-index label is not set", func() {
BeforeEach(func() {
podMetrics[0].Pod.Spec.Containers[0].Name = "i-contain-no-app"
delete(podMetrics[0].Pod.ObjectMeta.Labels, LabelPodIndex)
})

It("returns an error", func() {
Expect(responseErr).To(MatchError(`container "application" not found`))
Expect(responseErr).To(MatchError(ContainSubstring("label not found")))
})
})

When("the CF_INSTANCE_INDEX env var is not set", func() {
When("the pod-index label value cannot be parsed to an int", func() {
BeforeEach(func() {
podMetrics[0].Pod.Spec.Containers[0].Env = []corev1.EnvVar{}
})

It("returns an error", func() {
Expect(responseErr).To(MatchError(`CF_INSTANCE_INDEX not set`))
})
})

When("the CF_INSTANCE_INDEX env var value cannot be parsed to an int", func() {
BeforeEach(func() {
podMetrics[0].Pod.Spec.Containers[0].Env = []corev1.EnvVar{{
Name: "CF_INSTANCE_INDEX",
Value: "one",
}}
podMetrics[0].Pod.ObjectMeta.Labels[LabelPodIndex] = "one"
})

It("returns an error", func() {
Expand All @@ -241,10 +228,7 @@ var _ = Describe("ProcessStats", func() {

When("the CF_INSTANCE_INDEX env var value is a negative integer", func() {
BeforeEach(func() {
podMetrics[0].Pod.Spec.Containers[0].Env = []corev1.EnvVar{{
Name: "CF_INSTANCE_INDEX",
Value: "-1",
}}
podMetrics[0].Pod.ObjectMeta.Labels[LabelPodIndex] = "-1"
})

It("returns an error", func() {
Expand Down Expand Up @@ -317,19 +301,14 @@ func createPod(index, version string) corev1.Pod {
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
LabelVersionKey: version,
LabelPodIndex: index,
},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "application",
Image: "some-image",
Env: []corev1.EnvVar{
{
Name: "CF_INSTANCE_INDEX",
Value: index,
},
},
},
},
},
Expand Down
3 changes: 2 additions & 1 deletion statefulset-runner/controllers/appworkload_to_stset.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"sort"
"strings"

"code.cloudfoundry.org/korifi/api/actions"
korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1"
"code.cloudfoundry.org/korifi/tools"
"github.com/BooleanCat/go-functional/v2/it"
Expand Down Expand Up @@ -75,7 +76,7 @@ func (r *AppWorkloadToStatefulsetConverter) Convert(appWorkload *korifiv1alpha1.
Name: EnvCFInstanceIndex,
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "metadata.labels['apps.kubernetes.io/pod-index']",
FieldPath: fmt.Sprintf("metadata.labels['%s']", actions.LabelPodIndex),
},
},
},
Expand Down

0 comments on commit dde7c8b

Please sign in to comment.