Skip to content

Commit 1be896d

Browse files
Merge pull request #17 from itdove/service-account
Use service-account to join if not --use-bootstrap-token on init
2 parents 5f45ae4 + bf2747d commit 1be896d

15 files changed

+128
-18
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Display the clusteradm version and the kubeversion
5959

6060
Initialize the hub by deploying the hub side resources to manage clusters.
6161

62-
`clusteradm init`
62+
`clusteradm init [--use-bootstrap-token]`
6363

6464
it returns the command line to launch on the spoke to join the hub.
6565

build/run-functional-tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ then
2626
fi
2727

2828
kubectl config use-context kind-${CLUSTER_NAME}-hub
29-
CMDINITRESULT=`clusteradm init`
29+
CMDINITRESULT=`clusteradm init --use-bootstrap-token`
3030
if [ $? != 0 ]
3131
then
3232
echo "init command result: "$CMDINITRESULT

pkg/cmd/init/cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,6 @@ func NewCmd(clusteradmFlags *genericclioptionsclusteradm.ClusteradmFlags, stream
4444
}
4545

4646
cmd.Flags().StringVar(&o.outputFile, "output-file", "", "The generated resources will be copied in the specified file")
47-
47+
cmd.Flags().BoolVar(&o.useBootstrapToken, "use-bootstrap-token", false, "If set then the boostrap token will used instead of a service account token")
4848
return cmd
4949
}

pkg/cmd/init/exec.go

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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

3439
func (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+
}

pkg/cmd/init/options.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ import (
1010
type Options struct {
1111
//ClusteradmFlags: The generic optiosn from the clusteradm cli-runtime.
1212
ClusteradmFlags *genericclioptionsclusteradm.ClusteradmFlags
13-
// factory cmdutil.Factory
14-
values Values
13+
values Values
1514
//The file to output the resources will be sent to the file.
1615
outputFile string
16+
//If true the bootstrap token will be used instead of the service account token
17+
useBootstrapToken bool
1718
}
1819

1920
//Valus: The values used in the template
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Copyright Contributors to the Open Cluster Management project
2+
apiVersion: v1
3+
kind: ServiceAccount
4+
metadata:
5+
name: cluster-bootstrap
6+
namespace: open-cluster-management
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright Contributors to the Open Cluster Management project
2+
apiVersion: rbac.authorization.k8s.io/v1
3+
kind: ClusterRoleBinding
4+
metadata:
5+
name: cluster-bootstrap
6+
roleRef:
7+
apiGroup: rbac.authorization.k8s.io
8+
kind: ClusterRole
9+
name: system:open-cluster-management:bootstrap
10+
subjects:
11+
- kind: ServiceAccount
12+
name: cluster-bootstrap
13+
namespace: open-cluster-management

0 commit comments

Comments
 (0)