Skip to content

Commit c42fc25

Browse files
committedMar 9, 2024·
Configure url and cabundle for self managed cluster
Signed-off-by: Tamal Saha <[email protected]>
1 parent c6544e2 commit c42fc25

File tree

9 files changed

+150
-14
lines changed

9 files changed

+150
-14
lines changed
 

‎charts/multicluster-controlplane/templates/deployment.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ spec:
3232
{{- if eq .Values.enableSelfManagement true }}
3333
- "--self-management"
3434
{{- end }}
35+
{{- if .Values.selfManagementClusterName }}
36+
- "--self-management-cluster-name={{ .Values.selfManagementClusterName }}"
37+
{{- end }}
3538
{{- if eq .Values.enableDelegatingAuthentication true }}
3639
- "--delegating-authentication"
3740
{{- end }}

‎charts/multicluster-controlplane/values.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ autoApprovalBootstrapUsers: ""
1313

1414
# TODO: should add restriction while enable selfmanagement
1515
enableSelfManagement: false
16+
selfManagementClusterName: ""
1617

1718
enableDelegatingAuthentication: false
1819

‎go.mod

+20
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,15 @@ require (
2828
k8s.io/metrics v0.29.2
2929
k8s.io/utils v0.0.0-20240102154912-e7106e64919e
3030
open-cluster-management.io/api v0.13.0
31+
open-cluster-management.io/clusteradm v0.8.0
3132
open-cluster-management.io/ocm v0.13.0
3233
sigs.k8s.io/controller-runtime v0.17.2
3334
)
3435

