Skip to content

Commit

Permalink
fix: Fix job pod label selector on Kubernetes <v1.27
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Oct 7, 2023
1 parent ac92e06 commit 0bc5e90
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
7 changes: 7 additions & 0 deletions internal/kubernetes/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"path/filepath"

"github.com/spf13/cobra"
"k8s.io/client-go/discovery"
"k8s.io/client-go/kubernetes"
v1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/rest"
Expand All @@ -13,6 +14,7 @@ import (
type KubeClient struct {
ClientSet kubernetes.Interface
ClientConfig *rest.Config
Discovery *discovery.DiscoveryClient
Namespace string
}

Expand Down Expand Up @@ -64,6 +66,11 @@ func NewClient(kubeconfig, context, namespace string) (config KubeClient, err er
return config, err
}

config.Discovery, err = discovery.NewDiscoveryClientForConfig(config.ClientConfig)
if err != nil {
return config, err
}

return config, err
}

Expand Down
33 changes: 33 additions & 0 deletions internal/kubernetes/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package kubernetes

import (
"fmt"
"strconv"
"strings"
)

func (c *KubeClient) MinServerVersion(wantMajor, wantMinor int) (bool, error) {
serverVersion, err := c.Discovery.ServerVersion()
if err != nil {
return false, err
}

vers := strings.TrimPrefix(serverVersion.GitVersion, "v")
majorStr, minorStr, found := strings.Cut(vers, ".")
if !found {
return false, fmt.Errorf("invalid version: %s", serverVersion.GitVersion)
}
minorStr, _, _ = strings.Cut(minorStr, ".")

major, err := strconv.Atoi(majorStr)
if err != nil {
return false, err
}

minor, err := strconv.Atoi(minorStr)
if err != nil {
return false, err
}

return wantMajor <= major && wantMinor <= minor, nil
}
17 changes: 15 additions & 2 deletions internal/util/cmd_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ func watchJobPod(cmd *cobra.Command, conf *config.Global) error {
defer cancel()

watch, err := conf.Client.ClientSet.CoreV1().Pods(conf.Namespace).Watch(ctx, metav1.ListOptions{
LabelSelector: "batch.kubernetes.io/controller-uid=" + string(conf.Job.ObjectMeta.UID),
LabelSelector: jobPodLabelSelector(conf, conf.Job),
})
if err != nil {
return pollJobPod(ctx, conf)
Expand Down Expand Up @@ -324,7 +324,7 @@ func pollJobPod(ctx context.Context, conf *config.Global) error {
return wait.PollUntilContextCancel(
ctx, time.Second, true, func(ctx context.Context) (done bool, err error) {
list, err := conf.Client.ClientSet.CoreV1().Pods(conf.Namespace).List(ctx, metav1.ListOptions{
LabelSelector: "batch.kubernetes.io/controller-uid=" + string(conf.Job.ObjectMeta.UID),
LabelSelector: jobPodLabelSelector(conf, conf.Job),
})
if err != nil {
return false, err
Expand All @@ -349,3 +349,16 @@ func pollJobPod(ctx context.Context, conf *config.Global) error {
},
)
}

func jobPodLabelSelector(conf *config.Global, job *batchv1.Job) string {
useNewLabel, err := conf.Client.MinServerVersion(1, 27)
if err != nil {
log.WithError(err).Warn("failed to query server version; assuming v1.27+")
useNewLabel = true
}

if useNewLabel {
return "batch.kubernetes.io/controller-uid=" + string(job.ObjectMeta.UID)
}
return "controller-uid=" + string(job.ObjectMeta.UID)
}

0 comments on commit 0bc5e90

Please sign in to comment.