Skip to content

Commit

Permalink
Merge branch 'master' into event-target-cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeykazakov authored Aug 20, 2024
2 parents beba64f + 11497da commit 29780be
Show file tree
Hide file tree
Showing 13 changed files with 22 additions and 383 deletions.
23 changes: 0 additions & 23 deletions cmd/sandbox-cli/main.go

This file was deleted.

2 changes: 1 addition & 1 deletion pkg/assets/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"
)

// interface that matches all the methods provided by embed.FS
// FS is an interface that matches all the methods provided by embed.FS
type FS interface {
fs.FS
fs.ReadDirFS
Expand Down
23 changes: 1 addition & 22 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
olmv1 "github.com/operator-framework/api/pkg/operators/v1"
olmv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
errs "github.com/pkg/errors"
authv1 "k8s.io/api/authentication/v1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -69,7 +68,7 @@ func NewClientWithTransport(token, apiEndpoint string, transport http.RoundTripp
}

cfg.Transport = transport
cfg.BearerToken = string(token)
cfg.BearerToken = token
cfg.QPS = 40.0
cfg.Burst = 50
cfg.Timeout = 60 * time.Second
Expand Down Expand Up @@ -119,26 +118,6 @@ func newTlsVerifySkippingTransport() http.RoundTripper {
}
}

var DefaultNewRESTClient = NewRESTClient

func NewRESTClient(token, apiEndpoint string) (*rest.RESTClient, error) {
if err := AddToScheme(); err != nil {
return nil, err
}
config := &rest.Config{
BearerToken: token,
Host: apiEndpoint,
Transport: newTlsVerifySkippingTransport(),
Timeout: 60 * time.Second,
// These fields need to be set when using the REST client ¯\_(ツ)_/¯
ContentConfig: rest.ContentConfig{
GroupVersion: &authv1.SchemeGroupVersion,
NegotiatedSerializer: scheme.Codecs,
},
}
return rest.RESTClientFor(config)
}

