Skip to content
This repository was archived by the owner on Apr 21, 2019. It is now read-only.

Commit 059aed2

Browse files
author
Irfan Ur Rehman
committed
Flag to allow non RBAC based setup using credentials from an alternative kubeconfig in kubefed init
1 parent 5b2fc9f commit 059aed2

File tree

3 files changed

+51
-13
lines changed

3 files changed

+51
-13
lines changed

pkg/kubefed/init/init.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -284,21 +284,31 @@ func (i *initFederation) Complete(cmd *cobra.Command, args []string) error {
284284
// See the design doc in https://github.com/kubernetes/kubernetes/pull/34484
285285
// for details.
286286
func (i *initFederation) Run(cmdOut io.Writer, config util.AdminConfig) error {
287-
hostFactory := config.ClusterFactory(i.commonOptions.Host, i.commonOptions.Kubeconfig)
287+
var hostFactory cmdutil.Factory
288+
useRBAC := true
289+
if i.commonOptions.CredentialsKubeconfig != "" {
290+
hostFactory = config.ClusterFactory(i.commonOptions.Host, i.commonOptions.CredentialsKubeconfig)
291+
useRBAC = false
292+
} else {
293+
hostFactory = config.ClusterFactory(i.commonOptions.Host, i.commonOptions.Kubeconfig)
294+
}
295+
288296
hostClientset, err := hostFactory.ClientSet()
289297
if err != nil {
290298
return err
291299
}
292300

293-
rbacAvailable := true
294-
rbacVersionedClientset, err := util.GetVersionedClientForRBACOrFail(hostFactory)
295-
if err != nil {
296-
if _, ok := err.(*util.NoRBACAPIError); !ok {
297-
return err
301+
var rbacVersionedClientset client.Interface
302+
if useRBAC {
303+
rbacVersionedClientset, err = util.GetVersionedClientForRBACOrFail(hostFactory)
304+
if err != nil {
305+
if _, ok := err.(*util.NoRBACAPIError); !ok {
306+
return err
307+
}
308+
// If the error is type NoRBACAPIError, We continue to create the rest of
309+
// the resources, without the SA and roles (in the absence of RBAC support).
310+
useRBAC = false
298311
}
299-
// If the error is type NoRBACAPIError, We continue to create the rest of
300-
// the resources, without the SA and roles (in the absence of RBAC support).
301-
rbacAvailable = false
302312
}
303313

304314
serverName := APIServerNameSuffix
@@ -384,7 +394,7 @@ func (i *initFederation) Run(cmdOut io.Writer, config util.AdminConfig) error {
384394
sa.Name = ""
385395
// Create a service account and related RBAC roles if the host cluster has RBAC support.
386396
// TODO: We must evaluate creating a separate service account even when RBAC support is missing
387-
if rbacAvailable {
397+
if useRBAC {
388398
glog.V(4).Info("Creating service account for federation controller manager in the host cluster")
389399
sa, err = createControllerManagerSA(rbacVersionedClientset, i.commonOptions.FederationSystemNamespace, i.commonOptions.Name, i.options.dryRun)
390400
if err != nil {

pkg/kubefed/init/init_test.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ func TestInitFederation(t *testing.T) {
9292
federation string
9393
kubeconfigGlobal string
9494
kubeconfigExplicit string
95+
kubeconfigForCredentials string
9596
dnsZoneName string
9697
lbIP string
9798
apiserverServiceType v1.ServiceType
@@ -221,6 +222,28 @@ func TestInitFederation(t *testing.T) {
221222
apiserverEnableTokenAuth: true,
222223
isRBACAPIAvailable: true,
223224
},
225+
// This test checks if init works ok when a RBAC usage is overridden
226+
// using a credentials kubeconfig (even when RBAC API is available).
227+
// The same (or default) kubeconfig can be used for both flags.
228+
{
229+
federation: "union",
230+
kubeconfigGlobal: fakeKubeFiles[0],
231+
kubeconfigForCredentials: fakeKubeFiles[0],
232+
dnsZoneName: "example.test.",
233+
apiserverServiceType: v1.ServiceTypeNodePort,
234+
advertiseAddress: nodeIP,
235+
serverImage: "example.test/foo:bar",
236+
imagePullPolicy: "IfNotPresent",
237+
etcdImage: "gcr.io/google_containers/etcd:latest",
238+
etcdPVCapacity: "5Gi",
239+
etcdPVStorageClass: "fast",
240+
etcdPersistence: "true",
241+
expectedErr: "",
242+
dryRun: "",
243+
apiserverEnableHTTPBasicAuth: true,
244+
apiserverEnableTokenAuth: true,
245+
isRBACAPIAvailable: true,
246+
},
224247
}
225248

226249
defaultEtcdImage := "gcr.io/google_containers/etcd:3.1.10"
@@ -261,7 +284,7 @@ func TestInitFederation(t *testing.T) {
261284
tc.imagePullPolicy = "IfNotPresent"
262285
}
263286

264-
hostFactory, err := fakeInitHostFactory(tc.apiserverServiceType, tc.federation, util.DefaultFederationSystemNamespace, tc.advertiseAddress, tc.lbIP, tc.dnsZoneName, tc.serverImage, tc.etcdImage, tc.dnsProvider, tc.dnsProviderConfig, tc.etcdPersistence, tc.etcdPVCapacity, tc.etcdPVStorageClass, tc.apiserverArgOverrides, tc.cmArgOverrides, tmpDirPath, tc.apiserverEnableHTTPBasicAuth, tc.apiserverEnableTokenAuth, tc.isRBACAPIAvailable, tc.nodeSelector, tc.imagePullPolicy, tc.imagePullSecrets)
287+
hostFactory, err := fakeInitHostFactory(tc.apiserverServiceType, tc.federation, util.DefaultFederationSystemNamespace, tc.advertiseAddress, tc.lbIP, tc.dnsZoneName, tc.serverImage, tc.etcdImage, tc.dnsProvider, tc.dnsProviderConfig, tc.etcdPersistence, tc.etcdPVCapacity, tc.etcdPVStorageClass, tc.apiserverArgOverrides, tc.cmArgOverrides, tmpDirPath, tc.apiserverEnableHTTPBasicAuth, tc.apiserverEnableTokenAuth, tc.isRBACAPIAvailable, tc.nodeSelector, tc.imagePullPolicy, tc.imagePullSecrets, tc.kubeconfigForCredentials)
265288
if err != nil {
266289
t.Fatalf("[%d] unexpected error: %v", i, err)
267290
}
@@ -274,6 +297,7 @@ func TestInitFederation(t *testing.T) {
274297
cmd := NewCmdInit(buf, adminConfig, "serverImage", defaultEtcdImage)
275298

276299
cmd.Flags().Set("kubeconfig", tc.kubeconfigExplicit)
300+
cmd.Flags().Set("use-credentials-kubeconfig", tc.kubeconfigForCredentials)
277301
cmd.Flags().Set("host-cluster-context", "substrate")
278302
cmd.Flags().Set("dns-zone-name", tc.dnsZoneName)
279303
cmd.Flags().Set("image", tc.serverImage)
@@ -643,7 +667,7 @@ func TestCertsHTTPS(t *testing.T) {
643667
}
644668
}
645669

646-
func fakeInitHostFactory(apiserverServiceType v1.ServiceType, federationName, namespaceName, advertiseAddress, lbIp, dnsZoneName, serverImage, etcdImage, dnsProvider, dnsProviderConfig, etcdPersistence, etcdPVCapacity, etcdPVStorageClass, apiserverOverrideArg, cmOverrideArg, tmpDirPath string, apiserverEnableHTTPBasicAuth, apiserverEnableTokenAuth, isRBACAPIAvailable bool, nodeSelectorString string, imagePullPolicy, imagePullSecrets string) (cmdutil.Factory, error) {
670+
func fakeInitHostFactory(apiserverServiceType v1.ServiceType, federationName, namespaceName, advertiseAddress, lbIp, dnsZoneName, serverImage, etcdImage, dnsProvider, dnsProviderConfig, etcdPersistence, etcdPVCapacity, etcdPVStorageClass, apiserverOverrideArg, cmOverrideArg, tmpDirPath string, apiserverEnableHTTPBasicAuth, apiserverEnableTokenAuth, isRBACAPIAvailable bool, nodeSelectorString string, imagePullPolicy, imagePullSecrets, kubeconfigForCredentials string) (cmdutil.Factory, error) {
647671
svcName := "apiserver"
648672
svcUrlPrefix := "/api/v1/namespaces/federation-system/services"
649673
credSecretName := "apiserver" + "-credentials"
@@ -1106,7 +1130,7 @@ func fakeInitHostFactory(apiserverServiceType v1.ServiceType, federationName, na
11061130
},
11071131
},
11081132
}
1109-
if isRBACAPIAvailable {
1133+
if isRBACAPIAvailable && (kubeconfigForCredentials == "") {
11101134
cm.Spec.Template.Spec.ServiceAccountName = "federation-controller-manager"
11111135
cm.Spec.Template.Spec.DeprecatedServiceAccount = "federation-controller-manager"
11121136
}

pkg/kubefed/util/util.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,16 @@ type SubcommandOptions struct {
141141
Host string
142142
FederationSystemNamespace string
143143
Kubeconfig string
144+
CredentialsKubeconfig string
144145
}
145146

146147
func (o *SubcommandOptions) Bind(flags *pflag.FlagSet) {
147148
flags.StringVar(&o.Kubeconfig, "kubeconfig", "", "Path to the kubeconfig file to use for CLI requests.")
148149
flags.StringVar(&o.Host, "host-cluster-context", "", "Host cluster context")
149150
flags.StringVar(&o.FederationSystemNamespace, "federation-system-namespace", DefaultFederationSystemNamespace, "Namespace in the host cluster where the federation system components are installed")
151+
flags.StringVar(&o.CredentialsKubeconfig, "use-credentials-kubeconfig", "", "Kubeconfig file path on local file system, which should be used to authenticate with base cluster (instead of the default kubeconfig)."+
152+
"This can be used to override the RBAC based authentication while initialising the federation control plane, even when the base cluster exposes the RBAC API.")
153+
150154
}
151155

152156
func (o *SubcommandOptions) SetName(cmd *cobra.Command, args []string) error {

0 commit comments

Comments
 (0)