Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup unused pod/pvc label logic #830

Merged
merged 5 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,10 @@ jobs:
GINKGO_NODES: "6"
run: |
hack/run-e2e-using-kind.sh
- name: cleanup kind
- name: cleanup kind and docker files
if: always()
run: |
curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.22.0/kind-linux-amd64
chmod +x ./kind
./kind delete cluster || true
make clean
docker image prune -f
7 changes: 5 additions & 2 deletions .github/workflows/preview.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,8 @@ jobs:
chmod +x ./kind
./kind delete cluster || true
make clean
docker image prune -f
docker buildx prune --all -f

echo cleaning up docker files as kind load docker-image seems to leave dangling files in the data directory that docker does not detect and so pruning with docker cli doesn't work
sudo systemctl stop docker
sudo rm -rf /var/lib/docker
sudo systemctl start docker
6 changes: 4 additions & 2 deletions api/v1alpha1/humiocluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,10 @@ type HumioPodStatusList []HumioPodStatus

// HumioPodStatus shows the status of individual humio pods
type HumioPodStatus struct {
PodName string `json:"podName,omitempty"`
PvcName string `json:"pvcName,omitempty"`
PodName string `json:"podName,omitempty"`
PvcName string `json:"pvcName,omitempty"`
// NodeId used to refer to the value of the BOOTSTRAP_HOST_ID environment variable for a Humio instance.
// Deprecated: No longer being used.
NodeId int `json:"nodeId,omitempty"`
NodeName string `json:"nodeName,omitempty"`
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15041,6 +15041,9 @@ spec:
pods
properties:
nodeId:
description: |-
NodeId used to refer to the value of the BOOTSTRAP_HOST_ID environment variable for a Humio instance.
Deprecated: No longer being used.
type: integer
nodeName:
type: string
Expand Down
3 changes: 3 additions & 0 deletions config/crd/bases/core.humio.com_humioclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15041,6 +15041,9 @@ spec:
pods
properties:
nodeId:
description: |-
NodeId used to refer to the value of the BOOTSTRAP_HOST_ID environment variable for a Humio instance.
Deprecated: No longer being used.
type: integer
nodeName:
type: string
Expand Down
83 changes: 0 additions & 83 deletions controllers/humiocluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,15 +322,6 @@ func (r *HumioClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request
}
}(ctx, r.HumioClient, hc)

if len(r.nodePoolsInMaintenance(hc, humioNodePools.Filter(NodePoolFilterHasNode))) == 0 {
for _, pool := range humioNodePools.Filter(NodePoolFilterHasNode) {
if err = r.ensureLabels(ctx, cluster.Config(), req, pool); err != nil {
return r.updateStatus(ctx, r.Client.Status(), hc, statusOptions().
withMessage(err.Error()))
}
}
}

