Skip to content

Commit

Permalink
Merge pull request #221 from qinqon/fallback-to-kube-system-namespace
Browse files Browse the repository at this point in the history
storage, k8s: Fallback to kube-system on empty ns
  • Loading branch information
maiqueb authored May 12, 2022
2 parents 8874106 + fe88268 commit 06d3340
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pkg/storage/kubernetes/ipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/k8snetworkplumbingwg/whereabouts/pkg/allocate"
Expand All @@ -34,7 +35,7 @@ func NewKubernetesIPAM(containerID string, ipamConf whereaboutstypes.IPAMConfig)
if cfg, err := clientcmd.LoadFromFile(ipamConf.Kubernetes.KubeConfigPath); err != nil {
return nil, err
} else if ctx, ok := cfg.Contexts[cfg.CurrentContext]; ok && ctx != nil {
namespace = ctx.Namespace
namespace = wbNamespaceFromCtx(ctx)
} else {
return nil, fmt.Errorf("k8s config: namespace not present in context")
}
Expand Down Expand Up @@ -543,3 +544,11 @@ RETRYLOOP:

return newip, err
}

func wbNamespaceFromCtx(ctx *clientcmdapi.Context) string {
namespace := ctx.Namespace
if namespace == "" {
return metav1.NamespaceSystem
}
return namespace
}

0 comments on commit 06d3340

Please sign in to comment.