refactor: openpitrix module
Signed-off-by: hongming <talonwan@yunify.com>
This commit is contained in:
@@ -21,6 +21,7 @@ package namespace
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/golang/protobuf/ptypes/wrappers"
|
||||
appsv1 "k8s.io/api/apps/v1"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
rbac "k8s.io/api/rbac/v1"
|
||||
@@ -35,7 +36,7 @@ import (
|
||||
"kubesphere.io/kubesphere/pkg/simple/client/openpitrix"
|
||||
"kubesphere.io/kubesphere/pkg/utils/k8sutil"
|
||||
"kubesphere.io/kubesphere/pkg/utils/sliceutil"
|
||||
"math"
|
||||
"openpitrix.io/openpitrix/pkg/pb"
|
||||
"reflect"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
"sigs.k8s.io/controller-runtime/pkg/controller"
|
||||
@@ -44,7 +45,6 @@ import (
|
||||
"sigs.k8s.io/controller-runtime/pkg/manager"
|
||||
"sigs.k8s.io/controller-runtime/pkg/reconcile"
|
||||
"sigs.k8s.io/controller-runtime/pkg/source"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -137,12 +137,14 @@ func (r *ReconcileNamespace) Reconcile(request reconcile.Request) (reconcile.Res
|
||||
} else {
|
||||
// The object is being deleted
|
||||
if sliceutil.HasString(instance.ObjectMeta.Finalizers, finalizer) {
|
||||
if err := r.deleteRouter(instance.Name); err != nil {
|
||||
if err = r.deleteRouter(instance.Name); err != nil {
|
||||
return reconcile.Result{}, err
|
||||
}
|
||||
|
||||
// delete runtime in the background, retry 3 times
|
||||
go r.deleteRuntime(instance)
|
||||
// delete runtime
|
||||
if err = r.deleteRuntime(instance); err != nil {
|
||||
return reconcile.Result{}, err
|
||||
}
|
||||
|
||||
// remove our finalizer from the list and update it.
|
||||
instance.ObjectMeta.Finalizers = sliceutil.RemoveString(instance.ObjectMeta.Finalizers, func(item string) bool {
|
||||
@@ -223,8 +225,7 @@ func (r *ReconcileNamespace) checkAndCreateRoles(namespace *corev1.Namespace) er
|
||||
}
|
||||
if !reflect.DeepEqual(found.Rules, role.Rules) {
|
||||
found.Rules = role.Rules
|
||||
err := r.Update(context.TODO(), found)
|
||||
if err != nil {
|
||||
if err := r.Update(context.TODO(), found); err != nil {
|
||||
klog.Errorf("updating default role namespace: %s, role: %s,error: %s", namespace.Name, role.Name, err)
|
||||
return err
|
||||
}
|
||||
@@ -351,30 +352,73 @@ func (r *ReconcileNamespace) checkAndCreateRoleBindings(namespace *corev1.Namesp
|
||||
|
||||
// Create openpitrix runtime
|
||||
func (r *ReconcileNamespace) checkAndCreateRuntime(namespace *corev1.Namespace) error {
|
||||
openPitrixClient, err := cs.ClientSets().OpenPitrix()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if runtimeId := namespace.Annotations[constants.OpenPitrixRuntimeAnnotationKey]; runtimeId != "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
cm := &corev1.ConfigMap{}
|
||||
configName := fmt.Sprintf("kubeconfig-%s", constants.AdminUserName)
|
||||
err = r.Get(context.TODO(), types.NamespacedName{Namespace: constants.KubeSphereControlNamespace, Name: configName}, cm)
|
||||
openPitrixClient, err := cs.ClientSets().OpenPitrix()
|
||||
|
||||
if _, notEnabled := err.(cs.ClientSetNotEnabledError); notEnabled {
|
||||
return nil
|
||||
} else if err != nil {
|
||||
klog.Error(fmt.Sprintf("create runtime, namespace: %s, error: %s", namespace.Name, err))
|
||||
return err
|
||||
}
|
||||
|
||||
adminKubeConfigName := fmt.Sprintf("kubeconfig-%s", constants.AdminUserName)
|
||||
|
||||
runtimeCredentials, err := openPitrixClient.Runtime().DescribeRuntimeCredentials(openpitrix.SystemContext(), &pb.DescribeRuntimeCredentialsRequest{SearchWord: &wrappers.StringValue{Value: adminKubeConfigName}, Limit: 1})
|
||||
|
||||
if err != nil {
|
||||
klog.Error(fmt.Sprintf("create runtime, namespace: %s, error: %s", namespace.Name, err))
|
||||
return err
|
||||
}
|
||||
|
||||
runtime := &openpitrix.RunTime{Name: namespace.Name, Zone: namespace.Name, Provider: "kubernetes", RuntimeCredential: cm.Data["config"]}
|
||||
var kubesphereRuntimeCredentialId string
|
||||
|
||||
if err := openPitrixClient.CreateRuntime(runtime); err != nil {
|
||||
klog.Errorf("creating openpitrix runtime namespace: %s, error: %s", namespace.Name, err)
|
||||
// runtime credential exist
|
||||
if len(runtimeCredentials.GetRuntimeCredentialSet()) > 0 {
|
||||
kubesphereRuntimeCredentialId = runtimeCredentials.GetRuntimeCredentialSet()[0].GetRuntimeCredentialId().GetValue()
|
||||
} else {
|
||||
adminKubeConfig := corev1.ConfigMap{}
|
||||
err := r.Get(context.TODO(), types.NamespacedName{Namespace: constants.KubeSphereControlNamespace, Name: adminKubeConfigName}, &adminKubeConfig)
|
||||
|
||||
if err != nil {
|
||||
klog.Error(fmt.Sprintf("create runtime, namespace: %s, error: %s", namespace.Name, err))
|
||||
return err
|
||||
}
|
||||
|
||||
resp, err := openPitrixClient.Runtime().CreateRuntimeCredential(openpitrix.SystemContext(), &pb.CreateRuntimeCredentialRequest{
|
||||
Name: &wrappers.StringValue{Value: adminKubeConfigName},
|
||||
Provider: &wrappers.StringValue{Value: "kubernetes"},
|
||||
Description: &wrappers.StringValue{Value: "kubeconfig"},
|
||||
RuntimeUrl: &wrappers.StringValue{Value: "kubesphere"},
|
||||
RuntimeCredentialContent: &wrappers.StringValue{Value: adminKubeConfig.Data["config"]},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
klog.Error(fmt.Sprintf("create runtime, namespace: %s, error: %s", namespace.Name, err))
|
||||
return err
|
||||
}
|
||||
|
||||
kubesphereRuntimeCredentialId = resp.GetRuntimeCredentialId().GetValue()
|
||||
}
|
||||
|
||||
runtimeId, err := openPitrixClient.Runtime().CreateRuntime(openpitrix.SystemContext(), &pb.CreateRuntimeRequest{
|
||||
Name: &wrappers.StringValue{Value: namespace.Name},
|
||||
RuntimeCredentialId: &wrappers.StringValue{Value: kubesphereRuntimeCredentialId},
|
||||
Provider: &wrappers.StringValue{Value: openpitrix.KubernetesProvider},
|
||||
Zone: &wrappers.StringValue{Value: namespace.Name},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
klog.Error(fmt.Sprintf("create runtime, namespace: %s, error: %s", namespace.Name, err))
|
||||
return err
|
||||
}
|
||||
|
||||
klog.V(4).Infof("runtime created successfully, namespace: %s, runtime id: %s", namespace.Name, runtimeId)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -382,22 +426,23 @@ func (r *ReconcileNamespace) checkAndCreateRuntime(namespace *corev1.Namespace)
|
||||
func (r *ReconcileNamespace) deleteRuntime(namespace *corev1.Namespace) error {
|
||||
|
||||
if runtimeId := namespace.Annotations[constants.OpenPitrixRuntimeAnnotationKey]; runtimeId != "" {
|
||||
maxRetries := float64(3)
|
||||
for i := float64(0); i < maxRetries; i++ {
|
||||
time.Sleep(time.Duration(i*math.Pow(2, i)) * time.Second)
|
||||
|
||||
openPitrixClient, err := cs.ClientSets().OpenPitrix()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
openPitrixClient, err := cs.ClientSets().OpenPitrix()
|
||||
|
||||
err = openPitrixClient.DeleteRuntime(runtimeId)
|
||||
if _, notEnabled := err.(cs.ClientSetNotEnabledError); notEnabled {
|
||||
return nil
|
||||
} else if err != nil {
|
||||
klog.Errorf("delete openpitrix runtime: %s, error: %s", runtimeId, err)
|
||||
return err
|
||||
}
|
||||
|
||||
if err == nil || openpitrix.IsNotFound(err) || openpitrix.IsDeleted(err) {
|
||||
return nil
|
||||
}
|
||||
_, err = openPitrixClient.Runtime().DeleteRuntimes(openpitrix.SystemContext(), &pb.DeleteRuntimesRequest{RuntimeId: []string{runtimeId}, Force: &wrappers.BoolValue{Value: true}})
|
||||
|
||||
klog.Errorf("delete openpitrix runtime: %v times left, error: %s", maxRetries-i-1, err)
|
||||
if err == nil || openpitrix.IsNotFound(err) || openpitrix.IsDeleted(err) {
|
||||
return nil
|
||||
} else {
|
||||
klog.Errorf("delete openpitrix runtime: %s, error: %s", runtimeId, err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
@@ -451,12 +496,12 @@ func (r *ReconcileNamespace) deleteRouter(namespace string) error {
|
||||
if errors.IsNotFound(err) {
|
||||
return nil
|
||||
}
|
||||
klog.V(6).Info("get router service failed", err)
|
||||
klog.Error(err)
|
||||
}
|
||||
|
||||
err = r.Delete(context.TODO(), &found)
|
||||
if err != nil {
|
||||
klog.Error(err, "delete router failed")
|
||||
klog.Error(err)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -467,13 +512,13 @@ func (r *ReconcileNamespace) deleteRouter(namespace string) error {
|
||||
if errors.IsNotFound(err) {
|
||||
return nil
|
||||
}
|
||||
klog.V(6).Info("get router deployment failed", err)
|
||||
klog.Error(err)
|
||||
return err
|
||||
}
|
||||
|
||||
err = r.Delete(context.TODO(), &deploy)
|
||||
if err != nil {
|
||||
klog.Error(err, "delete router deployment failed")
|
||||
klog.Error(err)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -482,7 +527,7 @@ func (r *ReconcileNamespace) deleteRouter(namespace string) error {
|
||||
}
|
||||
|
||||
func (r *ReconcileNamespace) deleteRoleBindings(namespace *corev1.Namespace) error {
|
||||
klog.V(6).Info("deleting role bindings namespace: ", namespace.Name)
|
||||
klog.V(4).Info("deleting role bindings namespace: ", namespace.Name)
|
||||
adminBinding := &rbac.RoleBinding{}
|
||||
adminBinding.Name = admin.Name
|
||||
adminBinding.Namespace = namespace.Name
|
||||
|
||||
Reference in New Issue
Block a user