Skip to content

add annotation to kubeconfig to identify contexts/clusters created by minikube #10126

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

Merged
merged 8 commits into from
Jan 15, 2021
Merged
Show file tree
Hide file tree
Changes from all 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: 2 additions & 1 deletion cmd/minikube/cmd/update-context.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ var updateContextCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
cname := ClusterFlagValue()
co := mustload.Running(cname)
// cluster extension metada for kubeconfig

updated, err := kubeconfig.UpdateEndpoint(cname, co.CP.Hostname, co.CP.Port, kubeconfig.PathFromEnv())
updated, err := kubeconfig.UpdateEndpoint(cname, co.CP.Hostname, co.CP.Port, kubeconfig.PathFromEnv(), kubeconfig.NewExtension())
if err != nil {
exit.Error(reason.HostKubeconfigUpdate, "update config", err)
}
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -988,8 +988,6 @@ github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/ulikunitz/xz v0.5.5 h1:pFrO0lVpTBXLpYw+pnLj6TbvHuyjXMfjGeCwSqCVwok=
github.com/ulikunitz/xz v0.5.5/go.mod h1:2bypXElzHzzJZwzH67Y6wb67pO62Rzfn7BSiF4ABRW8=
github.com/ulikunitz/xz v0.5.8 h1:ERv8V6GKqVi23rgu5cj9pVfVzJbOqAY2Ntl88O6c2nQ=
github.com/ulikunitz/xz v0.5.8/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
github.com/ultraware/funlen v0.0.1/go.mod h1:Dp4UiAus7Wdb9KUZsYWZEWiRzGuM2kXM1lPbfaF6xhA=
Expand Down
2 changes: 2 additions & 0 deletions pkg/minikube/bootstrapper/certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ func SetupCerts(cmd command.Runner, k8s config.KubernetesConfig, n config.Node)
ClientCertificate: path.Join(vmpath.GuestKubernetesCertsDir, "apiserver.crt"),
ClientKey: path.Join(vmpath.GuestKubernetesCertsDir, "apiserver.key"),
CertificateAuthority: path.Join(vmpath.GuestKubernetesCertsDir, "ca.crt"),
ExtensionContext: kubeconfig.NewExtension(),
ExtensionCluster: kubeconfig.NewExtension(),
KeepContext: false,
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/minikube/bootstrapper/kubeadm/kubeadm.go
Original file line number Diff line number Diff line change
Expand Up @@ -576,14 +576,14 @@ func (k *Bootstrapper) restartControlPlane(cfg config.ClusterConfig) error {
klog.Infof("restartCluster took %s", time.Since(start))
}()

version, err := util.ParseKubernetesVersion(cfg.KubernetesConfig.KubernetesVersion)
k8sVersion, err := util.ParseKubernetesVersion(cfg.KubernetesConfig.KubernetesVersion)
if err != nil {
return errors.Wrap(err, "parsing Kubernetes version")
}

phase := "alpha"
controlPlane := "controlplane"
if version.GTE(semver.MustParse("1.13.0")) {
if k8sVersion.GTE(semver.MustParse("1.13.0")) {
phase = "init"
controlPlane = "control-plane"
}
Expand All @@ -603,7 +603,7 @@ func (k *Bootstrapper) restartControlPlane(cfg config.ClusterConfig) error {
}

// Save the costly tax of reinstalling Kubernetes if the only issue is a missing kube context
_, err = kubeconfig.UpdateEndpoint(cfg.Name, hostname, port, kubeconfig.PathFromEnv())
_, err = kubeconfig.UpdateEndpoint(cfg.Name, hostname, port, kubeconfig.PathFromEnv(), kubeconfig.NewExtension())
if err != nil {
klog.Warningf("unable to update kubeconfig (cluster will likely require a reset): %v", err)
}
Expand Down
66 changes: 66 additions & 0 deletions pkg/minikube/kubeconfig/extension.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
Copyright 2021 The Kubernetes Authors 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 kubeconfig

import (
"time"

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/minikube/pkg/version"
)

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// implementing the runtime.Object internally so we can write extensions to kubeconfig
type Extension struct {
runtime.TypeMeta `json:",inline"`
Version string `json:"version"`
Provider string `json:"provider"`
LastUpdate string `json:"last-update"`
}

// NewExtension returns a minikube formated kubeconfig's extension block to idenity clusters and contexts
func NewExtension() *Extension {
return &Extension{
Provider: "minikube.sigs.k8s.io",
Version: version.GetVersion(),
// time format matching other RFC in notify.go
LastUpdate: time.Now().Format(time.RFC1123)}
}

// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Extension.
func (in *Extension) DeepCopy() *Extension {
if in == nil {
return nil
}
out := new(Extension)
in.DeepCopyInto(out)
return out
}

// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object.
func (in *Extension) DeepCopyObject() runtime.Object {
if c := in.DeepCopy(); c != nil {
return c
}
return nil
}

// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Extension) DeepCopyInto(out *Extension) {
*out = *in
out.TypeMeta = in.TypeMeta
}
5 changes: 4 additions & 1 deletion pkg/minikube/kubeconfig/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func Endpoint(contextName string, configPath ...string) (string, int, error) {
}