for _, pool := range humioNodePools.Filter(NodePoolFilterHasNode) {
if podsReady, err := r.nodePoolPodsReady(ctx, hc, pool); !podsReady || err != nil {
msg := "waiting on all pods to be ready"
Expand Down Expand Up @@ -1292,80 +1283,6 @@ func (r *HumioClusterReconciler) serviceAccountExists(ctx context.Context, names
return true, nil
}

func (r *HumioClusterReconciler) ensureLabels(ctx context.Context, config *humioapi.Config, req reconcile.Request, hnp *HumioNodePool) error {
r.Log.Info("ensuring labels")
cluster, err := r.HumioClient.GetClusters(config, req)
if err != nil {
return r.logErrorAndReturn(err, "failed to get clusters")
}

foundPodList, err := kubernetes.ListPods(ctx, r, hnp.GetNamespace(), hnp.GetNodePoolLabels())
if err != nil {
return r.logErrorAndReturn(err, "failed to list pods")
}

pvcList, err := r.pvcList(ctx, hnp)
if err != nil {
return r.logErrorAndReturn(err, "failed to list pvcs to assign labels")
}

for idx, pod := range foundPodList {
// Skip pods that already have a label. Check that the pvc also has the label if applicable
if kubernetes.LabelListContainsLabel(pod.GetLabels(), kubernetes.NodeIdLabelName) {
if hnp.PVCsEnabled() {
if err := r.ensurePvcLabels(ctx, hnp, pod, pvcList); err != nil {
return r.logErrorAndReturn(err, "could not ensure pvc labels")
}
}
continue
}
// If pod does not have an IP yet, so it is probably pending
if pod.Status.PodIP == "" {
r.Log.Info(fmt.Sprintf("not setting labels for pod %s because it is in state %s", pod.Name, pod.Status.Phase))
continue
}
for _, node := range cluster.Nodes {
if node.Uri == fmt.Sprintf("http://%s:%d", pod.Status.PodIP, HumioPort) {
labels := hnp.GetNodePoolLabels()
labels[kubernetes.NodeIdLabelName] = strconv.Itoa(node.Id)
r.Log.Info(fmt.Sprintf("setting labels for pod %s, labels=%v", pod.Name, labels))
pod.SetLabels(labels)
if err := r.Update(ctx, &foundPodList[idx]); err != nil {
return r.logErrorAndReturn(err, fmt.Sprintf("failed to update labels on pod %s", pod.Name))
}
if hnp.PVCsEnabled() {
if err = r.ensurePvcLabels(ctx, hnp, pod, pvcList); err != nil {
return r.logErrorAndReturn(err, "could not ensure pvc labels")
}
}
}
}
}
return nil
}

func (r *HumioClusterReconciler) ensurePvcLabels(ctx context.Context, hnp *HumioNodePool, pod corev1.Pod, pvcList []corev1.PersistentVolumeClaim) error {
pvc, err := FindPvcForPod(pvcList, pod)
if err != nil {
return r.logErrorAndReturn(err, "failed to get pvc for pod to assign labels")
}
if kubernetes.LabelListContainsLabel(pvc.GetLabels(), kubernetes.NodeIdLabelName) {
return nil
}
nodeId, err := strconv.Atoi(pod.Labels[kubernetes.NodeIdLabelName])
if err != nil {
return r.logErrorAndReturn(err, fmt.Sprintf("unable to set label on pvc, nodeid %v is invalid", pod.Labels[kubernetes.NodeIdLabelName]))
}
labels := hnp.GetNodePoolLabels()
labels[kubernetes.NodeIdLabelName] = strconv.Itoa(nodeId)
r.Log.Info(fmt.Sprintf("setting labels for pvc %s, labels=%v", pvc.Name, labels))
pvc.SetLabels(labels)
if err := r.Update(ctx, &pvc); err != nil {
return r.logErrorAndReturn(err, fmt.Sprintf("failed to update labels on pvc %s", pod.Name))
}
return nil
}

func (r *HumioClusterReconciler) isPvcOrphaned(ctx context.Context, hnp *HumioNodePool, hc *humiov1alpha1.HumioCluster, pvc corev1.PersistentVolumeClaim) (bool, error) {
// first check the pods
podList, err := kubernetes.ListPods(ctx, r.Client, hnp.GetNamespace(), hnp.GetCommonClusterLabels())
Expand Down
8 changes: 0 additions & 8 deletions controllers/humiocluster_pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"fmt"
"reflect"
"sort"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -1110,13 +1109,6 @@ func (r *HumioClusterReconciler) getPodStatusList(ctx context.Context, hc *humio
PodName: pod.Name,
NodeName: nodeName,
}
if nodeIdStr, ok := pod.Labels[kubernetes.NodeIdLabelName]; ok {
nodeId, err := strconv.Atoi(nodeIdStr)
if err != nil {
return podStatusList, r.logErrorAndReturn(err, fmt.Sprintf("unable to set pod status, node id %s is invalid", nodeIdStr))
}
podStatus.NodeId = nodeId
}
if pool.PVCsEnabled() {
for _, volume := range pod.Spec.Volumes {
if volume.Name == "humio-data" {
Expand Down
1 change: 0 additions & 1 deletion pkg/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
)

const (
NodeIdLabelName = "humio.com/node-id"
NodePoolLabelName = "humio.com/node-pool"
)

Expand Down
11 changes: 0 additions & 11 deletions pkg/kubernetes/persistent_volume_claims.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (
"context"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"

"sigs.k8s.io/controller-runtime/pkg/client"
)

Expand All @@ -35,12 +33,3 @@ func ListPersistentVolumeClaims(ctx context.Context, c client.Client, humioClust

return foundPersistentVolumeClaimList.Items, nil
}

func GetPersistentVolumeClaim(ctx context.Context, c client.Client, humioClusterNamespace string, persistentVolumeClaimName string) (*corev1.PersistentVolumeClaim, error) {
var foundPersistentVolumeClaim corev1.PersistentVolumeClaim
err := c.Get(ctx, types.NamespacedName{
Name: persistentVolumeClaimName,
Namespace: humioClusterNamespace,
}, &foundPersistentVolumeClaim)
return &foundPersistentVolumeClaim, err
}
Loading