3536
require (
37+
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
3638
github.com/BurntSushi/toml v1.3.2 // indirect
39+
github.com/MakeNowJust/heredoc v1.0.0 // indirect
3740
github.com/Masterminds/goutils v1.1.1 // indirect
3841
github.com/Masterminds/semver/v3 v3.2.1 // indirect
3942
github.com/Masterminds/sprig/v3 v3.2.3 // indirect
@@ -45,6 +48,7 @@ require (
4548
github.com/bwmarrin/snowflake v0.3.0 // indirect
4649
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
4750
github.com/cespare/xxhash/v2 v2.2.0 // indirect
51+
github.com/chai2010/gettext-go v1.0.2 // indirect
4852
github.com/cloudevents/sdk-go/protocol/mqtt_paho/v2 v2.0.0-20231030012137-0836a524e995 // indirect
4953
github.com/cloudevents/sdk-go/v2 v2.14.0 // indirect
5054
github.com/coreos/go-oidc v2.2.1+incompatible // indirect
@@ -58,10 +62,12 @@ require (
5862
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
5963
github.com/evanphx/json-patch v5.7.0+incompatible // indirect
6064
github.com/evanphx/json-patch/v5 v5.8.0 // indirect
65+
github.com/exponent-io/jsonpath v0.0.0-20210407135951-1de76d718b3f // indirect
6166
github.com/fatih/structs v1.1.0 // indirect
6267
github.com/felixge/httpsnoop v1.0.3 // indirect
6368
github.com/fsnotify/fsnotify v1.7.0 // indirect
6469
github.com/ghodss/yaml v1.0.0 // indirect
70+
github.com/go-errors/errors v1.4.2 // indirect
6571
github.com/go-logr/logr v1.4.1 // indirect
6672
github.com/go-logr/stdr v1.2.2 // indirect
6773
github.com/go-openapi/jsonpointer v0.19.6 // indirect
@@ -79,8 +85,10 @@ require (
7985
github.com/google/go-cmp v0.6.0 // indirect
8086
github.com/google/gofuzz v1.2.0 // indirect
8187
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 // indirect
88+
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
8289
github.com/google/uuid v1.3.0 // indirect
8390
github.com/gorilla/websocket v1.5.0 // indirect
91+
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect
8492
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
8593
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
8694
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
@@ -91,19 +99,24 @@ require (
9199
github.com/jonboulle/clockwork v0.3.0 // indirect
92100
github.com/josharian/intern v1.0.0 // indirect
93101
github.com/json-iterator/go v1.1.12 // indirect
102+
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect
94103
github.com/mailru/easyjson v0.7.7 // indirect
95104
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
96105
github.com/mitchellh/copystructure v1.2.0 // indirect
106+
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
97107
github.com/mitchellh/reflectwalk v1.0.2 // indirect
98108
github.com/moby/spdystream v0.2.0 // indirect
99109
github.com/moby/sys/mountinfo v0.6.2 // indirect
110+
github.com/moby/term v0.5.0 // indirect
100111
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
101112
github.com/modern-go/reflect2 v1.0.2 // indirect
113+
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
102114
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
103115
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
104116
github.com/opencontainers/go-digest v1.0.0 // indirect
105117
github.com/opencontainers/selinux v1.11.0 // indirect
106118
github.com/openshift/api v0.0.0-20231218131639-7a5aa77cc72d // indirect
119+
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
107120
github.com/pkg/errors v0.9.1 // indirect
108121
github.com/pkg/profile v1.3.0 // indirect
109122
github.com/pmezard/go-difflib v1.0.0 // indirect
@@ -114,6 +127,7 @@ require (
114127
github.com/prometheus/procfs v0.12.0 // indirect
115128
github.com/robfig/cron v1.2.0 // indirect
116129
github.com/robfig/cron/v3 v3.0.1 // indirect
130+
github.com/russross/blackfriday/v2 v2.1.0 // indirect
117131
github.com/shopspring/decimal v1.3.1 // indirect
118132
github.com/sirupsen/logrus v1.9.3 // indirect
119133
github.com/soheilhy/cmux v0.1.5 // indirect
@@ -126,6 +140,7 @@ require (
126140
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
127141
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
128142
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 // indirect
143+
github.com/xlab/treeprint v1.2.0 // indirect
129144
go.etcd.io/bbolt v1.3.8 // indirect
130145
go.etcd.io/etcd/api/v3 v3.5.10 // indirect
131146
go.etcd.io/etcd/client/pkg/v3 v3.5.10 // indirect
@@ -142,6 +157,7 @@ require (
142157
go.opentelemetry.io/otel/sdk v1.19.0 // indirect
143158
go.opentelemetry.io/otel/trace v1.19.0 // indirect
144159
go.opentelemetry.io/proto/otlp v1.0.0 // indirect
160+
go.starlark.net v0.0.0-20230525235612-a134d8f9ddca // indirect
145161
go.uber.org/multierr v1.11.0 // indirect
146162
go.uber.org/zap v1.26.0 // indirect
147163
golang.org/x/crypto v0.17.0 // indirect
@@ -166,10 +182,12 @@ require (
166182
gopkg.in/square/go-jose.v2 v2.6.0 // indirect
167183
gopkg.in/yaml.v3 v3.0.1 // indirect
168184
helm.sh/helm/v3 v3.14.1 // indirect
185+
k8s.io/cli-runtime v0.29.2 // indirect
169186
k8s.io/cloud-provider v0.29.2 // indirect
170187
k8s.io/component-helpers v0.29.2 // indirect
171188
k8s.io/dynamic-resource-allocation v0.29.2 // indirect
172189
k8s.io/kms v0.29.2 // indirect
190+
k8s.io/kubectl v0.29.0 // indirect
173191
k8s.io/kubelet v0.29.2 // indirect
174192
k8s.io/mount-utils v0.29.2 // indirect
175193
k8s.io/pod-security-admission v0.29.2 // indirect
@@ -178,6 +196,8 @@ require (
178196
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.28.0 // indirect
179197
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
180198
sigs.k8s.io/kube-storage-version-migrator v0.0.6-0.20230721195810-5c8923c5ff96 // indirect
199+
sigs.k8s.io/kustomize/api v0.13.5-0.20230601165947-6ce0bf390ce3 // indirect
200+
sigs.k8s.io/kustomize/kyaml v0.14.3-0.20230601165947-6ce0bf390ce3 // indirect
181201
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
182202
sigs.k8s.io/yaml v1.4.0 // indirect
183203
)

‎go.sum

+65
Large diffs are not rendered by default.

‎pkg/certificate/certificate.go

+11
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,17 @@ func InitKubeconfig(
264264
return err
265265
}
266266

267+
// save controlplane in-cluster kubeconfig to the data directory for self management
268+
if err := util.KubeconfigWriteToFile(
269+
InclusterKubeconfigFile(certDir),
270+
fmt.Sprintf("https://multicluster-controlplane.%s.svc/", util.GetComponentNamespace()),
271+
inClusterTrustBundlePEM,
272+
kubeconfigCertPEM,
273+
kubeconfigKeyPEM,
274+
); err != nil {
275+
return err
276+
}
277+
267278
// expose controlplane in-cluster kubeconfig in a secret
268279
if err := util.KubeconfigWroteToSecret(
269280
config,

‎pkg/certificate/certificateinfo.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import (
88
)
99

1010
const (
11-
ServiceAccountKeyFileName = "kube-serviceaccount.key"
12-
KubeconfigFileName = "kube-aggregator.kubeconfig"
11+
ServiceAccountKeyFileName = "kube-serviceaccount.key"
12+
KubeconfigFileName = "kube-aggregator.kubeconfig"
13+
InclusterKubeconfigFileName = "incluster.kubeconfig"
1314
// client user info
1415
UserAdmin = "system:admin"
1516
UserKubeApiserver = "kube-apiserver"
@@ -53,6 +54,9 @@ func ServiceAccountKeyFile(certsDir string) string {
5354
func KubeConfigFile(certsDir string) string {
5455
return filepath.Join(certsDir, KubeconfigFileName)
5556
}
57+
func InclusterKubeconfigFile(certsDir string) string {
58+
return filepath.Join(certsDir, InclusterKubeconfigFileName)
59+
}
5660
func DefaultRootCAFile(certsDir string) string {
5761
return filepath.Join(certsDir, RootCACertDirName, certchains.CACertFileName)
5862
}

‎pkg/controllers/ocmcontroller/ocmagent.go

+42-10
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,24 @@ import (
1616
"k8s.io/client-go/rest"
1717
"k8s.io/klog/v2"
1818
aggregatorapiserver "k8s.io/kube-aggregator/pkg/apiserver"
19-
2019
clusterclient "open-cluster-management.io/api/client/cluster/clientset/versioned"
2120
clusterv1 "open-cluster-management.io/api/cluster/v1"
21+
"open-cluster-management.io/clusteradm/pkg/helpers"
2222

2323
"open-cluster-management.io/multicluster-controlplane/pkg/agent"
24+
"open-cluster-management.io/multicluster-controlplane/pkg/certificate"
2425
"open-cluster-management.io/multicluster-controlplane/pkg/servers/options"
2526
"open-cluster-management.io/multicluster-controlplane/pkg/util"
2627
)
2728

2829
const SelfManagementClusterLabel = "multicluster-controlplane.open-cluster-management.io/selfmanagement"
2930

31+
type ClusterInfo struct {
32+
ClusterName string
33+
URL string
34+
CABundle []byte
35+
}
36+
3037
func InstallSelfManagementCluster(options options.ServerRunOptions) func(<-chan struct{}, *aggregatorapiserver.Config) error {
3138
return func(stopCh <-chan struct{}, aggregatorConfig *aggregatorapiserver.Config) error {
3239
inClusterConfig, err := rest.InClusterConfig()
@@ -52,13 +59,32 @@ func InstallSelfManagementCluster(options options.ServerRunOptions) func(<-chan
5259
}
5360
}
5461

55-
go EnableSelfManagement(ctx, hubRestConfig, options.ControlplaneDataDir, clusterName)
62+
kubeClient, err := kubernetes.NewForConfig(inClusterConfig)
63+
if err != nil {
64+
return err
65+
}
66+
apiserverURL, err := helpers.GetAPIServer(kubeClient)
67+
if err != nil {
68+
return err
69+
}
70+
caBundle, err := helpers.GetCACert(kubeClient)
71+
if err != nil {
72+
return err
73+
}
74+
75+
selfClusterInfo := ClusterInfo{
76+
ClusterName: clusterName,
77+
URL: apiserverURL,
78+
CABundle: caBundle,
79+
}
80+
81+
go EnableSelfManagement(ctx, hubRestConfig, options.ControlplaneDataDir, &selfClusterInfo)
5682

5783
return nil
5884
}
5985
}
6086

61-
func EnableSelfManagement(ctx context.Context, hubRestConfig *rest.Config, controlplaneCertDir, selfClusterName string) {
87+
func EnableSelfManagement(ctx context.Context, hubRestConfig *rest.Config, controlplaneCertDir string, selfClusterInfo *ClusterInfo) {
6288
kubeClient, err := kubernetes.NewForConfig(hubRestConfig)
6389
if err != nil {
6490
klog.Fatalf("Failed to kube client, %v", err)
@@ -69,24 +95,24 @@ func EnableSelfManagement(ctx context.Context, hubRestConfig *rest.Config, contr
6995
klog.Fatalf("Failed to cluster client, %v", err)
7096
}
7197

72-
if err := createNamespace(ctx, kubeClient, selfClusterName); err != nil {
98+
if err := createNamespace(ctx, kubeClient, selfClusterInfo.ClusterName); err != nil {
7399
klog.Fatalf("Failed to create self managed cluster namespace, %v", err)
74100
}
75101

76102
// TODO need a controller to maintain the self managed cluster
77-
if err := waitForSelfManagedCluster(ctx, clusterClient, selfClusterName); err != nil {
103+
if err := waitForSelfManagedCluster(ctx, clusterClient, selfClusterInfo); err != nil {
78104
klog.Fatalf("Failed to create self managed cluster, %v", err)
79105
}
80106

81-
bootstrapKubeConfig := path.Join(controlplaneCertDir, "cert", "kube-aggregator.kubeconfig")
107+
bootstrapKubeConfig := path.Join(controlplaneCertDir, "cert", certificate.InclusterKubeconfigFileName)
82108
agentHubKubeconfigDir := path.Join(controlplaneCertDir, "agent", "hub-kubeconfig")
83109
if err := os.MkdirAll(agentHubKubeconfigDir, os.ModePerm); err != nil {
84110
klog.Fatalf("Failed to create dir %s, %v", agentHubKubeconfigDir, err)
85111
}
86112

87113
// TODO also need provide feature gates
88114
klusterletAgent := agent.NewAgentOptions().
89-
WithClusterName(selfClusterName).
115+
WithClusterName(selfClusterInfo.ClusterName).
90116
WithBootstrapKubeconfig(bootstrapKubeConfig).
91117
WithHubKubeconfigDir(agentHubKubeconfigDir).
92118
WithWorkloadSourceDriverConfig(agentHubKubeconfigDir + "/kubeconfig")
@@ -114,21 +140,27 @@ func createNamespace(ctx context.Context, kubeClient kubernetes.Interface, ns st
114140
return err
115141
}
116142

117-
func waitForSelfManagedCluster(ctx context.Context, clusterClient clusterclient.Interface, selfClusterName string) error {
143+
func waitForSelfManagedCluster(ctx context.Context, clusterClient clusterclient.Interface, selfClusterInfo *ClusterInfo) error {
118144
return wait.PollUntilContextCancel(ctx, 5*time.Second, true, func(ctx context.Context) (bool, error) {
119-
selfCluster, err := clusterClient.ClusterV1().ManagedClusters().Get(ctx, selfClusterName, metav1.GetOptions{})
145+
selfCluster, err := clusterClient.ClusterV1().ManagedClusters().Get(ctx, selfClusterInfo.ClusterName, metav1.GetOptions{})
120146
if errors.IsNotFound(err) {
121147
_, err := clusterClient.ClusterV1().ManagedClusters().Create(
122148
ctx,
123149
&clusterv1.ManagedCluster{
124150
ObjectMeta: metav1.ObjectMeta{
125-
Name: selfClusterName,
151+
Name: selfClusterInfo.ClusterName,
126152
Labels: map[string]string{
127153
SelfManagementClusterLabel: "",
128154
},
129155
},
130156
Spec: clusterv1.ManagedClusterSpec{
131157
HubAcceptsClient: true,
158+
ManagedClusterClientConfigs: []clusterv1.ClientConfig{
159+
{
160+
URL: selfClusterInfo.URL,
161+
CABundle: selfClusterInfo.CABundle,
162+
},
163+
},
132164
},
133165
},
134166
metav1.CreateOptions{},

‎pkg/servers/options/plugins.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func RegisterAllAdmissionPlugins(plugins *admission.Plugins) {
7878

7979
// DefaultOffAdmissionPlugins get admission plugins off by default for kube-apiserver.
8080
func DefaultOffAdmissionPlugins() sets.Set[string] {
81-
defaultOnPlugins := sets.New(
81+
defaultOnPlugins := sets.New[string](
8282
lifecycle.PluginName, // NamespaceLifecycle
8383
serviceaccount.PluginName, // ServiceAccount
8484
mutatingwebhook.PluginName, // MutatingAdmissionWebhook

‎pkg/util/util.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"k8s.io/apimachinery/pkg/util/uuid"
2323
"k8s.io/client-go/kubernetes"
2424
"k8s.io/client-go/rest"
25-
clientcmd "k8s.io/client-go/tools/clientcmd"
25+
"k8s.io/client-go/tools/clientcmd"
2626
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
2727
"k8s.io/client-go/util/retry"
2828
"k8s.io/klog/v2"

0 commit comments

Comments
 (0)
Please sign in to comment.