Skip to content

Commit 489d7e8

Browse files
committed
Avoid init twice and add get token
Signed-off-by: Dominique Vernier <[email protected]>
1 parent 678feb8 commit 489d7e8

File tree

19 files changed

+592
-87
lines changed

19 files changed

+592
-87
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
[comment]: # ( Copyright Contributors to the Open Cluster Management project )
22
# Release Content
33

4-
- Add support for non-bootstrap token enabled environment [issue 16](https://github.com/open-cluster-management-io/clusteradm/issues/16)
4+
- Add support for non-bootstrap token enabled environment [issue 16](https://github.com/open-cluster-management-io/clusteradm/issues/16)
5+
- Avoid to run the `clusteradm init` twice on the hub [issue 21](https://github.com/open-cluster-management-io/clusteradm/issues/21)
6+
- Add command `clusteradm get token` [issue 22](https://github.com/open-cluster-management-io/clusteradm/issues/22)

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@ export GOPACKAGES = $(shell go list ./... | grep -v /vendor | grep -v /build |
1717
.PHONY: clean
1818
clean: clean-test
1919
kind delete cluster --name ${PROJECT_NAME}-functional-test-hub
20-
kind delete cluster --name ${PROJECT_NAME}-functional-test-spoke
20+
kind delete cluster --name ${PROJECT_NAME}-functional-test-c1
21+
kind delete cluster --name ${PROJECT_NAME}-functional-test-c2
2122

2223
.PHONY: deps
2324
deps:
2425
@$(INSTALL_DEPENDENCIES)
2526

2627
.PHONY: build
2728
build:
29+
rm -f ${GOPATH}/bin/clusteradm
2830
go install ./cmd/clusteradm.go
2931

3032
.PHONY:

VERSION.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.1.0-alpha
1+
0.1.0-alpha.3

build/run-functional-tests.sh

Lines changed: 129 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,110 @@ export KUBECONFIG=$TEST_DIR/tmp/kind.yaml
1212
rm -rf $TEST_RESULT_DIR
1313
mkdir -p $TEST_RESULT_DIR
1414

15+
function init_hub() {
16+
echo "init_hub 1st parameter: "$1 >&2
17+
local _CMDINITRESULT=`clusteradm init $1`
18+
if [ $? != 0 ]
19+
then
20+
ERROR_REPORT=$ERROR_REPORT+"clusteradm init failed\n"
21+
fi
22+
echo $_CMDINITRESULT
23+
}
24+
25+
function join_hub() {
26+
echo "join_hub 1st parameter: "$1 >&2
27+
echo "join_hub 2nd parameter: "$2 >&2
28+
local _CMDJOIN=`echo "$1" | cut -d ':' -f2-4 | cut -d '<' -f1`
29+
_CMDJOIN="$_CMDJOIN $2"
30+
local _CMDJOINRESULT=`$_CMDJOIN`
31+
if [ $? != 0 ]
32+
then
33+
ERROR_REPORT=$ERROR_REPORT+"clusteradm join failed\n"
34+
fi
35+
echo $_CMDJOINRESULT
36+
}
37+
38+
function accept_cluster() {
39+
echo "accept_cluster 1st parameter: "$1 >&2
40+
local _CMDACCEPT=`echo "$1" | cut -d ':' -f2`
41+
_CMDACCEPT="$_CMDACCEPT"
42+
local _CMDACCEPTRESULT=`$_CMDACCEPT --wait 240`
43+
if [ $? != 0 ]
44+
then
45+
ERROR_REPORT=$ERROR_REPORT+"clusteradm accept failed\n"
46+
fi
47+
echo $_CMDACCEPTRESULT
48+
}
49+
50+
function gettoken() {
51+
local _CMDINITRESULT=`clusteradm get token`
52+
if [ $? != 0 ]
53+
then
54+
ERROR_REPORT=$ERROR_REPORT+"clusteradm get token failed\n"
55+
fi
56+
echo $_CMDINITRESULT
57+
}
58+
59+
function joinscenario() {
60+
echo "joinscenario 1st parameter: "$1 >&2
61+
echo "joinscenario 2nd parameter: "$2 >&2
62+
echo "init cluster" >&2
63+
kubectl config use-context kind-${CLUSTER_NAME}-hub
64+
CMDINITRESULT=$(init_hub $2)
65+
echo "init command result: "$CMDINITRESULT >&2
66+
67+
echo "join hub" >&2
68+
kubectl config use-context kind-${CLUSTER_NAME}-$1
69+
CMDJOINRESULT=$(join_hub "${CMDINITRESULT}" $1)
70+
echo "join command result: "$CMDJOINRESULT >&2
71+
72+
echo "Wait 4 min to stabilize" >&2
73+
74+
kubectl config use-context kind-${CLUSTER_NAME}-hub
75+
CMDACCEPTRESULT=$(accept_cluster "${CMDJOINRESULT}")
76+
echo $CMDACCEPTRESULT | grep approved
77+
if [ $? != 0 ]
78+
then
79+
echo "accept command result: "$CMDACCEPTRESULT >&2
80+
ERROR_REPORT=$ERROR_REPORT+"no CSR get approved\n"
81+
else
82+
echo "accept command result: "$CMDACCEPTRESULT >&2
83+
fi
84+
}
85+
86+
function gettokenscenario() {
87+
echo "gettokenscenario 1st parameter: "$1 >&2
88+
echo "get token from hub" >&2
89+
kubectl config use-context kind-${CLUSTER_NAME}-hub
90+
CMGETTOKENRESULT=$(gettoken)
91+
echo "get token command result: "$CMGETTOKENRESULT >&2
92+
93+
echo "join hub" >&2
94+
kubectl config use-context kind-${CLUSTER_NAME}-$1
95+
CMDJOINRESULT=$(join_hub "${CMGETTOKENRESULT}" $1)
96+
echo "join command result: "$CMDJOINRESULT >&1
97+
98+
echo "Wait 4 min to stabilize" >&2
99+
100+
kubectl config use-context kind-${CLUSTER_NAME}-hub
101+
CMDACCEPTRESULT=$(accept_cluster "${CMDJOINRESULT}")
102+
echo $CMDACCEPTRESULT | grep approved
103+
if [ $? != 0 ]
104+
then
105+
echo "accept command result: "$CMDACCEPTRESULT >&2
106+
ERROR_REPORT=$ERROR_REPORT+"no CSR get approved\n"
107+
else
108+
echo "accept command result: "$CMDACCEPTRESULT >&2
109+
fi
110+
}
111+
112+
echo "With bootstrap token"
113+
echo "--------------------"
15114
export KUBECONFIG=$TEST_DIR/tmp/config.yaml
16115
kind create cluster --name ${CLUSTER_NAME}-hub --config $TEST_DIR/kind-config/kind119-hub.yaml
17-
kind create cluster --name ${CLUSTER_NAME}-spoke
116+
kind create cluster --name ${CLUSTER_NAME}-c1
18117
#Wait for cluster to setup
118+
echo "Sleep 10 sec"
19119
sleep 10
20120

21121
echo "Test clusteradm version"
@@ -25,56 +125,38 @@ then
25125
ERROR_REPORT=$ERROR_REPORT+"clusteradm version failed\n"
26126
fi
27127

28-
kubectl config use-context kind-${CLUSTER_NAME}-hub
29-
CMDINITRESULT=`clusteradm init --use-bootstrap-token`
30-
if [ $? != 0 ]
31-
then
32-
echo "init command result: "$CMDINITRESULT
33-
ERROR_REPORT=$ERROR_REPORT+"clusteradm init failed\n"
34-
else
35-
echo "init command result: "$CMDINITRESULT
36-
echo $CMDINITRESULT
37-
fi
128+
echo "Joining with init and bootstrap token"
129+
echo "-------------------------------------"
130+
joinscenario c1 --use-bootstrap-token
131+
kind delete cluster --name ${CLUSTER_NAME}-c1
132+
kind create cluster --name ${CLUSTER_NAME}-c2
133+
echo "Joining with get token and bootstrap token"
134+
echo "------------------------------------------"
135+
gettokenscenario c2
38136

39-
CMDJOIN=`echo $CMDINITRESULT | cut -d ':' -f2,3,4 | cut -d '<' -f1`
40-
CMDJOIN="$CMDJOIN c1"
41-
echo "Join command: "$CMDJOIN
42-
kubectl config use-context kind-${CLUSTER_NAME}-spoke
43-
CMDJOINRESULT=`$CMDJOIN`
44-
if [ $? != 0 ]
45-
then
46-
echo "join command result: " $CMDJOINRESULT
47-
ERROR_REPORT=$ERROR_REPORT+"clusteradm join failed\n"
48-
else
49-
echo "join command result: " $CMDJOINRESULT
50-
fi
137+
kind delete cluster --name ${CLUSTER_NAME}-hub
138+
kind delete cluster --name ${CLUSTER_NAME}-c2
51139

52-
echo "Sleep 4 min to stabilize"
53-
# we need to wait 2 min but once we will have watch status monitor
54-
# we will not need to sleep anymore
55-
sleep 240
140+
echo "With Service account"
141+
echo "--------------------"
142+
export KUBECONFIG=$TEST_DIR/tmp/config.yaml
143+
kind create cluster --name ${CLUSTER_NAME}-hub --config $TEST_DIR/kind-config/kind119-hub.yaml
144+
kind create cluster --name ${CLUSTER_NAME}-c1
145+
#Wait for cluster to setup
146+
echo "Sleep 10 sec"
147+
sleep 10
56148

57-
CMDACCEPT=`echo $CMDJOINRESULT | cut -d ':' -f2`
58-
CMDACCEPT="$CMDACCEPT c1"
59-
echo "accept command: "$CMDACCEPT
60-
kubectl config use-context kind-${CLUSTER_NAME}-hub
61-
CMDACCEPTRESULT=`$CMDACCEPT`
62-
if [ $? != 0 ]
63-
then
64-
echo "accept command result: "$CMDACCEPTRESULT
65-
ERROR_REPORT=$ERROR_REPORT+"clusteradm accept failed\n"
66-
else
67-
echo "accept command result: "$CMDACCEPTRESULT
68-
fi
149+
echo "Joining with init and service account"
150+
echo "-------------------------------------"
151+
joinscenario c1
152+
kind delete cluster --name ${CLUSTER_NAME}-c1
153+
kind create cluster --name ${CLUSTER_NAME}-c2
154+
echo "Joining with get token and service account"
155+
echo "------------------------------------------"
156+
gettokenscenario c2
69157

70-
echo $CMDACCEPTRESULT | grep approved
71-
if [ $? != 0 ]
72-
then
73-
echo "accept command result: "$CMDACCEPTRESULT
74-
ERROR_REPORT=$ERROR_REPORT+"no CSR get approved\n"
75-
else
76-
echo "accept command result: "$CMDACCEPTRESULT
77-
fi
158+
kind delete cluster --name ${CLUSTER_NAME}-hub
159+
kind delete cluster --name ${CLUSTER_NAME}-c2
78160

79161
if [ -z "$ERROR_REPORT" ]
80162
then
@@ -83,6 +165,3 @@ else
83165
echo -e "\n\nErrors\n======\n"$ERROR_REPORT
84166
exit 1
85167
fi
86-
87-
kind delete cluster --name $CLUSTER_NAME-hub
88-
kind delete cluster --name $CLUSTER_NAME-spoke

cmd/clusteradm.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"open-cluster-management.io/clusteradm/pkg/cmd/version"
1919

2020
acceptclusters "open-cluster-management.io/clusteradm/pkg/cmd/accept"
21+
"open-cluster-management.io/clusteradm/pkg/cmd/get"
2122
inithub "open-cluster-management.io/clusteradm/pkg/cmd/init"
2223
joinhub "open-cluster-management.io/clusteradm/pkg/cmd/join"
2324
genericclioptionsclusteradm "open-cluster-management.io/clusteradm/pkg/genericclioptions"
@@ -63,6 +64,7 @@ func main() {
6364
{
6465
Message: "Registration commands:",
6566
Commands: []*cobra.Command{
67+
get.NewCmd(clusteradmFlags, streams),
6668
inithub.NewCmd(clusteradmFlags, streams),
6769
joinhub.NewCmd(clusteradmFlags, streams),
6870
acceptclusters.NewCmd(clusteradmFlags, streams),

pkg/cmd/accept/exec.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ import (
2121
)
2222

2323
const (
24-
groupName = "system:bootstrappers:managedcluster"
25-
userNameSignaturePrefix = "system:bootstrap:"
26-
clusterLabel = "open-cluster-management.io/cluster-name"
24+
groupNameBootstrap = "system:bootstrappers:managedcluster"
25+
userNameSignatureBootstrapPrefix = "system:bootstrap:"
26+
userNameSignatureSA = "system:serviceaccount:open-cluster-management:cluster-bootstrap"
27+
groupNameSA = "system:serviceaccounts:open-cluster-management"
28+
clusterLabel = "open-cluster-management.io/cluster-name"
2729
)
2830

2931
func (o *Options) complete(cmd *cobra.Command, args []string) (err error) {
@@ -110,12 +112,14 @@ func (o *Options) approveCSR(kubeClient *kubernetes.Clientset, clusterName strin
110112
var csr *certificatesv1.CertificateSigningRequest
111113
for _, item := range csrs.Items {
112114
//Does not have the correct name prefix
113-
if !strings.HasPrefix(item.Spec.Username, userNameSignaturePrefix) {
115+
if !strings.HasPrefix(item.Spec.Username, userNameSignatureBootstrapPrefix) &&
116+
!strings.HasPrefix(item.Spec.Username, userNameSignatureSA) {
114117
continue
115118
}
116119
//Check groups
117120
groups := sets.NewString(item.Spec.Groups...)
118-
if !groups.Has(groupName) {
121+
if !groups.Has(groupNameBootstrap) &&
122+
!groups.Has(groupNameSA) {
119123
continue
120124
}
121125
//Check if already approved or denied

pkg/cmd/get/cmd.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright Contributors to the Open Cluster Management project
2+
package get
3+
4+
import (
5+
"github.com/spf13/cobra"
6+
"k8s.io/cli-runtime/pkg/genericclioptions"
7+
"open-cluster-management.io/clusteradm/pkg/cmd/get/token"
8+
genericclioptionsclusteradm "open-cluster-management.io/clusteradm/pkg/genericclioptions"
9+
)
10+
11+
// NewCmd provides a cobra command wrapping NewCmdImportCluster
12+
func NewCmd(clusteradmFlags *genericclioptionsclusteradm.ClusteradmFlags, streams genericclioptions.IOStreams) *cobra.Command {
13+
cmd := &cobra.Command{
14+
Use: "get",
15+
Short: "get a resource",
16+
}
17+
18+
cmd.AddCommand(token.NewCmd(clusteradmFlags, streams))
19+
20+
return cmd
21+
}

pkg/cmd/get/token/cmd.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright Contributors to the Open Cluster Management project
2+
package token
3+
4+
import (
5+
"fmt"
6+
7+
"open-cluster-management.io/clusteradm/pkg/helpers"
8+
9+
"github.com/spf13/cobra"
10+
"k8s.io/cli-runtime/pkg/genericclioptions"
11+
genericclioptionsclusteradm "open-cluster-management.io/clusteradm/pkg/genericclioptions"
12+
)
13+
14+
var example = `
15+
# Get the bootstrap token
16+
%[1]s get token
17+
`
18+
19+
// NewCmd ...
20+
func NewCmd(clusteradmFlags *genericclioptionsclusteradm.ClusteradmFlags, streams genericclioptions.IOStreams) *cobra.Command {
21+
o := newOptions(clusteradmFlags, streams)
22+
23+
cmd := &cobra.Command{
24+
Use: "token",
25+
Short: "get the bootsrap token",
26+
Example: fmt.Sprintf(example, helpers.GetExampleHeader()),
27+
SilenceUsage: true,
28+
PreRun: func(c *cobra.Command, args []string) {
29+
helpers.DryRunMessage(o.ClusteradmFlags.DryRun)
30+
},
31+
RunE: func(c *cobra.Command, args []string) error {
32+
if err := o.complete(c, args); err != nil {
33+
return err
34+
}
35+
if err := o.validate(); err != nil {
36+
return err
37+
}
38+
if err := o.run(); err != nil {
39+
return err
40+
}
41+
42+
return nil
43+
},
44+
}
45+
46+
cmd.Flags().StringVar(&o.outputFile, "output-file", "", "The generated resources will be copied in the specified file")
47+
cmd.Flags().BoolVar(&o.useBootstrapToken, "use-bootstrap-token", false, "If set then the boostrap token will used instead of a service account token")
48+
49+
return cmd
50+
}

0 commit comments

Comments
 (0)