/
reconciler.go
99 lines (79 loc) · 3.21 KB
/
reconciler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
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 host
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
clog "sigs.k8s.io/controller-runtime/pkg/log"
tenancyv1alpha1 "github.com/kubestellar/kubeflex/api/v1alpha1"
"github.com/kubestellar/kubeflex/pkg/reconcilers/shared"
"github.com/kubestellar/kubeflex/pkg/util"
)
// HostReconciler reconciles a Host ControlPlane that exposed the hosting cluster with the ControlPlane abstraction
type HostReconciler struct {
*shared.BaseReconciler
}
func New(cl client.Client, scheme *runtime.Scheme, version string, clientSet *kubernetes.Clientset, dynamicClient *dynamic.DynamicClient) *HostReconciler {
return &HostReconciler{
BaseReconciler: &shared.BaseReconciler{
Client: cl,
Scheme: scheme,
ClientSet: clientSet,
DynamicClient: dynamicClient,
},
}
}
func (r *HostReconciler) Reconcile(ctx context.Context, hcp *tenancyv1alpha1.ControlPlane) (ctrl.Result, error) {
_ = clog.FromContext(ctx)
if err := r.BaseReconciler.ReconcileNamespace(ctx, hcp); err != nil {
return r.UpdateStatusForSyncingError(hcp, err)
}
if err := r.ReconcileServiceAccount(ctx, hcp); err != nil {
return r.UpdateStatusForSyncingError(hcp, err)
}
if err := r.ReconcileServiceAccountSecret(ctx, hcp); err != nil {
return r.UpdateStatusForSyncingError(hcp, err)
}
if err := r.ReconcileKubeconfigSecret(ctx, hcp); err != nil {
return r.UpdateStatusForSyncingError(hcp, err)
}
if err := r.ReconcileClusterRoleBinding(ctx, hcp); err != nil {
return r.UpdateStatusForSyncingError(hcp, err)
}
r.UpdateStatusWithSecretRef(hcp, util.AdminConfSecret, util.KubeconfigSecretKeyDefault, util.KubeconfigSecretKeyInCluster)
if hcp.Spec.PostCreateHook != nil &&
tenancyv1alpha1.HasConditionAvailable(hcp.Status.Conditions) {
if err := r.ReconcileUpdatePostCreateHook(ctx, hcp); err != nil {
return r.UpdateStatusForSyncingError(hcp, err)
}
}
return r.UpdateStatusForSyncingSuccess(ctx, hcp)
}
func (r *HostReconciler) checkOnlyOneCPOfTypeHostExists(ctx context.Context, hcp *tenancyv1alpha1.ControlPlane) error {
list := &tenancyv1alpha1.ControlPlaneList{}
if err := r.Client.List(ctx, list, &client.ListOptions{}); err != nil {
return err
}
for _, item := range list.Items {
if item.Spec.Type == tenancyv1alpha1.ControlPlaneTypeHost && item.GetName() != hcp.GetName() {
return fmt.Errorf("found another control plane with name %s of type %s. Only one control plane of type %s can be created",
item.Name, tenancyv1alpha1.ControlPlaneTypeHost, tenancyv1alpha1.ControlPlaneTypeHost)
}
}
return nil
}