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

add support for adopting external cluster #306

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion api/v1alpha1/conditions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,5 @@ func generateCondition(ctype ConditionType, reason ConditionReason, message stri
}

func addTime(t time.Duration) metav1.Time {
return metav1.NewTime(time.Now().Add(2 * time.Hour))
return metav1.NewTime(time.Now().Add(t))
}
1 change: 1 addition & 0 deletions api/v1alpha1/controlplane_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const (
ControlPlaneTypeOCM ControlPlaneType = "ocm"
ControlPlaneTypeVCluster ControlPlaneType = "vcluster"
ControlPlaneTypeHost ControlPlaneType = "host"
ControlPlaneTypeExternal ControlPlaneType = "external"
)

// We do not use ObjectReference as its use is discouraged in favor of a locally defined type.
Expand Down
78 changes: 78 additions & 0 deletions cmd/kflex/adopt/adopt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
Copyright 2024 The KubeStellar Authors.

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 adopt

import (
"context"
"fmt"
"os"
"sync"

tenancyv1alpha1 "github.com/kubestellar/kubeflex/api/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/kubestellar/kubeflex/cmd/kflex/common"
cont "github.com/kubestellar/kubeflex/cmd/kflex/ctx"
kfclient "github.com/kubestellar/kubeflex/pkg/client"
"github.com/kubestellar/kubeflex/pkg/kubeconfig"
"github.com/kubestellar/kubeflex/pkg/util"
)

type CPAdopt struct {
common.CP
}

// Adopt a control plane from another cluster
func (c *CPAdopt) Adopt(hook string, hookVars []string, chattyStatus bool) {
done := make(chan bool)
var wg sync.WaitGroup
cx := cont.CPCtx{}
controlPlaneType := tenancyv1alpha1.ControlPlaneTypeExternal

cx.Context(chattyStatus, false, false, false)

clp, err := kfclient.GetClient(c.Kubeconfig)
if err != nil {
fmt.Fprintf(os.Stderr, "Error getting client: %v\n", err)
os.Exit(1)
}
cl := *clp
Copy link
Contributor

@MikeSpreitzer MikeSpreitzer Dec 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does kfclient.GetClient return a pointer? Every call site does nothing with that pointer but dereference it --- to get an interface value that could itself be nil.


cp := common.GenerateControlPlane(c.Name, string(controlPlaneType), "", hook, hookVars)

util.PrintStatus(fmt.Sprintf("Adopting control plane %s of type %s ...", c.Name, controlPlaneType), done, &wg, chattyStatus)
if err := cl.Create(context.TODO(), cp, &client.CreateOptions{}); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, all writes should supply a FieldManager in the XXXOptions.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to leave this to a separate campaign/PR as there are many other places where there are writes so should probably be done consistently everywhere.

fmt.Fprintf(os.Stderr, "Error creating ControlPlane object: %v\n", err)
os.Exit(1)
}
done <- true

clientsetp, err := kfclient.GetClientSet(c.Kubeconfig)
if err != nil {
fmt.Fprintf(os.Stderr, "Error getting clientset: %v\n", err)
os.Exit(1)
}
clientset := *clientsetp
done <- true

if err := kubeconfig.LoadAndMerge(c.Ctx, clientset, c.Name, string(controlPlaneType)); err != nil {
fmt.Fprintf(os.Stderr, "Error loading and merging kubeconfig: %s\n", err)
os.Exit(1)
}

wg.Wait()
}
35 changes: 35 additions & 0 deletions cmd/kflex/common/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,45 @@ package common

import (
"context"
"strings"

v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
MikeSpreitzer marked this conversation as resolved.
Show resolved Hide resolved

tenancyv1alpha1 "github.com/kubestellar/kubeflex/api/v1alpha1"
)

type CP struct {
Ctx context.Context
Kubeconfig string
Name string
}

func GenerateControlPlane(name, controlPlaneType, backendType, hook string, hookVars []string) *tenancyv1alpha1.ControlPlane {
cp := &tenancyv1alpha1.ControlPlane{
ObjectMeta: v1.ObjectMeta{
Name: name,
},
Spec: tenancyv1alpha1.ControlPlaneSpec{
Type: tenancyv1alpha1.ControlPlaneType(controlPlaneType),
Backend: tenancyv1alpha1.BackendDBType(backendType),
},
}
if hook != "" {
cp.Spec.PostCreateHook = &hook
cp.Spec.PostCreateHookVars = convertToMap(hookVars)
}
return cp
}

