@@ -11,6 +11,7 @@ import (
11
11
"github.com/spf13/pflag"
12
12
corev1 "k8s.io/api/core/v1"
13
13
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14
+ "sigs.k8s.io/cluster-api/controllers/remote"
14
15
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
15
16
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
16
17
@@ -114,7 +115,23 @@ func (s crsStrategy) apply(
114
115
targetCluster = cluster
115
116
}
116
117
117
- if err := utils .EnsureCRSForClusterFromConfigMaps (ctx , cm .Name , s .client , targetCluster , cm ); err != nil {
118
+ // In the case when existingManagementCluster is nil, i.e. when s.client points to a bootstrap cluster,
119
+ // it is possible that the namespace where the cluster will be moved to, and where the cluster-autoscaler resources
120
+ // will be created, does not exist yet.
121
+ // In that case, we need to create the namespace in the target cluster.
122
+ clusterKey := ctrlclient .ObjectKeyFromObject (targetCluster )
123
+ remoteClient , err := remote .NewClusterClient (ctx , "" , s .client , clusterKey )
124
+ if err != nil {
125
+ return fmt .Errorf ("error creating remote cluster client: %w" , err )
126
+ }
127
+ if err = utils .EnsureNamespace (ctx , remoteClient , cluster .Namespace ); err != nil {
128
+ return fmt .Errorf (
129
+ "failed to create Namespace in remote cluster: %w" ,
130
+ err ,
131
+ )
132
+ }
133
+
134
+ if err = utils .EnsureCRSForClusterFromConfigMaps (ctx , cm .Name , s .client , targetCluster , cm ); err != nil {
118
135
return fmt .Errorf (
119
136
"failed to apply cluster-autoscaler installation ClusterResourceSet: %w" ,
120
137
err ,
0 commit comments