func PatchUserSignup(ctx *clicontext.CommandContext, name string, changeUserSignup func(*toolchainv1alpha1.UserSignup) (bool, error), afterMessage string) error {
cfg, err := configuration.LoadClusterConfig(ctx, configuration.HostName)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/adm/restart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func checkDeploymentBeingUpdated(t *testing.T, fakeClient *test.FakeClient, name
// check the current deployment's replicas field
AssertDeploymentHasReplicas(t, fakeClient, namespacedName, 0)
// check the requested deployment's replicas field
assert.Equal(t, int32(currentReplicas), *deployment.Spec.Replicas)
assert.Equal(t, currentReplicas, *deployment.Spec.Replicas)
}
*numberOfUpdateCalls++
}
2 changes: 0 additions & 2 deletions pkg/cmd/ban.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ only one parameter which is the name of the UserSignup to be used for banning`,
}
}

const BannedByLabel = toolchainv1alpha1.LabelKeyPrefix + "banned-by"

func Ban(ctx *clicontext.CommandContext, args ...string) error {
return CreateBannedUser(ctx, args[0], func(userSignup *toolchainv1alpha1.UserSignup, bannedUser *toolchainv1alpha1.BannedUser) (bool, error) {
if _, exists := bannedUser.Labels[toolchainv1alpha1.BannedUserPhoneNumberHashLabelKey]; !exists {
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/generate/assertion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,11 @@ type groupsUserBelongsTo []string
type extraGroupsPresentInCluster []string

func groups(groups ...string) groupsUserBelongsTo {
return groupsUserBelongsTo(groups)
return groups
}

func extraGroupsUserIsNotPartOf(groups ...string) extraGroupsPresentInCluster {
return extraGroupsPresentInCluster(groups)
return groups
}

func (a userAssertion) belongsToGroups(groups groupsUserBelongsTo, extraGroups extraGroupsPresentInCluster) userAssertion {
Expand Down
18 changes: 15 additions & 3 deletions pkg/cmd/generate/cli_configs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
)

Expand Down Expand Up @@ -232,9 +233,7 @@ func TestGetServiceAccountToken(t *testing.T) {

setupGockForServiceAccounts(t, "https://api.example.com", 365, newServiceAccount("openshift-customer-monitoring", "loki"))
t.Cleanup(gock.OffAll)
cl, err := client.NewRESTClient("secret_token", "https://api.example.com")
cl.Client.Transport = gock.DefaultTransport // make sure that the underlying client's request are intercepted by Gock
// gock.Observe(gock.DumpRequest)
cl, err := newGockRESTClient("secret_token", "https://api.example.com")
require.NoError(t, err)
// when
actualToken, err := getServiceAccountToken(cl, types.NamespacedName{
Expand All @@ -247,6 +246,19 @@ func TestGetServiceAccountToken(t *testing.T) {
assert.Equal(t, "token-secret-for-loki", actualToken) // `token-secret-for-loki` is the answered mock by Gock in `setupGockForServiceAccounts(...)`
}

func newGockRESTClient(token, apiEndpoint string) (*rest.RESTClient, error) {
config := &rest.Config{
BearerToken: token,
Host: apiEndpoint,
Transport: gock.DefaultTransport, // make sure that the underlying client's request are intercepted by Gock
ContentConfig: rest.ContentConfig{
GroupVersion: &authv1.SchemeGroupVersion,
NegotiatedSerializer: scheme.Codecs,
},
}
return rest.RESTClientFor(config)
}

func verifyKsctlConfigFiles(t *testing.T, tempDir string, clusterAssertions ...userConfigClusterAssertions) {
tempDirInfo, err := os.ReadDir(tempDir)
require.NoError(t, err)
Expand Down
31 changes: 0 additions & 31 deletions pkg/configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,37 +208,6 @@ func (c ClusterConfig) GetNamespaceParam() string {
return "--namespace=" + c.OperatorNamespace
}

// ConfigurePath returns the path to the 'configure' directory, using the clusterConfigName arg if it's not empty,
// or the Host cluster's server name (even if the current config applies to a Member cluster)
func (c ClusterConfig) ConfigurePath(term ioutils.Terminal, clusterConfigName, component string) (string, error) {
return c.Path(term, clusterConfigName, "configure", component)
}

// InstallPath returns the path to the 'install' directory, using the clusterConfigName arg if it's not empty,
// or the Host cluster's server name (even if the current config applies to a Member cluster)
func (c ClusterConfig) InstallPath(term ioutils.Terminal, clusterConfigName, component string) (string, error) {
return c.Path(term, clusterConfigName, "install", component)
}

// Path returns the path to the directory for the given action, using the clusterConfigName arg if it's not empty,
// or the Host cluster's server name (even if the current config applies to a Member cluster)
func (c ClusterConfig) Path(term ioutils.Terminal, clusterConfigName, section, component string) (string, error) {
baseDir := c.ServerName
if c.ClusterType == Member {
// for member clusters, we use the associated host's serverName to retrieve the configuration
var err error
clusterDef, err := LoadClusterAccessDefinition(term, HostName)
if err != nil {
return "", err
}
baseDir = clusterDef.ServerName
}
if clusterConfigName != "" {
baseDir = clusterConfigName
}
return fmt.Sprintf("%s/%s/%s/%s", baseDir, section, c.ClusterType, component), nil
}

// GetMemberClusterName returns the full name of the member cluster (used in ToolchainCluster CRs)
// for the provided shot cluster name such as member-1 (used in ksctl.yaml)
func GetMemberClusterName(term ioutils.Terminal, ctlClusterName string) (string, error) {
Expand Down
36 changes: 0 additions & 36 deletions pkg/context/clusterconfig_command_context.go

This file was deleted.

57 changes: 0 additions & 57 deletions pkg/context/clusterconfig_command_context_test.go

This file was deleted.

40 changes: 0 additions & 40 deletions pkg/test/client.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,20 @@
package test

import (
"context"
"os/exec"
"testing"

"github.com/codeready-toolchain/toolchain-common/pkg/test"
"github.com/kubesaw/ksctl/pkg/client"
clicontext "github.com/kubesaw/ksctl/pkg/context"

"github.com/h2non/gock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"
)

func NewFakeClients(t *testing.T, initObjs ...runtime.Object) (clicontext.NewClientFunc, *test.FakeClient) {
fakeClient := test.NewFakeClient(t, initObjs...)
fakeClient.MockCreate = func(ctx context.Context, obj runtimeclient.Object, opts ...runtimeclient.CreateOption) error {
stringDataToData(obj)
return fakeClient.Client.Create(ctx, obj, opts...)
}
fakeClient.MockUpdate = func(ctx context.Context, obj runtimeclient.Object, opts ...runtimeclient.UpdateOption) error {
stringDataToData(obj)
return fakeClient.Client.Update(ctx, obj, opts...)
}
return func(token, apiEndpoint string) (runtimeclient.Client, error) {
t.Helper()
assert.Equal(t, "cool-token", token)
Expand All @@ -39,33 +26,6 @@ func NewFakeClients(t *testing.T, initObjs ...runtime.Object) (clicontext.NewCli
fakeClient
}

func NewFakeExternalClient(t *testing.T, token string, apiEndpoint string) *rest.RESTClient {
t.Helper()
cl, err := client.NewRESTClient(token, apiEndpoint)
require.NoError(t, err)
// override the underlying client's transport with Gock to intercep requests
cl.Client.Transport = gock.DefaultTransport
return cl
}

func stringDataToData(obj runtimeclient.Object) {
if obj.GetObjectKind().GroupVersionKind().Kind == "Secret" {
secret := obj.(*corev1.Secret)
if secret.Data == nil {
secret.Data = map[string][]byte{}
}
for key, value := range secret.StringData {
secret.Data[key] = []byte(value)
}
}
}

func AssertArgsEqual(expArgs ...string) ArgsAssertion {
return func(t *testing.T, actualArgs ...string) {
assert.Equal(t, expArgs, actualArgs)
}
}

func AssertFirstArgPrefixRestEqual(firstArgPrefix string, toEqual ...string) ArgsAssertion {
return func(t *testing.T, actualArgs ...string) {
t.Helper()
Expand Down
Loading

0 comments on commit 29780be

Please sign in to comment.