func convertToMap(pairs []string) map[string]string {
params := make(map[string]string)

for _, pair := range pairs {
split := strings.SplitN(pair, "=", 2)
if len(split) == 2 {
params[split[0]] = split[1]
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the user should be given an error message if len(split) != 2.

}

return params
}
34 changes: 1 addition & 33 deletions cmd/kflex/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ import (
"context"
"fmt"
"os"
"strings"
"sync"

tenancyv1alpha1 "github.com/kubestellar/kubeflex/api/v1alpha1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/kubestellar/kubeflex/cmd/kflex/common"
Expand Down Expand Up @@ -52,7 +50,7 @@ func (c *CPCreate) Create(controlPlaneType, backendType, hook string, hookVars [
}
cl := *clp

cp := c.generateControlPlane(controlPlaneType, backendType, hook, hookVars)
cp := common.GenerateControlPlane(c.Name, controlPlaneType, backendType, hook, hookVars)

util.PrintStatus(fmt.Sprintf("Creating new control plane %s of type %s ...", c.Name, controlPlaneType), done, &wg, chattyStatus)
if err := cl.Create(context.TODO(), cp, &client.CreateOptions{}); err != nil {
Expand Down Expand Up @@ -104,33 +102,3 @@ func (c *CPCreate) Create(controlPlaneType, backendType, hook string, hookVars [

wg.Wait()
}

func (c *CPCreate) generateControlPlane(controlPlaneType, backendType, hook string, hookVars []string) *tenancyv1alpha1.ControlPlane {
cp := &tenancyv1alpha1.ControlPlane{
ObjectMeta: v1.ObjectMeta{
Name: c.Name,
},
Spec: tenancyv1alpha1.ControlPlaneSpec{
Type: tenancyv1alpha1.ControlPlaneType(controlPlaneType),
Backend: tenancyv1alpha1.BackendDBType(backendType),
},
}
if hook != "" {
cp.Spec.PostCreateHook = &hook
cp.Spec.PostCreateHookVars = convertToMap(hookVars)
}
return cp
}

func convertToMap(pairs []string) map[string]string {
params := make(map[string]string)

for _, pair := range pairs {
split := strings.SplitN(pair, "=", 2)
if len(split) == 2 {
params[split[0]] = split[1]
}
}

return params
}
14 changes: 5 additions & 9 deletions cmd/kflex/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ type CPDelete struct {

func (c *CPDelete) Delete(chattyStatus bool) {
done := make(chan bool)
cp := c.generateControlPlane()
cp := &tenancyv1alpha1.ControlPlane{
ObjectMeta: v1.ObjectMeta{
Name: c.Name,
},
}
var wg sync.WaitGroup

util.PrintStatus(fmt.Sprintf("Deleting control plane %s...", c.Name), done, &wg, chattyStatus)
Expand Down Expand Up @@ -88,11 +92,3 @@ func (c *CPDelete) Delete(chattyStatus bool) {
done <- true
wg.Wait()
}

func (c *CPDelete) generateControlPlane() *tenancyv1alpha1.ControlPlane {
return &tenancyv1alpha1.ControlPlane{
ObjectMeta: v1.ObjectMeta{
Name: c.Name,
},
}
}
4 changes: 4 additions & 0 deletions internal/controller/controlplane_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
clog "sigs.k8s.io/controller-runtime/pkg/log"

tenancyv1alpha1 "github.com/kubestellar/kubeflex/api/v1alpha1"
"github.com/kubestellar/kubeflex/pkg/reconcilers/external"
"github.com/kubestellar/kubeflex/pkg/reconcilers/host"
"github.com/kubestellar/kubeflex/pkg/reconcilers/k8s"
"github.com/kubestellar/kubeflex/pkg/reconcilers/ocm"
Expand Down Expand Up @@ -154,6 +155,9 @@ func (r *ControlPlaneReconciler) Reconcile(ctx context.Context, req ctrl.Request
case tenancyv1alpha1.ControlPlaneTypeHost:
reconciler := host.New(r.Client, r.Scheme, r.Version, r.ClientSet, r.DynamicClient)
return reconciler.Reconcile(ctx, hcp)
case tenancyv1alpha1.ControlPlaneTypeExternal:
reconciler := external.New(r.Client, r.Scheme, r.Version, r.ClientSet, r.DynamicClient)
return reconciler.Reconcile(ctx, hcp)
default:
return ctrl.Result{}, fmt.Errorf("unsupported control plane type: %s", hcp.Spec.Type)
}
Expand Down
85 changes: 85 additions & 0 deletions pkg/reconcilers/external/rbac.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
Copyright 2024 The KubeStellar Authors.

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 external

import (
"context"
"fmt"

"github.com/kubestellar/kubeflex/pkg/util"
rbacv1 "k8s.io/api/rbac/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
clog "sigs.k8s.io/controller-runtime/pkg/log"

tenancyv1alpha1 "github.com/kubestellar/kubeflex/api/v1alpha1"
)

func (r *ExternalReconciler) ReconcileClusterRoleBinding(ctx context.Context, hcp *tenancyv1alpha1.ControlPlane) error {
_ = clog.FromContext(ctx)
namespace := util.GenerateNamespaceFromControlPlaneName(hcp.Name)
bindingName := generateBindingName(hcp.Name)

// create cluster role binding object
binding := &rbacv1.ClusterRoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: bindingName,
},
}

err := r.Client.Get(context.TODO(), client.ObjectKeyFromObject(binding), binding, &client.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
binding := generateClusterRoleBinding(hcp.Name, namespace)
if err := controllerutil.SetControllerReference(hcp, binding, r.Scheme); err != nil {
return nil
}
err = r.Client.Create(context.TODO(), binding, &client.CreateOptions{})
if err != nil {
return err
}
}
return err
}
return nil
}

func generateClusterRoleBinding(name, namespace string) *rbacv1.ClusterRoleBinding {
return &rbacv1.ClusterRoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: generateBindingName(name),
},
RoleRef: rbacv1.RoleRef{
APIGroup: rbacv1.GroupName,
Kind: "ClusterRole",
Name: "cluster-admin",
},
Subjects: []rbacv1.Subject{
{
Kind: "ServiceAccount",
Name: name,
Namespace: namespace,
},
},
}
}

func generateBindingName(cpName string) string {
return fmt.Sprintf("%s-binding", cpName)
}
Loading
Loading