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

Kubernetes Config Update: Prioritize InClusterConfig function #3584

Merged
merged 19 commits into from
Jan 26, 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
6 changes: 3 additions & 3 deletions cmd/controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"agones.dev/agones/pkg/gameservers"
"agones.dev/agones/pkg/gameserversets"
"agones.dev/agones/pkg/metrics"
k8sconfig "agones.dev/agones/pkg/util/k8sconfig"
"agones.dev/agones/pkg/util/runtime"
"agones.dev/agones/pkg/util/signals"
"github.com/google/uuid"
Expand All @@ -49,7 +50,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/leaderelection"
"k8s.io/client-go/tools/leaderelection/resourcelock"
)
Expand Down Expand Up @@ -133,8 +133,8 @@ func main() {
logger.Fatal("Could not create controller from environment or flags")
}

// if the kubeconfig fails BuildConfigFromFlags will try in cluster config
clientConf, err := clientcmd.BuildConfigFromFlags("", ctlConf.KubeConfig)
// if the kubeconfig fails InClusterBuildConfig will try in cluster config
clientConf, err := k8sconfig.InClusterBuildConfig("", ctlConf.KubeConfig)
if err != nil {
logger.WithError(err).Fatal("Could not create in cluster config")
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/extensions/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"agones.dev/agones/pkg/metrics"
"agones.dev/agones/pkg/util/apiserver"
"agones.dev/agones/pkg/util/https"
"agones.dev/agones/pkg/util/k8sconfig"
"agones.dev/agones/pkg/util/runtime"
"agones.dev/agones/pkg/util/signals"
"agones.dev/agones/pkg/util/webhooks"
Expand All @@ -48,7 +49,6 @@ import (
"gopkg.in/natefinch/lumberjack.v2"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)

const (
Expand Down Expand Up @@ -114,8 +114,8 @@ func main() {
logger.WithField("version", pkg.Version).WithField("featureGates", runtime.EncodeFeatures()).
WithField("ctlConf", ctlConf).Info("starting extensions operator...")

// if the kubeconfig fails BuildConfigFromFlags will try in cluster config
clientConf, err := clientcmd.BuildConfigFromFlags("", ctlConf.KubeConfig)
// if the kubeconfig fails InClusterBuildConfig will try in cluster config
clientConf, err := k8sconfig.InClusterBuildConfig("", ctlConf.KubeConfig)
if err != nil {
logger.WithError(err).Fatal("Could not create in cluster config")
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/sdk-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
sdkalpha "agones.dev/agones/pkg/sdk/alpha"
sdkbeta "agones.dev/agones/pkg/sdk/beta"
"agones.dev/agones/pkg/sdkserver"
k8sconfig "agones.dev/agones/pkg/util/k8sconfig"
"agones.dev/agones/pkg/util/runtime"
"agones.dev/agones/pkg/util/signals"
gwruntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
Expand All @@ -42,7 +43,6 @@ import (
"google.golang.org/grpc/credentials/insecure"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)

const (
Expand Down Expand Up @@ -119,8 +119,8 @@ func main() {
}
default:
var config *rest.Config
// if the kubeconfig fails BuildConfigFromFlags will try in cluster config
config, err := clientcmd.BuildConfigFromFlags("", ctlConf.KubeConfig)
// if the kubeconfig fails InClusterBuildConfig will try in cluster config
config, err := k8sconfig.InClusterBuildConfig("", ctlConf.KubeConfig)
if err != nil {
logger.WithError(err).Fatal("Could not create in cluster config")
}
Expand Down
33 changes: 33 additions & 0 deletions pkg/util/k8sconfig/k8s_client_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2024 Google LLC All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package k8sconfig helps to build kubernetes configurations.
package k8sconfig

import (
restclient "k8s.io/client-go/rest"
clientcmd "k8s.io/client-go/tools/clientcmd"
"k8s.io/klog"
)

// InClusterBuildConfig is a helper function that builds configs by trying the InClusterConfig().
// If InClusterConfig is unsuccessful, it falls back to BuildConfigFromFlags.
func InClusterBuildConfig(masterURL, kubeconfigPath string) (*restclient.Config, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func InClusterBuildConfig(masterURL, kubeconfigPath string) (*restclient.Config, error) {
func InClusterBuildConfig(kubeconfigPath string) (*restclient.Config, error) {

Since we never use a masterURL other than "" ?

kubeconfig, err := restclient.InClusterConfig()
if err == nil {
return kubeconfig, nil
}
klog.Warning("error creating inClusterConfig, trying BuildConfigFromFlags()", err)
return clientcmd.BuildConfigFromFlags(masterURL, kubeconfigPath)
}
4 changes: 2 additions & 2 deletions test/e2e/framework/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
allocationv1 "agones.dev/agones/pkg/apis/allocation/v1"
autoscaling "agones.dev/agones/pkg/apis/autoscaling/v1"
"agones.dev/agones/pkg/client/clientset/versioned"
k8sconfig "agones.dev/agones/pkg/util/k8sconfig"
"agones.dev/agones/pkg/util/runtime"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
Expand All @@ -53,7 +54,6 @@ import (

// required to use gcloud login see: https://github.com/kubernetes/client-go/issues/242
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"k8s.io/client-go/tools/clientcmd"
)

// special labels that can be put on pods to trigger automatic cleanup.
Expand Down Expand Up @@ -81,7 +81,7 @@ type Framework struct {
}

func newFramework(kubeconfig string, qps float32, burst int) (*Framework, error) {
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
config, err := k8sconfig.InClusterBuildConfig("", kubeconfig)
if err != nil {
return nil, errors.Wrap(err, "build config from flags failed")
}
Expand Down
4 changes: 2 additions & 2 deletions test/eviction/evictpod.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import (
"log"
"path/filepath"

k8sconfig "agones.dev/agones/pkg/util/k8sconfig"
policy "k8s.io/api/policy/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
)

Expand All @@ -50,7 +50,7 @@ func main() {
log.Fatal("--pod must be non-empty")
}

config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
config, err := k8sconfig.InClusterBuildConfig("", *kubeconfig)
if err != nil {
log.Fatalf("Could not build config: %v", err)
}
Expand Down
15 changes: 5 additions & 10 deletions vendor/k8s.io/client-go/tools/clientcmd/client_config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.