@@ -27,6 +27,7 @@ import (
27
27
"sigs.k8s.io/controller-runtime/pkg/reconcile"
28
28
29
29
"github.com/kosmos.io/kosmos/cmd/clustertree/cluster-manager/app/options"
30
+ kosmosv1alpha1 "github.com/kosmos.io/kosmos/pkg/apis/kosmos/v1alpha1"
30
31
"github.com/kosmos.io/kosmos/pkg/clustertree/cluster-manager/extensions/daemonset"
31
32
leafUtils "github.com/kosmos.io/kosmos/pkg/clustertree/cluster-manager/utils"
32
33
"github.com/kosmos.io/kosmos/pkg/utils"
@@ -139,15 +140,15 @@ func (r *RootPodReconciler) Reconcile(ctx context.Context, request reconcile.Req
139
140
return reconcile.Result {RequeueAfter : RootPodRequeueTime }, nil
140
141
}
141
142
142
- rootpod := * (cachepod .DeepCopy ())
143
+ rootPod := * (cachepod .DeepCopy ())
143
144
144
145
// node filter
145
- if ! strings .HasPrefix (rootpod .Spec .NodeName , utils .KosmosNodePrefix ) {
146
+ if ! strings .HasPrefix (rootPod .Spec .NodeName , utils .KosmosNodePrefix ) {
146
147
// ignore the pod who donnot has the annotations "kosmos-io/owned-by-cluster"
147
148
// TODO: use const
148
149
nn := types.NamespacedName {
149
150
Namespace : "" ,
150
- Name : rootpod .Spec .NodeName ,
151
+ Name : rootPod .Spec .NodeName ,
151
152
}
152
153
153
154
targetNode := & corev1.Node {}
@@ -168,23 +169,23 @@ func (r *RootPodReconciler) Reconcile(ctx context.Context, request reconcile.Req
168
169
169
170
// TODO: GlobalLeafResourceManager may not inited....
170
171
// belongs to the current node
171
- if ! r .GlobalLeafManager .HasNode (rootpod .Spec .NodeName ) {
172
+ if ! r .GlobalLeafManager .HasNode (rootPod .Spec .NodeName ) {
172
173
return reconcile.Result {RequeueAfter : RootPodRequeueTime }, nil
173
174
}
174
175
175
- lr , err := r .GlobalLeafManager .GetLeafResourceByNodeName (rootpod .Spec .NodeName )
176
+ lr , err := r .GlobalLeafManager .GetLeafResourceByNodeName (rootPod .Spec .NodeName )
176
177
if err != nil {
177
178
// wait for leaf resource init
178
179
return reconcile.Result {RequeueAfter : RootPodRequeueTime }, nil
179
180
}
180
181
181
182
// skip namespace
182
- if len (lr .Namespace ) > 0 && lr .Namespace != rootpod .Namespace {
183
+ if len (lr .Namespace ) > 0 && lr .Namespace != rootPod .Namespace {
183
184
return reconcile.Result {}, nil
184
185
}
185
186
186
187
// delete pod in leaf
187
- if ! rootpod .GetDeletionTimestamp ().IsZero () {
188
+ if ! rootPod .GetDeletionTimestamp ().IsZero () {
188
189
if err := r .DeletePodInLeafCluster (ctx , lr , request .NamespacedName ); err != nil {
189
190
klog .Errorf ("delete pod in leaf error[1]: %v, %s" , err , request .NamespacedName )
190
191
return reconcile.Result {RequeueAfter : RootPodRequeueTime }, nil
@@ -198,7 +199,7 @@ func (r *RootPodReconciler) Reconcile(ctx context.Context, request reconcile.Req
198
199
// create pod in leaf
199
200
if err != nil {
200
201
if errors .IsNotFound (err ) {
201
- if err := r .CreatePodInLeafCluster (ctx , lr , & rootpod ); err != nil {
202
+ if err := r .CreatePodInLeafCluster (ctx , lr , & rootPod , r . GlobalLeafManager . GetClusterNode ( rootPod . Spec . NodeName ). LeafNodeSelector ); err != nil {
202
203
klog .Errorf ("create pod inleaf error, err: %s" , err )
203
204
return reconcile.Result {RequeueAfter : RootPodRequeueTime }, nil
204
205
} else {
@@ -211,8 +212,8 @@ func (r *RootPodReconciler) Reconcile(ctx context.Context, request reconcile.Req
211
212
}
212
213
213
214
// update pod in leaf
214
- if podutils .ShouldEnqueue (leafPod , & rootpod ) {
215
- if err := r .UpdatePodInLeafCluster (ctx , lr , & rootpod , leafPod ); err != nil {
215
+ if podutils .ShouldEnqueue (leafPod , & rootPod ) {
216
+ if err := r .UpdatePodInLeafCluster (ctx , lr , & rootPod , leafPod , r . GlobalLeafManager . GetClusterNode ( rootPod . Spec . NodeName ). LeafNodeSelector ); err != nil {
216
217
return reconcile.Result {RequeueAfter : RootPodRequeueTime }, nil
217
218
}
218
219
}
@@ -700,7 +701,7 @@ func (r *RootPodReconciler) createVolumes(ctx context.Context, lr *leafUtils.Lea
700
701
return nil
701
702
}
702
703
703
- func (r * RootPodReconciler ) CreatePodInLeafCluster (ctx context.Context , lr * leafUtils.LeafResource , pod * corev1.Pod ) error {
704
+ func (r * RootPodReconciler ) CreatePodInLeafCluster (ctx context.Context , lr * leafUtils.LeafResource , pod * corev1.Pod , nodeSelector kosmosv1alpha1. NodeSelector ) error {
704
705
if err := podutils .PopulateEnvironmentVariables (ctx , pod , r .envResourceManager ); err != nil {
705
706
// span.SetStatus(err)
706
707
return err
@@ -711,7 +712,7 @@ func (r *RootPodReconciler) CreatePodInLeafCluster(ctx context.Context, lr *leaf
711
712
return fmt .Errorf ("clusternode info is nil , name: %s" , pod .Spec .NodeName )
712
713
}
713
714
714
- basicPod := podutils .FitPod (pod , lr .IgnoreLabels , clusterNodeInfo .LeafMode == leafUtils . ALL )
715
+ basicPod := podutils .FitPod (pod , lr .IgnoreLabels , clusterNodeInfo .LeafMode , nodeSelector )
715
716
klog .V (4 ).Infof ("Creating pod %v/%+v" , pod .Namespace , pod .Name )
716
717
717
718
// create ns
@@ -765,24 +766,28 @@ func (r *RootPodReconciler) CreatePodInLeafCluster(ctx context.Context, lr *leaf
765
766
return nil
766
767
}
767
768
768
- func (r * RootPodReconciler ) UpdatePodInLeafCluster (ctx context.Context , lr * leafUtils.LeafResource , rootpod * corev1.Pod , leafpod * corev1.Pod ) error {
769
+ func (r * RootPodReconciler ) UpdatePodInLeafCluster (ctx context.Context , lr * leafUtils.LeafResource , rootPod * corev1.Pod , leafPod * corev1.Pod , nodeSelector kosmosv1alpha1. NodeSelector ) error {
769
770
// TODO: update env
770
771
// TODO: update config secret pv pvc ...
771
- klog .V (4 ).Infof ("Updating pod %v/%+v" , rootpod .Namespace , rootpod .Name )
772
+ klog .V (4 ).Infof ("Updating pod %v/%+v" , rootPod .Namespace , rootPod .Name )
772
773
773
- if ! podutils .IsKosmosPod (leafpod ) {
774
+ if ! podutils .IsKosmosPod (leafPod ) {
774
775
klog .V (4 ).Info ("Pod is not created by kosmos tree, ignore" )
775
776
return nil
776
777
}
777
778
// not used
778
- podutils .FitLabels (leafpod .ObjectMeta .Labels , lr .IgnoreLabels )
779
- podCopy := leafpod .DeepCopy ()
779
+ podutils .FitLabels (leafPod .ObjectMeta .Labels , lr .IgnoreLabels )
780
+ podCopy := leafPod .DeepCopy ()
780
781
// util.GetUpdatedPod update PodCopy container image, annotations, labels.
781
782
// recover toleration, affinity, tripped ignore labels.
782
- podutils .GetUpdatedPod (podCopy , rootpod , lr .IgnoreLabels )
783
- if reflect .DeepEqual (leafpod .Spec , podCopy .Spec ) &&
784
- reflect .DeepEqual (leafpod .Annotations , podCopy .Annotations ) &&
785
- reflect .DeepEqual (leafpod .Labels , podCopy .Labels ) {
783
+ clusterNodeInfo := r .GlobalLeafManager .GetClusterNode (rootPod .Spec .NodeName )
784
+ if clusterNodeInfo == nil {
785
+ return fmt .Errorf ("clusternode info is nil , name: %s" , rootPod .Spec .NodeName )
786
+ }
787
+ podutils .GetUpdatedPod (podCopy , rootPod , lr .IgnoreLabels , clusterNodeInfo .LeafMode , nodeSelector )
788
+ if reflect .DeepEqual (leafPod .Spec , podCopy .Spec ) &&
789
+ reflect .DeepEqual (leafPod .Annotations , podCopy .Annotations ) &&
790
+ reflect .DeepEqual (leafPod .Labels , podCopy .Labels ) {
786
791
return nil
787
792
}
788
793
@@ -798,7 +803,7 @@ func (r *RootPodReconciler) UpdatePodInLeafCluster(ctx context.Context, lr *leaf
798
803
if err != nil {
799
804
return fmt .Errorf ("could not update pod: %v" , err )
800
805
}
801
- klog .V (4 ).Infof ("Update pod %v/%+v success " , rootpod .Namespace , rootpod .Name )
806
+ klog .V (4 ).Infof ("Update pod %v/%+v success " , rootPod .Namespace , rootPod .Name )
802
807
return nil
803
808
}
804
809
0 commit comments