-
Notifications
You must be signed in to change notification settings - Fork 24
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
Export k3k cluster kubeconfig in k3kcli #36
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 | ||||
---|---|---|---|---|---|---|
|
@@ -3,21 +3,39 @@ package cluster | |||||
import ( | ||||||
"context" | ||||||
"errors" | ||||||
"fmt" | ||||||
"net/url" | ||||||
"os" | ||||||
"strings" | ||||||
"time" | ||||||
|
||||||
"github.com/galal-hussein/k3k/cli/cmds" | ||||||
"github.com/galal-hussein/k3k/pkg/apis/k3k.io/v1alpha1" | ||||||
"github.com/galal-hussein/k3k/pkg/controller/util" | ||||||
"github.com/sirupsen/logrus" | ||||||
"github.com/urfave/cli" | ||||||
v1 "k8s.io/api/core/v1" | ||||||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||||||
"k8s.io/apimachinery/pkg/runtime" | ||||||
"k8s.io/apimachinery/pkg/types" | ||||||
"k8s.io/apimachinery/pkg/util/wait" | ||||||
clientgoscheme "k8s.io/client-go/kubernetes/scheme" | ||||||
"k8s.io/client-go/rest" | ||||||
"k8s.io/client-go/tools/clientcmd" | ||||||
clientcmdapi "k8s.io/client-go/tools/clientcmd/api" | ||||||
"k8s.io/client-go/util/retry" | ||||||
"sigs.k8s.io/controller-runtime/pkg/client" | ||||||
) | ||||||
|
||||||
var ( | ||||||
Scheme = runtime.NewScheme() | ||||||
Scheme = runtime.NewScheme() | ||||||
backoff = wait.Backoff{ | ||||||
Steps: 5, | ||||||
Duration: 3 * time.Second, | ||||||
Factor: 2, | ||||||
Jitter: 0.1, | ||||||
} | ||||||
) | ||||||
|
||||||
func init() { | ||||||
|
@@ -101,10 +119,11 @@ func createCluster(clx *cli.Context) error { | |||||
ctrlClient, err := client.New(restConfig, client.Options{ | ||||||
Scheme: Scheme, | ||||||
}) | ||||||
|
||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
logrus.Infof("creating a new cluster [%s]", name) | ||||||
logrus.Infof("Creating a new cluster [%s]", name) | ||||||
cluster := newCluster( | ||||||
name, | ||||||
token, | ||||||
|
@@ -116,7 +135,46 @@ func createCluster(clx *cli.Context) error { | |||||
agentArgs, | ||||||
) | ||||||
|
||||||
return ctrlClient.Create(ctx, cluster) | ||||||
cluster.Spec.Expose = &v1alpha1.ExposeConfig{ | ||||||
NodePort: &v1alpha1.NodePortConfig{ | ||||||
Enabled: true, | ||||||
}, | ||||||
} | ||||||
|
||||||
// add Host IP address as an extra TLS-SAN to expose the k3k cluster | ||||||
url, err := url.Parse(restConfig.Host) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
host := strings.Split(url.Host, ":") | ||||||
cluster.Spec.TLSSANs = []string{ | ||||||
host[0], | ||||||
} | ||||||
|
||||||
if err := ctrlClient.Create(ctx, cluster); err != nil { | ||||||
if apierrors.IsAlreadyExists(err) { | ||||||
logrus.Infof("Cluster [%s] already exists", name) | ||||||
} else { | ||||||
return err | ||||||
} | ||||||
} | ||||||
|
||||||
logrus.Infof("Extracting Kubeconfig for [%s] cluster", name) | ||||||
var kubeconfig []byte | ||||||
err = retry.OnError(backoff, apierrors.IsNotFound, func() error { | ||||||
kubeconfig, err = extractKubeconfig(ctx, ctrlClient, cluster, host[0]) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
return nil | ||||||
}) | ||||||
|
||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
// export kubeconfig | ||||||
|
||||||
return os.WriteFile(cluster.Name+"-kubeconfig.yaml", kubeconfig, 0644) | ||||||
} | ||||||
|
||||||
func validateCreateFlags(clx *cli.Context) error { | ||||||
|
@@ -157,3 +215,79 @@ func newCluster(name, token string, servers, agents int32, clusterCIDR, serviceC | |||||
}, | ||||||
} | ||||||
} | ||||||
|
||||||
func extractKubeconfig(ctx context.Context, client client.Client, cluster *v1alpha1.Cluster, serverIP string) ([]byte, error) { | ||||||
nn := types.NamespacedName{ | ||||||
Name: cluster.Name + "-kubeconfig", | ||||||
Namespace: util.ClusterNamespace(cluster), | ||||||
} | ||||||
var kubeSecret v1.Secret | ||||||
if err := client.Get(ctx, nn, &kubeSecret); err != nil { | ||||||
return nil, err | ||||||
} | ||||||
|
||||||
kubeconfig := kubeSecret.Data["kubeconfig.yaml"] | ||||||
if kubeconfig == nil { | ||||||
return nil, errors.New("empty kubeconfig") | ||||||
} | ||||||
|
||||||
nn = types.NamespacedName{ | ||||||
Name: "k3k-server-service", | ||||||
Namespace: util.ClusterNamespace(cluster), | ||||||
} | ||||||
var k3kService v1.Service | ||||||
if err := client.Get(ctx, nn, &k3kService); err != nil { | ||||||
return nil, err | ||||||
} | ||||||
if k3kService.Spec.Type == v1.ServiceTypeNodePort { | ||||||
nodePort := k3kService.Spec.Ports[0].NodePort | ||||||
|
||||||
restConfig, err := clientcmd.RESTConfigFromKubeConfig(kubeconfig) | ||||||
if err != nil { | ||||||
return nil, err | ||||||
} | ||||||
hostURL := fmt.Sprintf("https://%s:%d", serverIP, nodePort) | ||||||
logrus.Infof(hostURL) | ||||||
restConfig.Host = hostURL | ||||||
|
||||||
clientConfig := generateKubeconfigFromRest(restConfig) | ||||||
|
||||||
b, err := clientcmd.Write(clientConfig) | ||||||
if err != nil { | ||||||
return nil, err | ||||||
} | ||||||
kubeconfig = b | ||||||
} | ||||||
return kubeconfig, nil | ||||||
} | ||||||
|
||||||
func generateKubeconfigFromRest(config *rest.Config) clientcmdapi.Config { | ||||||
clusters := make(map[string]*clientcmdapi.Cluster) | ||||||
clusters["default-cluster"] = &clientcmdapi.Cluster{ | ||||||
Server: config.Host, | ||||||
CertificateAuthorityData: config.CAData, | ||||||
} | ||||||
|
||||||
contexts := make(map[string]*clientcmdapi.Context) | ||||||
contexts["default-context"] = &clientcmdapi.Context{ | ||||||
Cluster: "default-cluster", | ||||||
Namespace: "default", | ||||||
AuthInfo: "default", | ||||||
} | ||||||
|
||||||
authinfos := make(map[string]*clientcmdapi.AuthInfo) | ||||||
authinfos["default"] = &clientcmdapi.AuthInfo{ | ||||||
ClientCertificateData: config.CertData, | ||||||
ClientKeyData: config.KeyData, | ||||||
} | ||||||
|
||||||
clientConfig := clientcmdapi.Config{ | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
Kind: "Config", | ||||||
APIVersion: "v1", | ||||||
Clusters: clusters, | ||||||
Contexts: contexts, | ||||||
CurrentContext: "default-context", | ||||||
AuthInfos: authinfos, | ||||||
} | ||||||
return clientConfig | ||||||
} |
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally don't want to reuse the
err
var for a few safety reasons. it's best to scope lock it in the "if".