Skip to content

Commit

Permalink
rename most of the sandbox vars, params, and references (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
MatousJobanek authored Mar 22, 2024
1 parent f92e184 commit bf4324c
Show file tree
Hide file tree
Showing 25 changed files with 155 additions and 155 deletions.
10 changes: 5 additions & 5 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func PatchUserSignup(ctx *clicontext.CommandContext, name string, changeUserSign
if err != nil {
return err
}
userSignup, err := GetUserSignup(cl, cfg.SandboxNamespace, name)
userSignup, err := GetUserSignup(cl, cfg.OperatorNamespace, name)
if err != nil {
return err
}
Expand Down Expand Up @@ -151,7 +151,7 @@ func PatchMasterUserRecord(ctx *clicontext.CommandContext, name string, changeMa
if err != nil {
return err
}
mur, err := GetMasterUserRecord(cl, cfg.SandboxNamespace, name)
mur, err := GetMasterUserRecord(cl, cfg.OperatorNamespace, name)
if err != nil {
return err
}
Expand Down Expand Up @@ -188,7 +188,7 @@ func PatchSpace(ctx *clicontext.CommandContext, name string, changeSpace func(*t
if err != nil {
return err
}
space, err := GetSpace(cl, cfg.SandboxNamespace, name)
space, err := GetSpace(cl, cfg.OperatorNamespace, name)
if err != nil {
return err
}
Expand Down Expand Up @@ -244,7 +244,7 @@ func ListSpaceBindings(cl runtimeclient.Client, namespace string, opts ...SpaceB

func GetNSTemplateTier(cfg configuration.ClusterConfig, cl runtimeclient.Client, name string) (*toolchainv1alpha1.NSTemplateTier, error) {
namespacedName := types.NamespacedName{
Namespace: cfg.SandboxNamespace,
Namespace: cfg.OperatorNamespace,
Name: name,
}
obj := &toolchainv1alpha1.NSTemplateTier{}
Expand All @@ -256,7 +256,7 @@ func GetNSTemplateTier(cfg configuration.ClusterConfig, cl runtimeclient.Client,

func GetUserTier(cfg configuration.ClusterConfig, cl runtimeclient.Client, name string) (*toolchainv1alpha1.UserTier, error) {
namespacedName := types.NamespacedName{
Namespace: cfg.SandboxNamespace,
Namespace: cfg.OperatorNamespace,
Name: name,
}
obj := &toolchainv1alpha1.UserTier{}
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func TestUpdateUserSignupLacksPermissions(t *testing.T) {
}, "updated")

// then
require.EqualError(t, err, "sandbox command failed: the token in your ksctl.yaml file is missing")
require.EqualError(t, err, "ksctl command failed: the token in your ksctl.yaml file is missing")
AssertUserSignupSpec(t, fakeClient, userSignup)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/client/sandbox_config_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"path"
)

func EnsureSandboxCliConfigFile() (string, error) {
func EnsureKsctlConfigFile() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", err
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/add_space_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func AddSpaceUsers(ctx *clicontext.CommandContext, spaceName, role string, users

// get Space
ctx.Println("Checking space...")
space, err := client.GetSpace(cl, cfg.SandboxNamespace, spaceName)
space, err := client.GetSpace(cl, cfg.OperatorNamespace, spaceName)
if err != nil {
return err
}
Expand Down Expand Up @@ -85,7 +85,7 @@ func AddSpaceUsers(ctx *clicontext.CommandContext, spaceName, role string, users
ctx.Println("Checking users...")
spaceBindingsToCreate := []*toolchainv1alpha1.SpaceBinding{}
for _, murName := range usersToAdd {
mur, err := client.GetMasterUserRecord(cl, cfg.SandboxNamespace, murName)
mur, err := client.GetMasterUserRecord(cl, cfg.OperatorNamespace, murName)
if err != nil {
return err
}
Expand Down
34 changes: 17 additions & 17 deletions pkg/cmd/adm/generate_cli_configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func NewGenerateCliConfigsCmd() *cobra.Command {
return generate(term, f, DefaultNewExternalClientFromConfig)
},
}
command.Flags().StringVarP(&f.kubeSawAdminsFile, "kubesaw-admins", "c", "", "Use the given sandbox config file")
command.Flags().StringVarP(&f.kubeSawAdminsFile, "kubesaw-admins", "c", "", "Use the given kubesaw-admin file")
flags.MustMarkRequired(command, "kubesaw-admins")
command.Flags().BoolVarP(&f.dev, "dev", "d", false, "If running in a dev cluster")

Expand Down Expand Up @@ -93,8 +93,8 @@ func generate(term ioutils.Terminal, flags generateFlags, newExternalClient NewR
kubeconfigPaths: flags.kubeconfigs,
}

// sandboxUserConfigsPerName contains all sandboxUserConfig objects that will be marshalled to ksctl.yaml files
sandboxUserConfigsPerName := map[string]configuration.SandboxUserConfig{}
// ksctlConfigsPerName contains all ksctlConfig objects that will be marshalled to ksctl.yaml files
ksctlConfigsPerName := map[string]configuration.KsctlConfig{}

// use host API either from the kubesaw-admins.yaml or from kubeconfig if --dev flag was used
hostSpec := kubeSawAdmins.Clusters.Host
Expand All @@ -108,7 +108,7 @@ func generate(term ioutils.Terminal, flags generateFlags, newExternalClient NewR
}

// firstly generate for the host cluster
if err := generateForCluster(ctx, configuration.Host, "host", hostSpec, sandboxUserConfigsPerName); err != nil {
if err := generateForCluster(ctx, configuration.Host, "host", hostSpec, ksctlConfigsPerName); err != nil {
return err
}

Expand All @@ -121,29 +121,29 @@ func generate(term ioutils.Terminal, flags generateFlags, newExternalClient NewR
memberSpec.API = hostSpec.API
}

if err := generateForCluster(ctx, configuration.Member, member.Name, memberSpec, sandboxUserConfigsPerName); err != nil {
if err := generateForCluster(ctx, configuration.Member, member.Name, memberSpec, ksctlConfigsPerName); err != nil {
return err
}
}

return writeSandboxUserConfigs(term, flags.outDir, sandboxUserConfigsPerName)
return writeKsctlConfigs(term, flags.outDir, ksctlConfigsPerName)
}

func serverName(API string) string {
return strings.Split(strings.Split(API, "api.")[1], ":")[0]
}

// writeSandboxUserConfigs marshals the given SandboxUserConfig objects and stored them in sandbox-sre/out/config/<name>/ directories
func writeSandboxUserConfigs(term ioutils.Terminal, configDirPath string, sandboxUserConfigsPerName map[string]configuration.SandboxUserConfig) error {
// writeKsctlConfigs marshals the given KsctlConfig objects and stored them in sandbox-sre/out/config/<name>/ directories
func writeKsctlConfigs(term ioutils.Terminal, configDirPath string, ksctlConfigsPerName map[string]configuration.KsctlConfig) error {
if err := os.RemoveAll(configDirPath); err != nil {
return err
}
for name, sandboxUserConfig := range sandboxUserConfigsPerName {
for name, ksctlConfig := range ksctlConfigsPerName {
pathDir := fmt.Sprintf("%s/%s", configDirPath, name)
if err := os.MkdirAll(pathDir, 0744); err != nil {
return err
}
content, err := yaml.Marshal(sandboxUserConfig)
content, err := yaml.Marshal(ksctlConfig)
if err != nil {
return err
}
Expand All @@ -166,7 +166,7 @@ type generateContext struct {
// contains tokens mapped by SA name
type tokenPerSA map[string]string

func generateForCluster(ctx *generateContext, clusterType configuration.ClusterType, clusterName string, clusterSpec assets.ClusterConfig, sandboxUserConfigsPerName map[string]configuration.SandboxUserConfig) error {
func generateForCluster(ctx *generateContext, clusterType configuration.ClusterType, clusterName string, clusterSpec assets.ClusterConfig, ksctlConfigsPerName map[string]configuration.KsctlConfig) error {
ctx.PrintContextSeparatorf("Generating the content of the ksctl.yaml files for %s cluster running at %s", clusterName, clusterSpec.API)

// find config we can build client for the cluster from
Expand Down Expand Up @@ -203,7 +203,7 @@ func generateForCluster(ctx *generateContext, clusterType configuration.ClusterT
}
}

addToSandboxUserConfigs(clusterDef, clusterName, sandboxUserConfigsPerName, tokenPerSAName)
addToKsctlConfigs(clusterDef, clusterName, ksctlConfigsPerName, tokenPerSAName)

return nil
}
Expand Down Expand Up @@ -251,17 +251,17 @@ func getServiceAccountToken(cl *rest.RESTClient, namespacedName types.Namespaced
return result.Status.Token, nil
}

// addToSandboxUserConfigs adds to sandboxUserConfig objects information about the cluster as well as the SA token
func addToSandboxUserConfigs(clusterDev configuration.ClusterDefinition, clusterName string, sandboxUserConfigsPerName map[string]configuration.SandboxUserConfig, tokensPerSA tokenPerSA) {
// addToKsctlConfigs adds to ksctlConfig objects information about the cluster as well as the SA token
func addToKsctlConfigs(clusterDev configuration.ClusterDefinition, clusterName string, ksctlConfigsPerName map[string]configuration.KsctlConfig, tokensPerSA tokenPerSA) {
for name, token := range tokensPerSA {
if _, ok := sandboxUserConfigsPerName[name]; !ok {
sandboxUserConfigsPerName[name] = configuration.SandboxUserConfig{
if _, ok := ksctlConfigsPerName[name]; !ok {
ksctlConfigsPerName[name] = configuration.KsctlConfig{
Name: name,
ClusterAccessDefinitions: map[string]configuration.ClusterAccessDefinition{},
}
}
clusterName := utils.KebabToCamelCase(clusterName)
sandboxUserConfigsPerName[name].ClusterAccessDefinitions[clusterName] = configuration.ClusterAccessDefinition{
ksctlConfigsPerName[name].ClusterAccessDefinitions[clusterName] = configuration.ClusterAccessDefinition{
ClusterDefinition: clusterDev,
Token: token,
}
Expand Down
66 changes: 33 additions & 33 deletions pkg/cmd/adm/generate_cli_configs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestGenerateCliConfigs(t *testing.T) {

kubeSawAdminsContent, err := yaml.Marshal(kubeSawAdmins)
require.NoError(t, err)
kubeconfigFiles := createKubeconfigFiles(t, sandboxKubeconfigContent, sandboxKubeconfigContentMember2)
kubeconfigFiles := createKubeconfigFiles(t, ksctlKubeconfigContent, ksctlKubeconfigContentMember2)

setupGockForServiceAccounts(t, HostServerAPI,
newServiceAccount("sandbox-sre-host", "john"),
Expand Down Expand Up @@ -77,7 +77,7 @@ func TestGenerateCliConfigs(t *testing.T) {
// then
require.NoError(t, err)

verifySandboxUserConfigFiles(t, tempDir, hasHost(), hasMember("member1", "member1"), hasMember("member2", "member2"))
verifyKsctlConfigFiles(t, tempDir, hasHost(), hasMember("member1", "member1"), hasMember("member2", "member2"))
})

t.Run("when there SAs are defined for host cluster only", func(t *testing.T) {
Expand Down Expand Up @@ -105,7 +105,7 @@ func TestGenerateCliConfigs(t *testing.T) {
// then
require.NoError(t, err)

verifySandboxUserConfigFiles(t, tempDir, hasHost())
verifyKsctlConfigFiles(t, tempDir, hasHost())
})

t.Run("in dev mode", func(t *testing.T) {
Expand All @@ -116,7 +116,7 @@ func TestGenerateCliConfigs(t *testing.T) {
)
tempDir, err := os.MkdirTemp("", "sandbox-sre-out-")
require.NoError(t, err)
kubeconfigFiles := createKubeconfigFiles(t, sandboxKubeconfigContent)
kubeconfigFiles := createKubeconfigFiles(t, ksctlKubeconfigContent)
flags := generateFlags{kubeconfigs: kubeconfigFiles, kubeSawAdminsFile: configFile, outDir: tempDir, dev: true}

// when
Expand All @@ -125,7 +125,7 @@ func TestGenerateCliConfigs(t *testing.T) {
// then
require.NoError(t, err)

verifySandboxUserConfigFiles(t, tempDir, hasHost(), hasMember("member1", "host"), hasMember("member2", "host"))
verifyKsctlConfigFiles(t, tempDir, hasHost(), hasMember("member1", "host"), hasMember("member2", "host"))
})
})

Expand Down Expand Up @@ -221,7 +221,7 @@ 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 verifySandboxUserConfigFiles(t *testing.T, tempDir string, clusterAssertions ...userConfigClusterAssertions) {
func verifyKsctlConfigFiles(t *testing.T, tempDir string, clusterAssertions ...userConfigClusterAssertions) {
tempDirInfo, err := os.ReadDir(tempDir)
require.NoError(t, err)
assert.Len(t, tempDirInfo, 2)
Expand All @@ -235,64 +235,64 @@ func verifySandboxUserConfigFiles(t *testing.T, tempDir string, clusterAssertion
content, err := os.ReadFile(path.Join(tempDir, userDir.Name(), userDirInfo[0].Name()))
require.NoError(t, err)

sandboxUserconfig := configuration.SandboxUserConfig{}
err = yaml.Unmarshal(content, &sandboxUserconfig)
ksctlConfig := configuration.KsctlConfig{}
err = yaml.Unmarshal(content, &ksctlConfig)
require.NoError(t, err)

userConfig := assertSandboxUserConfig(t, sandboxUserconfig, userDir.Name()).
userConfig := assertKsctlConfig(t, ksctlConfig, userDir.Name()).
hasNumberOfClusters(len(clusterAssertions))
for _, applyAssertion := range clusterAssertions {
applyAssertion(t, userDir.Name(), userConfig)
}
}
}

type userConfigClusterAssertions func(*testing.T, string, *sandboxUserConfigAssertion)
type userConfigClusterAssertions func(*testing.T, string, *ksctlConfigAssertion)

func hasHost() userConfigClusterAssertions {
return func(t *testing.T, name string, assertion *sandboxUserConfigAssertion) {
return func(t *testing.T, name string, assertion *ksctlConfigAssertion) {
assertion.hasCluster("host", "host", configuration.Host)
}
}

func hasMember(memberName, subDomain string) userConfigClusterAssertions {
return func(t *testing.T, name string, assertion *sandboxUserConfigAssertion) {
return func(t *testing.T, name string, assertion *ksctlConfigAssertion) {
assertion.hasCluster(memberName, subDomain, configuration.Member)
}
}

// SandboxUserConfig assertions
// KsctlConfig assertions

type sandboxUserConfigAssertion struct {
t *testing.T
sandboxUserConfig configuration.SandboxUserConfig
saBaseName string
type ksctlConfigAssertion struct {
t *testing.T
ksctlConfig configuration.KsctlConfig
saBaseName string
}

func assertSandboxUserConfig(t *testing.T, sandboxUserConfig configuration.SandboxUserConfig, saBaseName string) *sandboxUserConfigAssertion {
require.NotNil(t, sandboxUserConfig)
assert.Equal(t, saBaseName, sandboxUserConfig.Name)
return &sandboxUserConfigAssertion{
t: t,
sandboxUserConfig: sandboxUserConfig,
saBaseName: saBaseName,
func assertKsctlConfig(t *testing.T, ksctlConfig configuration.KsctlConfig, saBaseName string) *ksctlConfigAssertion {
require.NotNil(t, ksctlConfig)
assert.Equal(t, saBaseName, ksctlConfig.Name)
return &ksctlConfigAssertion{
t: t,
ksctlConfig: ksctlConfig,
saBaseName: saBaseName,
}
}

func (a *sandboxUserConfigAssertion) hasNumberOfClusters(number int) *sandboxUserConfigAssertion {
require.Len(a.t, a.sandboxUserConfig.ClusterAccessDefinitions, number)
func (a *ksctlConfigAssertion) hasNumberOfClusters(number int) *ksctlConfigAssertion {
require.Len(a.t, a.ksctlConfig.ClusterAccessDefinitions, number)
return a
}

func (a *sandboxUserConfigAssertion) hasCluster(clusterName, subDomain string, clusterType configuration.ClusterType) {
require.NotNil(a.t, a.sandboxUserConfig.ClusterAccessDefinitions[clusterName])
func (a *ksctlConfigAssertion) hasCluster(clusterName, subDomain string, clusterType configuration.ClusterType) {
require.NotNil(a.t, a.ksctlConfig.ClusterAccessDefinitions[clusterName])

assert.NotNil(a.t, a.sandboxUserConfig.ClusterAccessDefinitions[clusterName])
assert.Equal(a.t, clusterType, a.sandboxUserConfig.ClusterAccessDefinitions[clusterName].ClusterType)
assert.Equal(a.t, fmt.Sprintf("sandbox.%s.openshiftapps.com", subDomain), a.sandboxUserConfig.ClusterAccessDefinitions[clusterName].ServerName)
assert.Equal(a.t, fmt.Sprintf("https://api.sandbox.%s.openshiftapps.com:6443", subDomain), a.sandboxUserConfig.ClusterAccessDefinitions[clusterName].ServerAPI)
assert.NotNil(a.t, a.ksctlConfig.ClusterAccessDefinitions[clusterName])
assert.Equal(a.t, clusterType, a.ksctlConfig.ClusterAccessDefinitions[clusterName].ClusterType)
assert.Equal(a.t, fmt.Sprintf("sandbox.%s.openshiftapps.com", subDomain), a.ksctlConfig.ClusterAccessDefinitions[clusterName].ServerName)
assert.Equal(a.t, fmt.Sprintf("https://api.sandbox.%s.openshiftapps.com:6443", subDomain), a.ksctlConfig.ClusterAccessDefinitions[clusterName].ServerAPI)

assert.Equal(a.t, fmt.Sprintf("token-secret-for-%s", a.saBaseName), a.sandboxUserConfig.ClusterAccessDefinitions[clusterName].Token)
assert.Equal(a.t, fmt.Sprintf("token-secret-for-%s", a.saBaseName), a.ksctlConfig.ClusterAccessDefinitions[clusterName].Token)
}

func setupGockForServiceAccounts(t *testing.T, apiEndpoint string, sas ...*corev1.ServiceAccount) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/adm/register_member.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func registerMemberCluster(ctx *clicontext.CommandContext, newCommand client.Com

func disableAutomaticApproval(hostClusterConfig configuration.ClusterConfig, cl runtimeclient.Client) error {
configs := &toolchainv1alpha1.ToolchainConfigList{}
if err := cl.List(context.TODO(), configs, runtimeclient.InNamespace(hostClusterConfig.SandboxNamespace)); err != nil {
if err := cl.List(context.TODO(), configs, runtimeclient.InNamespace(hostClusterConfig.OperatorNamespace)); err != nil {
return err
}

Expand Down
12 changes: 6 additions & 6 deletions pkg/cmd/adm/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ func restart(ctx *clicontext.CommandContext, clusterName string, deployments ...
deploymentName := deployments[0]

if !ctx.AskForConfirmation(
ioutils.WithMessagef("restart the deployment '%s' in namespace '%s'", deploymentName, cfg.SandboxNamespace)) {
ioutils.WithMessagef("restart the deployment '%s' in namespace '%s'", deploymentName, cfg.OperatorNamespace)) {
return nil
}
return restartDeployment(ctx, cl, cfg, deploymentName)
}

func restartDeployment(ctx *clicontext.CommandContext, cl runtimeclient.Client, cfg configuration.ClusterConfig, deploymentName string) error {
namespacedName := types.NamespacedName{
Namespace: cfg.SandboxNamespace,
Namespace: cfg.OperatorNamespace,
Name: deploymentName,
}

Expand All @@ -92,28 +92,28 @@ func restartDeployment(ctx *clicontext.CommandContext, cl runtimeclient.Client,
func restartHostOperator(ctx *clicontext.CommandContext, hostClient runtimeclient.Client, hostConfig configuration.ClusterConfig) error {
deployments := &appsv1.DeploymentList{}
if err := hostClient.List(context.TODO(), deployments,
runtimeclient.InNamespace(hostConfig.SandboxNamespace),
runtimeclient.InNamespace(hostConfig.OperatorNamespace),
runtimeclient.MatchingLabels{"olm.owner.namespace": "toolchain-host-operator"}); err != nil {
return err
}
if len(deployments.Items) != 1 {
return fmt.Errorf("there should be a single deployment matching the label olm.owner.namespace=toolchain-host-operator in %s ns, but %d was found. "+
"It's not possible to restart the Host Operator deployment", hostConfig.SandboxNamespace, len(deployments.Items))
"It's not possible to restart the Host Operator deployment", hostConfig.OperatorNamespace, len(deployments.Items))
}

return restartDeployment(ctx, hostClient, hostConfig, deployments.Items[0].Name)
}

func printExistingDeployments(term ioutils.Terminal, cl runtimeclient.Client, cfg configuration.ClusterConfig) error {
deployments := &appsv1.DeploymentList{}
if err := cl.List(context.TODO(), deployments, runtimeclient.InNamespace(cfg.SandboxNamespace)); err != nil {
if err := cl.List(context.TODO(), deployments, runtimeclient.InNamespace(cfg.OperatorNamespace)); err != nil {
return err
}
deploymentList := "\n"
for _, deployment := range deployments.Items {
deploymentList += fmt.Sprintf("%s\n", deployment.Name)
}
term.PrintContextSeparatorWithBodyf(deploymentList, "Existing deployments in %s namespace", cfg.SandboxNamespace)
term.PrintContextSeparatorWithBodyf(deploymentList, "Existing deployments in %s namespace", cfg.OperatorNamespace)
return nil
}

Expand Down
Loading

0 comments on commit bf4324c

Please sign in to comment.