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

Support kubectl bind with kubectl connect #7

Merged
merged 1 commit into from
Feb 26, 2024
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
1 change: 1 addition & 0 deletions pkg/konnector/models/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ package models

const (
AnnotationProviderClusterID = "provider.kube-bind.appscode.com/cluster-id"
KonnectorNamespace = "ace"
)
16 changes: 9 additions & 7 deletions pkg/kubectl/base/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"context"
"fmt"

"go.bytebuilders.dev/kube-bind/pkg/konnector/models"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -48,12 +50,12 @@ func ParseRemoteKubeconfig(kubeconfig []byte) (host string, ns string, err error
func FindRemoteKubeconfig(ctx context.Context, kubeClient *kubernetes.Clientset, remoteNamespace string, remoteHost string) (string, error) {
logger := klog.FromContext(ctx)

secrets, err := kubeClient.CoreV1().Secrets("kube-bind").List(ctx, v1.ListOptions{})
secrets, err := kubeClient.CoreV1().Secrets(models.KonnectorNamespace).List(ctx, v1.ListOptions{})
if err != nil {
return "", err
}
for _, s := range secrets.Items {
logger := logger.WithValues("namespace", "kube-bind", "name", s.Name)
logger := logger.WithValues("namespace", models.KonnectorNamespace, "name", s.Name)
bs, found := s.Data["kubeconfig"]
if !found {
logger.V(6).Info("secret does not contain kubeconfig")
Expand Down Expand Up @@ -92,15 +94,15 @@ func EnsureKubeconfigSecret(ctx context.Context, kubeconfig, name string, client
if name == "" {
secret := &corev1.Secret{
ObjectMeta: v1.ObjectMeta{
Namespace: "kube-bind",
Namespace: models.KonnectorNamespace,
GenerateName: "kubeconfig-",
},
Data: map[string][]byte{
"kubeconfig": []byte(kubeconfig),
},
}

secret, err := client.CoreV1().Secrets("kube-bind").Create(ctx, secret, v1.CreateOptions{})
secret, err := client.CoreV1().Secrets(models.KonnectorNamespace).Create(ctx, secret, v1.CreateOptions{})
if err != nil {
return nil, false, err
}
Expand All @@ -111,13 +113,13 @@ func EnsureKubeconfigSecret(ctx context.Context, kubeconfig, name string, client
var secret *corev1.Secret
if err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
var err error
secret, err = client.CoreV1().Secrets("kube-bind").Get(ctx, name, v1.GetOptions{})
secret, err = client.CoreV1().Secrets(models.KonnectorNamespace).Get(ctx, name, v1.GetOptions{})
if err != nil {
return err
}
bs, found := secret.Data["kubeconfig"]
if !found {
return fmt.Errorf("secret %s/%s does not contain a kubeconfig", "kube-bind", name)
return fmt.Errorf("secret %s/%s does not contain a kubeconfig", models.KonnectorNamespace, name)
}
existingHost, existingNamespace, err := ParseRemoteKubeconfig(bs)
if err != nil {
Expand All @@ -127,7 +129,7 @@ func EnsureKubeconfigSecret(ctx context.Context, kubeconfig, name string, client
return errors.NewAlreadyExists(corev1.Resource("secret"), secret.Name)
}
secret.Data["kubeconfig"] = []byte(kubeconfig)
if _, err := client.CoreV1().Secrets("kube-bind").Update(ctx, secret, v1.UpdateOptions{}); err != nil {
if _, err := client.CoreV1().Secrets(models.KonnectorNamespace).Update(ctx, secret, v1.UpdateOptions{}); err != nil {
return err
}
return nil
Expand Down
3 changes: 2 additions & 1 deletion pkg/kubectl/bind-apiservice/plugin/konnector.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

bindclient "go.bytebuilders.dev/kube-bind/client/clientset/versioned"
"go.bytebuilders.dev/kube-bind/hack/deploy/konnector"
"go.bytebuilders.dev/kube-bind/pkg/konnector/models"
"go.bytebuilders.dev/kube-bind/pkg/version"

"github.com/blang/semver/v4"
Expand Down Expand Up @@ -131,7 +132,7 @@ func (b *BindAPIServiceOptions) deployKonnector(ctx context.Context, config *res
}

func currentKonnectorVersion(ctx context.Context, kubeClient kubeclient.Interface) (string, bool, error) {
deployment, err := kubeClient.AppsV1().Deployments("kube-bind").Get(ctx, "konnector", metav1.GetOptions{})
deployment, err := kubeClient.AppsV1().Deployments(models.KonnectorNamespace).Get(ctx, "konnector", metav1.GetOptions{})
if err != nil && !errors.IsNotFound(err) {
return "", false, err
} else if errors.IsNotFound(err) {
Expand Down
7 changes: 4 additions & 3 deletions pkg/kubectl/bind-apiservice/plugin/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"

"go.bytebuilders.dev/kube-bind/pkg/konnector/models"
"go.bytebuilders.dev/kube-bind/pkg/kubectl/base"

corev1 "k8s.io/api/core/v1"
Expand All @@ -38,7 +39,7 @@ func (b *BindAPIServiceOptions) createKubeconfigSecret(ctx context.Context, conf
// create kube-bind namespace
if _, err := kubeClient.CoreV1().Namespaces().Create(ctx, &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "kube-bind",
Name: models.KonnectorNamespace,
},
}, metav1.CreateOptions{}); err != nil && !apierrors.IsAlreadyExists(err) {
return "", err
Expand Down Expand Up @@ -76,9 +77,9 @@ func (b *BindAPIServiceOptions) ensureKubeconfigSecretWithLogging(ctx context.Co

if b.remoteKubeconfigFile != "" {
if created {
fmt.Fprintf(b.Options.ErrOut, "🔒 Created secret %s/%s for host %s, namespace %s\n", "kube-bind", secret.Name, remoteHost, remoteNamespace)
fmt.Fprintf(b.Options.ErrOut, "🔒 Created secret %s/%s for host %s, namespace %s\n", models.KonnectorNamespace, secret.Name, remoteHost, remoteNamespace)
} else {
fmt.Fprintf(b.Options.ErrOut, "🔒 Updated secret %s/%s for host %s, namespace %s\n", "kube-bind", secret.Name, remoteHost, remoteNamespace)
fmt.Fprintf(b.Options.ErrOut, "🔒 Updated secret %s/%s for host %s, namespace %s\n", models.KonnectorNamespace, secret.Name, remoteHost, remoteNamespace)
}
}

Expand Down
13 changes: 5 additions & 8 deletions pkg/kubectl/bind-apiservice/plugin/servicebindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"go.bytebuilders.dev/kube-bind/apis/kubebind/v1alpha1"
"go.bytebuilders.dev/kube-bind/apis/kubebind/v1alpha1/helpers"
bindclient "go.bytebuilders.dev/kube-bind/client/clientset/versioned"
"go.bytebuilders.dev/kube-bind/pkg/konnector/models"

apiextensionsclientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -34,10 +35,6 @@ import (
"kmodules.xyz/client-go/conditions"
)

const (
kubeconfigSecretNamespace = "kube-bind"
)

func (b *BindAPIServiceOptions) createAPIServiceBindings(ctx context.Context, config *rest.Config, request *v1alpha1.APIServiceExportRequest, secretName string) ([]*v1alpha1.APIServiceBinding, error) {
bindClient, err := bindclient.NewForConfig(config)
if err != nil {
Expand All @@ -61,7 +58,7 @@ func (b *BindAPIServiceOptions) createAPIServiceBindings(ctx context.Context, co

hasSecret := false
for _, secRef := range existing.Spec.KubeconfigSecretRefs {
if secRef.Namespace == kubeconfigSecretNamespace && secRef.Name == secretName {
if secRef.Namespace == models.KonnectorNamespace && secRef.Name == secretName {
hasSecret = true
fmt.Fprintf(b.Options.IOStreams.ErrOut, "✅ Existing APIServiceBinding \"%s\" already has the secret \"%s\".\n", existing.Name, secretName) // nolint: errcheck
break
Expand All @@ -78,7 +75,7 @@ func (b *BindAPIServiceOptions) createAPIServiceBindings(ctx context.Context, co
Name: secretName,
Key: "kubeconfig",
},
Namespace: kubeconfigSecretNamespace,
Namespace: models.KonnectorNamespace,
})

existing, err = bindClient.KubeBindV1alpha1().APIServiceBindings().Update(ctx, existing, metav1.UpdateOptions{})
Expand Down Expand Up @@ -110,7 +107,7 @@ func (b *BindAPIServiceOptions) createAPIServiceBindings(ctx context.Context, co
created, err := bindClient.KubeBindV1alpha1().APIServiceBindings().Create(ctx, &v1alpha1.APIServiceBinding{
ObjectMeta: metav1.ObjectMeta{
Name: resource.Resource + "." + resource.Group,
Namespace: "kube-bind",
Namespace: models.KonnectorNamespace,
},
Spec: v1alpha1.APIServiceBindingSpec{
KubeconfigSecretRefs: []v1alpha1.ClusterSecretKeyRef{
Expand All @@ -119,7 +116,7 @@ func (b *BindAPIServiceOptions) createAPIServiceBindings(ctx context.Context, co
Name: secretName,
Key: "kubeconfig",
},
Namespace: "kube-bind",
Namespace: models.KonnectorNamespace,
},
},
},
Expand Down
11 changes: 6 additions & 5 deletions pkg/kubectl/bind/plugin/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"time"

"go.bytebuilders.dev/kube-bind/apis/kubebind/v1alpha1"
"go.bytebuilders.dev/kube-bind/pkg/konnector/models"
"go.bytebuilders.dev/kube-bind/pkg/kubectl/base"
"go.bytebuilders.dev/kube-bind/pkg/kubectl/bind/authenticator"

Expand Down Expand Up @@ -158,19 +159,19 @@ func (b *BindOptions) Run(ctx context.Context, urlCh chan<- string) error {
return fmt.Errorf("unsupported binding provider version: %q", provider.APIVersion)
}

ns, err := kubeClient.CoreV1().Namespaces().Get(ctx, "kube-bind", metav1.GetOptions{})
ns, err := kubeClient.CoreV1().Namespaces().Get(ctx, models.KonnectorNamespace, metav1.GetOptions{})
if err != nil && !apierrors.IsNotFound(err) {
return err
} else if apierrors.IsNotFound(err) {
ns = &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "kube-bind",
Name: models.KonnectorNamespace,
},
}
if ns, err = kubeClient.CoreV1().Namespaces().Create(ctx, ns, metav1.CreateOptions{}); err != nil {
return err
} else {
fmt.Fprintf(b.Options.IOStreams.ErrOut, "📦 Created kube-bind namespace.\n") // nolint: errcheck
fmt.Fprintf(b.Options.IOStreams.ErrOut, "📦 Created ace namespace.\n") // nolint: errcheck
}
}

Expand Down Expand Up @@ -241,9 +242,9 @@ func (b *BindOptions) Run(ctx context.Context, urlCh chan<- string) error {
return err
}
if created {
fmt.Fprintf(b.Options.ErrOut, "🔒 Created secret %s/%s for host %s, namespace %s\n", "kube-bind", secret.Name, remoteHost, remoteNamespace)
fmt.Fprintf(b.Options.ErrOut, "🔒 Created secret %s/%s for host %s, namespace %s\n", models.KonnectorNamespace, secret.Name, remoteHost, remoteNamespace)
} else {
fmt.Fprintf(b.Options.ErrOut, "🔒 Updated secret %s/%s for host %s, namespace %s\n", "kube-bind", secret.Name, remoteHost, remoteNamespace)
fmt.Fprintf(b.Options.ErrOut, "🔒 Updated secret %s/%s for host %s, namespace %s\n", models.KonnectorNamespace, secret.Name, remoteHost, remoteNamespace)
}

// print the request in dry-run mode
Expand Down
Loading