// UpdateEndpoint overwrites the IP stored in kubeconfig with the provided IP.
func UpdateEndpoint(contextName string, hostname string, port int, confpath string) (bool, error) {
func UpdateEndpoint(contextName string, hostname string, port int, confpath string, ext *Extension) (bool, error) {
if hostname == "" {
return false, fmt.Errorf("empty ip")
}
Expand Down Expand Up @@ -136,6 +136,9 @@ func UpdateEndpoint(contextName string, hostname string, port int, confpath stri
CertificateAuthority: path.Join(gp, "ca.crt"),
KeepContext: false,
}
if ext != nil {
kcs.ExtensionCluster = ext
}
err = PopulateFromSettings(kcs, cfg)
if err != nil {
return false, errors.Wrap(err, "populating kubeconfig")
Expand Down
4 changes: 2 additions & 2 deletions pkg/minikube/kubeconfig/kubeconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ func TestUpdateIP(t *testing.T) {
t.Parallel()
configFilename := tempFile(t, test.existing)
defer os.Remove(configFilename)
statusActual, err := UpdateEndpoint("minikube", test.hostname, test.port, configFilename)
statusActual, err := UpdateEndpoint("minikube", test.hostname, test.port, configFilename, nil)
if err != nil && !test.err {
t.Errorf("Got unexpected error: %v", err)
}
Expand All @@ -430,7 +430,7 @@ func TestUpdateIP(t *testing.T) {
t.Fatal(err)
}
if !configEquals(actual, expected) {
t.Fatal("configs did not match")
t.Fatalf("configs did not match: Actual:\n%+v\n Expected:\n%+v", actual, expected)
}
})

Expand Down
18 changes: 18 additions & 0 deletions pkg/minikube/kubeconfig/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/juju/mutex"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/clientcmd/api"
"k8s.io/klog/v2"
"k8s.io/minikube/pkg/util/lock"
Expand Down Expand Up @@ -54,6 +55,12 @@ type Settings struct {
// Should the certificate files be embedded instead of referenced by path
EmbedCerts bool

// Extension meta data for the cluster
ExtensionCluster *Extension

// Extension meta data for the cluster
ExtensionContext *Extension

// kubeConfigFile is the path where the kube config is stored
// Only access this with atomic ops
kubeConfigFile atomic.Value
Expand Down Expand Up @@ -83,6 +90,10 @@ func PopulateFromSettings(cfg *Settings, apiCfg *api.Config) error {
} else {
cluster.CertificateAuthority = cfg.CertificateAuthority
}

if cfg.ExtensionCluster != nil {
cluster.Extensions = map[string]runtime.Object{"cluster_info": cfg.ExtensionCluster.DeepCopy()}
}
apiCfg.Clusters[clusterName] = cluster

// user
Expand All @@ -109,6 +120,10 @@ func PopulateFromSettings(cfg *Settings, apiCfg *api.Config) error {
context.Cluster = cfg.ClusterName
context.Namespace = cfg.Namespace
context.AuthInfo = userName
if cfg.ExtensionContext != nil {
context.Extensions = map[string]runtime.Object{"context_info": cfg.ExtensionContext.DeepCopy()}
}

apiCfg.Contexts[contextName] = context

// Only set current context to minikube if the user has not used the keepContext flag
Expand Down Expand Up @@ -138,6 +153,9 @@ func Update(kcs *Settings) error {
return err
}

ext := NewExtension()
kcs.ExtensionCluster = ext
kcs.ExtensionContext = ext
err = PopulateFromSettings(kcs, kcfg)
if err != nil {
return err
Expand Down