-
Notifications
You must be signed in to change notification settings - Fork 13
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
||
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 { | ||
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. FYI, all writes should supply a FieldManager in the XXXOptions. 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. 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() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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] | ||
} | ||
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. I think the user should be given an error message if |
||
} | ||
|
||
return params | ||
} |
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) | ||
} |
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.
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.