Skip to content

Commit

Permalink
Set CF_INSTANCE_INDEX env variable with downward API
Browse files Browse the repository at this point in the history
  • Loading branch information
pbusko authored and danail-branekov committed Sep 19, 2024
1 parent 6412fb3 commit 854ea80
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 381 deletions.
35 changes: 5 additions & 30 deletions api/actions/process_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (

const (
ApplicationContainerName = "application"
EnvCFInstanceIndex = "CF_INSTANCE_INDEX"
LabelGUID = "korifi.cloudfoundry.org/guid"
stateStarting = "STARTING"
stateRunning = "RUNNING"
Expand Down Expand Up @@ -155,47 +154,23 @@ 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[korifiv1alpha1.PodIndexLabelKey]
if !exists {
return 0, fmt.Errorf("%s label not found", korifiv1alpha1.PodIndexLabelKey)
}

index, err := strconv.Atoi(indexString)
if err != nil {
return 0, fmt.Errorf("%s is not a valid index: %w", EnvCFInstanceIndex, err)
return 0, fmt.Errorf("%s is not a valid index: %w", korifiv1alpha1.PodIndexLabelKey, err)
}

if index < 0 {
return 0, fmt.Errorf("%s is not a valid index: instance indexes can't be negative", EnvCFInstanceIndex)
return 0, fmt.Errorf("%s is not a valid index: instance indexes can't be negative", korifiv1alpha1.PodIndexLabelKey)
}

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
39 changes: 9 additions & 30 deletions api/actions/process_stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,45 +206,29 @@ 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, korifiv1alpha1.PodIndexLabelKey)
})

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[korifiv1alpha1.PodIndexLabelKey] = "one"
})

It("returns an error", func() {
Expect(responseErr).To(MatchError(ContainSubstring(`parsing "one"`)))
})
})

When("the CF_INSTANCE_INDEX env var value is a negative integer", func() {
When("the pod-index label 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[korifiv1alpha1.PodIndexLabelKey] = "-1"
})

It("returns an error", func() {
Expand Down Expand Up @@ -316,20 +300,15 @@ func createPod(index, version string) corev1.Pod {
},
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
LabelVersionKey: version,
LabelVersionKey: version,
korifiv1alpha1.PodIndexLabelKey: index,
},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "application",
Image: "some-image",
Env: []corev1.EnvVar{
{
Name: "CF_INSTANCE_INDEX",
Value: index,
},
},
},
},
},
Expand Down
2 changes: 2 additions & 0 deletions controllers/api/v1alpha1/shared_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const (
CFRouteGUIDLabelKey = "korifi.cloudfoundry.org/route-guid"
CFTaskGUIDLabelKey = "korifi.cloudfoundry.org/task-guid"

PodIndexLabelKey = "apps.kubernetes.io/pod-index"

StagingConditionType = "Staging"
SucceededConditionType = "Succeeded"

Expand Down
8 changes: 0 additions & 8 deletions controllers/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ import (
jobtaskrunnercontrollers "code.cloudfoundry.org/korifi/job-task-runner/controllers"
"code.cloudfoundry.org/korifi/kpack-image-builder/controllers"
kpackimagebuilderfinalizer "code.cloudfoundry.org/korifi/kpack-image-builder/controllers/webhooks/finalizer"
statesetfulrunnerv1 "code.cloudfoundry.org/korifi/statefulset-runner/api/v1"
statefulsetcontrollers "code.cloudfoundry.org/korifi/statefulset-runner/controllers"
"code.cloudfoundry.org/korifi/tools"
"code.cloudfoundry.org/korifi/tools/image"
Expand Down Expand Up @@ -556,13 +555,6 @@ func main() {
os.Exit(1)
}

if controllerConfig.IncludeStatefulsetRunner {
if err = statesetfulrunnerv1.NewSTSPodDefaulter().SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "Pod")
os.Exit(1)
}
}

if controllerConfig.IncludeKpackImageBuilder {
kpackimagebuilderfinalizer.NewKpackImageBuilderFinalizerWebhook().SetupWebhookWithManager(mgr)
}
Expand Down
30 changes: 0 additions & 30 deletions helm/korifi/statefulset-runner/manifests.yaml

This file was deleted.

12 changes: 1 addition & 11 deletions statefulset-runner/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,12 @@ help: ## Display this help.
export GOBIN = $(shell pwd)/bin
export PATH := $(shell pwd)/bin:$(PATH)

webhooks-file = ../helm/korifi/statefulset-runner/manifests.yaml
.PHONY: manifests
manifests: bin/controller-gen bin/yq
controller-gen \
paths="./..." \
rbac:roleName=korifi-statefulset-runner-appworkload-manager-role \
webhook \
output:rbac:artifacts:config=../helm/korifi/statefulset-runner \
output:webhook:artifacts:config=../helm/korifi/statefulset-runner

yq -i 'with(.metadata; .annotations["cert-manager.io/inject-ca-from"]="{{ .Release.Namespace }}/korifi-controllers-serving-cert")' $(webhooks-file)
yq -i 'with(.metadata; .name="korifi-statefulset-runner-" + .name)' $(webhooks-file)
yq -i 'with(.webhooks[]; .clientConfig.service.namespace="{{ .Release.Namespace }}")' $(webhooks-file)
yq -i 'with(.webhooks[]; .clientConfig.service.name="korifi-controllers-" + .clientConfig.service.name)' $(webhooks-file)
yq -i 'with(.webhooks[]; .objectSelector.matchLabels["korifi.cloudfoundry.org/add-stsr-index"]="true")' $(webhooks-file)

output:rbac:artifacts:config=../helm/korifi/statefulset-runner

.PHONY: generate
generate: bin/controller-gen
Expand Down
94 changes: 0 additions & 94 deletions statefulset-runner/api/v1/pod_webhook.go

This file was deleted.

75 changes: 0 additions & 75 deletions statefulset-runner/api/v1/pod_webhook_test.go

This file was deleted.

Loading

0 comments on commit 854ea80

Please sign in to comment.