Skip to content

Commit 654aed0

Browse files
committed
support getting cluster authentication info from secrets
Signed-off-by: Iceber Gu <[email protected]>
1 parent 3c5a4e6 commit 654aed0

File tree

15 files changed

+406
-14
lines changed

15 files changed

+406
-14
lines changed

cmd/apiserver/app/options/options.go

+6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import (
2626
storageoptions "github.com/clusterpedia-io/clusterpedia/pkg/storage/options"
2727
)
2828

29+
const DefaultNamespace = "clusterpedia-system"
30+
2931
type ClusterPediaServerOptions struct {
3032
MaxRequestsInFlight int
3133
MaxMutatingRequestsInFlight int
@@ -41,6 +43,7 @@ type ClusterPediaServerOptions struct {
4143
Traces *genericoptions.TracingOptions
4244
Metrics *metrics.Options
4345

46+
RunInNamespace string
4447
Storage *storageoptions.StorageOptions
4548
ResourceServer *kubeapiserver.Options
4649
}
@@ -72,6 +75,7 @@ func NewServerOptions() *ClusterPediaServerOptions {
7275
Traces: genericoptions.NewTracingOptions(),
7376
Metrics: metrics.NewOptions(),
7477

78+
RunInNamespace: DefaultNamespace,
7579
Storage: storageoptions.NewStorageOptions(),
7680
ResourceServer: kubeapiserver.NewOptions(),
7781
}
@@ -101,6 +105,7 @@ func (o *ClusterPediaServerOptions) Config() (*apiserver.Config, error) {
101105
if err != nil {
102106
return nil, err
103107
}
108+
resourceServerConfig.SecretNamespace = o.RunInNamespace
104109

105110
if err := o.SecureServing.MaybeDefaultWithSelfSignedCerts("localhost", nil, []net.IP{net.ParseIP("127.0.0.1")}); err != nil {
106111
return nil, fmt.Errorf("error create self-signed certificates: %v", err)
@@ -171,6 +176,7 @@ func (o *ClusterPediaServerOptions) Flags() cliflag.NamedFlagSets {
171176
var fss cliflag.NamedFlagSets
172177

173178
genericfs := fss.FlagSet("generic")
179+
genericfs.StringVar(&o.RunInNamespace, "namespace", o.RunInNamespace, "The namespace in which the Pod is running.")
174180
genericfs.IntVar(&o.MaxRequestsInFlight, "max-requests-inflight", o.MaxRequestsInFlight, ""+
175181
"Otherwise, this flag limits the maximum number of non-mutating requests in flight, or a zero value disables the limit completely.")
176182
genericfs.IntVar(&o.MaxMutatingRequestsInFlight, "max-mutating-requests-inflight", o.MaxMutatingRequestsInFlight, ""+

cmd/binding-apiserver/app/binding_apiserver.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/spf13/cobra"
99
"k8s.io/apimachinery/pkg/util/runtime"
1010
genericfeatures "k8s.io/apiserver/pkg/features"
11+
clientset "k8s.io/client-go/kubernetes"
1112
cliflag "k8s.io/component-base/cli/flag"
1213
"k8s.io/component-base/cli/globalflag"
1314
"k8s.io/component-base/featuregate"
@@ -60,12 +61,16 @@ func NewClusterPediaServerCommand(ctx context.Context) *cobra.Command {
6061
return fmt.Errorf("CompletedConfig.New() called with config.StorageFactory == nil")
6162
}
6263

64+
client, err := clientset.NewForConfig(completedConfig.ClientConfig)
65+
if err != nil {
66+
return err
67+
}
6368
crdclient, err := versioned.NewForConfig(completedConfig.ClientConfig)
6469
if err != nil {
6570
return err
6671
}
6772

68-
synchromanager := synchromanager.NewManager(crdclient, config.StorageFactory, clustersynchro.ClusterSyncConfig{}, "")
73+
synchromanager := synchromanager.NewManager(client, crdclient, config.StorageFactory, clustersynchro.ClusterSyncConfig{}, "", config.ExtraConfig.SecretNamespace)
6974
go synchromanager.Run(1, ctx.Done())
7075

7176
server, err := completedConfig.New()

cmd/clustersynchro-manager/app/config/config.go

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package config
22

33
import (
4+
"k8s.io/client-go/kubernetes"
45
restclient "k8s.io/client-go/rest"
56
"k8s.io/client-go/tools/record"
67
componentbaseconfig "k8s.io/component-base/config"
@@ -14,6 +15,8 @@ import (
1415

1516
type Config struct {
1617
Kubeconfig *restclient.Config
18+
Namespace string
19+
Client kubernetes.Interface
1720
CRDClient *crdclientset.Clientset
1821
EventRecorder record.EventRecorder
1922

cmd/clustersynchro-manager/app/options/options.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import (
3131

3232
const (
3333
ClusterSynchroManagerUserAgent = "cluster-synchro-manager"
34+
35+
DefaultNamespace = "clusterpedia-system"
3436
)
3537

3638
type Options struct {
@@ -45,6 +47,7 @@ type Options struct {
4547
Metrics *MetricsOptions
4648
KubeStateMetrics *kubestatemetrics.Options
4749

50+
RunInNamespace string
4851
WorkerNumber int // WorkerNumber is the number of worker goroutines
4952
PageSizeForResourceSync int64
5053
ShardingName string
@@ -59,7 +62,7 @@ func NewClusterSynchroManagerOptions() (*Options, error) {
5962
componentbaseconfigv1alpha1.RecommendedDefaultClientConnectionConfiguration(&clientConnection)
6063

6164
leaderElection.ResourceName = "clusterpedia-clustersynchro-manager"
62-
leaderElection.ResourceNamespace = "clusterpedia-system"
65+
leaderElection.ResourceNamespace = DefaultNamespace
6366
leaderElection.ResourceLock = resourcelock.LeasesResourceLock
6467

6568
clientConnection.ContentType = runtime.ContentTypeJSON
@@ -78,6 +81,7 @@ func NewClusterSynchroManagerOptions() (*Options, error) {
7881
options.Storage = storageoptions.NewStorageOptions()
7982
options.Metrics = NewMetricsOptions()
8083
options.KubeStateMetrics = kubestatemetrics.NewOptions()
84+
options.RunInNamespace = DefaultNamespace
8185

8286
options.WorkerNumber = 5
8387
return &options, nil
@@ -92,6 +96,7 @@ func (o *Options) Flags() cliflag.NamedFlagSets {
9296
genericfs.Int32Var(&o.ClientConnection.Burst, "kube-api-burst", o.ClientConnection.Burst, "Burst to use while talking with kubernetes apiserver.")
9397
genericfs.IntVar(&o.WorkerNumber, "worker-number", o.WorkerNumber, "The number of worker goroutines.")
9498
genericfs.StringVar(&o.ShardingName, "sharding-name", o.ShardingName, "The sharding name of manager.")
99+
genericfs.StringVar(&o.RunInNamespace, "namespace", o.RunInNamespace, "The namespace in which the Pod is running.")
95100

96101
syncfs := fss.FlagSet("resource sync")
97102
syncfs.Int64Var(&o.PageSizeForResourceSync, "page-size", o.PageSizeForResourceSync, "The requested chunk size of initial and resync watch lists for resource sync")
@@ -169,8 +174,12 @@ func (o *Options) Config() (*config.Config, error) {
169174
if o.ShardingName != "" {
170175
o.LeaderElection.ResourceName = fmt.Sprintf("%s-%s", o.LeaderElection.ResourceName, o.ShardingName)
171176
}
177+
// Override the namespace for leader election resource.
178+
o.LeaderElection.ResourceNamespace = o.RunInNamespace
172179

173180
return &config.Config{
181+
Namespace: o.RunInNamespace,
182+
Client: client,
174183
CRDClient: crdclient,
175184
Kubeconfig: kubeconfig,
176185
EventRecorder: eventRecorder,

cmd/clustersynchro-manager/app/synchro.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func NewClusterSynchroManagerCommand(ctx context.Context) *cobra.Command {
9292
}
9393

9494
func Run(ctx context.Context, c *config.Config) error {
95-
synchromanager := synchromanager.NewManager(c.CRDClient, c.StorageFactory, c.ClusterSyncConfig, c.ShardingName)
95+
synchromanager := synchromanager.NewManager(c.Client, c.CRDClient, c.StorageFactory, c.ClusterSyncConfig, c.ShardingName, c.Namespace)
9696

9797
go func() {
9898
metricsserver.Run(c.MetricsServerConfig)

kustomize/crds/cluster.clusterpedia.io_pediaclusters.yaml

+58
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,64 @@ spec:
7171
certData:
7272
format: byte
7373
type: string
74+
certificationFrom:
75+
properties:
76+
ca:
77+
properties:
78+
key:
79+
type: string
80+
name:
81+
description: Namespace string `json:"namespace"`
82+
type: string
83+
required:
84+
- key
85+
- name
86+
type: object
87+
cert:
88+
properties:
89+
key:
90+
type: string
91+
name:
92+
description: Namespace string `json:"namespace"`
93+
type: string
94+
required:
95+
- key
96+
- name
97+
type: object
98+
key:
99+
properties:
100+
key:
101+
type: string
102+
name:
103+
description: Namespace string `json:"namespace"`
104+
type: string
105+
required:
106+
- key
107+
- name
108+
type: object
109+
kubeconfig:
110+
properties:
111+
key:
112+
type: string
113+
name:
114+
description: Namespace string `json:"namespace"`
115+
type: string
116+
required:
117+
- key
118+
- name
119+
type: object
120+
token:
121+
properties:
122+
key:
123+
type: string
124+
name:
125+
description: Namespace string `json:"namespace"`
126+
type: string
127+
required:
128+
- key
129+
- name
130+
type: object
131+
type: object
74132
keyData:
75133
format: byte
76134
type: string

pkg/apiserver/apiserver.go

+1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ func (config completedConfig) New() (*ClusterPediaServer, error) {
124124
resourceServerConfig.GenericConfig.ExternalAddress = config.GenericConfig.ExternalAddress
125125
resourceServerConfig.GenericConfig.LoopbackClientConfig = config.GenericConfig.LoopbackClientConfig
126126
resourceServerConfig.GenericConfig.TracerProvider = config.GenericConfig.TracerProvider
127+
resourceServerConfig.GenericConfig.SharedInformerFactory = config.GenericConfig.SharedInformerFactory
127128
resourceServerConfig.InformerFactory = clusterpediaInformerFactory
128129
resourceServerConfig.StorageFactory = config.StorageFactory
129130
resourceServerConfig.InitialAPIGroupResources = initialAPIGroupResources

pkg/generated/openapi/zz_generated.openapi.go

+104-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/kubeapiserver/apiserver.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ func NewDefaultConfig() *Config {
6666
}
6767

6868
type ExtraConfig struct {
69+
SecretNamespace string
6970
AllowPediaClusterConfigReuse bool
7071
ExtraProxyRequestHeaderPrefixes []string
7172
AllowedProxySubresources map[schema.GroupResource]sets.Set[string]
@@ -103,6 +104,7 @@ func (c *Config) Complete() CompletedConfig {
103104
type completedConfig struct {
104105
GenericConfig genericapiserver.CompletedConfig
105106

107+
SecretNamespace string
106108
StorageFactory storage.StorageFactory
107109
InformerFactory informers.SharedInformerFactory
108110
InitialAPIGroupResources []*restmapper.APIGroupResources
@@ -138,8 +140,9 @@ func (c completedConfig) New(delegationTarget genericapiserver.DelegationTarget)
138140
restManager := NewRESTManager(c.GenericConfig.Serializer, runtime.ContentTypeJSON, c.StorageFactory, c.InitialAPIGroupResources)
139141
discoveryManager := discovery.NewDiscoveryManager(c.GenericConfig.Serializer, restManager, delegate)
140142

143+
secretLister := c.GenericConfig.SharedInformerFactory.Core().V1().Secrets().Lister().Secrets(c.ExtraConfig.SecretNamespace)
141144
clusterInformer := c.InformerFactory.Cluster().V1alpha2().PediaClusters()
142-
connector := proxyrest.NewProxyConnector(clusterInformer.Lister(), c.ExtraConfig.AllowPediaClusterConfigReuse, c.ExtraConfig.ExtraProxyRequestHeaderPrefixes)
145+
connector := proxyrest.NewProxyConnector(clusterInformer.Lister(), secretLister, c.ExtraConfig.AllowPediaClusterConfigReuse, c.ExtraConfig.ExtraProxyRequestHeaderPrefixes)
143146

144147
methodSet := sets.New("GET")
145148
for _, rest := range proxyrest.GetSubresourceRESTs(connector) {

0 commit comments

Comments
 (0)