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

wip: kubeconfig command added to export kubeconfigs of running clusters #25

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
<a href="https://github.com/Trendyol/kink/releases/latest"><img src="https://img.shields.io/github/release/Trendyol/kink.svg" alt="GitHub release"></a>
<a href="https://github.com/Trendyol/kink/"><img src="https://img.shields.io/github/go-mod/go-version/Trendyol/kink" alt="Go Mod"></a>
</p>
</p>

---

Expand Down
15 changes: 4 additions & 11 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,14 @@ package cmd
import (
"context"
"fmt"
"os"
"os/user"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/client-go/kubernetes/typed/core/v1"

"github.com/AlecAivazis/survey/v2"
"github.com/Trendyol/kink/pkg/kubernetes"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/client-go/kubernetes/typed/core/v1"
)

// NewCmdDelete represents the delete command
Expand Down Expand Up @@ -62,18 +60,13 @@ func NewCmdDelete() *cobra.Command {

ctx := context.TODO()

currentUser, err := user.Current()
if err != nil {
return err
}

hostname, err := os.Hostname()
selector, err := getClusterSelector()
if err != nil {
return err
}

pods, err := podClient.List(ctx, metav1.ListOptions{
LabelSelector: fmt.Sprintf("runned-by=%s", fmt.Sprintf("%s_%s", currentUser.Username, hostname)),
LabelSelector: selector,
})

options := metav1.DeleteOptions{}
Expand Down
126 changes: 126 additions & 0 deletions cmd/kubeconfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
Copyright © 2021 pe.container <pe.container@trendyol.com>

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 cmd

import (
"context"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/Trendyol/kink/pkg/kubernetes"
"github.com/pkg/errors"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func init() {
rootCmd.AddCommand(NewCmdKubeConfig())
}

func NewCmdKubeConfig() *cobra.Command {
var (
name string
namespace string
outputPath string
)

cmd := &cobra.Command{
Use: "kubeconfig",
Short: "Get running cluster's kubeconfig",
Long: `Get running cluster's kubeconfig
usage: kink kubeconfig --name kink-demo --namespace default`,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
client, err := kubernetes.Client()
if err != nil {
return err
}

if namespace == "" {
n, _, err := kubernetes.DefaultClientConfig().Namespace()
if err != nil {
return err
}

namespace = n
}

if outputPath != "" {
outputPath, err = filepath.Abs(outputPath)
if err != nil {
return errors.Wrap(err, "failed to get absolute path of output path")
}

pathStat, err := os.Stat(outputPath)
if err != nil {
return errors.Wrap(err, "failed to get stat output path")
}

if pathStat.IsDir() {
outputPath = fmt.Sprintf("%s/kink-%s-kubeconfig", outputPath, name)
}
}

ctx := context.TODO()
serviceClient := client.CoreV1().Services(namespace)

svc, err := serviceClient.Get(ctx, name, metav1.GetOptions{})
if err != nil {
return err
}

kubeconfig, err := doExec(name, namespace, []string{"kubectl", "config", "view", "--minify", "--flatten"})
if err != nil {
return err
}

hostIP, err := doExec(name, namespace, []string{"sh", "-c", "echo $CERT_SANS"})
if err != nil {
return err
}

podIP, err := doExec(name, namespace, []string{"sh", "-c", "echo $API_SERVER_ADDRESS"})
if err != nil {
return err
}

kubeconfig = strings.ReplaceAll(kubeconfig, podIP, hostIP)

nodePort := svc.Spec.Ports[0].NodePort
kubeconfig = strings.ReplaceAll(kubeconfig, "30001", fmt.Sprint(nodePort))

if outputPath != "" {
if err := os.WriteFile(outputPath, []byte(kubeconfig), 0o600); err != nil {
return errors.Wrap(err, "failed to write kubeconfig to "+kubeconfig)
}
fmt.Printf("kubeconfig exported for %s to %s\n", name, outputPath)
} else {
fmt.Print(kubeconfig) // write kubeconfig to stdout
}

return nil
},
}

cmd.Flags().StringVar(&name, "name", "", "Cluster name")
cmd.Flags().StringVarP(&namespace, "namespace", "n", "", "Target namespace")
cmd.Flags().StringVarP(&outputPath, "output", "o", "", "Output path for kubeconfig")

return cmd
}
13 changes: 3 additions & 10 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@ package cmd

import (
"context"
"fmt"
"os"
"os/user"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/cli-runtime/pkg/printers"

"github.com/Trendyol/kink/pkg/kubernetes"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// NewCmdList represents the list command
Expand Down Expand Up @@ -57,18 +55,13 @@ func NewCmdList() *cobra.Command {

kubeclient := client.CoreV1().Pods(namespace)

user, err := user.Current()
if err != nil {
return err
}

hostname, err := os.Hostname()
selector, err := getClusterSelector()
if err != nil {
return err
}

pods, err := kubeclient.List(context.TODO(), metav1.ListOptions{
LabelSelector: fmt.Sprintf("runned-by=%s", fmt.Sprintf("%s_%s", user.Username, hostname)),
LabelSelector: selector,
})
if err != nil {
return err
Expand Down
21 changes: 21 additions & 0 deletions cmd/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cmd

import (
"fmt"
"os"
"os/user"
)

func getClusterSelector() (string, error) {
usr, err := user.Current()
if err != nil {
return "", err
}

hostname, err := os.Hostname()
if err != nil {
return "", err
}

return fmt.Sprintf("runned-by=%s", fmt.Sprintf("%s_%s", usr.Username, hostname)), nil
}