-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Try to reload config when disconnected from the cluster (#6130)
* Try to reload config when disconnected from the cluster * Fix integration test * Message also when isForbidden
- Loading branch information
Showing
4 changed files
with
107 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package kclient | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
|
||
"k8s.io/client-go/tools/clientcmd" | ||
) | ||
|
||
// Refresh re-creates a new Kubernetes client and checks if the Config changes | ||
// If config changed, updates the Kubernetes client with the new configuration and returns true | ||
// If the namespace or cluster of the current context has changed since the last time | ||
// the config has been loaded, the function will not update the configuration | ||
func (c *Client) Refresh() (bool, error) { | ||
newClient, err := New() | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
oldCluster, oldNs, err := getContext(c) | ||
if err != nil { | ||
return false, err | ||
} | ||
newCluster, newNs, err := getContext(newClient) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
if oldCluster != newCluster { | ||
return false, fmt.Errorf("cluster changed (%q -> %q), won't refresh the configuration", oldCluster, newCluster) | ||
} | ||
if oldNs != newNs { | ||
return false, fmt.Errorf("namespace changed (%q -> %q), won't refresh the configuration", oldNs, newNs) | ||
} | ||
|
||
updated, err := isConfigUpdated(c.GetConfig(), newClient.GetConfig()) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
if updated { | ||
*c = *newClient | ||
} | ||
return updated, nil | ||
} | ||
|
||
func getContext(c *Client) (cluster string, namespace string, err error) { | ||
raw, err := c.GetConfig().RawConfig() | ||
if err != nil { | ||
return "", "", err | ||
} | ||
|
||
currentCtx := raw.Contexts[raw.CurrentContext] | ||
return currentCtx.Cluster, currentCtx.Namespace, nil | ||
} | ||
|
||
func isConfigUpdated(oldC, newC clientcmd.ClientConfig) (bool, error) { | ||
oldRaw, err := oldC.RawConfig() | ||
if err != nil { | ||
return false, err | ||
} | ||
newRaw, err := newC.RawConfig() | ||
if err != nil { | ||
return false, err | ||
} | ||
return !reflect.DeepEqual(oldRaw, newRaw), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters