Skip to content

Commit

Permalink
Check capi status if it is provisioned
Browse files Browse the repository at this point in the history
Signed-off-by: Jian Qiu <[email protected]>
  • Loading branch information
qiujian16 committed Jan 15, 2025
1 parent 54a9764 commit f71b70f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 9 deletions.
5 changes: 3 additions & 2 deletions pkg/registration/hub/importer/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ import (

const (
operatorNamesapce = "open-cluster-management"
kluterletNamespace = "open-cluster-management-agent"
bootstrapSA = "cluster-bootstrap"
ManagedClusterConditionImported = "Imported"
ManagedClusterConditionImported = "ManagedClusterImportSucceeded"
)

var (
Expand Down Expand Up @@ -194,7 +195,7 @@ func (i *Importer) reconcile(
return cluster, err
}
}
rawManifests, err := chart.RenderKlusterletChart(klusterletChartConfig, operatorNamesapce)
rawManifests, err := chart.RenderKlusterletChart(klusterletChartConfig, kluterletNamespace)
if err != nil {
return cluster, err
}
Expand Down
19 changes: 17 additions & 2 deletions pkg/registration/hub/importer/providers/capi/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/dynamic"
Expand Down Expand Up @@ -45,7 +46,8 @@ type CAPIProvider struct {
}

func NewCAPIProvider(
kubeconfig *rest.Config, clusterInformer clusterinformerv1.ManagedClusterInformer) providers.Interface {
kubeconfig *rest.Config,
clusterInformer clusterinformerv1.ManagedClusterInformer) providers.Interface {
dynamicClient := dynamic.NewForConfigOrDie(kubeconfig)
kubeClient := kubernetes.NewForConfigOrDie(kubeconfig)

Expand All @@ -70,7 +72,7 @@ func (c *CAPIProvider) Clients(ctx context.Context, cluster *clusterv1.ManagedCl
if err != nil {
return nil, err
}
_, err = c.lister.ByNamespace(namespace).Get(name)
capiCluster, err := c.lister.ByNamespace(namespace).Get(name)
switch {
case apierrors.IsNotFound(err):
logger.V(4).Info("cluster is not found", "name", name, "namespace", namespace)
Expand All @@ -80,6 +82,19 @@ func (c *CAPIProvider) Clients(ctx context.Context, cluster *clusterv1.ManagedCl
return nil, err
}

// check phase field of capi cluster
capiClusterUnstructured, ok := capiCluster.(*unstructured.Unstructured)
if !ok {
return nil, fmt.Errorf("invalid cluster type: %T", capiCluster)
}
status, exists, err := unstructured.NestedString(capiClusterUnstructured.Object, "status", "phase")
if err != nil {
return nil, err
}
if !exists || status != "Provisioned" {
return nil, nil
}

secret, err := c.kubeClient.CoreV1().Secrets(namespace).Get(ctx, name+"-kubeconfig", metav1.GetOptions{})
switch {
case apierrors.IsNotFound(err):
Expand Down
32 changes: 28 additions & 4 deletions pkg/registration/hub/importer/providers/capi/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,19 @@ func TestClients(t *testing.T) {
name: "capi cluster not found",
cluster: &clusterv1.ManagedCluster{ObjectMeta: metav1.ObjectMeta{Name: "cluster1"}},
},
{
name: "capi cluster not provisionde",
cluster: &clusterv1.ManagedCluster{ObjectMeta: metav1.ObjectMeta{Name: "cluster1"}},
capiObjects: []runtime.Object{
testingcommon.NewUnstructuredWithContent("cluster.x-k8s.io/v1beta1", "Cluster", "cluster1", "cluster1",
map[string]interface{}{
"status": map[string]interface{}{
"phase": "Provisioning",
},
},
),
},
},
{
name: "secret not found",
cluster: &clusterv1.ManagedCluster{ObjectMeta: metav1.ObjectMeta{Name: "cluster1"}},
Expand All @@ -108,8 +121,14 @@ func TestClients(t *testing.T) {
name: "secret found with invalid key",
cluster: &clusterv1.ManagedCluster{ObjectMeta: metav1.ObjectMeta{Name: "cluster1"}},
capiObjects: []runtime.Object{
testingcommon.NewUnstructured(
"cluster.x-k8s.io/v1beta1", "Cluster", "cluster1", "cluster1")},
testingcommon.NewUnstructuredWithContent("cluster.x-k8s.io/v1beta1", "Cluster", "cluster1", "cluster1",
map[string]interface{}{
"status": map[string]interface{}{
"phase": "Provisioned",
},
},
),
},
kubeObjects: []runtime.Object{
&corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -124,8 +143,13 @@ func TestClients(t *testing.T) {
name: "build client successfully",
cluster: &clusterv1.ManagedCluster{ObjectMeta: metav1.ObjectMeta{Name: "cluster1"}},
capiObjects: []runtime.Object{
testingcommon.NewUnstructured(
"cluster.x-k8s.io/v1beta1", "Cluster", "cluster1", "cluster1")},
testingcommon.NewUnstructuredWithContent("cluster.x-k8s.io/v1beta1", "Cluster", "cluster1", "cluster1",
map[string]interface{}{
"status": map[string]interface{}{
"phase": "Provisioned",
},
},
)},
kubeObjects: []runtime.Object{
func() *corev1.Secret {
clientConfig := clientcmdapiv1.Config{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/apimachinery/pkg/util/rand"
Expand Down Expand Up @@ -146,7 +147,9 @@ var _ = ginkgo.Describe("Cluster Auto Importer", func() {
if err != nil {
return err
}
capiCluster.SetLabels(map[string]string{"reconcile": "trigger"})
unstructured.SetNestedField(capiCluster.Object, map[string]interface{}{
"phase": "Provisioned",
}, "status")
_, err = dynamicClient.Resource(capi.ClusterAPIGVR).Namespace(managedClusterName).Update(
context.TODO(), capiCluster, metav1.UpdateOptions{})
if err != nil {
Expand Down

0 comments on commit f71b70f

Please sign in to comment.