-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #480 from fluxcd/imprv-kube
- Loading branch information
Showing
11 changed files
with
798 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
Copyright 2022 The Flux authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package kube | ||
|
||
import ( | ||
"github.com/fluxcd/pkg/runtime/client" | ||
"k8s.io/cli-runtime/pkg/genericclioptions" | ||
) | ||
|
||
const ( | ||
// DefaultKubeConfigSecretKey is the default data key ConfigFromSecret | ||
// looks at when no data key is provided. | ||
DefaultKubeConfigSecretKey = "value" | ||
// DefaultKubeConfigSecretKeyExt is the default data key ConfigFromSecret | ||
// looks at when no data key is provided, and DefaultKubeConfigSecretKey | ||
// does not exist. | ||
DefaultKubeConfigSecretKeyExt = DefaultKubeConfigSecretKey + ".yaml" | ||
) | ||
|
||
// clientGetterOptions used to BuildClientGetter. | ||
type clientGetterOptions struct { | ||
namespace string | ||
kubeConfig []byte | ||
impersonateAccount string | ||
clientOptions client.Options | ||
kubeConfigOptions client.KubeConfigOptions | ||
} | ||
|
||
// ClientGetterOption configures a genericclioptions.RESTClientGetter. | ||
type ClientGetterOption func(o *clientGetterOptions) | ||
|
||
// WithKubeConfig creates a MemoryRESTClientGetter configured with the provided | ||
// KubeConfig and other values. | ||
func WithKubeConfig(kubeConfig []byte, opts client.KubeConfigOptions) func(o *clientGetterOptions) { | ||
return func(o *clientGetterOptions) { | ||
o.kubeConfig = kubeConfig | ||
o.kubeConfigOptions = opts | ||
} | ||
} | ||
|
||
// WithClientOptions configures the genericclioptions.RESTClientGetter with | ||
// provided options. | ||
func WithClientOptions(opts client.Options) func(o *clientGetterOptions) { | ||
return func(o *clientGetterOptions) { | ||
o.clientOptions = opts | ||
} | ||
} | ||
|
||
// WithImpersonate configures the genericclioptions.RESTClientGetter to | ||
// impersonate the provided account name. | ||
func WithImpersonate(accountName string) func(o *clientGetterOptions) { | ||
return func(o *clientGetterOptions) { | ||
o.impersonateAccount = accountName | ||
} | ||
} | ||
|
||
// BuildClientGetter builds a genericclioptions.RESTClientGetter based on the | ||
// provided options and returns the result. Namespace is not expected to be | ||
// empty. In case it fails to construct using NewInClusterRESTClientGetter, it | ||
// returns an error. | ||
func BuildClientGetter(namespace string, opts ...ClientGetterOption) (genericclioptions.RESTClientGetter, error) { | ||
o := &clientGetterOptions{ | ||
namespace: namespace, | ||
} | ||
for _, opt := range opts { | ||
opt(o) | ||
} | ||
if len(o.kubeConfig) > 0 { | ||
return NewMemoryRESTClientGetter(o.kubeConfig, namespace, o.impersonateAccount, o.clientOptions, o.kubeConfigOptions), nil | ||
} | ||
return NewInClusterRESTClientGetter(namespace, o.impersonateAccount, &o.clientOptions) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
Copyright 2022 The Flux authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package kube | ||
|
||
import ( | ||
"k8s.io/client-go/rest" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"testing" | ||
|
||
"github.com/fluxcd/pkg/runtime/client" | ||
. "github.com/onsi/gomega" | ||
"k8s.io/cli-runtime/pkg/genericclioptions" | ||
) | ||
|
||
func TestBuildClientGetter(t *testing.T) { | ||
t.Run("with namespace and retrieved config", func(t *testing.T) { | ||
g := NewWithT(t) | ||
cfg := &rest.Config{Host: "https://example.com"} | ||
ctrl.GetConfig = func() (*rest.Config, error) { | ||
return cfg, nil | ||
} | ||
|
||
namespace := "a-namespace" | ||
getter, err := BuildClientGetter(namespace) | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
g.Expect(getter).To(BeAssignableToTypeOf(&genericclioptions.ConfigFlags{})) | ||
|
||
flags := getter.(*genericclioptions.ConfigFlags) | ||
g.Expect(flags.Namespace).ToNot(BeNil()) | ||
g.Expect(*flags.Namespace).To(Equal(namespace)) | ||
g.Expect(flags.APIServer).ToNot(BeNil()) | ||
g.Expect(*flags.APIServer).To(Equal(cfg.Host)) | ||
}) | ||
|
||
t.Run("with kubeconfig, impersonate and client options", func(t *testing.T) { | ||
g := NewWithT(t) | ||
ctrl.GetConfig = mockGetConfig | ||
|
||
namespace := "a-namespace" | ||
cfg := []byte(`apiVersion: v1 | ||
clusters: | ||
- cluster: | ||
server: https://example.com | ||
name: example-cluster | ||
contexts: | ||
- context: | ||
cluster: example-cluster | ||
namespace: flux-system | ||
kind: Config | ||
preferences: {} | ||
users:`) | ||
clientOpts := client.Options{QPS: 600, Burst: 1000} | ||
cfgOpts := client.KubeConfigOptions{InsecureTLS: true} | ||
impersonate := "jane" | ||
|
||
getter, err := BuildClientGetter(namespace, WithClientOptions(clientOpts), WithKubeConfig(cfg, cfgOpts), WithImpersonate(impersonate)) | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
g.Expect(getter).To(BeAssignableToTypeOf(&MemoryRESTClientGetter{})) | ||
|
||
got := getter.(*MemoryRESTClientGetter) | ||
g.Expect(got.namespace).To(Equal(namespace)) | ||
g.Expect(got.kubeConfig).To(Equal(cfg)) | ||
g.Expect(got.clientOpts).To(Equal(clientOpts)) | ||
g.Expect(got.kubeConfigOpts).To(Equal(cfgOpts)) | ||
g.Expect(got.impersonateAccount).To(Equal(impersonate)) | ||
}) | ||
|
||
t.Run("with impersonate account", func(t *testing.T) { | ||
g := NewWithT(t) | ||
ctrl.GetConfig = mockGetConfig | ||
|
||
namespace := "a-namespace" | ||
impersonate := "frank" | ||
getter, err := BuildClientGetter(namespace, WithImpersonate(impersonate)) | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
g.Expect(getter).To(BeAssignableToTypeOf(&genericclioptions.ConfigFlags{})) | ||
|
||
flags := getter.(*genericclioptions.ConfigFlags) | ||
g.Expect(flags.Namespace).ToNot(BeNil()) | ||
g.Expect(*flags.Namespace).To(Equal(namespace)) | ||
g.Expect(flags.Impersonate).ToNot(BeNil()) | ||
g.Expect(*flags.Impersonate).To(Equal("system:serviceaccount:a-namespace:frank")) | ||
}) | ||
|
||
t.Run("with DefaultServiceAccount", func(t *testing.T) { | ||
g := NewWithT(t) | ||
ctrl.GetConfig = mockGetConfig | ||
|
||
namespace := "a-namespace" | ||
DefaultServiceAccountName = "frank" | ||
getter, err := BuildClientGetter(namespace) | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
g.Expect(getter).To(BeAssignableToTypeOf(&genericclioptions.ConfigFlags{})) | ||
|
||
flags := getter.(*genericclioptions.ConfigFlags) | ||
g.Expect(flags.Namespace).ToNot(BeNil()) | ||
g.Expect(*flags.Namespace).To(Equal(namespace)) | ||
g.Expect(flags.Impersonate).ToNot(BeNil()) | ||
g.Expect(*flags.Impersonate).To(Equal("system:serviceaccount:a-namespace:frank")) | ||
}) | ||
} | ||
|
||
func mockGetConfig() (*rest.Config, error) { | ||
return &rest.Config{}, nil | ||
} |
Oops, something went wrong.