Skip to content

Commit

Permalink
Check if managed cluster is created during join
Browse files Browse the repository at this point in the history
This is to check if managed cluster is created
when using --wait flag with join command, using
the bootstrap secret

Signed-off-by: Jian Qiu <jqiu@redhat.com>
  • Loading branch information
qiujian16 committed Apr 8, 2024
1 parent 4cd7991 commit 8ad4da3
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 3 deletions.
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("")
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

0 comments on commit 8ad4da3

Please sign in to comment.