@@ -5,15 +5,20 @@ import (
55 "fmt"
66 "time"
77
8+ corev1 "k8s.io/api/core/v1"
9+ "k8s.io/apimachinery/pkg/util/wait"
10+
811 "github.com/openshift/library-go/pkg/operator/resource/resourceapply"
912 "open-cluster-management.io/clusteradm/pkg/cmd/init/scenario"
13+ "open-cluster-management.io/clusteradm/pkg/config"
1014 "open-cluster-management.io/clusteradm/pkg/helpers"
1115 "open-cluster-management.io/clusteradm/pkg/helpers/apply"
1216
1317 "github.com/spf13/cobra"
1418
1519 apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
1620 "k8s.io/client-go/discovery"
21+ "k8s.io/client-go/kubernetes"
1722 "k8s.io/client-go/util/retry"
1823)
1924
@@ -32,6 +37,7 @@ func (o *Options) validate() error {
3237}
3338
3439func (o * Options ) run () error {
40+ token := fmt .Sprintf ("%s.%s" , o .values .Hub .TokenID , o .values .Hub .TokenSecret )
3541 output := make ([]string , 0 )
3642 reader := scenario .GetScenarioResourcesReader ()
3743
@@ -60,22 +66,44 @@ func (o *Options) run() error {
6066 WithDynamicClient (dynamicClient )
6167
6268 files := []string {
63- "init/bootstrap-token-secret.yaml" ,
64- "init/cluster_role_bootstrap.yaml" ,
65- "init/cluster_role_binding_bootstrap.yaml" ,
66- "init/cluster_role.yaml" ,
67- "init/cluster_role_binding.yaml" ,
68- "init/clustermanagers.crd.yaml" ,
6969 "init/namespace.yaml" ,
70- "init/service_account.yaml" ,
70+ }
71+ if o .useBootstrapToken {
72+ files = append (files ,
73+ "init/bootstrap-token-secret.yaml" ,
74+ "init/bootstrap_cluster_role.yaml" ,
75+ "init/bootstrap_cluster_role_binding.yaml" ,
76+ )
77+ } else {
78+ files = append (files ,
79+ "init/bootstrap_sa.yaml" ,
80+ "init/bootstrap_cluster_role.yaml" ,
81+ "init/bootstrap_sa_cluster_role_binding.yaml" ,
82+ )
7183 }
7284
85+ files = append (files ,
86+ "init/clustermanager_cluster_role.yaml" ,
87+ "init/clustermanager_cluster_role_binding.yaml" ,
88+ "init/clustermanagers.crd.yaml" ,
89+ "init/clustermanager_sa.yaml" ,
90+ )
91+
7392 out , err := apply .ApplyDirectly (clientHolder , reader , o .values , o .ClusteradmFlags .DryRun , "" , files ... )
7493 if err != nil {
7594 return err
7695 }
7796 output = append (output , out ... )
7897
98+ if ! o .useBootstrapToken {
99+ b := retry .DefaultBackoff
100+ b .Duration = 100 * time .Millisecond
101+ secret , err := waitForBootstrapSecret (kubeClient , b )
102+ if err != nil {
103+ return err
104+ }
105+ token = string (secret .Data ["token" ])
106+ }
79107 out , err = apply .ApplyDeployments (kubeClient , reader , o .values , o .ClusteradmFlags .DryRun , "" , "init/operator.yaml" )
80108 if err != nil {
81109 return err
@@ -92,18 +120,31 @@ func (o *Options) run() error {
92120 }
93121
94122 discoveryClient := discovery .NewDiscoveryClientForConfigOrDie (restConfig )
95- out , err = apply .ApplyCustomResouces (dynamicClient , discoveryClient , reader , o .values , o .ClusteradmFlags .DryRun , "" , "init/clustermanagers .cr.yaml" )
123+ out , err = apply .ApplyCustomResouces (dynamicClient , discoveryClient , reader , o .values , o .ClusteradmFlags .DryRun , "" , "init/clustermanager .cr.yaml" )
96124 if err != nil {
97125 return err
98126 }
99127 output = append (output , out ... )
100128
101- fmt .Printf ("please log on spoke and run:\n %s join --hub-token %s.%s --hub-apiserver %s --cluster-name <cluster_name>\n " ,
129+ fmt .Printf ("please log on spoke and run:\n %s join --hub-token %s --hub-apiserver %s --cluster-name <cluster_name>\n " ,
102130 helpers .GetExampleHeader (),
103- o .values .Hub .TokenID ,
104- o .values .Hub .TokenSecret ,
131+ token ,
105132 restConfig .Host ,
106133 )
107134
108135 return apply .WriteOutput (o .outputFile , output )
109136}
137+
138+ func waitForBootstrapSecret (kubeClient kubernetes.Interface , b wait.Backoff ) (secret * corev1.Secret , err error ) {
139+ err = retry .OnError (b , func (err error ) bool {
140+ if err != nil {
141+ fmt .Printf ("Wait for sa %s secret to be ready\n " , config .BootstrapSAName )
142+ return true
143+ }
144+ return false
145+ }, func () error {
146+ secret , err = helpers .GetBootstrapSecret (kubeClient )
147+ return err
148+ })
149+ return
150+ }
0 commit comments