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 if managed cluster is created during join #411

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
2 changes: 1 addition & 1 deletion pkg/cmd/init/scenario/init/bootstrap_cluster_role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ rules:
- get
- create
- list
- update
- watch
- apiGroups:
- "cluster.open-cluster-management.io"
resources:
Expand Down
52 changes: 52 additions & 0 deletions pkg/cmd/join/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ import (
certutil "k8s.io/client-go/util/cert"
"k8s.io/klog/v2"
"k8s.io/kubectl/pkg/cmd/util"
clusterclient "open-cluster-management.io/api/client/cluster/clientset/versioned"
operatorclient "open-cluster-management.io/api/client/operator/clientset/versioned"
clusterv1 "open-cluster-management.io/api/cluster/v1"
ocmfeature "open-cluster-management.io/api/feature"
operatorv1 "open-cluster-management.io/api/operator/v1"
"open-cluster-management.io/clusteradm/pkg/cmd/join/preflight"
Expand Down Expand Up @@ -419,6 +421,11 @@ func (o *Options) applyKlusterlet(r *reader.ResourceReader, operatorClient opera
return err
}
}

err = o.waitUntilManagedClusterIsCreated(int64(o.ClusteradmFlags.Timeout), o.values.ClusterName)
if err != nil {
return err
}
}
return nil
}
Expand Down Expand Up @@ -455,6 +462,51 @@ func checkIfRegistrationOperatorAvailable(f util.Factory) (bool, error) {
return meta.IsStatusConditionTrue(conds, "Available"), nil
}

func (o *Options) waitUntilManagedClusterIsCreated(timeout int64, clusterName string) error {
//Create an unsecure bootstrap
bootstrapExternalConfigUnSecure := o.createExternalBootstrapConfig()
restConfig, err := helpers.CreateRESTConfigFromClientcmdapiv1Config(bootstrapExternalConfigUnSecure)
if err != nil {
return fmt.Errorf("failed to create rest config: %v", err)
}
clusterClient, err := clusterclient.NewForConfig(restConfig)
if err != nil {
return fmt.Errorf("failed to create client: %v", err)
}

phase := &atomic.Value{}
phase.Store("")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the phase.Store("") used for?
we did not print any status

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it could show whether cluster is created

operatorSpinner := printer.NewSpinnerWithStatus(
"Waiting for managed cluster to be created...",
time.Millisecond*500,
"Managed cluster is created.\n",
func() string {
return phase.Load().(string)
})
operatorSpinner.Start()
defer operatorSpinner.Stop()

return helpers.WatchUntil(
func() (watch.Interface, error) {
w, err := clusterClient.ClusterV1().ManagedClusters().
Watch(context.TODO(), metav1.ListOptions{
TimeoutSeconds: &timeout,
FieldSelector: fmt.Sprintf("metadata.name=%s", clusterName),
})
if err != nil {
return nil, fmt.Errorf("failed to watch: %v", err)
}
return w, nil
},
func(event watch.Event) bool {
cluster, ok := event.Object.(*clusterv1.ManagedCluster)
if !ok {
return false
}
return cluster.Name == clusterName
})
}

func waitUntilRegistrationOperatorConditionIsTrue(f util.Factory, timeout int64) error {
var restConfig *rest.Config
restConfig, err := f.ToRESTConfig()
Expand Down
10 changes: 9 additions & 1 deletion pkg/cmd/join/preflight/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,17 @@ func (c HubKubeconfigCheck) Check() (warningList []string, errorList []error) {
_, err = discoveryClient.ServerVersion()
if err != nil {
return nil, []error{err}
}

apiResourceList, err := discoveryClient.ServerResourcesForGroupVersion("cluster.open-cluster-management.io/v1")
var errs []error
if err != nil {
errs = append(errs, err)
}
return nil, nil
if len(apiResourceList.APIResources) == 0 {
errs = append(errs, fmt.Errorf("no apigroup cluster.open-cluster-management.io/v1 detected"))
}
return nil, errs
}

func (c HubKubeconfigCheck) Name() string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ rules:
- get
- create
- list
- update
- watch
- apiGroups:
- "cluster.open-cluster-management.io"
resources:
Expand Down
Loading