Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱 Check capi status if it is provisioned #801

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = "agent-registration-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
1 change: 1 addition & 0 deletions pkg/registration/hub/importer/renderers.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ func RenderImagePullSecret(kubeClient kubernetes.Interface, namespace string) Kl
return config, nil
}

config.Images.ImageCredentials.CreateImageCredentials = true
config.Images.ImageCredentials.DockerConfigJson = string(secret.Data[corev1.DockerConfigJsonKey])
return config, nil
}
Expand Down
8 changes: 6 additions & 2 deletions test/integration/registration/managedcluster_importer_test.go
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,8 +147,10 @@ var _ = ginkgo.Describe("Cluster Auto Importer", func() {
if err != nil {
return err
}
capiCluster.SetLabels(map[string]string{"reconcile": "trigger"})
_, err = dynamicClient.Resource(capi.ClusterAPIGVR).Namespace(managedClusterName).Update(
unstructured.SetNestedField(capiCluster.Object, map[string]interface{}{
"phase": "Provisioned",
}, "status")
_, err = dynamicClient.Resource(capi.ClusterAPIGVR).Namespace(managedClusterName).UpdateStatus(
context.TODO(), capiCluster, metav1.UpdateOptions{})
if err != nil {
return err
Expand All @@ -162,6 +165,7 @@ var _ = ginkgo.Describe("Cluster Auto Importer", func() {
}
if !meta.IsStatusConditionTrue(
spokeCluster.Status.Conditions, importer.ManagedClusterConditionImported) {
ginkgo.By(fmt.Sprintf("condition is %v", spokeCluster.Status.Conditions))
return fmt.Errorf("cluster should have imported")
}
_, err = operatorClient.OperatorV1().Klusterlets().Get(
Expand Down
Loading